dartist / dart-bignum

Other
14 stars 11 forks source link

Implementation of BigInteger.fromBytes() is not conform with Java's #23

Closed stevenroose closed 10 years ago

stevenroose commented 10 years ago

I'm confused by BigInteger.fromBytes implemented in this commit. Magnitude should be interpreted unsigned, so fromBytes(1, [0xff]) should give BigInteger(255), right? It doesn't. Anyway, a fromBytes(1, anything) should never give a negative number, right?

I tried

print(new BigInteger(new Uint8List.fromList([0xff])));
print(new BigInteger.fromBytes(1, [0xff]));

And they both give -1. In Java, the second constructor gives 255, like I think it should do.

adam-singer commented 10 years ago

@izaera could you comment on this? I don't have the bandwidth at the moment.

adam-singer commented 10 years ago

@stevenroose does https://github.com/dartist/dart-bignum/pull/24 fix your issue?

izaera commented 10 years ago

Yes. I detected it too and it should be fixed in #24 AFAIK. The problem was that 0xFF has 1 as its MSB and Dart's BigInteger gets fooled thinking it is a negative. I tried to fix it adding a leading 0 byte if the MSB==1 and seems to work for me as extra leading zeros are supposed to be stripped.

It's incredible how this bug didn't hit me before given that I'm using BigInteger.fromBytes() intensively in cipher. I think I'll buy some lottery this week ;-P.

stevenroose commented 10 years ago

I don't know how I can easily test the pull request without cloning the repo to my HD. I suppose it works now, I trust in your opinions :) Thanks for the quick fix!

adam-singer commented 10 years ago

should be deployed to pub also

izaera commented 10 years ago

I think this is duplicate of #24. If not, please reopen.