Skip to content

Commit 854fa48

Browse files
committed
feat: constrain matching is_inf for double case
1 parent 19b58ca commit 854fa48

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

barretenberg/cpp/pil/vm2/ecc.pil

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* This subtrace supports point addition over the Grumpkin curve.
44
* Given two points, P & Q, this trace computes R = P + Q.
5-
* PRECONDITIONS: The only assumption here is that the inputs P & Q are points on the Grumpkin curve (note that the Point at Infinity is considered on the curve):
5+
* PRECONDITIONS: The only assumption here is that the inputs P & Q are points on the Grumpkin curve (note that the Point at Infinity = (0, 0) is considered on the curve):
66
* Grumpkin Curve Eqn in SW form: Y^2 = X^3 − 17.
77
* Note: Grumpkin forms a 2-cycle with BN254, i.e the base field of one is the scalar field of the other and vice-versa.
88
*
@@ -40,7 +40,7 @@ namespace ecc;
4040
// P = (p_x, p_y, p_is_inf), Q = (q_x, q_y, q_is_inf), R = (r_x, r_y, r_is_inf),
4141
// where the coordinates satisfy:
4242
// y^2 = x^3 - 17 (unless is_inf is true).
43-
// The point at infinity, O, does not exist on the curve (a property of SW curves). We represent it as:
43+
// The point at infinity, O, does not have valid coordinates (a property of SW curves). We represent it as:
4444
// O = (0, 0, true).
4545
// Note: this is NOT enforced here for inputs, see ecc_mem.pil for example of constraining.
4646
//
@@ -124,6 +124,11 @@ namespace ecc;
124124
#[DOUBLE_PRED]
125125
double_op - (x_match * y_match) = 0;
126126

127+
// If double_op is true, we check that p_is_inf == q_is_inf. #[DOUBLE_PRED] implies this (since x_match &
128+
// y_match must imply p_is_inf == q_is_inf for points on the curve) however we include an extra check for
129+
// the #[DOUBLE] lookup in scalar_mul, which ignores q_is_inf.
130+
double_op * (p_is_inf - q_is_inf) = 0;
131+
127132
pol commit inv_2_p_y;
128133
(1 - result_infinity) * double_op * (2 * p_y * inv_2_p_y - 1) = 0;
129134

