Skip to content

Commit

Permalink
chore!: remove fixed-length keccak256 (#5617)
Browse files Browse the repository at this point in the history
Cleaning up some tech debt. We never actually create this opcode so we
can just remove it in favour of the variable length version.

---------

Co-authored-by: Maxim Vezenov <mvezenov@gmail.com>
  • Loading branch information
2 people authored and AztecBot committed Apr 10, 2024
1 parent c9cd68c commit 76532e1
Show file tree
Hide file tree
Showing 15 changed files with 11 additions and 162 deletions.
3 changes: 0 additions & 3 deletions cpp/src/barretenberg/dsl/acir_format/acir_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ void build_constraints(Builder& builder, AcirFormat const& constraint_system, bo
for (const auto& constraint : constraint_system.keccak_constraints) {
create_keccak_constraints(builder, constraint);
}
for (const auto& constraint : constraint_system.keccak_var_constraints) {
create_keccak_var_constraints(builder, constraint);
}
for (const auto& constraint : constraint_system.keccak_permutations) {
create_keccak_permutations(builder, constraint);
}
Expand Down
2 changes: 0 additions & 2 deletions cpp/src/barretenberg/dsl/acir_format/acir_format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ struct AcirFormat {
std::vector<Blake2sConstraint> blake2s_constraints;
std::vector<Blake3Constraint> blake3_constraints;
std::vector<KeccakConstraint> keccak_constraints;
std::vector<KeccakVarConstraint> keccak_var_constraints;
std::vector<Keccakf1600> keccak_permutations;
std::vector<PedersenConstraint> pedersen_constraints;
std::vector<PedersenHashConstraint> pedersen_hash_constraints;
Expand Down Expand Up @@ -76,7 +75,6 @@ struct AcirFormat {
blake2s_constraints,
blake3_constraints,
keccak_constraints,
keccak_var_constraints,
keccak_permutations,
pedersen_constraints,
pedersen_hash_constraints,
Expand Down
10 changes: 2 additions & 8 deletions cpp/src/barretenberg/dsl/acir_format/acir_format.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ TEST_F(AcirFormatTests, TestASingleConstraintNoPubInputs)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down Expand Up @@ -159,7 +158,6 @@ TEST_F(AcirFormatTests, TestLogicGateFromNoirCircuit)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down Expand Up @@ -227,7 +225,6 @@ TEST_F(AcirFormatTests, TestSchnorrVerifyPass)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down Expand Up @@ -322,7 +319,6 @@ TEST_F(AcirFormatTests, TestSchnorrVerifySmallRange)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down Expand Up @@ -388,7 +384,7 @@ TEST_F(AcirFormatTests, TestVarKeccak)
HashInput input3;
input3.witness = 2;
input3.num_bits = 8;
KeccakVarConstraint keccak;
KeccakConstraint keccak;
keccak.inputs = { input1, input2, input3 };
keccak.var_message_size = 3;
keccak.result = { 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
Expand Down Expand Up @@ -435,8 +431,7 @@ TEST_F(AcirFormatTests, TestVarKeccak)
.ecdsa_r1_constraints = {},
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = { keccak },
.keccak_constraints = { keccak },
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down Expand Up @@ -483,7 +478,6 @@ TEST_F(AcirFormatTests, TestKeccakPermutation)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = { keccak_permutation },
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down
11 changes: 0 additions & 11 deletions cpp/src/barretenberg/dsl/acir_format/acir_to_constraint_buf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,17 +244,6 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg, Aci
};
}),
.result = map(arg.outputs, [](auto& e) { return e.value; }),
});
} else if constexpr (std::is_same_v<T, Program::BlackBoxFuncCall::Keccak256VariableLength>) {
af.keccak_var_constraints.push_back(KeccakVarConstraint{
.inputs = map(arg.inputs,
[](auto& e) {
return HashInput{
.witness = e.witness.value,
.num_bits = e.num_bits,
};
}),
.result = map(arg.outputs, [](auto& e) { return e.value; }),
.var_message_size = arg.var_message_size.witness.value,
});
} else if constexpr (std::is_same_v<T, Program::BlackBoxFuncCall::Keccakf1600>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ TEST_F(BigIntTests, TestBigIntConstraintMultiple)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down Expand Up @@ -248,7 +247,6 @@ TEST_F(BigIntTests, TestBigIntConstraintSimple)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down Expand Up @@ -301,7 +299,6 @@ TEST_F(BigIntTests, TestBigIntConstraintReuse)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down Expand Up @@ -358,7 +355,6 @@ TEST_F(BigIntTests, TestBigIntConstraintReuse2)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down Expand Up @@ -436,7 +432,6 @@ TEST_F(BigIntTests, TestBigIntDIV)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down Expand Up @@ -466,4 +461,4 @@ TEST_F(BigIntTests, TestBigIntDIV)
auto verifier2 = composer.create_ultra_with_keccak_verifier(builder);
EXPECT_EQ(verifier2.verify_proof(proof), true);
}
} // namespace acir_format::tests
} // namespace acir_format::tests
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ TEST_F(UltraPlonkRAM, TestBlockConstraint)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ TEST_F(EcOperations, TestECOperations)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down
3 changes: 0 additions & 3 deletions cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256k1.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ TEST_F(ECDSASecp256k1, TestECDSAConstraintSucceed)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down Expand Up @@ -151,7 +150,6 @@ TEST_F(ECDSASecp256k1, TestECDSACompilesForVerifier)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down Expand Up @@ -195,7 +193,6 @@ TEST_F(ECDSASecp256k1, TestECDSAConstraintFail)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down
4 changes: 0 additions & 4 deletions cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256r1.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ TEST(ECDSASecp256r1, test_hardcoded)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down Expand Up @@ -187,7 +186,6 @@ TEST(ECDSASecp256r1, TestECDSAConstraintSucceed)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down Expand Up @@ -236,7 +234,6 @@ TEST(ECDSASecp256r1, TestECDSACompilesForVerifier)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down Expand Up @@ -280,7 +277,6 @@ TEST(ECDSASecp256r1, TestECDSAConstraintFail)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down
37 changes: 0 additions & 37 deletions cpp/src/barretenberg/dsl/acir_format/keccak_constraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,6 @@
namespace acir_format {

template <typename Builder> void create_keccak_constraints(Builder& builder, const KeccakConstraint& constraint)
{
using byte_array_ct = bb::stdlib::byte_array<Builder>;
using field_ct = bb::stdlib::field_t<Builder>;

// 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);
}

byte_array_ct output_bytes = bb::stdlib::keccak<Builder>::hash(arr);

// Convert byte array to vector of field_t
auto bytes = output_bytes.bytes();

for (size_t i = 0; i < bytes.size(); ++i) {
builder.assert_equal(bytes[i].normalize().witness_index, constraint.result[i]);
}
}

