Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Fixed Limb Add
Browse files Browse the repository at this point in the history
  • Loading branch information
jalextowle committed Mar 26, 2020
1 parent 03616bc commit ae82725
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions contracts/LibLimb.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pragma solidity ^0.5.11;


library LibLimb {

using LibLimb for *;

struct Branch {
uint256[] limbs;
}
Expand All @@ -33,15 +36,20 @@ library LibLimb {
returns (Branch memory c)
{
uint256 carry = 0;
for (uint i = 0; i < max(a.limbs.length, b.limbs.length); i++) {
c.limbs[i] = a.limbs[i] + b.limbs[i] + carry;
carry = shouldCarry(a.limbs[i], b.limbs[i], carry);
uint256 numLimbs = max(a.limbs.length, b.limbs.length);
c.limbs = new uint256[](numLimbs);
for (uint i = 0; i < numLimbs; i++) {
uint256 limbA = a.limbs.get(i);
uint256 limbB = b.limbs.get(i);
c.limbs[i] = limbA + limbB + carry;
carry = shouldAdditionCarry(limbA, limbB, carry);
}
if (carry > 0) {
append(c, carry);
}
}

// FIXME(jalextowle): This is not fully implemented yet.
function sub(
Branch memory a,
Branch memory b
Expand All @@ -50,7 +58,14 @@ library LibLimb {
pure
returns (Branch memory c)
{

uint256 carry = 0;
for (uint i = 0; i < max(a.limbs.length, b.limbs.length); i++) {
c.limbs[i] = a.limbs[i] - b.limbs[i] - carry;
carry = shouldAdditionCarry(a.limbs[i], b.limbs[i], carry);
}
if (carry > 0) {
append(c, carry);
}
}

function append(
Expand Down Expand Up @@ -82,6 +97,17 @@ library LibLimb {
}
}

function get(
uint256[] memory array,
uint256 idx
)
internal
pure
returns (uint256)
{
return idx < array.length ? array[idx] : 0;
}

function max(
uint256 a,
uint256 b
Expand All @@ -93,10 +119,10 @@ library LibLimb {
return a > b ? a : b;
}

function shouldCarry(
function shouldAdditionCarry(
uint256 a,
uint256 b,
uint256 c,
uint256 c
)
private
pure
Expand Down

0 comments on commit ae82725

Please sign in to comment.