diff --git a/compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs b/compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs index 160105d27e6..799be38e783 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs @@ -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); + } } diff --git a/test_programs/execution_success/regression_5756/Nargo.toml b/test_programs/execution_success/regression_5756/Nargo.toml new file mode 100644 index 00000000000..8c9343f6c91 --- /dev/null +++ b/test_programs/execution_success/regression_5756/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "regression_5756" +type = "bin" +authors = [""] +compiler_version = ">=0.33.0" + +[dependencies] \ No newline at end of file diff --git a/test_programs/execution_success/regression_5756/src/main.nr b/test_programs/execution_success/regression_5756/src/main.nr new file mode 100644 index 00000000000..8c4a0f7dc7a --- /dev/null +++ b/test_programs/execution_success/regression_5756/src/main.nr @@ -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); +}