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

Commit

Permalink
Merge pull request #19 from shuklaayush/fix/no-overflow
Browse files Browse the repository at this point in the history
Unpin compiler version
  • Loading branch information
shuklaayush authored Nov 28, 2023
2 parents 50e889d + cfd409b commit d368267
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 204 deletions.
3 changes: 1 addition & 2 deletions crates/biguint/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
name = "biguint"
authors = ["shuklaayush"]
type = "lib"
compiler_version = "0.9.0"

# compiler_version = "0.9.0"
58 changes: 30 additions & 28 deletions crates/biguint/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ mod utils;

// Top-level constants related to the size of BigUint56 limbs and bytes.
global BITS_PER_LIMB: Field = 56; /// Number of bits per limb.
global NUM_LIMBS: Field = 5; /// Number of limbs.
global NUM_LIMBS: Field = 5; /// Number of limbs.

global BYTES_PER_LIMB: Field = 7; /// Number of bytes per limb (BITS_PER_LIMB / 8).
global MAX_BITS: Field = 280; /// Maximum number of bits (BITS_PER_LIMB * NUM_LIMBS).
global MAX_BYTES: Field = 35; /// Maximum number of bytes (NUM_LIMBS * BYTES_PER_LIMB).
global MAX_BITS: Field = 280; /// Maximum number of bits (BITS_PER_LIMB * NUM_LIMBS).
global MAX_BYTES: Field = 35; /// Maximum number of bytes (NUM_LIMBS * BYTES_PER_LIMB).

