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

fix: Check that RuntimeBigNums have the same BigNumParams on operations #46

Merged
merged 5 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 10 additions & 0 deletions src/params.nr
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ 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.redc_param == other.redc_param)
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
}
}

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
19 changes: 18 additions & 1 deletion src/runtime_bignum.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::utils::map::map;
use crate::params::BigNumParams;
use crate::utils::map::map;

use crate::fns::{
constrained_ops::{
Expand Down 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)
}
}
Loading