Skip to content

Commit

Permalink
chore: eprintln on violations
Browse files Browse the repository at this point in the history
  • Loading branch information
nhtyy committed Nov 21, 2024
1 parent 964054d commit b81be57
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 3 deletions.
10 changes: 8 additions & 2 deletions crates/core/executor/src/events/precompiles/ec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,12 @@ pub fn create_ec_add_event<E: EllipticCurve>(
let start_clk = rt.clk;
let p_ptr = arg1;
if p_ptr % 4 > 0 {
eprintln!("EC_ADD: ptr alignment violation");
return rt.invariant_violated();
}
let q_ptr = arg2;
if q_ptr % 4 > 0 {
eprintln!("EC_ADD: ptr alignment violation");
return rt.invariant_violated();
}

Expand All @@ -127,11 +129,13 @@ pub fn create_ec_add_event<E: EllipticCurve>(

let p_affine = AffinePoint::<E>::from_words_le(&p);
if !E::is_valid_point(&p_affine) {
eprintln!("EC_ADD: invalid p point, invariant violation");
return rt.invariant_violated();
}

let q_affine = AffinePoint::<E>::from_words_le(&q);
if !E::is_valid_point(&q_affine) {
eprintln!("EC_ADD: invalid q point, invariant violation");
return rt.invariant_violated();
}

Expand Down Expand Up @@ -176,6 +180,7 @@ pub fn create_ec_double_event<E: EllipticCurve>(

let p_affine = AffinePoint::<E>::from_words_le(&p);
if !E::is_valid_point(&p_affine) {
eprintln!("EC_DOUBLE: invalid point, invariant violation");
return rt.invariant_violated();
}

Expand Down Expand Up @@ -226,9 +231,9 @@ pub fn create_ec_decompress_event<E: EllipticCurve>(
x_bytes_be.reverse();
x_bytes_be
};

// The decompress_fn takes in an X coordinate and a parity,
// This means whatever point we get is guarnteed to be on the curve
// This means whatever point we get is guarnteed to be on the curve
// (it computes the corresponding y)
//
// However its falliable if the X coordinate is not in the field
Expand All @@ -240,6 +245,7 @@ pub fn create_ec_decompress_event<E: EllipticCurve>(
};

let Some(computed_point) = decompress_fn(&x_bytes_be, sign_bit) else {
eprintln!("EC_DECOMPRESS: decompression failed, invariant violation");
return rt.invariant_violated();
};

Expand Down
4 changes: 3 additions & 1 deletion crates/core/executor/src/syscalls/precompiles/fptower/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ impl<P: FpOpField> Syscall for FpOpSyscall<P> {
let y_ptr = arg2;
// Need to check alignment
if x_ptr % 4 > 0 || y_ptr % 4 > 0 {
eprintln!("FpOpSyscall: alignment violation");
return rt.invariant_violated();
}


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

let x = rt.slice_unsafe(x_ptr, num_words);
Expand All @@ -47,11 +47,13 @@ impl<P: FpOpField> Syscall for FpOpSyscall<P> {
let modulus = &BigUint::from_bytes_le(P::MODULUS);
let a = BigUint::from_slice(&x);
if &a >= modulus {
eprintln!("FpOpSyscall: a >= modulus, invariant violation");
return rt.invariant_violated();
}

let b = BigUint::from_slice(&y);
if &b >= modulus {
eprintln!("FpOpSyscall: b >= modulus, invariant violation");
return rt.invariant_violated();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl<P: FpOpField> Syscall for Fp2AddSubSyscall<P> {
let modulus = &BigUint::from_bytes_le(P::MODULUS);

if ac0 >= modulus || ac1 >= modulus || bc0 >= modulus || bc1 >= modulus {
eprintln!("Fp2AddSubSyscall: invariant violation, inputs greater than modulus");
return rt.invariant_violated();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl<P: FpOpField> Syscall for Fp2MulSyscall<P> {
let modulus = &BigUint::from_bytes_le(P::MODULUS);

if ac0 >= modulus || ac1 >= modulus || bc0 >= modulus || bc1 >= modulus {
eprintln!("Fp2MulSyscall: invariant violation, inputs greater than modulus");
return rt.invariant_violated();
}

Expand Down
1 change: 1 addition & 0 deletions crates/core/executor/src/syscalls/precompiles/uint256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ impl Syscall for Uint256MulSyscall {

// Check alignment.
if x_ptr % 4 > 0 || y_ptr % 4 > 0 {
eprintln!("Uint256 Precompile: Ptr alignment violation.");
return rt.invariant_violated();
}

Expand Down

0 comments on commit b81be57

Please sign in to comment.