From f966f4ac42cefb688515c1a47be7af9e6f58e3fb Mon Sep 17 00:00:00 2001 From: Michael Connor Date: Tue, 5 Nov 2024 10:54:16 +0000 Subject: [PATCH 1/4] eq checks on params --- src/params.nr | 10 ++++++++++ src/runtime_bignum.nr | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/params.nr b/src/params.nr index 01854ab3..453ebd7a 100644 --- a/src/params.nr +++ b/src/params.nr @@ -39,6 +39,16 @@ impl BigNumParams { } } +impl std::cmp::Eq for BigNumParams { + 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) + } +} + fn get_double_modulus(modulus: [Field; N]) -> [Field; N] { let TWO_POW_120: Field = 0x1000000000000000000000000000000; let m: U60Repr = U60Repr::from(modulus); diff --git a/src/runtime_bignum.nr b/src/runtime_bignum.nr index c66bf2a2..77d661f5 100644 --- a/src/runtime_bignum.nr +++ b/src/runtime_bignum.nr @@ -201,6 +201,7 @@ impl RuntimeBigNumTrait for RuntimeB } unconstrained fn __eq(self, other: Self) -> bool { + assert(self.params == other.params); __eq(self.limbs, other.limbs) } @@ -218,6 +219,7 @@ impl RuntimeBigNumTrait 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 } } @@ -225,6 +227,7 @@ impl RuntimeBigNumTrait for RuntimeB // 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 } } @@ -232,6 +235,7 @@ impl RuntimeBigNumTrait for RuntimeB // 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 } } @@ -239,6 +243,7 @@ impl RuntimeBigNumTrait for RuntimeB // 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 } } @@ -246,6 +251,7 @@ impl RuntimeBigNumTrait for RuntimeB // 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 }) } @@ -261,6 +267,7 @@ impl RuntimeBigNumTrait 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 } } @@ -348,6 +355,7 @@ impl RuntimeBigNumTrait 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); } @@ -358,22 +366,26 @@ impl RuntimeBigNumTrait 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 } } } @@ -383,6 +395,7 @@ impl std::ops::Add for RuntimeBigNum // 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 } } } @@ -392,6 +405,7 @@ impl std::ops::Sub for RuntimeBigNum // 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 } } } @@ -403,6 +417,7 @@ impl std::ops::Mul for RuntimeBigNum // 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 } } } @@ -411,6 +426,7 @@ impl std::ops::Div for RuntimeBigNum // 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 } } } @@ -418,6 +434,7 @@ impl std::ops::Div for RuntimeBigNum impl std::cmp::Eq for RuntimeBigNum { fn eq(self, other: Self) -> bool { let params = self.params; + assert(params == other.params); eq::<_, MOD_BITS>(params, self.limbs, other.limbs) } } From 193838674c8fa4b5cb0f6558819264c02eb59d89 Mon Sep 17 00:00:00 2001 From: Michael Connor Date: Tue, 5 Nov 2024 10:56:33 +0000 Subject: [PATCH 2/4] fmt --- src/bignum.nr | 3 ++- src/fns/constrained_ops.nr | 3 ++- src/params.nr | 10 +++++----- src/runtime_bignum.nr | 5 +++-- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/bignum.nr b/src/bignum.nr index 787d0ed0..5b9fe7c9 100644 --- a/src/bignum.nr +++ b/src/bignum.nr @@ -6,7 +6,8 @@ use crate::fns::{ constrained_ops::{ add, assert_is_not_equal, conditional_select, derive_from_seed, div, eq, mul, neg, sub, udiv, udiv_mod, umod, validate_in_field, validate_in_range, - }, expressions::{__compute_quadratic_expression, evaluate_quadratic_expression}, + }, + expressions::{__compute_quadratic_expression, evaluate_quadratic_expression}, serialization::{from_be_bytes, to_le_bytes}, unconstrained_ops::{ __add, __batch_invert, __batch_invert_slice, __derive_from_seed, __div, __eq, __invmod, diff --git a/src/fns/constrained_ops.nr b/src/fns/constrained_ops.nr index d457cf85..d0b70e20 100644 --- a/src/fns/constrained_ops.nr +++ b/src/fns/constrained_ops.nr @@ -5,7 +5,8 @@ use crate::fns::{ unconstrained_helpers::{ __add_with_flags, __neg_with_flags, __sub_with_flags, __validate_gt_remainder, __validate_in_field_compute_borrow_flags, - }, unconstrained_ops::{__div, __mul, __udiv_mod}, + }, + unconstrained_ops::{__div, __mul, __udiv_mod}, }; /** diff --git a/src/params.nr b/src/params.nr index 453ebd7a..f856995b 100644 --- a/src/params.nr +++ b/src/params.nr @@ -41,11 +41,11 @@ impl BigNumParams { impl std::cmp::Eq for BigNumParams { 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) + (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) } } diff --git a/src/runtime_bignum.nr b/src/runtime_bignum.nr index 77d661f5..ec141ebd 100644 --- a/src/runtime_bignum.nr +++ b/src/runtime_bignum.nr @@ -1,11 +1,12 @@ -use crate::utils::map::map; use crate::params::BigNumParams; +use crate::utils::map::map; use crate::fns::{ constrained_ops::{ add, assert_is_not_equal, conditional_select, derive_from_seed, div, eq, mul, neg, sub, udiv, udiv_mod, umod, validate_in_field, validate_in_range, - }, expressions::{__compute_quadratic_expression, evaluate_quadratic_expression}, + }, + expressions::{__compute_quadratic_expression, evaluate_quadratic_expression}, serialization::{from_be_bytes, to_le_bytes}, unconstrained_ops::{ __add, __batch_invert, __batch_invert_slice, __derive_from_seed, __div, __eq, __invmod, From 4a7384155adc3d9e7ba7e5fba312383feac3a41f Mon Sep 17 00:00:00 2001 From: Michael Connor Date: Tue, 5 Nov 2024 10:57:49 +0000 Subject: [PATCH 3/4] fmt with 0.36.0 instead of nightly --- src/bignum.nr | 3 +-- src/fns/constrained_ops.nr | 3 +-- src/runtime_bignum.nr | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/bignum.nr b/src/bignum.nr index 5b9fe7c9..787d0ed0 100644 --- a/src/bignum.nr +++ b/src/bignum.nr @@ -6,8 +6,7 @@ use crate::fns::{ constrained_ops::{ add, assert_is_not_equal, conditional_select, derive_from_seed, div, eq, mul, neg, sub, udiv, udiv_mod, umod, validate_in_field, validate_in_range, - }, - expressions::{__compute_quadratic_expression, evaluate_quadratic_expression}, + }, expressions::{__compute_quadratic_expression, evaluate_quadratic_expression}, serialization::{from_be_bytes, to_le_bytes}, unconstrained_ops::{ __add, __batch_invert, __batch_invert_slice, __derive_from_seed, __div, __eq, __invmod, diff --git a/src/fns/constrained_ops.nr b/src/fns/constrained_ops.nr index d0b70e20..d457cf85 100644 --- a/src/fns/constrained_ops.nr +++ b/src/fns/constrained_ops.nr @@ -5,8 +5,7 @@ use crate::fns::{ unconstrained_helpers::{ __add_with_flags, __neg_with_flags, __sub_with_flags, __validate_gt_remainder, __validate_in_field_compute_borrow_flags, - }, - unconstrained_ops::{__div, __mul, __udiv_mod}, + }, unconstrained_ops::{__div, __mul, __udiv_mod}, }; /** diff --git a/src/runtime_bignum.nr b/src/runtime_bignum.nr index ec141ebd..0325ca54 100644 --- a/src/runtime_bignum.nr +++ b/src/runtime_bignum.nr @@ -5,8 +5,7 @@ use crate::fns::{ constrained_ops::{ add, assert_is_not_equal, conditional_select, derive_from_seed, div, eq, mul, neg, sub, udiv, udiv_mod, umod, validate_in_field, validate_in_range, - }, - expressions::{__compute_quadratic_expression, evaluate_quadratic_expression}, + }, expressions::{__compute_quadratic_expression, evaluate_quadratic_expression}, serialization::{from_be_bytes, to_le_bytes}, unconstrained_ops::{ __add, __batch_invert, __batch_invert_slice, __derive_from_seed, __div, __eq, __invmod, From 0428c7094d65984ac3da44f7119b9de9a654e11c Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Fri, 8 Nov 2024 11:41:08 +0000 Subject: [PATCH 4/4] Update src/params.nr --- src/params.nr | 1 + 1 file changed, 1 insertion(+) diff --git a/src/params.nr b/src/params.nr index f856995b..f5beb504 100644 --- a/src/params.nr +++ b/src/params.nr @@ -45,6 +45,7 @@ impl std::cmp::Eq for BigNumParams { & (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) } }