BIgNumber library question.

Hello!

I am currently trying to implement RSA on an Arduino MEGA 2560, and am running into some problems. I am running the following code:

BigNumber me = 183;
for(int i = 1; i<= 107;i++){
    Serial.print ("183^");
    Serial.print (i);
    Serial.print (" = ");
    BigNumber x = me.pow(i);
    Serial.println(x);
  }

and the code cuts out after calculating 183^40 = 3148064262786984389766395753793008609830152603594197508066177418742117851469136711472539201

i.e. calculating 183^41.

Could someone help me out why this is happening? :slight_smile:
Thanks!

Explain what you mean by "the code cuts out".

jremington:
Explain what you mean by "the code cuts out".

Uhh, it stops haha, I don't know how to explain it better than it completely goes to a halt, i.e. stopping/quitting/ not running/executing, etc. As the output shows here: https://i.imgur.com/g61OeAJ.png

Well, you are doing something wrong. But you forgot to post all the code, so it is anyone's guess what that might be.

Study this tutorial. You will note that the author uses a custom function to print a BigNumber.

jremington:
Well, you are doing something wrong. But you forgot to post all the code, so it is anyone's guess what that might be.

Study this tutorial. You will note that the author uses a custom function to print a BigNumber.

Obviously. Alas, that is why I am here, perhaps someone has encountered the same problem.

If you would actually look, at the page you are referencing, you would realize that the author updated the library to support the builtin serial output....

"[...] This means that you can now print big numbers from any class derived from Stream (eg. Serial, Serial1, etc.)."

Maybe some dynamic memory isn't being freed. Why do you need to create a new BigNumber instance on every pass through the 'for' loop?

If you would actually look, at the page you are referencing

Something you might try, as well.

If you use that custom function, it frees the memory you are filling up. I thought you might be bright enough to see that.

for(int i = 1; i<= 107;i++){

BigNumber x = me.pow(i);
  }

As I understand C++, this will create a new local variable x each time through the loop (on the stack?) and not delete them until they "go out of scope" (ie the loop is done.) That means that you're trying to put 107 bignums into memory, and there probably isn't room (~1 byte per digit, and you're using 50+ digit numbers!) You've overwritten access to the older variable "handles" ("x"), so you can't access the older versions of the variable, but they're still there - if they're on the stack as I expect, they can be successfully deleted automatically IFF the device had enough memory (ie, this would probably run OK on a desktop PC.)
Try something more like:

BigNumber me = 183;
BigNumber x = 0;
for(int i = 1; i<= 107;i++){
    Serial.print ("183^");
    Serial.print (i);
    Serial.print (" = ");
    x = me.pow(i);
    Serial.println(x);
  }

That's still not likely to be GOOD; the loop will have to keep re-allocating a slightly bigger bignum, but it should be better...