barretenberg/cpp/src/barretenberg/vm2/generated/relations/ecc.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ template <typename FF_> class eccImpl {
1414
public:
1515
using FF = FF_;
1616

17-
static constexpr std::array<size_t, 19> SUBRELATION_PARTIAL_LENGTHS = { 3, 3, 3, 3, 3, 3, 3, 3, 5, 3,
18-
5, 3, 5, 6, 6, 5, 6, 6, 3 };
17+
static constexpr std::array<size_t, 20> SUBRELATION_PARTIAL_LENGTHS = { 3, 3, 3, 3, 3, 3, 3, 3, 5, 3,
18+
5, 3, 3, 5, 6, 6, 5, 6, 6, 3 };
1919

2020
template <typename AllEntities> inline static bool skip(const AllEntities& in)
2121
{
@@ -40,11 +40,11 @@ template <typename FF> class ecc : public Relation<eccImpl<FF>> {
4040
static constexpr size_t SR_X_MATCH = 8;
4141
static constexpr size_t SR_Y_MATCH = 10;
4242
static constexpr size_t SR_DOUBLE_PRED = 11;
43-
static constexpr size_t SR_COMPUTED_LAMBDA = 13;
44-
static constexpr size_t SR_INFINITY_RESULT = 15;
45-
static constexpr size_t SR_OUTPUT_X_COORD = 16;
46-
static constexpr size_t SR_OUTPUT_Y_COORD = 17;
47-
static constexpr size_t SR_OUTPUT_INF_FLAG = 18;
43+
static constexpr size_t SR_COMPUTED_LAMBDA = 14;
44+
static constexpr size_t SR_INFINITY_RESULT = 16;
45+
static constexpr size_t SR_OUTPUT_X_COORD = 17;
46+
static constexpr size_t SR_OUTPUT_Y_COORD = 18;
47+
static constexpr size_t SR_OUTPUT_INF_FLAG = 19;
4848

4949
static std::string get_subrelation_label(size_t index)
5050
{

barretenberg/cpp/src/barretenberg/vm2/generated/relations/ecc_impl.hpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,61 +103,67 @@ void eccImpl<FF_>::accumulate(ContainerOverSubrelations& evals,
103103
}
104104
{
105105
using View = typename std::tuple_element_t<12, ContainerOverSubrelations>::View;
106+
auto tmp = static_cast<View>(in.get(C::ecc_double_op)) *
107+
(static_cast<View>(in.get(C::ecc_p_is_inf)) - static_cast<View>(in.get(C::ecc_q_is_inf)));
108+
std::get<12>(evals) += (tmp * scaling_factor);
109+
}
110+
{
111+
using View = typename std::tuple_element_t<13, ContainerOverSubrelations>::View;
106112
auto tmp =
107113
(FF(1) - static_cast<View>(in.get(C::ecc_result_infinity))) * static_cast<View>(in.get(C::ecc_double_op)) *
108114
(FF(2) * static_cast<View>(in.get(C::ecc_p_y)) * static_cast<View>(in.get(C::ecc_inv_2_p_y)) - FF(1));
109-
std::get<12>(evals) += (tmp * scaling_factor);
115+
std::get<13>(evals) += (tmp * scaling_factor);
110116
}
111117
{ // COMPUTED_LAMBDA
112-
using View = typename std::tuple_element_t<13, ContainerOverSubrelations>::View;
118+
using View = typename std::tuple_element_t<14, ContainerOverSubrelations>::View;
113119
auto tmp = static_cast<View>(in.get(C::ecc_sel)) *
114120
(static_cast<View>(in.get(C::ecc_lambda)) -
115121
(static_cast<View>(in.get(C::ecc_double_op)) * FF(3) * static_cast<View>(in.get(C::ecc_p_x)) *
116122
static_cast<View>(in.get(C::ecc_p_x)) * static_cast<View>(in.get(C::ecc_inv_2_p_y)) +
117123
static_cast<View>(in.get(C::ecc_add_op)) * CView(ecc_Y_DIFF) *
118124
static_cast<View>(in.get(C::ecc_inv_x_diff))));
119-
std::get<13>(evals) += (tmp * scaling_factor);
125+
std::get<14>(evals) += (tmp * scaling_factor);
120126
}
121127
{
122-
using View = typename std::tuple_element_t<14, ContainerOverSubrelations>::View;
128+
using View = typename std::tuple_element_t<15, ContainerOverSubrelations>::View;
123129
auto tmp =
124130
(static_cast<View>(in.get(C::ecc_use_computed_result)) -
125131
static_cast<View>(in.get(C::ecc_sel)) * CView(ecc_BOTH_NON_INF) * (FF(1) - CView(ecc_INVERSE_PRED)));
126-
std::get<14>(evals) += (tmp * scaling_factor);
132+
std::get<15>(evals) += (tmp * scaling_factor);
127133
}
128134
{ // INFINITY_RESULT
129-
using View = typename std::tuple_element_t<15, ContainerOverSubrelations>::View;
135+
using View = typename std::tuple_element_t<16, ContainerOverSubrelations>::View;
130136
auto tmp = (static_cast<View>(in.get(C::ecc_result_infinity)) -
131137
(CView(ecc_INVERSE_PRED) * CView(ecc_BOTH_NON_INF) + CView(ecc_BOTH_INF)));
132-
std::get<15>(evals) += (tmp * scaling_factor);
138+
std::get<16>(evals) += (tmp * scaling_factor);
133139
}
134140
{ // OUTPUT_X_COORD
135-
using View = typename std::tuple_element_t<16, ContainerOverSubrelations>::View;
141+
using View = typename std::tuple_element_t<17, ContainerOverSubrelations>::View;
136142
auto tmp = static_cast<View>(in.get(C::ecc_sel)) *
137143
(((static_cast<View>(in.get(C::ecc_r_x)) -
138144
CView(ecc_EITHER_INF) *
139145
(static_cast<View>(in.get(C::ecc_p_is_inf)) * static_cast<View>(in.get(C::ecc_q_x)) +
140146
static_cast<View>(in.get(C::ecc_q_is_inf)) * static_cast<View>(in.get(C::ecc_p_x)))) -
141147
static_cast<View>(in.get(C::ecc_result_infinity)) * CView(ecc_INFINITY_X)) -
142148
static_cast<View>(in.get(C::ecc_use_computed_result)) * CView(ecc_COMPUTED_R_X));
143-
std::get<16>(evals) += (tmp * scaling_factor);
149+
std::get<17>(evals) += (tmp * scaling_factor);
144150
}
145151
{ // OUTPUT_Y_COORD
146-
using View = typename std::tuple_element_t<17, ContainerOverSubrelations>::View;
152+
using View = typename std::tuple_element_t<18, ContainerOverSubrelations>::View;
147153
auto tmp = static_cast<View>(in.get(C::ecc_sel)) *
148154
(((static_cast<View>(in.get(C::ecc_r_y)) -
149155
CView(ecc_EITHER_INF) *
150156
(static_cast<View>(in.get(C::ecc_p_is_inf)) * static_cast<View>(in.get(C::ecc_q_y)) +
151157
static_cast<View>(in.get(C::ecc_q_is_inf)) * static_cast<View>(in.get(C::ecc_p_y)))) -
152158
static_cast<View>(in.get(C::ecc_result_infinity)) * CView(ecc_INFINITY_Y)) -
153159
static_cast<View>(in.get(C::ecc_use_computed_result)) * CView(ecc_COMPUTED_R_Y));
154-
std::get<17>(evals) += (tmp * scaling_factor);
160+
std::get<18>(evals) += (tmp * scaling_factor);
155161
}
156162
{ // OUTPUT_INF_FLAG
157-
using View = typename std::tuple_element_t<18, ContainerOverSubrelations>::View;
163+
using View = typename std::tuple_element_t<19, ContainerOverSubrelations>::View;
158164
auto tmp = static_cast<View>(in.get(C::ecc_sel)) *
159165
(static_cast<View>(in.get(C::ecc_r_is_inf)) - static_cast<View>(in.get(C::ecc_result_infinity)));
160-
std::get<18>(evals) += (tmp * scaling_factor);
166+
std::get<19>(evals) += (tmp * scaling_factor);
161167
}
162168
}
163169

0 commit comments

Comments
 (0)