Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed bitlen calc #18

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/BigNumbers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -997,8 +997,8 @@ library BigNumbers {
// with addition, if we assume that some a is at least equal to some b, then the resulting bit length will
// be a's bit length or (a's bit length)+1, depending on carry bit.this is cheaper than calling bitLength.
let msword := mload(add(result,0x20)) // get most significant word of result
// if(msword==1 || msword>>(max_bitlen % 256)==1):
if or( eq(msword, 1), eq(shr(mod(max_bitlen,256),msword),1) ) {
// if(carry==1 || msword>>(max_bitlen % 256)==1):
if or( eq(carry, 1), eq(shr(mod(max_bitlen,256),msword),1) ) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should be fairly simple to illustrate with an example, right ?

Copy link
Contributor Author

@dovgopoly dovgopoly Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Say we have two normalized big numbers a = [a2, a1] and b = [b1].
a1 = 0000000000000000000000000000000000000000000000000000000000000001
a2 = 0000000000000000000000000000000000000000000000000000000000000001
b1 = 0000000000000000000000000000000000000000000000000000000000000001
The result would be r = [r2, r1] = [a2, a1 + b1].
r1 = 0000000000000000000000000000000000000000000000000000000000000002
r2 = 0000000000000000000000000000000000000000000000000000000000000001

Here you set the most significant word of the result to 1 if an overflow occurs. But in some cases msword equals 1 even without an overflow, as shown in the example (r2). This is why I suggest using carry instead of msword to determine whether to increase the bit length.

Copy link
Collaborator

@riordant riordant Oct 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for your contribution!
would you mind adding a unit test (BigNumbers.t.sol) implementing this example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@riordant Done

max_bitlen := add(max_bitlen, 1) // if msword's bit length is 1 greater
// than max_bitlen, OR overflow occured,
// new bitlen is max_bitlen+1.
Expand Down
29 changes: 29 additions & 0 deletions test/BigNumbers.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,35 @@ contract BigNumbersTest is Test {
assertEq(BigNumbers.bitLength(1 << 255), 256);
}

function testAdd() public {
BigNumber memory lhs = hex"010000000000000000000000000000000000000000000000000000000000000001".init(false);
BigNumber memory rhs = hex"01".init(false);

BigNumber memory r = lhs.add(rhs);

assertEq(r.val, hex"00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002");
assertEq(r.bitlen, 257);
assertEq(r.neg, false);

lhs = hex"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff".init(false);
rhs = hex"01".init(false);

r = lhs.add(rhs);

assertEq(r.val, hex"00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000");
assertEq(r.bitlen, 257);
assertEq(r.neg, false);

lhs = hex"0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff".init(false);
rhs = hex"01".init(false);

r = lhs.add(rhs);

assertEq(r.val, hex"00000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000");
assertEq(r.bitlen, 261);
assertEq(r.neg, false);
}

function testShiftRight() public {
// shift by value greater than word length
BigNumber memory r;
Expand Down
Loading