-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: implement poseidon2 opcode (#4446)
This PR create constraints for poseidon2permutation Noir opcodes. I added a unit test for the opcode. It also implements poseidon2 permutation circuit for UltraPlonk. Because the s-box function would require an increase of the polynomial degree in Ultraplonk relations, it is not compatible with GoblinPlonk circuits. So I added a non-optimised UltraPlonk version for Poseidon2 permutation. This is necessary because Noir requires the UltraPlonk backend and it would not work if we had only the GoblinPlonk version of the opcode. Thank you @lucasxia01 for the templatised version of the function. --------- Co-authored-by: lucasxia01 <lucasxia01@gmail.com>
- Loading branch information
1 parent
5ddfa16
commit 491a8df
Showing
19 changed files
with
291 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
barretenberg/cpp/src/barretenberg/dsl/acir_format/poseidon2_constraint.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include "poseidon2_constraint.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; | ||
output_state = bb::stdlib::Poseidon2Permutation<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 |
21 changes: 21 additions & 0 deletions
21
barretenberg/cpp/src/barretenberg/dsl/acir_format/poseidon2_constraint.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
82 changes: 82 additions & 0 deletions
82
barretenberg/cpp/src/barretenberg/dsl/acir_format/poseidon2_constraint.test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#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>; | ||
|
||
/** | ||
* @brief Create a circuit testing the Poseidon2 permutation function | ||
* | ||
*/ | ||
TEST_F(Poseidon2Tests, TestPoseidon2Permutation) | ||
{ | ||
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 = {}, | ||
.sha256_compression = {}, | ||
.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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.