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
51 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
52 inline static void accumulate(ContainerOverSubrelations& accumulators,
53 const AllEntities& in,
54 const Parameters&,
55 const FF& scaling_factor)
56 {
57 using Accumulator = typename std::tuple_element_t<0, ContainerOverSubrelations>;
58 using CoefficientAccumulator = typename Accumulator::CoefficientAccumulator;
59 auto x_3_m = CoefficientAccumulator(in.w_r_shift);
60 auto y_1_m = CoefficientAccumulator(in.w_o);
61 auto y_2_m = CoefficientAccumulator(in.w_4_shift);
62
63 auto x_1_m = CoefficientAccumulator(in.w_r);
64 auto x_2_m = CoefficientAccumulator(in.w_l_shift);
65 auto y_3_m = CoefficientAccumulator(in.w_o_shift);
66 auto q_elliptic_m = CoefficientAccumulator(in.q_elliptic);
67 auto q_is_double_m = CoefficientAccumulator(in.q_m);
68 auto q_sign_m = CoefficientAccumulator(in.q_l);
69
70 // we need to efficiently construct the following:
71 // 1. (x2 - x1)
72 // 2. (x3 - x1)
73 // 3. (x3 + x2 + x1)
74 // 4. (x1 + x1 + x1)
75 auto x2_sub_x1_m = (x_2_m - x_1_m);
76 auto x1_mul_3_m = (x_1_m + x_1_m + x_1_m); // used
77 auto x3_sub_x1_m = x_3_m - x_1_m; // used
78 auto x3_plus_two_x1_m = x3_sub_x1_m + x1_mul_3_m; // used
79 auto x3_plus_x2_plus_x1_m = x3_plus_two_x1_m + x2_sub_x1_m; // used
80 Accumulator x3_plus_x2_plus_x1(x3_plus_x2_plus_x1_m);
81 Accumulator x3_sub_x1(x3_sub_x1_m);
82 Accumulator x1_mul_3(x1_mul_3_m);
83 Accumulator x3_plus_two_x1(x3_plus_two_x1_m);
84
85 // Contribution (1) point addition, x-coordinate check
86 // q_elliptic * (x3 + x2 + x1)(x2 - x1)(x2 - x1) - y2^2 - y1^2 + 2(y2y1)*q_sign = 0
87 auto y2_sqr_m = y_2_m.sqr();
88 auto y1_sqr_m = y_1_m.sqr();
89 auto y2_mul_q_sign_m = y_2_m * q_sign_m;
90 auto x_add_identity = x3_plus_x2_plus_x1 * Accumulator(x2_sub_x1_m.sqr()) - Accumulator(y2_sqr_m + y1_sqr_m) +
91 Accumulator(y2_mul_q_sign_m + y2_mul_q_sign_m) * Accumulator(y_1_m);
92
93 auto q_elliptic_by_scaling_m = q_elliptic_m * scaling_factor;
94 auto q_elliptic_q_double_scaling_m = (q_elliptic_by_scaling_m * q_is_double_m);
95 Accumulator q_elliptic_q_double_scaling(q_elliptic_q_double_scaling_m);
96 auto neg_q_elliptic_not_double_scaling = Accumulator(q_elliptic_q_double_scaling_m - q_elliptic_by_scaling_m);
97 std::get<0>(accumulators) -= x_add_identity * neg_q_elliptic_not_double_scaling;
98
99 // Contribution (2) point addition, x-coordinate check
100 // q_elliptic * (q_sign * y1 + y3)(x2 - x1) + (x3 - x1)(y2 - q_sign * y1) = 0
101 auto y1_plus_y3_m = y_1_m + y_3_m;
102 auto y_diff_m = y2_mul_q_sign_m - y_1_m;
103 auto y_diff = Accumulator(y_diff_m);
104 auto y_add_identity = Accumulator(y1_plus_y3_m * x2_sub_x1_m) + (x3_sub_x1)*y_diff;
105 std::get<1>(accumulators) -= y_add_identity * neg_q_elliptic_not_double_scaling;
106
107 // Contribution (3) point doubling, x-coordinate check
108 // (x3 + x1 + x1) (4y1*y1) - 9 * x1 * x1 * x1 * x1 = 0
109 // N.B. we're using the equivalence x1*x1*x1 === y1*y1 - curve_b to reduce degree by 1
110 const auto curve_b = get_curve_b();
111 auto x_pow_4_mul_3 = (Accumulator(y1_sqr_m - curve_b)) * x1_mul_3;
112 auto y1_sqr_mul_4_m = y1_sqr_m + y1_sqr_m;
113 y1_sqr_mul_4_m += y1_sqr_mul_4_m;
114 auto x1_pow_4_mul_9 = x_pow_4_mul_3 + x_pow_4_mul_3 + x_pow_4_mul_3;
115 auto x_double_identity = x3_plus_two_x1 * Accumulator(y1_sqr_mul_4_m) - x1_pow_4_mul_9;
116 std::get<0>(accumulators) += x_double_identity * q_elliptic_q_double_scaling;
117
118 // Contribution (4) point doubling, y-coordinate check
119 // (y1 + y3) (2y1) - (3 * x1 * x1)(x1 - x3) = 0
120 auto x1_sqr_mul_3 = Accumulator(x1_mul_3_m * x_1_m);
121 auto neg_y_double_identity = x1_sqr_mul_3 * (x3_sub_x1) + Accumulator((y_1_m + y_1_m) * (y1_plus_y3_m));
122 std::get<1>(accumulators) -= neg_y_double_identity * q_elliptic_q_double_scaling;
123 };
124};
125
127} // 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 the Ultra Arithmetic gate.
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.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static constexpr uint256_t modulus