Skip to content

Commit

Permalink
chore: add regression test for #5756
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Aug 20, 2024
1 parent 5192e53 commit 03f0d6f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
53 changes: 53 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,4 +841,57 @@ mod test {
let instructions = main.dfg[main.entry_block()].instructions();
assert_eq!(instructions.len(), 10);
}

// Regression for #5736
#[test]
fn keccak256_regression() {
// fn main f0 {
// b0(v0: u8):
// v1 = cast v0 as Field
// v3 = add v1, Field 2⁵
// v4 = truncate v3 to 64 bits, max_bit_size: 254
// v5 = cast v3 as u64
// v10 = call keccakf1600([v5, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0])
// v11 = call keccakf1600([v5, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0])
// }
let main_id = Id::test_new(0);

// Compiling main
let mut builder = FunctionBuilder::new("main".into(), main_id);
let v0 = builder.add_parameter(Type::unsigned(8));

let v1 = builder.insert_cast(v0, Type::field());
let thirty_two = builder.field_constant(32u128);
let v3 = builder.insert_binary(v1, BinaryOp::Add, thirty_two);
let v4 = builder.insert_truncate(v3, 64, 254);
let v5 = builder.insert_cast(v4, Type::unsigned(64));

let zero = builder.numeric_constant(0u128, Type::unsigned(64));
let two_pow_sixty_three = builder.numeric_constant(FieldElement::from(2u128).pow(&FieldElement::from(63u128)), Type::unsigned(64));
let typ = Type::Array(Rc::new(vec![Type::unsigned(64)]), 25);
let array = builder.array_constant(vec![v5, zero, zero, zero, zero, zero, zero, zero, zero, zero,zero, zero, zero, zero, zero,zero, two_pow_sixty_three, zero, zero, zero,zero, zero, zero, zero, zero].into(), typ.clone());


let keccakf1600 = builder.import_intrinsic("keccakf1600").expect("keccakf1600 intrinsic should exist");
let _v10 = builder.insert_call(keccakf1600, vec![array], vec![typ.clone()]);
let _v11 = builder.insert_call(keccakf1600, vec![array], vec![typ.clone()]);

let ssa = builder.finish();

println!("{ssa}");

let main = ssa.main();
let instructions = main.dfg[main.entry_block()].instructions();
let starting_instruction_count = instructions.len();
assert_eq!(starting_instruction_count, 6);

let ssa = ssa.fold_constants();

println!("{ssa}");

let main = ssa.main();
let instructions = main.dfg[main.entry_block()].instructions();
let ending_instruction_count = instructions.len();
assert_eq!(ending_instruction_count, 5);
}
}
7 changes: 7 additions & 0 deletions test_programs/execution_success/regression_5756/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "regression_5756"
type = "bin"
authors = [""]
compiler_version = ">=0.33.0"

[dependencies]
5 changes: 5 additions & 0 deletions test_programs/execution_success/regression_5756/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main(x: u8) {
let digest1 = std::hash::keccak256([x], 1);
let digest2 = std::hash::keccak256([x], 1);
assert_eq(digest1, digest2);
}

0 comments on commit 03f0d6f

Please sign in to comment.