Constification can lead you to very nice discoveries when you really
start digging.  The BIGNUM code can be quite a treasure of surprises.
For example, I discovered that the second argument to bn_sqr_recursive()
can safely be const (I looked through the downstream call tree, and
found that for all functions that are given that same argument,
there's no reason for them to touch it, and a constification of them
proved me right).  This means that in the part of BN_sqr() where a
bn_wexpand() is made to the size k*2, followed by a bn_sqr_recursive(),
it's perfectly safe to use a variant of bn_dup_expand() that doesn't
BN_dup() when no actual expansion is done.  However, if expansion is
actually needed, one must of course remember to free the expanded
version of the BIGNUM in question.

One might wonder what the importance of this is, until you realise
that a realloc() most often has the same cost as malloc() and a free()
put together, especially when we you have growing things, like a
BIGNUM with a data area the doubles in size :-).

So, I ended up making a bn_dup_expand2() that works just like
bn_dup_expand(), except that it doesn't do a BN_dup(), and the part in
BN_sqr() that I'm fiddling with now looks like this:

                        if (al == j)
                                {
                                const BIGNUM *tmp_bn = NULL;
                                BIGNUM *free_bn = NULL;

                                if ((tmp_bn = bn_dup_expand2(a,k*2,&free_bn))
                                        == NULL) goto err;
                                if (bn_wexpand(tmp,k*2) == NULL) goto err;
                                bn_sqr_recursive(rr->d,tmp_bn->d,al,tmp->d);
                                if (free_bn)
                                        BN_free(free_bn);
                                }

bn_dup_expand2() looks like this:

const BIGNUM *bn_dup_expand2(const BIGNUM *b, int words, BIGNUM **free_r);

The return value will either be the pointer to an expanded copy of b
if expansion was needed, or simply b.  *free_r will be assigned the
pointer to the expanded copy of b if expansion occured, otherwise it
will be NULL.

I'd love if someone could check my thinking so I don't do any booboo
that I'll just regret later.  In the mean time, I'm going to see if
it's possible to do something similar with BN_mul()...

-- 
Richard Levitte   \ Spannv�gen 38, II \ [EMAIL PROTECTED]
Chairman@Stacken   \ S-168 35  BROMMA  \ T: +46-8-26 52 47
Redakteur@Stacken   \      SWEDEN       \ or +46-709-50 36 10
Procurator Odiosus Ex Infernis                -- [EMAIL PROTECTED]
Member of the OpenSSL development team: http://www.openssl.org/
Software Engineer, Celo Communications: http://www.celocom.com/

Unsolicited commercial email is subject to an archival fee of $400.
See <http://www.stacken.kth.se/~levitte/mail/> for more info.

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       [EMAIL PROTECTED]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to