Skip to content

Commit

Permalink
fix: uint256 precompile invaraint checks
Browse files Browse the repository at this point in the history
  • Loading branch information
nhtyy committed Nov 21, 2024
1 parent 58787d3 commit 964054d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ impl Syscall for Keccak256PermuteSyscall {
let start_clk = rt.clk;
let state_ptr = arg1;
if arg2 != 0 {
panic!("Expected arg2 to be 0, got {arg2}");
eprintln!("Expected arg2 to be 0, got {arg2}, this violates the Keccak precompile invariant.");
return rt.invariant_violated();
}

let mut state_read_records = Vec::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ impl Syscall for Sha256CompressSyscall {
) -> Option<u32> {
let w_ptr = arg1;
let h_ptr = arg2;
assert_ne!(w_ptr, h_ptr);
if w_ptr == h_ptr {
eprintln!("w_ptr == h_ptr, violation of the sha256 invariant");
return rt.invariant_violated();
}

let start_clk = rt.clk;
let mut h_read_records = Vec::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ impl Syscall for Sha256ExtendSyscall {
) -> Option<u32> {
let clk_init = rt.clk;
let w_ptr = arg1;
assert!(arg2 == 0, "arg2 must be 0");
if arg2 != 0 {
eprintln!("Warning: sha256_extend syscall arg2 is not zero, this violates the precompile invariants");

return rt.invariant_violated();
}

let w_ptr_init = w_ptr;
let mut w_i_minus_15_reads = Vec::with_capacity(48);
Expand Down
15 changes: 10 additions & 5 deletions crates/core/executor/src/syscalls/precompiles/uint256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ impl Syscall for Uint256MulSyscall {
let clk = rt.clk;

let x_ptr = arg1;
if x_ptr % 4 != 0 {
panic!();
}
let y_ptr = arg2;
if y_ptr % 4 != 0 {
panic!();

// Check alignment.
if x_ptr % 4 > 0 || y_ptr % 4 > 0 {
return rt.invariant_violated();
}

// First read the words for the x value. We can read a slice_unsafe here because we write
Expand All @@ -45,6 +44,12 @@ impl Syscall for Uint256MulSyscall {
let uint256_y = BigUint::from_bytes_le(&words_to_bytes_le_vec(&y));
let uint256_modulus = BigUint::from_bytes_le(&words_to_bytes_le_vec(&modulus));

if uint256_x >= uint256_modulus || uint256_y >= uint256_modulus {
eprintln!("Uint256 Precompile Invariant violated: x or y is greater than or equal to the modulus.");

return rt.invariant_violated();
}

// Perform the multiplication and take the result modulo the modulus.
let result: BigUint = if uint256_modulus.is_zero() {
let modulus = BigUint::one() << 256;
Expand Down

0 comments on commit 964054d

Please sign in to comment.