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

feat: Add is_zero #111

Merged
merged 6 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion src/bignum.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use crate::params::BigNumParamsGetter;
use crate::fns::{
constrained_ops::{
add, assert_is_not_equal, conditional_select, derive_from_seed, div, eq, from_field,
limbs_to_field, mul, neg, sub, udiv, udiv_mod, umod, validate_in_field, validate_in_range,
is_zero, limbs_to_field, mul, neg, sub, udiv, udiv_mod, umod, validate_in_field,
validate_in_range,
},
expressions::{__compute_quadratic_expression, evaluate_quadratic_expression},
serialization::{from_be_bytes, to_le_bytes},
Expand Down Expand Up @@ -84,6 +85,7 @@ pub trait BigNumTrait: Neg + Add + Sub + Mul + Div + Eq {
pub fn umod(self, divisor: Self) -> Self;

pub fn conditional_select(lhs: Self, rhs: Self, predicate: bool) -> Self;
pub fn is_zero(self) -> bool;
}

impl<let N: u32, let MOD_BITS: u32, Params> std::convert::From<Field> for BigNum<N, MOD_BITS, Params>
Expand Down Expand Up @@ -317,6 +319,10 @@ where
fn conditional_select(lhs: Self, rhs: Self, predicate: bool) -> Self {
Self { limbs: conditional_select(lhs.limbs, rhs.limbs, predicate) }
}

fn is_zero(self) -> bool {
is_zero::<N, MOD_BITS>(self.limbs)
}
}

// impl<let N: u32, Params> BigNumTrait<N, Params> for BigNum<N, Params> where Params: BigNumParamsGetter<N> {}
Expand Down
4 changes: 4 additions & 0 deletions src/fns/constrained_ops.nr
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ pub(crate) fn eq<let N: u32, let MOD_BITS: u32>(
is_equal_modulus | is_equal_zero
}

pub(crate) fn is_zero<let N: u32, let MOD_BITS: u32>(val: [Field; N]) -> bool {
val.all(|limb| limb == 0)
}

pub(crate) fn validate_in_field<let N: u32, let MOD_BITS: u32>(
params: P<N, MOD_BITS>,
val: [Field; N],
Expand Down
9 changes: 7 additions & 2 deletions src/runtime_bignum.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ 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,
add, assert_is_not_equal, conditional_select, derive_from_seed, div, eq, is_zero, mul, neg,
sub, udiv, udiv_mod, umod, validate_in_field, validate_in_range,
},
expressions::{__compute_quadratic_expression, evaluate_quadratic_expression},
serialization::{from_be_bytes, to_le_bytes},
Expand Down Expand Up @@ -112,6 +112,7 @@ pub(crate) trait RuntimeBigNumTrait<let N: u32, let MOD_BITS: u32>: Neg + Add +
pub fn umod(numerator: Self, divisor: Self) -> Self;

pub fn conditional_select(lhs: Self, rhs: Self, predicate: bool) -> Self;
pub fn is_zero(self) -> bool;
}

impl<let N: u32, let MOD_BITS: u32> Neg for RuntimeBigNum<N, MOD_BITS> {
Expand Down Expand Up @@ -379,6 +380,10 @@ impl<let N: u32, let MOD_BITS: u32> RuntimeBigNumTrait<N, MOD_BITS> for RuntimeB
assert(params == rhs.params);
Self { limbs: conditional_select(lhs.limbs, rhs.limbs, predicate), params }
}

fn is_zero(self) -> bool {
is_zero::<N, MOD_BITS>(self.limbs)
}
}

impl<let N: u32, let MOD_BITS: u32> std::ops::Add for RuntimeBigNum<N, MOD_BITS> {
Expand Down
26 changes: 26 additions & 0 deletions src/tests/bignum_test.nr
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,16 @@ where
assert(c.eq(a) == false);
}

fn test_is_zero<let N: u32, BN>()
where
BN: BigNumTrait,
{
let a = BN::derive_from_seed([1, 2, 3, 4]);
let zero = BN::zero();
assert(a.is_zero() == false);
assert(zero.is_zero() == true);
}

// fn test_eq<let N: u32, Params>(_: BigNum<N, Params>) where Params: BigNumParamsGetter<N> + RuntimeBigNumParamsTrait<N> {
// let a = BigNum::__derive_from_seed([1, 2, 3, 4]);
// let b = BigNum::__derive_from_seed([1, 2, 3, 4]);
Expand Down Expand Up @@ -410,6 +420,12 @@ fn test_eq_BN() {
// let stub: Fq = BigNum::new();
test_eq::<3, Fq>();
}

#[test]
fn test_is_zero_BN() {
test_is_zero::<3, Fq>();
}

#[test]
fn test_add_BN() {
let mut a: Fq = BigNum::modulus();
Expand Down Expand Up @@ -544,6 +560,11 @@ fn test_eq_2048() {
test_eq::<18, BN2048>();
}

#[test]
fn test_is_zero_2048() {
test_is_zero::<18, BN2048>();
}

#[test]
fn test_mul_2048() {
test_mul::<18, BN2048>();
Expand Down Expand Up @@ -584,6 +605,11 @@ fn test_eq_U256() {
test_eq::<3, BN256>();
}

#[test]
fn test_is_zero_U256() {
test_is_zero::<3, BN256>();
}

#[test]
fn test_mul_U256() {
test_mul::<3, BN256>();
Expand Down
11 changes: 11 additions & 0 deletions src/tests/runtime_bignum_test.nr
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,17 @@ fn test_eq() {
assert((c == a) == false);
}

#[test]
fn test_is_zero() {
let params = $typ ::get_params();

let a: $RuntimeBigNum<$N, $MOD_BITS> = $RuntimeBigNum::new(params);
let b: $RuntimeBigNum<$N, $MOD_BITS> = $RuntimeBigNum::derive_from_seed(params, [1, 2, 3, 4]);

assert(a.is_zero() == true);
assert(b.is_zero() == false);
}

}
}

Expand Down