Skip to content

Commit

Permalink
fix: fp2 invariant checks
Browse files Browse the repository at this point in the history
  • Loading branch information
nhtyy committed Nov 21, 2024
1 parent d6eb344 commit 58787d3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
9 changes: 4 additions & 5 deletions crates/core/executor/src/syscalls/precompiles/fptower/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@ impl<P: FpOpField> Syscall for FpOpSyscall<P> {
) -> Option<u32> {
let clk = rt.clk;
let x_ptr = arg1;
if x_ptr % 4 != 0 {
panic!();
}
let y_ptr = arg2;
if y_ptr % 4 != 0 {
panic!();
// Need to check alignment
if x_ptr % 4 > 0 || y_ptr % 4 > 0 {
return rt.invariant_violated();
}


let num_words = <P as NumWords>::WordsFieldElement::USIZE;

let x = rt.slice_unsafe(x_ptr, num_words);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ impl<P: FpOpField> Syscall for Fp2AddSubSyscall<P> {
) -> Option<u32> {
let clk = rt.clk;
let x_ptr = arg1;
if x_ptr % 4 != 0 {
panic!();
}
let y_ptr = arg2;
if y_ptr % 4 != 0 {
panic!();
// Need to check alignment
if x_ptr % 4 > 0 || y_ptr % 4 > 0 {
return rt.invariant_violated();
}

let num_words = <P as NumWords>::WordsCurvePoint::USIZE;
Expand All @@ -55,6 +53,10 @@ impl<P: FpOpField> Syscall for Fp2AddSubSyscall<P> {
let bc1 = &BigUint::from_slice(bc1);
let modulus = &BigUint::from_bytes_le(P::MODULUS);

if ac0 >= modulus || ac1 >= modulus || bc0 >= modulus || bc1 >= modulus {
return rt.invariant_violated();
}

let (c0, c1) = match self.op {
FieldOperation::Add => ((ac0 + bc0) % modulus, (ac1 + bc1) % modulus),
FieldOperation::Sub => {
Expand Down
12 changes: 7 additions & 5 deletions crates/core/executor/src/syscalls/precompiles/fptower/fp2_mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ impl<P: FpOpField> Syscall for Fp2MulSyscall<P> {
) -> Option<u32> {
let clk = rt.clk;
let x_ptr = arg1;
if x_ptr % 4 != 0 {
panic!();
}
let y_ptr = arg2;
if y_ptr % 4 != 0 {
panic!();
// Need to check alignment
if x_ptr % 4 > 0 || y_ptr % 4 > 0 {
return rt.invariant_violated();
}

let num_words = <P as NumWords>::WordsCurvePoint::USIZE;
Expand All @@ -55,6 +53,10 @@ impl<P: FpOpField> Syscall for Fp2MulSyscall<P> {
let bc1 = &BigUint::from_slice(bc1);
let modulus = &BigUint::from_bytes_le(P::MODULUS);

if ac0 >= modulus || ac1 >= modulus || bc0 >= modulus || bc1 >= modulus {
return rt.invariant_violated();
}

#[allow(clippy::match_bool)]
let c0 = match (ac0 * bc0) % modulus < (ac1 * bc1) % modulus {
true => ((modulus + (ac0 * bc0) % modulus) - (ac1 * bc1) % modulus) % modulus,
Expand Down

0 comments on commit 58787d3

Please sign in to comment.