21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

In addition, you might wish to test a number to see if it is prime. The API for that<br />

one is a bit complex:<br />

int BN_is_prime(BIGNUM *bn, int numchecks, void (*callback)(int, int, void *),<br />

BN_CTX *ctx, void *cb_arg);<br />

int BN_is_prime_fasttest(BIGNUM *bn, int numchecks,<br />

void (*callback)(int, int, void *), BN_CTX *ctx,<br />

void *cb_arg);<br />

These functions do not guarantee that the number is prime. OpenSSLuses the<br />

Rabin-Miller primality test, which is an iterative, probabilistic algorithm, where the<br />

probability that the algorithm is right increases dramatically with every iteration. The<br />

checks argument specifies how many iterations to use. We strongly recommend<br />

using the built-in constant BN_prime_checks, which makes probability of the result<br />

being wrong negligible. When using that value, the odds of the result being wrong<br />

are 1 in 280. This function requires you to pass in a pointer to an initialized BN_CTX object, which<br />

it uses as scratch space.<br />

Prime number testing isn’t that cheap. BN_is_prime_fasttest( ) explicitly tries factoring<br />

by a bunch of small primes, which speeds things up when the value you’re checking<br />

might not be prime (which is the case when you’re generating a random prime).<br />

Because testing the primality of a number can be quite expensive, OpenSSLprovides<br />

a way to monitor status by using the callback and cb_arg arguments. In addition,<br />

because the primality-testing algorithm consists of performing a fixed number of iterations,<br />

this callback can be useful for implementing a status meter of some sort.<br />

If you define the callback, it is called after each iteration. The first argument is always<br />

1, the second is always the iteration number (starting with 0), and the third is the<br />

value of cb_arg (this can be used to identify the calling thread if multiple threads are<br />

sharing the same callback).<br />

Math operations on BIGNUM objects<br />

Yes, we saved the best for last. Table 7-2 lists the math operations supported by<br />

OpenSSL’s BIGNUM library.<br />

Table 7-2. Math operations supported by OpenSSL’s BIGNUM library<br />

Function Description Limitations Comments<br />

int BN_add(BIGNUM *r, BIGNUM<br />

*a, BIGNUM *b);<br />

r = a+b<br />

int BN_sub(BIGNUM *r, BIGNUM<br />

*a, BIGNUM *b);<br />

int BN_mul(BIGNUM *r, BIGNUM<br />

*a, BIGNUM *b, BN_CTX *ctx);<br />

r = a-b r≠a and r≠b Values may be the same, but<br />

the objects may not be.<br />

r = a×b Use BN_lshift or BN_lshift1<br />

instead to multiply by a<br />

known power of 2 (it’s<br />

faster).<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

Manipulating Big Numbers | 321

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!