Skip to content

Commit

Permalink
dev: implement Zero for u384 (#6329)
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat authored Sep 4, 2024
1 parent 74c1bdc commit 7eb3c15
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
14 changes: 14 additions & 0 deletions corelib/src/circuit.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -571,3 +571,17 @@ impl U384TryIntoU256 of TryInto<u384, u256> {
conversions::try_into_u256(self)
}
}

impl U384Zero of crate::num::traits::Zero<u384> {
fn zero() -> u384 {
u384 { limb0: 0, limb1: 0, limb2: 0, limb3: 0 }
}

fn is_zero(self: @u384) -> bool {
*self == Self::zero()
}

fn is_non_zero(self: @u384) -> bool {
!self.is_zero()
}
}
8 changes: 8 additions & 0 deletions corelib/src/test/circuit_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::circuit::{
AddInputResultTrait, CircuitInputs,
};

use crate::num::traits::Zero;
use crate::traits::TryInto;

#[test]
Expand Down Expand Up @@ -133,3 +134,10 @@ fn test_fill_inputs_loop() {
let modulus = TryInto::<_, CircuitModulus>::try_into([55, 0, 0, 0]).unwrap();
circuit_inputs.done().eval(modulus).unwrap();
}

#[test]
fn test_u384_zero() {
assert_eq!(Zero::zero(), u384 { limb0: 0, limb1: 0, limb2: 0, limb3: 0 });
assert!(Zero::is_zero(@u384 { limb0: 0, limb1: 0, limb2: 0, limb3: 0 }));
assert!(Zero::is_non_zero(@u384 { limb0: 0, limb1: 1, limb2: 0, limb3: 0 }));
}

0 comments on commit 7eb3c15

Please sign in to comment.