Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: implement poseidon2 opcode #4446

Merged
merged 25 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
21c1eea
implement poseidon2 opcode
guipublic Feb 6, 2024
c509587
Merge branch 'master' into gd/posiedon2_bb
guipublic Feb 6, 2024
90cca61
try to fix the build
guipublic Feb 6, 2024
b71c6d0
Merge branch 'master' into gd/posiedon2_bb
guipublic Feb 6, 2024
ccc3201
Merge branch 'master' into gd/posiedon2_bb
guipublic Feb 7, 2024
8176276
Merge branch 'master' into gd/posiedon2_bb
guipublic Feb 8, 2024
0ffcb3d
classic poseidon2 permutation (i.e without lookups) and duplicated co…
guipublic Feb 9, 2024
65760b4
Merge branch 'master' into gd/posiedon2_bb
guipublic Feb 9, 2024
f415444
Merge branch 'master' into gd/posiedon2_bb
guipublic Feb 9, 2024
66926c3
fix dsl tests
guipublic Feb 9, 2024
eeb401f
Merge branch 'master' into gd/posiedon2_bb
guipublic Feb 9, 2024
a508ffc
fix test case
guipublic Feb 9, 2024
3412aad
code review
guipublic Feb 9, 2024
965cb5c
Merge branch 'master' into gd/posiedon2_bb
guipublic Feb 9, 2024
95e332a
another test case
guipublic Feb 9, 2024
07a2338
Merge branch 'master' into gd/posiedon2_bb
guipublic Feb 12, 2024
dcfe902
refactoring
lucasxia01 Feb 12, 2024
0cda5cc
Merge branch 'master' into gd/posiedon2_bb
guipublic Feb 13, 2024
459cf8f
Merge branch 'master' into gd/posiedon2_bb
guipublic Feb 13, 2024
7f39066
fix new test case
guipublic Feb 13, 2024
ad69eaf
attempt to fix CI
lucasxia01 Feb 13, 2024
6d89851
fix poseidon2 test case
guipublic Feb 13, 2024
c7d859f
code review: add comment
guipublic Feb 13, 2024
0dd4bd3
Merge branch 'master' into gd/posiedon2_bb
guipublic Feb 13, 2024
c361b07
Merge branch 'master' into gd/posiedon2_bb
guipublic Feb 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions barretenberg/cpp/src/barretenberg/dsl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ barretenberg_module(
stdlib_blake2s
stdlib_keccak
stdlib_pedersen_hash
stdlib_poseidon2
crypto_merkle_tree
stdlib_schnorr
crypto_sha256
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "acir_format.hpp"
#include "barretenberg/common/log.hpp"
#include "barretenberg/dsl/acir_format/bigint_constraint.hpp"
#include "barretenberg/proof_system/circuit_builder/ultra_circuit_builder.hpp"
#include <cstddef>

Expand Down Expand Up @@ -78,6 +77,9 @@ void build_constraints(Builder& builder, AcirFormat const& constraint_system, bo
create_pedersen_hash_constraint(builder, constraint);
}

for (const auto& constraint : constraint_system.poseidon2_constraints) {
create_poseidon2_permutations(builder, constraint);
}
// Add fixed base scalar mul constraints
for (const auto& constraint : constraint_system.fixed_base_scalar_mul_constraints) {
create_fixed_base_constraint(builder, constraint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "keccak_constraint.hpp"
#include "logic_constraint.hpp"
#include "pedersen.hpp"
#include "poseidon2_constraint.hpp"
#include "range_constraint.hpp"
#include "recursion_constraint.hpp"
#include "schnorr_verify.hpp"
Expand Down Expand Up @@ -44,6 +45,7 @@ struct AcirFormat {
std::vector<Keccakf1600> keccak_permutations;
std::vector<PedersenConstraint> pedersen_constraints;
std::vector<PedersenHashConstraint> pedersen_hash_constraints;
std::vector<Poseidon2Constraint> poseidon2_constraints;
std::vector<FixedBaseScalarMul> fixed_base_scalar_mul_constraints;
std::vector<EcAdd> ec_add_constraints;
std::vector<RecursionConstraint> recursion_constraints;
Expand Down Expand Up @@ -75,6 +77,7 @@ struct AcirFormat {
keccak_permutations,
pedersen_constraints,
pedersen_hash_constraints,
poseidon2_constraints,
fixed_base_scalar_mul_constraints,
ec_add_constraints,
recursion_constraints,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ TEST_F(AcirFormatTests, TestASingleConstraintNoPubInputs)
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = {},
Expand Down Expand Up @@ -158,6 +159,7 @@ TEST_F(AcirFormatTests, TestLogicGateFromNoirCircuit)
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = {},
Expand Down Expand Up @@ -224,6 +226,7 @@ TEST_F(AcirFormatTests, TestSchnorrVerifyPass)
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = {},
Expand Down Expand Up @@ -318,6 +321,7 @@ TEST_F(AcirFormatTests, TestSchnorrVerifySmallRange)
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = {},
Expand Down Expand Up @@ -431,6 +435,7 @@ TEST_F(AcirFormatTests, TestVarKeccak)
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = {},
Expand Down Expand Up @@ -476,6 +481,7 @@ TEST_F(AcirFormatTests, TestKeccakPermutation)
.keccak_permutations = { keccak_permutation },
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "barretenberg/dsl/acir_format/keccak_constraint.hpp"
#include "barretenberg/dsl/acir_format/logic_constraint.hpp"
#include "barretenberg/dsl/acir_format/pedersen.hpp"
#include "barretenberg/dsl/acir_format/poseidon2_constraint.hpp"
#include "barretenberg/dsl/acir_format/range_constraint.hpp"
#include "barretenberg/dsl/acir_format/recursion_constraint.hpp"
#include "barretenberg/dsl/acir_format/schnorr_verify.hpp"
Expand Down Expand Up @@ -289,6 +290,12 @@ void handle_blackbox_func_call(Circuit::Opcode::BlackBoxFuncCall const& arg, Aci
.result = arg.output,
.opcode = BigIntOperationType::Div,
});
} else if constexpr (std::is_same_v<T, Circuit::BlackBoxFuncCall::Poseidon2Permutation>) {
af.poseidon2_constraints.push_back(Poseidon2Constraint{
.state = map(arg.inputs, [](auto& e) { return e.witness.value; }),
.result = map(arg.outputs, [](auto& e) { return e.value; }),
.len = arg.len,
});
}
},
arg.value.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ TEST_F(BigIntTests, TestBigIntConstraintMultiple)
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = {},
Expand Down Expand Up @@ -248,6 +249,7 @@ TEST_F(BigIntTests, TestBigIntConstraintSimple)
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = {},
Expand Down Expand Up @@ -299,6 +301,7 @@ TEST_F(BigIntTests, TestBigIntConstraintReuse)
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = {},
Expand Down Expand Up @@ -354,6 +357,7 @@ TEST_F(BigIntTests, TestBigIntConstraintReuse2)
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ TEST_F(UltraPlonkRAM, TestBlockConstraint)
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ TEST_F(EcOperations, TestECOperations)
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = { ec_add_constraint },
.recursion_constraints = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ TEST_F(ECDSASecp256k1, TestECDSAConstraintSucceed)
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = {},
Expand Down Expand Up @@ -151,6 +152,7 @@ TEST_F(ECDSASecp256k1, TestECDSACompilesForVerifier)
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = {},
Expand Down Expand Up @@ -193,6 +195,7 @@ TEST_F(ECDSASecp256k1, TestECDSAConstraintFail)
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ TEST(ECDSASecp256r1, test_hardcoded)
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = {},
Expand Down Expand Up @@ -187,6 +188,7 @@ TEST(ECDSASecp256r1, TestECDSAConstraintSucceed)
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = {},
Expand Down Expand Up @@ -233,6 +235,7 @@ TEST(ECDSASecp256r1, TestECDSACompilesForVerifier)
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = {},
Expand Down Expand Up @@ -274,6 +277,7 @@ TEST(ECDSASecp256r1, TestECDSAConstraintFail)
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ template <typename Builder> void create_keccak_permutations(Builder& builder, co
// Get the witness assignment for each witness index
// Write the witness assignment to the byte_array
for (size_t i = 0; i < constraint.state.size(); ++i) {
info(constraint.state[i]);
state[i] = field_ct::from_witness_index(&builder, constraint.state[i]);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "poseidon2_constraint.hpp"
#include "barretenberg/stdlib/hash/poseidon2/poseidon2_permutation_classic.hpp"
#include "barretenberg/stdlib/primitives/circuit_builders/circuit_builders_fwd.hpp"

namespace acir_format {
template <typename Builder> void create_poseidon2_permutations(Builder& builder, const Poseidon2Constraint& constraint)
{
using field_ct = bb::stdlib::field_t<Builder>;
using Poseidon2Params = bb::stdlib::crypto::Poseidon2Bn254ScalarFieldParams;
using State = std::array<field_ct, Poseidon2Params::t>;

ASSERT(constraint.state.size() == constraint.len);
ASSERT(constraint.result.size() == constraint.len);
// Get the witness assignment for each witness index
// Write the witness assignment to the byte_array state
State state;
for (size_t i = 0; i < constraint.state.size(); ++i) {
state[i] = field_ct::from_witness_index(&builder, constraint.state[i]);
}
State output_state;
if constexpr (IsGoblinBuilder<Builder>) {
output_state =
bb::stdlib::Poseidon2PermutationClassic<Poseidon2Params, Builder>::goblin_permutation(&builder, state);
} else {
output_state = bb::stdlib::Poseidon2PermutationClassic<Poseidon2Params, Builder>::permutation(&builder, state);
}
for (size_t i = 0; i < output_state.size(); ++i) {
poly_triple assert_equal{
.a = output_state[i].normalize().witness_index,
.b = constraint.result[i],
.c = 0,
.q_m = 0,
.q_l = 1,
.q_r = -1,
.q_o = 0,
.q_c = 0,
};
builder.create_poly_gate(assert_equal);
}
}

template void create_poseidon2_permutations<UltraCircuitBuilder>(UltraCircuitBuilder& builder,
const Poseidon2Constraint& constraint);

template void create_poseidon2_permutations<GoblinUltraCircuitBuilder>(GoblinUltraCircuitBuilder& builder,
const Poseidon2Constraint& constraint);
} // namespace acir_format
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
#include "barretenberg/dsl/types.hpp"
#include "barretenberg/serialize/msgpack.hpp"
#include <cstdint>
#include <vector>

namespace acir_format {

struct Poseidon2Constraint {
std::vector<uint32_t> state;
std::vector<uint32_t> result;
uint32_t len;

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

template <typename Builder> void create_poseidon2_permutations(Builder& builder, const Poseidon2Constraint& constraint);

} // namespace acir_format
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "poseidon2_constraint.hpp"
#include "acir_format.hpp"
#include "barretenberg/numeric/uint256/uint256.hpp"
#include "barretenberg/plonk/proof_system/types/proof.hpp"
#include "barretenberg/plonk/proof_system/verification_key/verification_key.hpp"

#include <cstdint>
#include <gtest/gtest.h>
#include <vector>

namespace acir_format::tests {

class Poseidon2Tests : public ::testing::Test {
protected:
static void SetUpTestSuite() { bb::srs::init_crs_factory("../srs_db/ignition"); }
};
using fr = field<Bn254FrParams>;

TEST_F(Poseidon2Tests, TestPoseidon2Permutation)
guipublic marked this conversation as resolved.
Show resolved Hide resolved
guipublic marked this conversation as resolved.
Show resolved Hide resolved
{
Poseidon2Constraint
poseidon2_constraint{
.state = { 1, 2, 3, 4, },
.result = { 5, 6, 7, 8, },
.len = 4,
};

AcirFormat constraint_system{ .varnum = 9,
.recursive = false,
.public_inputs = {},
.logic_constraints = {},
.range_constraints = {},
.sha256_constraints = {},
.schnorr_constraints = {},
.ecdsa_k1_constraints = {},
.ecdsa_r1_constraints = {},
.blake2s_constraints = {},
.blake3_constraints = {},
.keccak_constraints = {},
.keccak_var_constraints = {},
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = { poseidon2_constraint },
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = {},
.bigint_from_le_bytes_constraints = {},
.bigint_to_le_bytes_constraints = {},
.bigint_operations = {},
.constraints = {},
.block_constraints = {} };

WitnessVector witness{
1,
0,
1,
2,
3,
bb::fr(std::string("0x01bd538c2ee014ed5141b29e9ae240bf8db3fe5b9a38629a9647cf8d76c01737")),
bb::fr(std::string("0x239b62e7db98aa3a2a8f6a0d2fa1709e7a35959aa6c7034814d9daa90cbac662")),
bb::fr(std::string("0x04cbb44c61d928ed06808456bf758cbf0c18d1e15a7b6dbc8245fa7515d5e3cb")),
bb::fr(std::string("0x2e11c5cff2a22c64d01304b778d78f6998eff1ab73163a35603f54794c30847a")),
};

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

} // namespace acir_format::tests
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Builder create_inner_circuit()
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = {},
Expand Down Expand Up @@ -252,6 +253,7 @@ Builder create_outer_circuit(std::vector<Builder>& inner_circuits)
.keccak_permutations = {},
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.poseidon2_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.recursion_constraints = recursion_constraints,
Expand Down
Loading
Loading