template <typename Builder> void create_keccak_var_constraints(Builder& builder, const KeccakVarConstraint& constraint)
{
using byte_array_ct = bb::stdlib::byte_array<Builder>;
using field_ct = bb::stdlib::field_t<Builder>;
Expand Down Expand Up @@ -95,15 +62,11 @@ template <typename Builder> void create_keccak_permutations(Builder& builder, co
}
template void create_keccak_constraints<UltraCircuitBuilder>(UltraCircuitBuilder& builder,
const KeccakConstraint& constraint);
template void create_keccak_var_constraints<UltraCircuitBuilder>(UltraCircuitBuilder& builder,
const KeccakVarConstraint& constraint);
template void create_keccak_permutations<UltraCircuitBuilder>(UltraCircuitBuilder& builder,
const Keccakf1600& constraint);

template void create_keccak_constraints<GoblinUltraCircuitBuilder>(GoblinUltraCircuitBuilder& builder,
const KeccakConstraint& constraint);
template void create_keccak_var_constraints<GoblinUltraCircuitBuilder>(GoblinUltraCircuitBuilder& builder,
const KeccakVarConstraint& constraint);

template void create_keccak_permutations<GoblinUltraCircuitBuilder>(GoblinUltraCircuitBuilder& builder,
const Keccakf1600& constraint);
Expand Down
12 changes: 1 addition & 11 deletions cpp/src/barretenberg/dsl/acir_format/keccak_constraint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,14 @@ struct Keccakf1600 {
struct KeccakConstraint {
std::vector<HashInput> inputs;
std::vector<uint32_t> result;

// For serialization, update with any new fields
MSGPACK_FIELDS(inputs, result);
friend bool operator==(KeccakConstraint const& lhs, KeccakConstraint const& rhs) = default;
};

struct KeccakVarConstraint {
std::vector<HashInput> inputs;
std::vector<uint32_t> result;
uint32_t var_message_size;

// For serialization, update with any new fields
MSGPACK_FIELDS(inputs, result, var_message_size);
friend bool operator==(KeccakVarConstraint const& lhs, KeccakVarConstraint const& rhs) = default;
friend bool operator==(KeccakConstraint const& lhs, KeccakConstraint const& rhs) = default;
};

template <typename Builder> void create_keccak_constraints(Builder& builder, const KeccakConstraint& constraint);
template <typename Builder> void create_keccak_var_constraints(Builder& builder, const KeccakVarConstraint& constraint);
template <typename Builder> void create_keccak_permutations(Builder& builder, const Keccakf1600& constraint);

} // namespace acir_format
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ TEST_F(Poseidon2Tests, TestPoseidon2Permutation)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down Expand Up @@ -79,4 +78,4 @@ TEST_F(Poseidon2Tests, TestPoseidon2Permutation)
EXPECT_EQ(verifier.verify_proof(proof), true);
}

} // namespace acir_format::tests
} // namespace acir_format::tests
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ Builder create_inner_circuit()
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down Expand Up @@ -251,7 +250,6 @@ Builder create_outer_circuit(std::vector<Builder>& inner_circuits)
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
Expand Down
Loading

0 comments on commit 76532e1

Please sign in to comment.