0

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!

Lorenz Lo Sauer
  • 23,698
  • 16
  • 85
  • 87
Chris C
  • 259
  • 2
  • 15
  • perhaps it would help to share more about the prime-calculating algorithm you are using. I cannot see any fault with what you provided so far... – Lorenz Lo Sauer Sep 22 '12 at 17:53
  • Why not simply subtract `0 - bigprime` (`BN_sub(inverse,zero,bigprime)` apparently)? Or am I missing something? – Daniel Fischer Sep 22 '12 at 18:24
  • D'oh, i think you're right Daniel--that should do it. I knew i was missing something obvious! – Chris C Sep 23 '12 at 01:19
  • Daniel if you could post it as an answer instead of a comment i'll flag it as an answer. Thanks again! – Chris C Sep 23 '12 at 01:19

1 Answers1

1

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.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431