Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
elliptic_relation.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
11
12namespace bb {
13
14template <typename FF_> class EllipticRelationImpl {
15 public:
16 using FF = FF_;
17
18 static constexpr std::array<size_t, 2> SUBRELATION_PARTIAL_LENGTHS{
19 6, // x-coordinate sub-relation
20 6, // y-coordinate sub-relation
21 };
22
27 template <typename AllEntities> inline static bool skip(const AllEntities& in) { return in.q_elliptic.is_zero(); }
28
29 // TODO(@zac-williamson #2609 find more generic way of doing this)
30 static constexpr FF get_curve_b()
31 {
32 if constexpr (FF::modulus == bb::fq::modulus) {
33 return bb::g1::curve_b;
34 } else if constexpr (FF::modulus == grumpkin::fq::modulus) {
36 } else {
37 return 0;
38 }
39 }
40
79 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
80 inline static void accumulate(ContainerOverSubrelations& accumulators,
81 const AllEntities& in,
82 const Parameters&,
83 const FF& scaling_factor)
84 {
85 using Accumulator = typename std::tuple_element_t<0, ContainerOverSubrelations>;
86 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
87 auto x_3_m = CoefficientAccumulator(in.w_r_shift);
88 auto y_1_m = CoefficientAccumulator(in.w_o);
89 auto y_2_m = CoefficientAccumulator(in.w_4_shift);
90
91 auto x_1_m = CoefficientAccumulator(in.w_r);
92 auto x_2_m = CoefficientAccumulator(in.w_l_shift);
93 auto y_3_m = CoefficientAccumulator(in.w_o_shift);
94 auto q_elliptic_m = CoefficientAccumulator(in.q_elliptic);
95 auto q_is_double_m = CoefficientAccumulator(in.q_m);
96 auto q_sign_m = CoefficientAccumulator(in.q_l);
97
98 // We efficiently construct the following:
99 auto x2_sub_x1_m = (x_2_m - x_1_m); // (x2 - x1)
100 auto x1_mul_3_m = (x_1_m + x_1_m + x_1_m); // (3*x1)
101 auto x3_sub_x1_m = x_3_m - x_1_m; // (x3 - x1)
102 auto x3_plus_two_x1_m = x3_sub_x1_m + x1_mul_3_m; // (x3 - x1 + 3*x1) = (x3 + 2*x1)
103 auto x3_plus_x2_plus_x1_m = x3_plus_two_x1_m + x2_sub_x1_m; // (x3 + 2*x1 + x2 - x1) = (x3 + x2 + x1)
104 Accumulator x3_plus_x2_plus_x1(x3_plus_x2_plus_x1_m);
105 Accumulator x3_sub_x1(x3_sub_x1_m);
106 Accumulator x1_mul_3(x1_mul_3_m);
107 Accumulator x3_plus_two_x1(x3_plus_two_x1_m);
108
109 // Contribution (1) point addition, x-coordinate check:
110 // q_elliptic * (q_is_double - 1) * (x3 + x2 + x1)(x2 - x1)(x2 - x1) - y2^2 - y1^2 + 2(y2y1)*q_sign = 0
111 auto y2_sqr_m = y_2_m.sqr();
112 auto y1_sqr_m = y_1_m.sqr();
113 auto y2_mul_q_sign_m = y_2_m * q_sign_m;
114 auto x_add_identity = x3_plus_x2_plus_x1 * Accumulator(x2_sub_x1_m.sqr()) - Accumulator(y2_sqr_m + y1_sqr_m) +
115 Accumulator(y2_mul_q_sign_m + y2_mul_q_sign_m) * Accumulator(y_1_m);
116
117 auto q_elliptic_by_scaling_m = q_elliptic_m * scaling_factor; // a * q_elliptic
118 auto q_elliptic_q_double_scaling_m = (q_elliptic_by_scaling_m * q_is_double_m); // a * q_elliptic * q_is_double
119 Accumulator q_elliptic_q_double_scaling(q_elliptic_q_double_scaling_m);
120 // (a * q_elliptic * q_is_double - a * q_elliptic) = a * q_elliptic * (q_is_double - 1)
121 auto neg_q_elliptic_not_double_scaling = Accumulator(q_elliptic_q_double_scaling_m - q_elliptic_by_scaling_m);
122 std::get<0>(accumulators) -= x_add_identity * neg_q_elliptic_not_double_scaling;
123
124 // Contribution (2) point addition, x-coordinate check:
125 // q_elliptic * (q_is_double - 1) * (y1 + y3)(x2 - x1) + (x3 - x1)(q_sign * y2 - y1) = 0
126 auto y1_plus_y3_m = y_1_m + y_3_m; // (y1 + y3)
127 auto y_diff_m = y2_mul_q_sign_m - y_1_m; // (q_sign * y2 - y1)
128 auto y_diff = Accumulator(y_diff_m);
129 auto y_add_identity = Accumulator(y1_plus_y3_m * x2_sub_x1_m) + (x3_sub_x1)*y_diff;
130 std::get<1>(accumulators) -= y_add_identity * neg_q_elliptic_not_double_scaling;
131
132 // Contribution (3) point doubling, x-coordinate check
133 // (x3 + x1 + x1) (4*y1*y1) - 9 * x1 * x1 * x1 * x1 = 0
134 // N.B. we're using the equivalence x1^3 === y1^2 - curve_b to reduce degree by 1
135 const auto curve_b = get_curve_b();
136 auto x_pow_4_mul_3 = (Accumulator(y1_sqr_m - curve_b)) * x1_mul_3;
137 auto y1_sqr_mul_4_m = y1_sqr_m + y1_sqr_m;
138 y1_sqr_mul_4_m += y1_sqr_mul_4_m;
139 auto x1_pow_4_mul_9 = x_pow_4_mul_3 + x_pow_4_mul_3 + x_pow_4_mul_3;
140 auto x_double_identity = x3_plus_two_x1 * Accumulator(y1_sqr_mul_4_m) - x1_pow_4_mul_9;
141 std::get<0>(accumulators) += x_double_identity * q_elliptic_q_double_scaling;
142
143 // Contribution (4) point doubling, y-coordinate check
144 // (y1 + y3) (2*y1) - (3 * x1 * x1)(x1 - x3) = 0
145 auto x1_sqr_mul_3 = Accumulator(x1_mul_3_m * x_1_m);
146 auto neg_y_double_identity = x1_sqr_mul_3 * (x3_sub_x1) + Accumulator((y_1_m + y_1_m) * (y1_plus_y3_m));
147 std::get<1>(accumulators) -= neg_y_double_identity * q_elliptic_q_double_scaling;
148 };
149};
150
152} // namespace bb
static constexpr std::array< size_t, 2 > SUBRELATION_PARTIAL_LENGTHS
static bool skip(const AllEntities &in)
Returns true if the contribution from all subrelations for the provided inputs is identically zero.
static constexpr FF get_curve_b()
static void accumulate(ContainerOverSubrelations &accumulators, const AllEntities &in, const Parameters &, const FF &scaling_factor)
Expression for elliptic curve point addition and doubling.
A wrapper for Relations to expose methods used by the Sumcheck prover or verifier to add the contribu...
static constexpr Fq curve_b
Definition group.hpp:51
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static constexpr uint256_t modulus