barretenberg
Loading...
Searching...
No Matches
elliptic_relation.hpp
1#pragma once
2#include "barretenberg/ecc/curves/bn254/bn254.hpp"
3#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp"
4#include "barretenberg/relations/relation_types.hpp"
5
6namespace proof_system {
7
8template <typename FF_> class EllipticRelationImpl {
9 public:
10 using FF = FF_;
11
12 static constexpr std::array<size_t, 2> SUBRELATION_PARTIAL_LENGTHS{
13 6, // x-coordinate sub-relation
14 6, // y-coordinate sub-relation
15 };
16
17 // TODO(@zac-williamson #2609 find more generic way of doing this)
18 static constexpr FF get_curve_b()
19 {
20 if constexpr (FF::modulus == barretenberg::fq::modulus) {
21 return barretenberg::g1::curve_b;
22 } else if constexpr (FF::modulus == grumpkin::fq::modulus) {
23 return grumpkin::g1::curve_b;
24 } else {
25 return 0;
26 }
27 }
28
39 template <typename ContainerOverSubrelations, typename AllEntities, typename Parameters>
40 inline static void accumulate(ContainerOverSubrelations& accumulators,
41 const AllEntities& in,
42 const Parameters&,
43 const FF& scaling_factor)
44 {
45 // TODO(@zac - williamson #2608 when Pedersen refactor is completed,
46 // replace old addition relations with these ones and
47 // remove endomorphism coefficient in ecc add gate(not used))
48
49 using Accumulator = typename std::tuple_element_t<0, ContainerOverSubrelations>;
50 using View = typename Accumulator::View;
51 auto x_1 = View(in.w_r);
52 auto y_1 = View(in.w_o);
53
54 auto x_2 = View(in.w_l_shift);
55 auto y_2 = View(in.w_4_shift);
56 auto y_3 = View(in.w_o_shift);
57 auto x_3 = View(in.w_r_shift);
58
59 auto q_sign = View(in.q_l);
60 auto q_elliptic = View(in.q_elliptic);
61 auto q_is_double = View(in.q_m);
62
63 // Contribution (1) point addition, x-coordinate check
64 // q_elliptic * (x3 + x2 + x1)(x2 - x1)(x2 - x1) - y2^2 - y1^2 + 2(y2y1)*q_sign = 0
65 auto x_diff = (x_2 - x_1);
66 auto y2_sqr = (y_2 * y_2);
67 auto y1_sqr = (y_1 * y_1);
68 auto y1y2 = y_1 * y_2 * q_sign;
69 auto x_add_identity = (x_3 + x_2 + x_1) * x_diff * x_diff - y2_sqr - y1_sqr + y1y2 + y1y2;
70 std::get<0>(accumulators) += x_add_identity * scaling_factor * q_elliptic * (-q_is_double + 1);
71
72 // Contribution (2) point addition, x-coordinate check
73 // q_elliptic * (q_sign * y1 + y3)(x2 - x1) + (x3 - x1)(y2 - q_sign * y1) = 0
74 auto y1_plus_y3 = y_1 + y_3;
75 auto y_diff = y_2 * q_sign - y_1;
76 auto y_add_identity = y1_plus_y3 * x_diff + (x_3 - x_1) * y_diff;
77 std::get<1>(accumulators) += y_add_identity * scaling_factor * q_elliptic * (-q_is_double + 1);
78
79 // Contribution (3) point doubling, x-coordinate check
80 // (x3 + x1 + x1) (4y1*y1) - 9 * x1 * x1 * x1 * x1 = 0
81 // N.B. we're using the equivalence x1*x1*x1 === y1*y1 - curve_b to reduce degree by 1
82 const auto curve_b = get_curve_b();
83 auto x_pow_4 = (y1_sqr - curve_b) * x_1;
84 auto y1_sqr_mul_4 = y1_sqr + y1_sqr;
85 y1_sqr_mul_4 += y1_sqr_mul_4;
86 auto x1_pow_4_mul_9 = x_pow_4 * 9;
87 auto x_double_identity = (x_3 + x_1 + x_1) * y1_sqr_mul_4 - x1_pow_4_mul_9;
88 std::get<0>(accumulators) += x_double_identity * scaling_factor * q_elliptic * q_is_double;
89
90 // Contribution (4) point doubling, y-coordinate check
91 // (y1 + y1) (2y1) - (3 * x1 * x1)(x1 - x3) = 0
92 auto x1_sqr_mul_3 = (x_1 + x_1 + x_1) * x_1;
93 auto y_double_identity = x1_sqr_mul_3 * (x_1 - x_3) - (y_1 + y_1) * (y_1 + y_3);
94 std::get<1>(accumulators) += y_double_identity * scaling_factor * q_elliptic * q_is_double;
95 };
96};
97
98template <typename FF> using EllipticRelation = Relation<EllipticRelationImpl<FF>>;
99} // namespace proof_system
Definition: elliptic_relation.hpp:8
static void accumulate(ContainerOverSubrelations &accumulators, const AllEntities &in, const Parameters &, const FF &scaling_factor)
Expression for the Ultra Arithmetic gate.
Definition: elliptic_relation.hpp:40