Take the 2-minute tour ×

I'm having some issues with OpenSSL BIGNUMs. I'm trying to implement a basic ElGamal protocol. The problem i'm having is that some of these calculations are not returning the values i'm expecting! This is mostly obvious when i try to calculate a multiplicative inverse.

For example:

static void roughExample() {
BIGNUM *u1 = BN_new();
BIGNUM *u2 = BN_new();

BIGNUM *one = BN_new();
BIGNUM *negOne = BN_new();
BN_sub(negOne, negOne, one); // So negOne should = -1 and it does

BN_print_fp(stdout, negOne);  //shows -1 here

BN_rand_range(u1, q); // q = large prime, point being that u1 = random large num
BN_print_fp(stdout, u1);

BN_exp(u2, u1, negOne, ctx);
BN_print_fp(stdout, u2); // in the output, u1 = u2.

The output above comes out as: -1, [large prime], [exact same large prime].

So clearly BN_exp isn't doing what i'm expecting it to. In this case I suppose i can just use BN_div to do 1/x, but i'm wondering if there is a larger picture that i'm missing. I am using doing lots of calculations like this and i won't always be able to visually verify that the value has changed like i expected.

Can anyone help? Thanks in advance!

Edit: I tried using 1/x, and it produces a value of 0... :/

share|improve this question
BN_exp() works for positive exponents. –  brian beuning Dec 19 '12 at 4:49
add comment

1 Answer

up vote 1 down vote accepted

You need a BN_mod_inverse function. Other functions don't provide such algorithm.

You can also perform exponentiation to (q-2) modulo q, if q is prime. And you will get inverse field member due to Fermat's little theorem.

share|improve this answer
I hadn't noticed that function, thanks Pavel! –  Chris C Oct 23 '12 at 15:15
add comment

Your Answer


