Skip to content

Commit

Permalink
feat: add ACIR opcodes for ECADD and ECDOUBLE (AztecProtocol#3878)
Browse files Browse the repository at this point in the history
Please provide a paragraph or two giving a summary of the change,
including relevant motivation and context.

Related to  noir-lang/noir#3958

This PR does not implements the opcodes, but modifies BB interface so
that it accept the new opcodes.
It also does not implement the solver for the opcodes.


# Checklist:
Remove the checklist to signal you've completed it. Enable auto-merge if
the PR is ready to merge.
- [ ] If the pull request requires a cryptography review (e.g.
cryptographic algorithm implementations) I have added the 'crypto' tag.
- [X] I have reviewed my diff in github, line by line and removed
unexpected formatting changes, testing logs, or commented-out code.
- [X] Every change is related to the PR description.
- [X] I have
[linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue)
this pull request to relevant issues (if any exist).

---------

Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
  • Loading branch information
guipublic and TomAFrench authored Jan 9, 2024
1 parent 7353a35 commit 537630f
Show file tree
Hide file tree
Showing 26 changed files with 872 additions and 7 deletions.
10 changes: 10 additions & 0 deletions barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ void build_constraints(Builder& builder, acir_format const& constraint_system, b
create_fixed_base_constraint(builder, constraint);
}

// Add ec add constraints
for (const auto& constraint : constraint_system.ec_add_constraints) {
create_ec_add_constraint(builder, constraint);
}

// Add ec double
for (const auto& constraint : constraint_system.ec_double_constraints) {
create_ec_double_constraint(builder, constraint);
}

// Add block constraints
for (const auto& constraint : constraint_system.block_constraints) {
create_block_constraints(builder, constraint, has_valid_witness_assignments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "blake2s_constraint.hpp"
#include "blake3_constraint.hpp"
#include "block_constraint.hpp"
#include "ec_operations.hpp"
#include "ecdsa_secp256k1.hpp"
#include "ecdsa_secp256r1.hpp"
#include "fixed_base_scalar_mul.hpp"
Expand Down Expand Up @@ -38,6 +39,8 @@ struct acir_format {
std::vector<PedersenConstraint> pedersen_constraints;
std::vector<PedersenHashConstraint> pedersen_hash_constraints;
std::vector<FixedBaseScalarMul> fixed_base_scalar_mul_constraints;
std::vector<EcAdd> ec_add_constraints;
std::vector<EcDouble> ec_double_constraints;
std::vector<RecursionConstraint> recursion_constraints;

// A standard plonk arithmetic constraint, as defined in the poly_triple struct, consists of selector values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ TEST_F(AcirFormatTests, TestASingleConstraintNoPubInputs)
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.ec_double_constraints = {},
.recursion_constraints = {},
.constraints = { constraint },
.block_constraints = {},
Expand Down Expand Up @@ -152,6 +154,8 @@ TEST_F(AcirFormatTests, TestLogicGateFromNoirCircuit)
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.ec_double_constraints = {},
.recursion_constraints = {},
.constraints = { expr_a, expr_b, expr_c, expr_d },
.block_constraints = {} };
Expand Down Expand Up @@ -218,6 +222,8 @@ TEST_F(AcirFormatTests, TestSchnorrVerifyPass)
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.ec_double_constraints = {},
.recursion_constraints = {},
.constraints = { poly_triple{
.a = schnorr_constraint.result,
Expand Down Expand Up @@ -307,6 +313,8 @@ TEST_F(AcirFormatTests, TestSchnorrVerifySmallRange)
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.ec_double_constraints = {},
.recursion_constraints = {},
.constraints = { poly_triple{
.a = schnorr_constraint.result,
Expand Down Expand Up @@ -415,6 +423,8 @@ TEST_F(AcirFormatTests, TestVarKeccak)
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.ec_double_constraints = {},
.recursion_constraints = {},
.constraints = { dummy },
.block_constraints = {},
Expand Down Expand Up @@ -454,6 +464,8 @@ TEST_F(AcirFormatTests, TestKeccakPermutation)
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.ec_double_constraints = {},
.recursion_constraints = {},
.constraints = {},
.block_constraints = {} };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ TEST_F(UltraPlonkRAM, TestBlockConstraint)
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.ec_double_constraints = {},
.recursion_constraints = {},
.constraints = {},
.block_constraints = { block },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "ec_operations.hpp"
#include "barretenberg/dsl/types.hpp"
#include "barretenberg/ecc/curves/bn254/fr.hpp"
#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp"
#include "barretenberg/proof_system/arithmetization/gate_data.hpp"

namespace acir_format {

template <typename Builder> void create_ec_add_constraint(Builder& builder, const EcAdd& input)
{
// TODO
builder.assert_equal(input.input1_x, input.input1_x);
ASSERT(false);
}

template void create_ec_add_constraint<UltraCircuitBuilder>(UltraCircuitBuilder& builder, const EcAdd& input);
template void create_ec_add_constraint<GoblinUltraCircuitBuilder>(GoblinUltraCircuitBuilder& builder,
const EcAdd& input);

template <typename Builder> void create_ec_double_constraint(Builder& builder, const EcDouble& input)
{
// TODO
builder.assert_equal(input.input_x, input.input_x);
ASSERT(false);
}

template void create_ec_double_constraint<UltraCircuitBuilder>(UltraCircuitBuilder& builder, const EcDouble& input);
template void create_ec_double_constraint<GoblinUltraCircuitBuilder>(GoblinUltraCircuitBuilder& builder,
const EcDouble& input);

} // namespace acir_format
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once
#include "barretenberg/dsl/types.hpp"
#include "barretenberg/serialize/msgpack.hpp"
#include <cstdint>

namespace acir_format {

struct EcAdd {
uint32_t input1_x;
uint32_t input1_y;
uint32_t input2_x;
uint32_t input2_y;
uint32_t result_x;
uint32_t result_y;

// for serialization, update with any new fields
MSGPACK_FIELDS(input1_x, input1_y, input2_x, input2_y, result_x, result_y);
friend bool operator==(EcAdd const& lhs, EcAdd const& rhs) = default;
};

template <typename Builder> void create_ec_add_constraint(Builder& builder, const EcAdd& input);

struct EcDouble {
uint32_t input_x;
uint32_t input_y;
uint32_t result_x;
uint32_t result_y;

// for serialization, update with any new fields
MSGPACK_FIELDS(input_x, input_y, result_x, result_y);
friend bool operator==(EcDouble const& lhs, EcDouble const& rhs) = default;
};

template <typename Builder> void create_ec_double_constraint(Builder& builder, const EcDouble& input);
} // namespace acir_format
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ TEST_F(ECDSASecp256k1, TestECDSAConstraintSucceed)
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.ec_double_constraints = {},
.recursion_constraints = {},
.constraints = {},
.block_constraints = {},
Expand Down Expand Up @@ -146,6 +148,8 @@ TEST_F(ECDSASecp256k1, TestECDSACompilesForVerifier)
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.ec_double_constraints = {},
.recursion_constraints = {},
.constraints = {},
.block_constraints = {},
Expand Down Expand Up @@ -183,6 +187,8 @@ TEST_F(ECDSASecp256k1, TestECDSAConstraintFail)
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.ec_double_constraints = {},
.recursion_constraints = {},
.constraints = {},
.block_constraints = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ TEST(ECDSASecp256r1, test_hardcoded)
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.ec_double_constraints = {},
.recursion_constraints = {},
.constraints = {},
.block_constraints = {},
Expand Down Expand Up @@ -181,6 +183,8 @@ TEST(ECDSASecp256r1, TestECDSAConstraintSucceed)
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.ec_double_constraints = {},
.recursion_constraints = {},
.constraints = {},
.block_constraints = {},
Expand Down Expand Up @@ -222,6 +226,8 @@ TEST(ECDSASecp256r1, TestECDSACompilesForVerifier)
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.ec_double_constraints = {},
.recursion_constraints = {},
.constraints = {},
.block_constraints = {},
Expand Down Expand Up @@ -258,6 +264,8 @@ TEST(ECDSASecp256r1, TestECDSAConstraintFail)
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.ec_double_constraints = {},
.recursion_constraints = {},
.constraints = {},
.block_constraints = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ Builder create_inner_circuit()
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.ec_double_constraints = {},
.recursion_constraints = {},
.constraints = { expr_a, expr_b, expr_c, expr_d },
.block_constraints = {} };
Expand Down Expand Up @@ -253,6 +255,8 @@ Builder create_outer_circuit(std::vector<Builder>& inner_circuits)
.pedersen_constraints = {},
.pedersen_hash_constraints = {},
.fixed_base_scalar_mul_constraints = {},
.ec_add_constraints = {},
.ec_double_constraints = {},
.recursion_constraints = recursion_constraints,
.constraints = {},
.block_constraints = {} };
Expand Down
Loading

0 comments on commit 537630f

Please sign in to comment.