Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
ec_operations.test.cpp
Go to the documentation of this file.
1#include "ec_operations.hpp"
2#include "acir_format.hpp"
5
8
9#include <gtest/gtest.h>
10#include <vector>
11
12namespace acir_format::tests {
13
15
16class EcOperations : public ::testing::Test {
17 protected:
19};
20
21size_t generate_ec_add_constraint(EcAdd& ec_add_constraint, WitnessVector& witness_values)
22{
23 using cycle_group_ct = bb::stdlib::cycle_group<Builder>;
24 witness_values.push_back(0);
26 cycle_group_ct input_point(g1);
27 // Doubling
28 cycle_group_ct result = input_point.dbl();
29 // add: x,y,x2,y2
30 witness_values.push_back(g1.x);
31 witness_values.push_back(g1.y);
32 witness_values.push_back(g1.x);
33 witness_values.push_back(g1.y);
34 witness_values.push_back(result.x.get_value());
35 witness_values.push_back(result.y.get_value());
36 witness_values.push_back(fr(0));
37 witness_values.push_back(fr(0));
38 ec_add_constraint = EcAdd{
41 .input1_infinite = WitnessOrConstant<bb::fr>::from_index(7),
44 .input2_infinite = WitnessOrConstant<bb::fr>::from_index(7),
45 .result_x = 5,
46 .result_y = 6,
47 .result_infinite = 8,
48 };
49 return witness_values.size();
50}
51
52TEST_F(EcOperations, TestECOperations)
53{
54 EcAdd ec_add_constraint;
55
56 WitnessVector witness_values;
57 size_t num_variables = generate_ec_add_constraint(ec_add_constraint, witness_values);
58
59 AcirFormat constraint_system{
60 .varnum = static_cast<uint32_t>(num_variables + 1),
61 .num_acir_opcodes = 1,
62 .public_inputs = {},
63 .ec_add_constraints = { ec_add_constraint },
64 .original_opcode_indices = create_empty_original_opcode_indices(),
65 };
66 mock_opcode_indices(constraint_system);
67
68 AcirProgram program{ constraint_system, witness_values };
69 auto builder = create_circuit(program);
70
71 EXPECT_TRUE(CircuitChecker::check(builder));
72}
73
74TEST_F(EcOperations, TestECMultiScalarMul)
75{
76 MultiScalarMul msm_constrain;
77
78 WitnessVector witness_values;
79 witness_values.emplace_back(fr(0));
80
81 witness_values = {
82 // dummy
83 fr(0),
84 // g1: x,y,infinite
85 fr(1),
86 fr("0x0000000000000002cf135e7506a45d632d270d45f1181294833fc48d823f272c"),
87 fr(0),
88 // low, high scalars
89 fr(1),
90 fr(0),
91 // result
92 fr("0x06ce1b0827aafa85ddeb49cdaa36306d19a74caa311e13d46d8bc688cdbffffe"),
93 fr("0x1c122f81a3a14964909ede0ba2a6855fc93faf6fa1a788bf467be7e7a43f80ac"),
94 fr(0),
95 };
96 msm_constrain = MultiScalarMul{
98 .index = 1,
99 .value = fr(0),
100 .is_constant = false,
101 },
103 .index = 2,
104 .value = fr(0),
105 .is_constant = false,
106 },
108 .index = 3,
109 .value = fr(0),
110 .is_constant = false,
111 },
113 .index = 1,
114 .value = fr(0),
115 .is_constant = false,
116 },
118 .index = 2,
119 .value = fr(0),
120 .is_constant = false,
121 },
123 .index = 3,
124 .value = fr(0),
125 .is_constant = false,
126 } },
127 .scalars = { WitnessOrConstant<fr>{
128 .index = 4,
129 .value = fr(0),
130 .is_constant = false,
131 },
133 .index = 5,
134 .value = fr(0),
135 .is_constant = false,
136 },
138 .index = 4,
139 .value = fr(0),
140 .is_constant = false,
141 },
143 .index = 5,
144 .value = fr(0),
145 .is_constant = false,
146 } },
147 .out_point_x = 6,
148 .out_point_y = 7,
149 .out_point_is_infinite = 0,
150 };
151 auto res_x = fr("0x06ce1b0827aafa85ddeb49cdaa36306d19a74caa311e13d46d8bc688cdbffffe");
152 auto assert_equal = poly_triple{
153 .a = 6,
154 .b = 0,
155 .c = 0,
156 .q_m = 0,
157 .q_l = fr::neg_one(),
158 .q_r = 0,
159 .q_o = 0,
160 .q_c = res_x,
161 };
162
163 size_t num_variables = witness_values.size();
164 AcirFormat constraint_system{
165 .varnum = static_cast<uint32_t>(num_variables + 1),
166 .num_acir_opcodes = 1,
167 .public_inputs = {},
168 .multi_scalar_mul_constraints = { msm_constrain },
169 .poly_triple_constraints = { assert_equal },
170 .original_opcode_indices = create_empty_original_opcode_indices(),
171 };
172 mock_opcode_indices(constraint_system);
173
174 AcirProgram program{ constraint_system, witness_values };
175 auto builder = create_circuit(program);
176
177 EXPECT_TRUE(CircuitChecker::check(builder));
178}
179
180} // namespace acir_format::tests
acir_format::AcirFormatOriginalOpcodeIndices create_empty_original_opcode_indices()
void mock_opcode_indices(acir_format::AcirFormat &constraint_system)
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
group class. Represents an elliptic curve group element. Group is parametrised by Fq and Fr
Definition group.hpp:36
static constexpr affine_element affine_one
Definition group.hpp:48
cycle_group represents a group Element of the proving system's embedded curve i.e....
AluTraceBuilder builder
Definition alu.test.cpp:123
size_t generate_ec_add_constraint(EcAdd &ec_add_constraint, WitnessVector &witness_values)
field< Bn254FrParams > fr
UltraCircuitBuilder create_circuit(AcirProgram &program, const ProgramMetadata &metadata)
Specialization for creating an Ultra circuit from an acir program.
bb::SlabVector< bb::fr > WitnessVector
std::filesystem::path bb_crs_path()
void init_file_crs_factory(const std::filesystem::path &path)
TEST_F(IPATest, ChallengesAreZero)
Definition ipa.test.cpp:123
WitnessOrConstant< bb::fr > input1_x
std::vector< WitnessOrConstant< bb::fr > > points
static WitnessOrConstant from_index(uint32_t index)
static constexpr field neg_one()