From db6a399560956d79df75b4e2195e97e0c5c32348 Mon Sep 17 00:00:00 2001 From: guipublic <47281315+guipublic@users.noreply.github.com> Date: Mon, 23 Sep 2024 17:34:57 +0200 Subject: [PATCH] feat!: remove sha256 opcode (#4571) This PR resolves Noir issue 4330: https://github.com/noir-lang/noir/issues/4330 by removing the sha256 opcode and replacing the sha256 function in the stdlib by the implementation using the sha256 compression opcode (also in the stdlib). --------- Co-authored-by: kevaundray Co-authored-by: Tom French Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com> Co-authored-by: dbanks12 Co-authored-by: David Banks <47112877+dbanks12@users.noreply.github.com> Co-authored-by: fcarreiro --- .../dsl/acir_format/acir_format.cpp | 7 - .../dsl/acir_format/acir_format.hpp | 3 - .../dsl/acir_format/acir_format.test.cpp | 12 +- .../dsl/acir_format/acir_format_mocks.cpp | 6 +- .../acir_format/acir_to_constraint_buf.cpp | 17 +-- .../acir_format/bigint_constraint.test.cpp | 5 - .../dsl/acir_format/block_constraint.test.cpp | 3 - .../dsl/acir_format/ec_operations.test.cpp | 2 - .../dsl/acir_format/ecdsa_secp256k1.test.cpp | 3 - .../dsl/acir_format/ecdsa_secp256r1.test.cpp | 4 - .../honk_recursion_constraint.test.cpp | 1 - .../dsl/acir_format/multi_scalar_mul.test.cpp | 1 - .../acir_format/poseidon2_constraint.test.cpp | 1 - .../acir_format/recursion_constraint.test.cpp | 2 - .../dsl/acir_format/serde/acir.hpp | 123 ------------------ .../dsl/acir_format/sha256_constraint.cpp | 62 +-------- .../dsl/acir_format/sha256_constraint.hpp | 13 -- .../acir_format/sha256_constraint.test.cpp | 1 - .../vm/avm/tests/execution.test.cpp | 78 +---------- .../vm/avm/trace/deserialization.cpp | 9 +- .../barretenberg/vm/avm/trace/execution.cpp | 11 +- .../barretenberg/vm/avm/trace/fixed_gas.cpp | 5 +- cpp/src/barretenberg/vm/avm/trace/opcode.cpp | 11 +- cpp/src/barretenberg/vm/avm/trace/opcode.hpp | 6 +- cpp/src/barretenberg/vm/avm/trace/trace.cpp | 80 +++--------- cpp/src/barretenberg/vm/avm/trace/trace.hpp | 8 +- 26 files changed, 58 insertions(+), 416 deletions(-) diff --git a/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp b/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp index 5ca7ebe20..30b9fca70 100644 --- a/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp @@ -74,13 +74,6 @@ void build_constraints(Builder& builder, } // Add sha256 constraints - for (size_t i = 0; i < constraint_system.sha256_constraints.size(); ++i) { - const auto& constraint = constraint_system.sha256_constraints.at(i); - create_sha256_constraints(builder, constraint); - gate_counter.track_diff(constraint_system.gates_per_opcode, - constraint_system.original_opcode_indices.sha256_constraints.at(i)); - } - for (size_t i = 0; i < constraint_system.sha256_compression.size(); ++i) { const auto& constraint = constraint_system.sha256_compression[i]; create_sha256_compression_constraints(builder, constraint); diff --git a/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp b/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp index fd9439cdf..02de8ac8b 100644 --- a/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp +++ b/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp @@ -41,7 +41,6 @@ struct AcirFormatOriginalOpcodeIndices { std::vector logic_constraints; std::vector range_constraints; std::vector aes128_constraints; - std::vector sha256_constraints; std::vector sha256_compression; std::vector schnorr_constraints; std::vector ecdsa_k1_constraints; @@ -90,7 +89,6 @@ struct AcirFormat { std::vector logic_constraints; std::vector range_constraints; std::vector aes128_constraints; - std::vector sha256_constraints; std::vector sha256_compression; std::vector schnorr_constraints; std::vector ecdsa_k1_constraints; @@ -137,7 +135,6 @@ struct AcirFormat { logic_constraints, range_constraints, aes128_constraints, - sha256_constraints, sha256_compression, schnorr_constraints, ecdsa_k1_constraints, diff --git a/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp b/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp index 64a7fea8a..f894d12ee 100644 --- a/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp @@ -45,7 +45,6 @@ TEST_F(AcirFormatTests, TestASingleConstraintNoPubInputs) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, @@ -169,7 +168,6 @@ TEST_F(AcirFormatTests, TestLogicGateFromNoirCircuit) .logic_constraints = { logic_constraint }, .range_constraints = { range_a, range_b }, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, @@ -243,6 +241,7 @@ TEST_F(AcirFormatTests, TestSchnorrVerifyPass) .result = 76, .signature = signature, }; + AcirFormat constraint_system{ .varnum = 81, .recursive = false, @@ -251,7 +250,6 @@ TEST_F(AcirFormatTests, TestSchnorrVerifyPass) .logic_constraints = {}, .range_constraints = range_constraints, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = { schnorr_constraint }, .ecdsa_k1_constraints = {}, @@ -360,7 +358,6 @@ TEST_F(AcirFormatTests, TestSchnorrVerifySmallRange) .logic_constraints = {}, .range_constraints = range_constraints, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = { schnorr_constraint }, .ecdsa_k1_constraints = {}, @@ -482,7 +479,6 @@ TEST_F(AcirFormatTests, TestVarKeccak) .logic_constraints = {}, .range_constraints = { range_a, range_b, range_c, range_d }, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, @@ -564,7 +560,6 @@ TEST_F(AcirFormatTests, TestKeccakPermutation) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, @@ -598,11 +593,9 @@ TEST_F(AcirFormatTests, TestKeccakPermutation) 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50 }; auto builder = create_circuit(constraint_system, /*size_hint=*/0, witness); - auto composer = Composer(); auto prover = composer.create_ultra_with_keccak_prover(builder); auto proof = prover.construct_proof(); - auto verifier = composer.create_ultra_with_keccak_verifier(builder); EXPECT_EQ(verifier.verify_proof(proof), true); @@ -643,7 +636,6 @@ TEST_F(AcirFormatTests, TestCollectsGateCounts) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, @@ -676,4 +668,4 @@ TEST_F(AcirFormatTests, TestCollectsGateCounts) create_circuit(constraint_system, /*size_hint*/ 0, witness, false, std::make_shared(), true); EXPECT_EQ(constraint_system.gates_per_opcode, std::vector({ 2, 1 })); -} \ No newline at end of file +} diff --git a/cpp/src/barretenberg/dsl/acir_format/acir_format_mocks.cpp b/cpp/src/barretenberg/dsl/acir_format/acir_format_mocks.cpp index e1240d85e..2173285be 100644 --- a/cpp/src/barretenberg/dsl/acir_format/acir_format_mocks.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/acir_format_mocks.cpp @@ -6,7 +6,6 @@ acir_format::AcirFormatOriginalOpcodeIndices create_empty_original_opcode_indice .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, @@ -46,9 +45,6 @@ void mock_opcode_indices(acir_format::AcirFormat& constraint_system) for (size_t i = 0; i < constraint_system.aes128_constraints.size(); i++) { constraint_system.original_opcode_indices.aes128_constraints.push_back(current_opcode++); } - for (size_t i = 0; i < constraint_system.sha256_constraints.size(); i++) { - constraint_system.original_opcode_indices.sha256_constraints.push_back(current_opcode++); - } for (size_t i = 0; i < constraint_system.sha256_compression.size(); i++) { constraint_system.original_opcode_indices.sha256_compression.push_back(current_opcode++); } @@ -127,4 +123,4 @@ void mock_opcode_indices(acir_format::AcirFormat& constraint_system) } constraint_system.num_acir_opcodes = static_cast(current_opcode); -} \ No newline at end of file +} diff --git a/cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.cpp b/cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.cpp index 245509e6b..91ed35c0b 100644 --- a/cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.cpp @@ -354,21 +354,6 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, af.constrained_witness.insert(output); } af.original_opcode_indices.aes128_constraints.push_back(opcode_index); - - } else if constexpr (std::is_same_v) { - af.sha256_constraints.push_back(Sha256Constraint{ - .inputs = map(arg.inputs, - [](auto& e) { - auto input_witness = get_witness_from_function_input(e); - return Sha256Input{ - .witness = input_witness, - .num_bits = e.num_bits, - }; - }), - .result = map(arg.outputs, [](auto& e) { return e.value; }), - }); - af.original_opcode_indices.sha256_constraints.push_back(opcode_index); - } else if constexpr (std::is_same_v) { af.sha256_compression.push_back(Sha256Compression{ .inputs = map(arg.inputs, [](auto& e) { return parse_input(e); }), @@ -823,4 +808,4 @@ AcirProgramStack get_acir_program_stack(std::string const& bytecode_path, return { constraint_systems, witness_stack }; } #endif -} // namespace acir_format \ No newline at end of file +} // namespace acir_format diff --git a/cpp/src/barretenberg/dsl/acir_format/bigint_constraint.test.cpp b/cpp/src/barretenberg/dsl/acir_format/bigint_constraint.test.cpp index a2dec9cce..541dbcab3 100644 --- a/cpp/src/barretenberg/dsl/acir_format/bigint_constraint.test.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/bigint_constraint.test.cpp @@ -178,7 +178,6 @@ TEST_F(BigIntTests, TestBigIntConstraintMultiple) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, @@ -254,7 +253,6 @@ TEST_F(BigIntTests, TestBigIntConstraintSimple) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, @@ -314,7 +312,6 @@ TEST_F(BigIntTests, TestBigIntConstraintReuse) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, @@ -379,7 +376,6 @@ TEST_F(BigIntTests, TestBigIntConstraintReuse2) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, @@ -465,7 +461,6 @@ TEST_F(BigIntTests, TestBigIntDIV) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, diff --git a/cpp/src/barretenberg/dsl/acir_format/block_constraint.test.cpp b/cpp/src/barretenberg/dsl/acir_format/block_constraint.test.cpp index 03ddf8035..45dece532 100644 --- a/cpp/src/barretenberg/dsl/acir_format/block_constraint.test.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/block_constraint.test.cpp @@ -146,7 +146,6 @@ TEST_F(UltraPlonkRAM, TestBlockConstraint) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, @@ -200,7 +199,6 @@ TEST_F(MegaHonk, Databus) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, @@ -309,7 +307,6 @@ TEST_F(MegaHonk, DatabusReturn) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, diff --git a/cpp/src/barretenberg/dsl/acir_format/ec_operations.test.cpp b/cpp/src/barretenberg/dsl/acir_format/ec_operations.test.cpp index ac86757bb..f12c39e8f 100644 --- a/cpp/src/barretenberg/dsl/acir_format/ec_operations.test.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/ec_operations.test.cpp @@ -68,7 +68,6 @@ TEST_F(EcOperations, TestECOperations) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, @@ -207,7 +206,6 @@ TEST_F(EcOperations, TestECMultiScalarMul) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, diff --git a/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256k1.test.cpp b/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256k1.test.cpp index d8bc40c53..24864b52f 100644 --- a/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256k1.test.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256k1.test.cpp @@ -100,7 +100,6 @@ TEST_F(ECDSASecp256k1, TestECDSAConstraintSucceed) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = { ecdsa_k1_constraint }, @@ -157,7 +156,6 @@ TEST_F(ECDSASecp256k1, TestECDSACompilesForVerifier) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = { ecdsa_k1_constraint }, @@ -209,7 +207,6 @@ TEST_F(ECDSASecp256k1, TestECDSAConstraintFail) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = { ecdsa_k1_constraint }, diff --git a/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256r1.test.cpp b/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256r1.test.cpp index 869c896ea..657eaa2d3 100644 --- a/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256r1.test.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256r1.test.cpp @@ -134,7 +134,6 @@ TEST(ECDSASecp256r1, test_hardcoded) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, @@ -193,7 +192,6 @@ TEST(ECDSASecp256r1, TestECDSAConstraintSucceed) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, @@ -250,7 +248,6 @@ TEST(ECDSASecp256r1, TestECDSACompilesForVerifier) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, @@ -303,7 +300,6 @@ TEST(ECDSASecp256r1, TestECDSAConstraintFail) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, diff --git a/cpp/src/barretenberg/dsl/acir_format/honk_recursion_constraint.test.cpp b/cpp/src/barretenberg/dsl/acir_format/honk_recursion_constraint.test.cpp index abf9be04f..5c3f1ab11 100644 --- a/cpp/src/barretenberg/dsl/acir_format/honk_recursion_constraint.test.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/honk_recursion_constraint.test.cpp @@ -96,7 +96,6 @@ class AcirHonkRecursionConstraint : public ::testing::Test { .logic_constraints = { logic_constraint }, .range_constraints = { range_a, range_b }, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, diff --git a/cpp/src/barretenberg/dsl/acir_format/multi_scalar_mul.test.cpp b/cpp/src/barretenberg/dsl/acir_format/multi_scalar_mul.test.cpp index 47bd777cf..a0adc6331 100644 --- a/cpp/src/barretenberg/dsl/acir_format/multi_scalar_mul.test.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/multi_scalar_mul.test.cpp @@ -68,7 +68,6 @@ TEST_F(MSMTests, TestMSM) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, diff --git a/cpp/src/barretenberg/dsl/acir_format/poseidon2_constraint.test.cpp b/cpp/src/barretenberg/dsl/acir_format/poseidon2_constraint.test.cpp index c3dc81fd9..ec2793c3a 100644 --- a/cpp/src/barretenberg/dsl/acir_format/poseidon2_constraint.test.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/poseidon2_constraint.test.cpp @@ -48,7 +48,6 @@ TEST_F(Poseidon2Tests, TestPoseidon2Permutation) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, diff --git a/cpp/src/barretenberg/dsl/acir_format/recursion_constraint.test.cpp b/cpp/src/barretenberg/dsl/acir_format/recursion_constraint.test.cpp index 1811b8d7f..ec22ec71f 100644 --- a/cpp/src/barretenberg/dsl/acir_format/recursion_constraint.test.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/recursion_constraint.test.cpp @@ -93,7 +93,6 @@ Builder create_inner_circuit() .logic_constraints = { logic_constraint }, .range_constraints = { range_a, range_b }, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, @@ -257,7 +256,6 @@ Builder create_outer_circuit(std::vector& inner_circuits) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = {}, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, diff --git a/cpp/src/barretenberg/dsl/acir_format/serde/acir.hpp b/cpp/src/barretenberg/dsl/acir_format/serde/acir.hpp index e20538346..5905b3378 100644 --- a/cpp/src/barretenberg/dsl/acir_format/serde/acir.hpp +++ b/cpp/src/barretenberg/dsl/acir_format/serde/acir.hpp @@ -256,15 +256,6 @@ struct BlackBoxOp { static AES128Encrypt bincodeDeserialize(std::vector); }; - struct Sha256 { - Program::HeapVector message; - Program::HeapArray output; - - friend bool operator==(const Sha256&, const Sha256&); - std::vector bincodeSerialize() const; - static Sha256 bincodeDeserialize(std::vector); - }; - struct Blake2s { Program::HeapVector message; Program::HeapArray output; @@ -472,7 +463,6 @@ struct BlackBoxOp { }; std::variant); }; - struct SHA256 { - std::vector inputs; - std::array outputs; - - friend bool operator==(const SHA256&, const SHA256&); - std::vector bincodeSerialize() const; - static SHA256 bincodeDeserialize(std::vector); - }; - struct Blake2s { std::vector inputs; std::array outputs; @@ -1086,7 +1067,6 @@ struct BlackBoxFuncCall { AND, XOR, RANGE, - SHA256, Blake2s, Blake3, SchnorrVerify, @@ -2922,58 +2902,6 @@ Program::BlackBoxFuncCall::RANGE serde::Deserializable BlackBoxFuncCall::SHA256::bincodeSerialize() const -{ - auto serializer = serde::BincodeSerializer(); - serde::Serializable::serialize(*this, serializer); - return std::move(serializer).bytes(); -} - -inline BlackBoxFuncCall::SHA256 BlackBoxFuncCall::SHA256::bincodeDeserialize(std::vector input) -{ - auto deserializer = serde::BincodeDeserializer(input); - auto value = serde::Deserializable::deserialize(deserializer); - if (deserializer.get_buffer_offset() < input.size()) { - throw_or_abort("Some input bytes were not read"); - } - return value; -} - -} // end of namespace Program - -template <> -template -void serde::Serializable::serialize(const Program::BlackBoxFuncCall::SHA256& obj, - Serializer& serializer) -{ - serde::Serializable::serialize(obj.inputs, serializer); - serde::Serializable::serialize(obj.outputs, serializer); -} - -template <> -template -Program::BlackBoxFuncCall::SHA256 serde::Deserializable::deserialize( - Deserializer& deserializer) -{ - Program::BlackBoxFuncCall::SHA256 obj; - obj.inputs = serde::Deserializable::deserialize(deserializer); - obj.outputs = serde::Deserializable::deserialize(deserializer); - return obj; -} - -namespace Program { - inline bool operator==(const BlackBoxFuncCall::Blake2s& lhs, const BlackBoxFuncCall::Blake2s& rhs) { if (!(lhs.inputs == rhs.inputs)) { @@ -4254,57 +4182,6 @@ Program::BlackBoxOp::AES128Encrypt serde::Deserializable BlackBoxOp::Sha256::bincodeSerialize() const -{ - auto serializer = serde::BincodeSerializer(); - serde::Serializable::serialize(*this, serializer); - return std::move(serializer).bytes(); -} - -inline BlackBoxOp::Sha256 BlackBoxOp::Sha256::bincodeDeserialize(std::vector input) -{ - auto deserializer = serde::BincodeDeserializer(input); - auto value = serde::Deserializable::deserialize(deserializer); - if (deserializer.get_buffer_offset() < input.size()) { - throw_or_abort("Some input bytes were not read"); - } - return value; -} - -} // end of namespace Program - -template <> -template -void serde::Serializable::serialize(const Program::BlackBoxOp::Sha256& obj, - Serializer& serializer) -{ - serde::Serializable::serialize(obj.message, serializer); - serde::Serializable::serialize(obj.output, serializer); -} - -template <> -template -Program::BlackBoxOp::Sha256 serde::Deserializable::deserialize(Deserializer& deserializer) -{ - Program::BlackBoxOp::Sha256 obj; - obj.message = serde::Deserializable::deserialize(deserializer); - obj.output = serde::Deserializable::deserialize(deserializer); - return obj; -} - -namespace Program { - inline bool operator==(const BlackBoxOp::Blake2s& lhs, const BlackBoxOp::Blake2s& rhs) { if (!(lhs.message == rhs.message)) { diff --git a/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.cpp b/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.cpp index cafec218b..80fd8a188 100644 --- a/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.cpp @@ -6,53 +6,10 @@ namespace acir_format { -using namespace bb; - -// This function does not work (properly) because the stdlib:sha256 function is not working correctly for 512 bits -// pair -template void create_sha256_constraints(Builder& builder, const Sha256Constraint& constraint) -{ - using byte_array_ct = stdlib::byte_array; - using field_ct = stdlib::field_t; - - // Create byte array struct - byte_array_ct arr(&builder); - - // Get the witness assignment for each witness index - // Write the witness assignment to the byte_array - for (const auto& witness_index_num_bits : constraint.inputs) { - auto witness_index = witness_index_num_bits.witness; - auto num_bits = witness_index_num_bits.num_bits; - - // XXX: The implementation requires us to truncate the element to the nearest byte and not bit - auto num_bytes = round_to_nearest_byte(num_bits); - - field_ct element = field_ct::from_witness_index(&builder, witness_index); - byte_array_ct element_bytes(element, num_bytes); - - arr.write(element_bytes); - } - - // Compute sha256 - byte_array_ct output_bytes = stdlib::sha256(arr); - - // Convert byte array to vector of field_t - auto bytes = output_bytes.bytes(); - - for (size_t i = 0; i < bytes.size(); ++i) { - auto normalised = bytes[i].normalize(); - if (normalised.is_constant()) { - builder.fix_witness(constraint.result[i], normalised.get_value()); - } else { - builder.assert_equal(bytes[i].normalize().witness_index, constraint.result[i]); - } - } -} - template void create_sha256_compression_constraints(Builder& builder, const Sha256Compression& constraint) { - using field_ct = stdlib::field_t; + using field_ct = bb::stdlib::field_t; std::array inputs; std::array hash_inputs; @@ -72,14 +29,14 @@ void create_sha256_compression_constraints(Builder& builder, const Sha256Compres } // Compute sha256 compression - auto output_bytes = stdlib::sha256_plookup::sha256_block(hash_inputs, inputs); + auto output_bytes = bb::stdlib::sha256_plookup::sha256_block(hash_inputs, inputs); for (size_t i = 0; i < 8; ++i) { auto normalised_output = output_bytes[i].normalize(); if (normalised_output.is_constant()) { builder.fix_witness(constraint.result[i], normalised_output.get_value()); } else { - poly_triple assert_equal{ + bb::poly_triple assert_equal{ .a = normalised_output.witness_index, .b = constraint.result[i], .c = 0, @@ -94,14 +51,9 @@ void create_sha256_compression_constraints(Builder& builder, const Sha256Compres } } -template void create_sha256_constraints(UltraCircuitBuilder& builder, - const Sha256Constraint& constraint); -template void create_sha256_constraints(MegaCircuitBuilder& builder, - const Sha256Constraint& constraint); - -template void create_sha256_compression_constraints(UltraCircuitBuilder& builder, - const Sha256Compression& constraint); -template void create_sha256_compression_constraints(MegaCircuitBuilder& builder, - const Sha256Compression& constraint); +template void create_sha256_compression_constraints(bb::UltraCircuitBuilder& builder, + const Sha256Compression& constraint); +template void create_sha256_compression_constraints(bb::MegaCircuitBuilder& builder, + const Sha256Compression& constraint); } // namespace acir_format diff --git a/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.hpp b/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.hpp index 4b243b5b6..cb5e95f33 100644 --- a/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.hpp +++ b/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.hpp @@ -16,15 +16,6 @@ struct Sha256Input { MSGPACK_FIELDS(witness, num_bits); }; -struct Sha256Constraint { - std::vector inputs; - std::array result; - - friend bool operator==(Sha256Constraint const& lhs, Sha256Constraint const& rhs) = default; - // for serialization, update with any new fields - MSGPACK_FIELDS(inputs, result); -}; - struct Sha256Compression { std::array, 16> inputs; std::array, 8> hash_values; @@ -35,10 +26,6 @@ struct Sha256Compression { MSGPACK_FIELDS(inputs, hash_values, result); }; -// This function does not work (properly) because the stdlib:sha256 function is not working correctly for 512 bits -// pair -template void create_sha256_constraints(Builder& builder, const Sha256Constraint& constraint); - template void create_sha256_compression_constraints(Builder& builder, const Sha256Compression& constraint); diff --git a/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.test.cpp b/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.test.cpp index 3f27b41c5..063ad4f62 100644 --- a/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.test.cpp +++ b/cpp/src/barretenberg/dsl/acir_format/sha256_constraint.test.cpp @@ -43,7 +43,6 @@ TEST_F(Sha256Tests, TestSha256Compression) .logic_constraints = {}, .range_constraints = {}, .aes128_constraints = {}, - .sha256_constraints = {}, .sha256_compression = { sha256_compression }, .schnorr_constraints = {}, .ecdsa_k1_constraints = {}, diff --git a/cpp/src/barretenberg/vm/avm/tests/execution.test.cpp b/cpp/src/barretenberg/vm/avm/tests/execution.test.cpp index 37b39d528..4e13ea0e5 100644 --- a/cpp/src/barretenberg/vm/avm/tests/execution.test.cpp +++ b/cpp/src/barretenberg/vm/avm/tests/execution.test.cpp @@ -944,10 +944,12 @@ TEST_F(AvmExecutionTests, sha256CompressionOpcode) "09" // value 9 (i.e. where the input will be read from) "23" // dst_offset 35 + to_hex(OpCode::SHA256COMPRESSION) + // opcode SHA256COMPRESSION - "07" // Indirect flag (first 3 operands indirect) - "00000024" // output offset (indirect 36) - "00000022" // state offset (indirect 34) - "00000023" // input offset (indirect 35) + "00" // Indirect flag + "00000100" // output offset + "00000001" // state offset + "0000000F" // state size + "00000009" // input offset + "00000008" // input size + to_hex(OpCode::RETURN) + // opcode RETURN "00" // Indirect flag "00000100" // ret offset 256 @@ -971,74 +973,6 @@ TEST_F(AvmExecutionTests, sha256CompressionOpcode) validate_trace(std::move(trace), public_inputs, calldata, returndata); } -// Positive test with SHA256 -TEST_F(AvmExecutionTests, sha256Opcode) -{ - - // Test vectors taken from noir black_box_solver - // Uint8Array.from([0x61, 0x62, 0x63]), - // Uint8Array.from([ - // 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, - // 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad, - // ]), - std::vector expected_output = { - FF(0xba), FF(0x78), FF(0x16), FF(0xbf), FF(0x8f), FF(0x01), FF(0xcf), FF(0xea), FF(0x41), FF(0x41), FF(0x40), - FF(0xde), FF(0x5d), FF(0xae), FF(0x22), FF(0x23), FF(0xb0), FF(0x03), FF(0x61), FF(0xa3), FF(0x96), FF(0x17), - FF(0x7a), FF(0x9c), FF(0xb4), FF(0x10), FF(0xff), FF(0x61), FF(0xf2), FF(0x00), FF(0x15), FF(0xad), - }; - std::string bytecode_hex = to_hex(OpCode::SET_8) + // Initial SET operations to store state and input - "00" // Indirect Flag - + to_hex(AvmMemoryTag::U8) + - "61" // val 97 - "01" // dst_offset 1 - + to_hex(OpCode::SET_8) + // opcode SET for indirect src (input) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U8) + - "62" // value 98 (i.e. where the src will be read from)A - "02" // input_offset 2 - + to_hex(OpCode::SET_8) + // opcode SET for indirect src (input) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "63" // value 99 (i.e. where the src will be read from) - "03" // input_offset 36 - + to_hex(OpCode::SET_8) + // opcode SET for indirect src (input) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "01" // value 1 (i.e. where the src will be read from) - "24" // input_offset 36 - + to_hex(OpCode::SET_8) + // - "00" // Indirect flag - + to_hex(AvmMemoryTag::U8) + - "03" // value 3 (i.e. where the length parameter is stored) - "25" // input_offset 37 - + to_hex(OpCode::SET_16) + // opcode SET for indirect dst (output) - "00" // Indirect flag - + to_hex(AvmMemoryTag::U32) + - "0100" // value 256 (i.e. where the ouput will be written to) - "0023" // dst_offset 35 - + to_hex(OpCode::SHA256) + // opcode SHA256 - "03" // Indirect flag (first 2 operands indirect) - "00000023" // output offset (indirect 35) - "00000024" // input offset (indirect 36) - "00000025" // length offset 37 - + to_hex(OpCode::RETURN) + // opcode RETURN - "00" // Indirect flag - "00000100" // ret offset 256 - "00000020"; // ret size 32 - - auto bytecode = hex_to_bytes(bytecode_hex); - auto instructions = Deserialization::parse(bytecode); - - // Assign a vector that we will mutate internally in gen_trace to store the return values; - std::vector returndata = std::vector(); - std::vector calldata = std::vector(); - auto trace = Execution::gen_trace(instructions, returndata, calldata, public_inputs_vec); - - EXPECT_EQ(returndata, expected_output); - - validate_trace(std::move(trace), public_inputs, calldata, returndata); -} - // Positive test with POSEIDON2_PERM. TEST_F(AvmExecutionTests, poseidon2PermutationOpCode) { diff --git a/cpp/src/barretenberg/vm/avm/trace/deserialization.cpp b/cpp/src/barretenberg/vm/avm/trace/deserialization.cpp index 4a1041643..bede0c625 100644 --- a/cpp/src/barretenberg/vm/avm/trace/deserialization.cpp +++ b/cpp/src/barretenberg/vm/avm/trace/deserialization.cpp @@ -174,7 +174,14 @@ const std::unordered_map> OPCODE_WIRE_FORMAT = // Gadgets - Hashing { OpCode::KECCAK, { OperandType::INDIRECT, OperandType::UINT32, OperandType::UINT32, OperandType::UINT32 } }, { OpCode::POSEIDON2, { OperandType::INDIRECT, OperandType::UINT32, OperandType::UINT32 } }, - { OpCode::SHA256, { OperandType::INDIRECT, OperandType::UINT32, OperandType::UINT32, OperandType::UINT32 } }, + { OpCode::SHA256COMPRESSION, + { OperandType::INDIRECT, + OperandType::UINT32, + OperandType::UINT32, + OperandType::UINT32, + OperandType::UINT32, + OperandType::UINT32 } }, + { OpCode::KECCAKF1600, { OperandType::INDIRECT, OperandType::UINT32, OperandType::UINT32, OperandType::UINT32 } }, { OpCode::PEDERSEN, { OperandType::INDIRECT, OperandType::UINT32, OperandType::UINT32, OperandType::UINT32, OperandType::UINT32 } }, // TEMP ECADD without relative memory diff --git a/cpp/src/barretenberg/vm/avm/trace/execution.cpp b/cpp/src/barretenberg/vm/avm/trace/execution.cpp index 6f3ad4ecd..ba62c7d0c 100644 --- a/cpp/src/barretenberg/vm/avm/trace/execution.cpp +++ b/cpp/src/barretenberg/vm/avm/trace/execution.cpp @@ -933,12 +933,6 @@ std::vector Execution::gen_trace(std::vector const& instructio std::get(inst.operands.at(2))); break; - case OpCode::SHA256: - trace_builder.op_sha256(std::get(inst.operands.at(0)), - std::get(inst.operands.at(1)), - std::get(inst.operands.at(2)), - std::get(inst.operands.at(3))); - break; case OpCode::PEDERSEN: trace_builder.op_pedersen_hash(std::get(inst.operands.at(0)), std::get(inst.operands.at(1)), @@ -974,12 +968,13 @@ std::vector Execution::gen_trace(std::vector const& instructio std::get(inst.operands.at(5))); break; - // Future Gadgets -- pending changes in noir case OpCode::SHA256COMPRESSION: trace_builder.op_sha256_compression(std::get(inst.operands.at(0)), std::get(inst.operands.at(1)), std::get(inst.operands.at(2)), - std::get(inst.operands.at(3))); + std::get(inst.operands.at(3)), + std::get(inst.operands.at(4)), + std::get(inst.operands.at(5))); break; case OpCode::KECCAKF1600: diff --git a/cpp/src/barretenberg/vm/avm/trace/fixed_gas.cpp b/cpp/src/barretenberg/vm/avm/trace/fixed_gas.cpp index cf34e1d46..e2c949b10 100644 --- a/cpp/src/barretenberg/vm/avm/trace/fixed_gas.cpp +++ b/cpp/src/barretenberg/vm/avm/trace/fixed_gas.cpp @@ -96,15 +96,14 @@ const std::unordered_map GAS_COST_TABLE = { { OpCode::DEBUGLOG, make_cost(AVM_DEBUGLOG_BASE_L2_GAS, 0, AVM_DEBUGLOG_DYN_L2_GAS, 0) }, { OpCode::KECCAK, make_cost(AVM_KECCAK_BASE_L2_GAS, 0, AVM_KECCAK_DYN_L2_GAS, 0) }, { OpCode::POSEIDON2, make_cost(AVM_POSEIDON2_BASE_L2_GAS, 0, AVM_POSEIDON2_DYN_L2_GAS, 0) }, - { OpCode::SHA256, make_cost(AVM_SHA256_BASE_L2_GAS, 0, AVM_SHA256_DYN_L2_GAS, 0) }, + { OpCode::SHA256COMPRESSION, make_cost(AVM_SHA256COMPRESSION_BASE_L2_GAS, 0, AVM_SHA256COMPRESSION_DYN_L2_GAS, 0) }, + { OpCode::KECCAKF1600, make_cost(AVM_KECCAKF1600_BASE_L2_GAS, 0, AVM_KECCAKF1600_DYN_L2_GAS, 0) }, { OpCode::PEDERSEN, make_cost(AVM_PEDERSEN_BASE_L2_GAS, 0, AVM_PEDERSEN_DYN_L2_GAS, 0) }, { OpCode::ECADD, make_cost(AVM_ECADD_BASE_L2_GAS, 0, AVM_ECADD_DYN_L2_GAS, 0) }, { OpCode::MSM, make_cost(AVM_MSM_BASE_L2_GAS, 0, AVM_MSM_DYN_L2_GAS, 0) }, { OpCode::PEDERSENCOMMITMENT, make_cost(AVM_PEDERSENCOMMITMENT_BASE_L2_GAS, 0, AVM_PEDERSENCOMMITMENT_DYN_L2_GAS, 0) }, { OpCode::TORADIXLE, make_cost(AVM_TORADIXLE_BASE_L2_GAS, 0, AVM_TORADIXLE_DYN_L2_GAS, 0) }, - { OpCode::SHA256COMPRESSION, make_cost(AVM_SHA256COMPRESSION_BASE_L2_GAS, 0, AVM_SHA256COMPRESSION_DYN_L2_GAS, 0) }, - { OpCode::KECCAKF1600, make_cost(AVM_KECCAKF1600_BASE_L2_GAS, 0, AVM_KECCAKF1600_DYN_L2_GAS, 0) }, }; } // namespace diff --git a/cpp/src/barretenberg/vm/avm/trace/opcode.cpp b/cpp/src/barretenberg/vm/avm/trace/opcode.cpp index b1cdfbb57..56d67fa90 100644 --- a/cpp/src/barretenberg/vm/avm/trace/opcode.cpp +++ b/cpp/src/barretenberg/vm/avm/trace/opcode.cpp @@ -191,8 +191,10 @@ std::string to_string(OpCode opcode) return "KECCAK"; case OpCode::POSEIDON2: return "POSEIDON2"; - case OpCode::SHA256: - return "SHA256"; + case OpCode::SHA256COMPRESSION: + return "SHA256COMPRESSION"; + case OpCode::KECCAKF1600: + return "KECCAKF1600"; case OpCode::PEDERSEN: return "PEDERSEN"; case OpCode::ECADD: @@ -202,11 +204,6 @@ std::string to_string(OpCode opcode) // Conversions case OpCode::TORADIXLE: return "TORADIXLE"; - // Future Gadgets -- pending changes in noir - case OpCode::SHA256COMPRESSION: - return "SHA256COMPRESSION"; - case OpCode::KECCAKF1600: - return "KECCAKF1600"; // Sentinel case OpCode::LAST_OPCODE_SENTINEL: return "LAST_OPCODE_SENTINEL"; diff --git a/cpp/src/barretenberg/vm/avm/trace/opcode.hpp b/cpp/src/barretenberg/vm/avm/trace/opcode.hpp index 1c789c4e9..36ffca923 100644 --- a/cpp/src/barretenberg/vm/avm/trace/opcode.hpp +++ b/cpp/src/barretenberg/vm/avm/trace/opcode.hpp @@ -116,16 +116,14 @@ enum class OpCode : uint8_t { // Gadgets KECCAK, POSEIDON2, - SHA256, + SHA256COMPRESSION, + KECCAKF1600, PEDERSEN, ECADD, MSM, PEDERSENCOMMITMENT, // Conversions TORADIXLE, - // Future Gadgets -- pending changes in noir - SHA256COMPRESSION, - KECCAKF1600, // Here for when we eventually support this // Sentinel LAST_OPCODE_SENTINEL, diff --git a/cpp/src/barretenberg/vm/avm/trace/trace.cpp b/cpp/src/barretenberg/vm/avm/trace/trace.cpp index c5d335904..7e3eb6cad 100644 --- a/cpp/src/barretenberg/vm/avm/trace/trace.cpp +++ b/cpp/src/barretenberg/vm/avm/trace/trace.cpp @@ -3030,61 +3030,6 @@ void AvmTraceBuilder::op_poseidon2_permutation(uint8_t indirect, uint32_t input_ AvmMemTraceBuilder::POSEIDON2); } -/** - * @brief SHA256 Hash with direct or indirect memory access. - * This function is temporary until we have transitioned to sha256Compression - * @param indirect byte encoding information about indirect/direct memory access. - * @param output_offset An index in memory pointing to where the first U32 value of the output array should be - * stored. - * @param input_offset An index in memory pointing to the first U8 value of the state array to be used in the next - * instance of sha256. - * @param input_size_offset An index in memory pointing to the U32 value of the input size. - */ -void AvmTraceBuilder::op_sha256(uint8_t indirect, - uint32_t output_offset, - uint32_t input_offset, - uint32_t input_size_offset) -{ - auto clk = static_cast(main_trace.size()) + 1; - auto [resolved_output_offset, resolved_input_offset, resolved_input_size_offset] = - unpack_indirects<3>(indirect, { output_offset, input_offset, input_size_offset }); - - gas_trace_builder.constrain_gas(clk, OpCode::SHA256); - - auto input_length_read = constrained_read_from_memory( - call_ptr, clk, resolved_input_size_offset, AvmMemoryTag::U32, AvmMemoryTag::U0, IntermRegister::IB); - - // Store the clock time that we will use to line up the gadget later - auto sha256_op_clk = clk; - main_trace.push_back(Row{ - .main_clk = clk, - .main_ib = input_length_read.val, // Message Length - .main_ind_addr_b = FF(input_length_read.indirect_address), - .main_internal_return_ptr = FF(internal_return_ptr), - .main_mem_addr_b = FF(input_length_read.direct_address), - .main_pc = FF(pc++), - .main_r_in_tag = FF(static_cast(AvmMemoryTag::U32)), - .main_sel_mem_op_b = FF(1), - .main_sel_op_sha256 = FF(1), - .main_sel_resolve_ind_addr_b = FF(static_cast(input_length_read.is_indirect)), - .main_tag_err = FF(static_cast(!input_length_read.tag_match)), - }); - clk++; - - std::vector input; - input.reserve(uint32_t(input_length_read.val)); - read_slice_from_memory(resolved_input_offset, uint32_t(input_length_read.val), input); - - std::array result = sha256_trace_builder.sha256(input, sha256_op_clk); - - std::vector ff_result; - for (uint32_t i = 0; i < 32; i++) { - ff_result.emplace_back(result[i]); - } - // Write the result to memory after - write_slice_to_memory(resolved_output_offset, AvmMemoryTag::U8, ff_result); -} - /** * @brief Pedersen Hash with direct or indirect memory access. * @param indirect byte encoding information about indirect/direct memory access. @@ -3419,7 +3364,7 @@ void AvmTraceBuilder::op_to_radix_le(uint8_t indirect, * @brief SHA256 Compression with direct or indirect memory access. * * @param indirect byte encoding information about indirect/direct memory access. - * @param h_init_offset An index in memory pointing to the first U32 value of the state array to be used in the next + * @param state_offset An index in memory pointing to the first U32 value of the state array to be used in the next * instance of sha256 compression. * @param input_offset An index in memory pointing to the first U32 value of the input array to be used in the next * instance of sha256 compression. @@ -3428,21 +3373,28 @@ void AvmTraceBuilder::op_to_radix_le(uint8_t indirect, */ void AvmTraceBuilder::op_sha256_compression(uint8_t indirect, uint32_t output_offset, - uint32_t h_init_offset, - uint32_t input_offset) + uint32_t state_offset, + uint32_t state_size_offset, + uint32_t inputs_offset, + uint32_t inputs_size_offset) { // The clk plays a crucial role in this function as we attempt to write across multiple lines in the main trace. auto clk = static_cast(main_trace.size()) + 1; // Resolve the indirect flags, the results of this function are used to determine the memory offsets // that point to the starting memory addresses for the input and output values. - auto [resolved_h_init_offset, resolved_input_offset, resolved_output_offset] = - unpack_indirects<3>(indirect, { h_init_offset, input_offset, output_offset }); + auto [resolved_output_offset, + resolved_state_offset, + resolved_state_size_offset, + resolved_inputs_offset, + resolved_inputs_size_offset] = + unpack_indirects<5>(indirect, + { output_offset, state_offset, state_size_offset, inputs_offset, inputs_size_offset }); auto read_a = constrained_read_from_memory( - call_ptr, clk, resolved_h_init_offset, AvmMemoryTag::U32, AvmMemoryTag::U0, IntermRegister::IA); + call_ptr, clk, resolved_state_offset, AvmMemoryTag::U32, AvmMemoryTag::U0, IntermRegister::IA); auto read_b = constrained_read_from_memory( - call_ptr, clk, resolved_input_offset, AvmMemoryTag::U32, AvmMemoryTag::U0, IntermRegister::IB); + call_ptr, clk, resolved_inputs_offset, AvmMemoryTag::U32, AvmMemoryTag::U0, IntermRegister::IB); bool tag_match = read_a.tag_match && read_b.tag_match; // Constrain gas cost @@ -3484,9 +3436,9 @@ void AvmTraceBuilder::op_sha256_compression(uint8_t indirect, // Input for hash is expanded to 512 bits std::vector input_vec; // Read results are written to h_init array. - read_slice_from_memory(resolved_h_init_offset, 8, h_init_vec); + read_slice_from_memory(resolved_state_offset, 8, h_init_vec); // Read results are written to input array - read_slice_from_memory(resolved_input_offset, 16, input_vec); + read_slice_from_memory(resolved_inputs_offset, 16, input_vec); // Now that we have read all the values, we can perform the operation to get the resulting witness. // Note: We use the sha_op_clk to ensure that the sha256 operation is performed at the same clock cycle as the diff --git a/cpp/src/barretenberg/vm/avm/trace/trace.hpp b/cpp/src/barretenberg/vm/avm/trace/trace.hpp index bd8f84bbc..6390d6a1a 100644 --- a/cpp/src/barretenberg/vm/avm/trace/trace.hpp +++ b/cpp/src/barretenberg/vm/avm/trace/trace.hpp @@ -162,7 +162,6 @@ class AvmTraceBuilder { // Gadgets void op_keccak(uint8_t indirect, uint32_t output_offset, uint32_t input_offset, uint32_t input_size_offset); void op_poseidon2_permutation(uint8_t indirect, uint32_t input_offset, uint32_t output_offset); - void op_sha256(uint8_t indirect, uint32_t output_offset, uint32_t input_offset, uint32_t input_size_offset); void op_pedersen_hash(uint8_t indirect, uint32_t gen_ctx_offset, uint32_t output_offset, @@ -195,7 +194,12 @@ class AvmTraceBuilder { uint8_t output_bits); // Future Gadgets -- pending changes in noir - void op_sha256_compression(uint8_t indirect, uint32_t output_offset, uint32_t h_init_offset, uint32_t input_offset); + void op_sha256_compression(uint8_t indirect, + uint32_t output_offset, + uint32_t state_offset, + uint32_t state_size_offset, + uint32_t inputs_offset, + uint32_t inputs_size_offset); void op_keccakf1600(uint8_t indirect, uint32_t output_offset, uint32_t input_offset, uint32_t input_size_offset); std::vector finalize();