// TODO/NOTES:
// 1. Noir doesn't support expressions on globals so these are hardcoded
Expand Down Expand Up @@ -383,21 +383,21 @@ impl BigUint56 {
fn test_from_bytes1() {
let bytes = [2 as u8];
let a = BigUint56::from_bytes(bytes);
assert(a.eq(BigUint56{ limbs: [2, 0, 0, 0, 0] }));
assert(a.eq(BigUint56 { limbs: [2, 0, 0, 0, 0] }));
}

#[test]
fn test_from_bytes2() {
let bytes = [255 as u8; 7];
let a = BigUint56::from_bytes(bytes);
assert(a.eq(BigUint56{ limbs: [0-1 as u56, 0, 0, 0, 0] }));
assert(a.eq(BigUint56 { limbs: [0xffffffffffffff, 0, 0, 0, 0] }));
}

#[test]
fn test_from_bytes3() {
let bytes = [255 as u8; 8];
let a = BigUint56::from_bytes(bytes);
assert(a.eq(BigUint56{ limbs: [0-1 as u56, 255, 0, 0, 0] }));
assert(a.eq(BigUint56 { limbs: [0xffffffffffffff, 255, 0, 0, 0] }));
}

#[test]
Expand All @@ -406,7 +406,7 @@ fn test_to_bytes1() {
let b = BigUint56::one();

let c = a.sub(b);
assert(c.to_bytes() == [0-1 as u8; MAX_BYTES]);
assert(c.to_bytes() == [0xff; MAX_BYTES]);
}

#[test]
Expand All @@ -429,16 +429,16 @@ fn test_add1() {

#[test]
fn test_add2() {
let a = BigUint56{ limbs: [0-1 as u56, 0-1 as u56, 0-1 as u56, 0-1 as u56, 0] };
let b = BigUint56{ limbs: [1, 0, 0, 0, 0] };
let a = BigUint56 { limbs: [0xffffffffffffff, 0xffffffffffffff, 0xffffffffffffff, 0xffffffffffffff, 0] };
let b = BigUint56 { limbs: [1, 0, 0, 0, 0] };
let sum = a.add(b);

assert(sum.eq(BigUint56{ limbs: [0, 0, 0, 0, 1] }));
assert(sum.eq(BigUint56 { limbs: [0, 0, 0, 0, 1] }));
}

#[test]
fn test_adc1() {
let a = BigUint56{ limbs: [0-1 as u56; NUM_LIMBS] };
let a = BigUint56 { limbs: [0xffffffffffffff; NUM_LIMBS] };
let b = BigUint56::one();
let (sum, carry) = a.adc(b);

Expand All @@ -457,20 +457,20 @@ fn test_sub1() {

#[test]
fn test_sub2() {
let a = BigUint56{ limbs: [1, 2, 0, 0, 0] };
let b = BigUint56{ limbs: [2, 0, 0, 0, 0] };
let a = BigUint56 { limbs: [1, 2, 0, 0, 0] };
let b = BigUint56 { limbs: [2, 0, 0, 0, 0] };
let diff = a.sub(b);

assert(diff.eq(BigUint56{ limbs: [0-1 as u56, 1, 0, 0, 0] }));
assert(diff.eq(BigUint56 { limbs: [0xffffffffffffff, 1, 0, 0, 0] }));
}

#[test]
fn test_sbb1() {
let a = BigUint56{ limbs: [1, 0, 0, 0, 0] };
let b = BigUint56{ limbs: [2, 0, 0, 0, 0] };
let a = BigUint56 { limbs: [1, 0, 0, 0, 0] };
let b = BigUint56 { limbs: [2, 0, 0, 0, 0] };
let (diff, borrow) = a.sbb(b);

assert(diff.eq(BigUint56{ limbs: [0-1 as u56; 5] }));
assert(diff.eq(BigUint56 { limbs: [0xffffffffffffff; 5] }));
assert(borrow >> (BITS_PER_LIMB as u56 - 1) == 1);
}

Expand All @@ -486,12 +486,16 @@ fn test_mul1() {

#[test]
fn test_mul2() {
let a = BigUint56{ limbs: [0-1 as u56; 5] };
let b = BigUint56{ limbs: [0-1 as u56; 5] };
let a = BigUint56 { limbs: [0xffffffffffffff; 5] };
let b = BigUint56 { limbs: [0xffffffffffffff; 5] };
let (lo, hi) = a.mul(b);

assert(lo.eq(BigUint56::one()));
assert(hi.eq(BigUint56{ limbs: [0-2 as u56, 0-1 as u56, 0-1 as u56, 0-1 as u56, 0-1 as u56] }));
assert(
hi.eq(
BigUint56 { limbs: [0xfffffffffffffe, 0xffffffffffffff, 0xffffffffffffff, 0xffffffffffffff, 0xffffffffffffff] }
)
);
}

#[test]
Expand Down Expand Up @@ -573,9 +577,7 @@ fn test_shl6() {
let a = BigUint56::from_u56(0x80000000000000);
let b = a.shl1();

assert(b.eq(BigUint56 {
limbs: [0, 1, 0, 0, 0],
}));
assert(b.eq(BigUint56 { limbs: [0, 1, 0, 0, 0] }));
}

#[test]
Expand Down Expand Up @@ -634,7 +636,7 @@ fn test_bits2() {

#[test]
fn test_bits3() {
let a = BigUint56::from_bytes([0-1 as u8]);
let a = BigUint56::from_bytes([0xff]);
let b = a.nbits();

assert(b == 8);
Expand Down Expand Up @@ -672,13 +674,13 @@ fn test_div2() {

#[test]
fn test_div3() {
let a = BigUint56{ limbs: [2, 0, 1, 0, 0] };
let b = BigUint56{ limbs: [0, 1, 0, 0, 0] };
let a = BigUint56 { limbs: [2, 0, 1, 0, 0] };
let b = BigUint56 { limbs: [0, 1, 0, 0, 0] };

let (q, r) = a.div(b);

assert(q.eq(BigUint56{ limbs: [0, 1, 0, 0, 0] }));
assert(r.eq(BigUint56{ limbs: [2, 0, 0, 0, 0] }));
assert(q.eq(BigUint56 { limbs: [0, 1, 0, 0, 0] }));
assert(r.eq(BigUint56 { limbs: [2, 0, 0, 0, 0] }));
}

#[test]
Expand Down
3 changes: 2 additions & 1 deletion crates/biguint/src/utils.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::BITS_PER_LIMB as BITS;
use dep::std;

// Compute a + b + carry, returning the result and the new carry over.
// TODO: Does carry need to be a u56?
Expand All @@ -9,7 +10,7 @@ pub fn adc(a: u56, b: u56, carry: u56) -> (u56, u56) {

// Compute a - (b + borrow), returning the result and the new borrow.
pub fn sbb(a: u56, b: u56, borrow: u56) -> (u56, u56) {
let ret = a as u112 - (b as u112 + (borrow as u112 >> (BITS as u112 - 1)));
let ret = std::wrapping_sub(a as u112, b as u112 + (borrow as u112 >> (BITS as u112 - 1)));
(ret as u56, (ret >> 56) as u56)
}

Expand Down
4 changes: 2 additions & 2 deletions crates/curves/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
name = "curves"
authors = ["shuklaayush"]
type = "lib"
compiler_version = "0.9.0"
# compiler_version = "0.9.0"

[dependencies]
biguint = { path = "../biguint" }
primefield = { path = "../primefield" }
primefield = { path = "../primefield" }
Loading

0 comments on commit d368267

Please sign in to comment.