Skip to content

Commit

Permalink
fix: Check that RuntimeBigNums have the same BigNumParams on oper…
Browse files Browse the repository at this point in the history
…ations (#46)

Co-authored-by: Tom French <[email protected]>
Co-authored-by: Tom French <[email protected]>
  • Loading branch information
3 people authored Nov 8, 2024
1 parent 53f652b commit 729dd24
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/params.nr
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ impl<let N: u32, let MOD_BITS: u32> BigNumParams<N, MOD_BITS> {
}
}

impl<let N: u32, let MOD_BITS: u32> std::cmp::Eq for BigNumParams<N, MOD_BITS> {
fn eq(self, other: Self) -> bool {
(self.has_multiplicative_inverse == other.has_multiplicative_inverse)
& (self.modulus == other.modulus)
& (self.modulus_u60 == other.modulus_u60)
& (self.modulus_u60_x4 == other.modulus_u60_x4)
& (self.double_modulus == other.double_modulus)
& (self.redc_param == other.redc_param)
}
}

fn get_double_modulus<let N: u32>(modulus: [Field; N]) -> [Field; N] {
let TWO_POW_120: Field = 0x1000000000000000000000000000000;
let m: U60Repr<N, 2> = U60Repr::from(modulus);
Expand Down
17 changes: 17 additions & 0 deletions src/runtime_bignum.nr
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ impl<let N: u32, let MOD_BITS: u32> RuntimeBigNumTrait<N, MOD_BITS> for RuntimeB
}

unconstrained fn __eq(self, other: Self) -> bool {
assert(self.params == other.params);
__eq(self.limbs, other.limbs)
}

Expand All @@ -218,34 +219,39 @@ impl<let N: u32, let MOD_BITS: u32> RuntimeBigNumTrait<N, MOD_BITS> for RuntimeB
// UNCONSTRAINED! (Hence `__` prefix).
fn __add(self, other: Self) -> Self {
let params = self.params;
assert(params == other.params);
let limbs = unsafe { __add(params, self.limbs, other.limbs) };
Self { params, limbs }
}

// UNCONSTRAINED! (Hence `__` prefix).
fn __sub(self, other: Self) -> Self {
let params = self.params;
assert(params == other.params);
let limbs = unsafe { __sub(params, self.limbs, other.limbs) };
Self { params, limbs }
}

// UNCONSTRAINED! (Hence `__` prefix).
fn __mul(self, other: Self) -> Self {
let params = self.params;
assert(params == other.params);
let limbs = unsafe { __mul::<_, MOD_BITS>(params, self.limbs, other.limbs) };
Self { params, limbs }
}

// UNCONSTRAINED! (Hence `__` prefix).
fn __div(self, divisor: Self) -> Self {
let params = self.params;
assert(params == divisor.params);
let limbs = unsafe { __div::<_, MOD_BITS>(params, self.limbs, divisor.limbs) };
Self { params, limbs }
}

// UNCONSTRAINED! (Hence `__` prefix).
fn __udiv_mod(self, divisor: Self) -> (Self, Self) {
let params = self.params;
assert(params == divisor.params);
let (q, r) = unsafe { __udiv_mod(self.limbs, divisor.limbs) };
(Self { limbs: q, params }, Self { limbs: r, params })
}
Expand All @@ -261,6 +267,7 @@ impl<let N: u32, let MOD_BITS: u32> RuntimeBigNumTrait<N, MOD_BITS> for RuntimeB
// UNCONSTRAINED! (Hence `__` prefix).
fn __pow(self, exponent: Self) -> Self {
let params = self.params;
assert(params == exponent.params);
let limbs = unsafe { __pow::<_, MOD_BITS>(params, self.limbs, exponent.limbs) };
Self { limbs, params }
}
Expand Down Expand Up @@ -348,6 +355,7 @@ impl<let N: u32, let MOD_BITS: u32> RuntimeBigNumTrait<N, MOD_BITS> for RuntimeB

fn assert_is_not_equal(self, other: Self) {
let params = self.params;
assert(params == other.params);
assert_is_not_equal(params, self.limbs, other.limbs);
}

Expand All @@ -358,22 +366,26 @@ impl<let N: u32, let MOD_BITS: u32> RuntimeBigNumTrait<N, MOD_BITS> for RuntimeB

fn udiv_mod(self, divisor: Self) -> (Self, Self) {
let params = self.params;
assert(params == divisor.params);
let (q, r) = udiv_mod::<_, MOD_BITS>(params, self.limbs, divisor.limbs);
(Self { limbs: q, params }, Self { limbs: r, params })
}

fn udiv(self, divisor: Self) -> Self {
let params = self.params;
assert(params == divisor.params);
Self { limbs: udiv::<_, MOD_BITS>(params, self.limbs, divisor.limbs), params }
}

fn umod(self, divisor: Self) -> Self {
let params = self.params;
assert(params == divisor.params);
Self { limbs: umod::<_, MOD_BITS>(params, self.limbs, divisor.limbs), params }
}

fn conditional_select(lhs: Self, rhs: Self, predicate: bool) -> Self {
let params = lhs.params;
assert(params == rhs.params);
Self { limbs: conditional_select(lhs.limbs, rhs.limbs, predicate), params }
}
}
Expand All @@ -383,6 +395,7 @@ impl<let N: u32, let MOD_BITS: u32> std::ops::Add for RuntimeBigNum<N, MOD_BITS>
// via evaluate_quadratic_expression
fn add(self, other: Self) -> Self {
let params = self.params;
assert(params == other.params);
Self { limbs: add::<_, MOD_BITS>(params, self.limbs, other.limbs), params }
}
}
Expand All @@ -392,6 +405,7 @@ impl<let N: u32, let MOD_BITS: u32> std::ops::Sub for RuntimeBigNum<N, MOD_BITS>
// via evaluate_quadratic_expression
fn sub(self, other: Self) -> Self {
let params = self.params;
assert(params == other.params);
Self { limbs: sub::<_, MOD_BITS>(params, self.limbs, other.limbs), params }
}
}
Expand All @@ -403,6 +417,7 @@ impl<let N: u32, let MOD_BITS: u32> std::ops::Mul for RuntimeBigNum<N, MOD_BITS>
// will create much fewer constraints than calling `mul` and `add` directly
fn mul(self, other: Self) -> Self {
let params = self.params;
assert(params == other.params);
Self { limbs: mul::<_, MOD_BITS>(params, self.limbs, other.limbs), params }
}
}
Expand All @@ -411,13 +426,15 @@ impl<let N: u32, let MOD_BITS: u32> std::ops::Div for RuntimeBigNum<N, MOD_BITS>
// Note: this method is expensive! Witness computation is extremely expensive as it requires modular exponentiation
fn div(self, divisor: Self) -> Self {
let params = self.params;
assert(params == divisor.params);
Self { limbs: div::<_, MOD_BITS>(params, self.limbs, divisor.limbs), params }
}
}

impl<let N: u32, let MOD_BITS: u32> std::cmp::Eq for RuntimeBigNum<N, MOD_BITS> {
fn eq(self, other: Self) -> bool {
let params = self.params;
assert(params == other.params);
eq::<_, MOD_BITS>(params, self.limbs, other.limbs)
}
}

0 comments on commit 729dd24

Please sign in to comment.