Finding additive inverse using OpenSSL BIGNUM
-
03-07-2021 - |
Question
I need to calculate the additive inverse of a large (BIGNUM) prime in C. So, basically, i need to multiply by -1. Is there an easy way to do this that i've missed? I don't see any functions in the documentation. All i've come up with is this, which i think is rather ugly:
//bigprime = my big prime
inverse = BN_new();
one = BN_new();
negOne = BN_new();
BN_one(one); // one = 1
BN_zero(negOne); // set negOne to zero
BN_sub(negOne, negOne, one) // subtract one from zero
BN_mul(inverse, bigprime, negOne, ctx); //multiply big prime by -1
More so, i'm not familiar enough with the bignum library to know whether "subtracting" one from zero is really going to do what i want it to. TIA!
Solution
The simpler solution is to just subtract the prime from zero.
inverse = BN_new();
nil = BN_new();
BN_zero(nil);
BN_sub(inverse,nil,bigprime);
should do it.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow