Skip to content

Commit df6c55e

Browse files
committed
Faster subgroup membership tests.
1 parent ce57d38 commit df6c55e

File tree

5 files changed

+25
-31
lines changed

5 files changed

+25
-31
lines changed

include/relic_epx.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ void ep2_rhs(fp2_t rhs, const ep2_t p);
707707
*
708708
* @param[in] p - the point to test.
709709
*/
710-
int ep2_on_curve(ep2_t p);
710+
int ep2_on_curve(const ep2_t p);
711711

712712
/**
713713
* Builds a precomputation table for multiplying a random prime elliptic point.
@@ -1347,7 +1347,7 @@ void ep4_rhs(fp4_t rhs, const ep4_t p);
13471347
*
13481348
* @param[in] p - the point to test.
13491349
*/
1350-
int ep4_on_curve(ep4_t p);
1350+
int ep4_on_curve(const ep4_t p);
13511351

13521352
/**
13531353
* Builds a precomputation table for multiplying a random prime elliptic point.

src/ep/relic_ep_map.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,8 @@ void ep_map_from_field(ep_t p, const uint8_t *uniform_bytes, int len) {
148148
break;
149149
case EP_B12:
150150
case EP_B24:
151-
/* multiply by 1-x (x the BLS parameter) to get the correct group. */
152-
/* XXX(rsw) is this guaranteed to work? It could fail if one
153-
* of the prime-squared subgroups is cyclic, but
154-
* maybe there's an argument that this is never the case...
155-
*/
151+
/* Multiply by (1-x) to get the correct group, as proven in
152+
* Piellard. https://eprint.iacr.org/2022/352.pdf */
156153
fp_prime_get_par(k);
157154
bn_neg(k, k);
158155
bn_add_dig(k, k, 1);

src/epx/relic_ep2_util.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ void ep2_rhs(fp2_t rhs, const ep2_t p) {
169169
}
170170

171171

172-
int ep2_on_curve(ep2_t p) {
172+
int ep2_on_curve(const ep2_t p) {
173173
ep2_t t;
174174
int r = 0;
175175

src/epx/relic_ep4_util.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ void ep4_rhs(fp4_t rhs, const ep4_t p) {
182182
}
183183

184184

185-
int ep4_on_curve(ep4_t p) {
185+
int ep4_on_curve(const ep4_t p) {
186186
ep4_t t;
187187
int r = 0;
188188

src/pc/relic_pc_util.c

+19-22
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,19 @@ void gt_get_gen(gt_t g) {
6666

6767
int g1_is_valid(const g1_t a) {
6868
bn_t n;
69-
g1_t t, u, v;
69+
g1_t u, v;
7070
int r = 0;
7171

7272
if (g1_is_infty(a)) {
7373
return 0;
7474
}
7575

7676
bn_null(n);
77-
g1_null(t);
7877
g1_null(u);
7978
g1_null(v);
8079

8180
RLC_TRY {
8281
bn_new(n);
83-
g1_new(t);
8482
g1_new(u);
8583
g1_new(v);
8684

@@ -90,24 +88,26 @@ int g1_is_valid(const g1_t a) {
9088
r = g1_on_curve(a);
9189
} else {
9290
switch (ep_curve_is_pairf()) {
93-
/* Formulas from "Faster Subgroup Checks for BLS12-381" by Bowe.
94-
* https://eprint.iacr.org/2019/814.pdf, together with tweaks
95-
* by Mike Scott. */
91+
/* Formulas from "Co-factor clearing and subgroup membership
92+
* testing on pairing-friendly curves" by El Housni, Guillevic,
93+
* Piellard. https://eprint.iacr.org/2022/352.pdf */
9694
case EP_B12:
97-
/* Check [(z^2−1)](\psi(P)+P) == -P.*/
95+
case EP_B24:
96+
/* Check [\psi(P) == [z^2 - 1]P. */
9897
fp_prime_get_par(n);
9998
bn_sqr(n, n);
99+
if (ep_curve_is_pairf() == EP_B24) {
100+
bn_sqr(n, n);
101+
}
100102
bn_sub_dig(n, n, 1);
101-
ep_psi(t, a);
102-
ep_add(t, t, a);
103-
ep_copy(u, t);
103+
ep_copy(u, a);
104104
for (int i = bn_bits(n) - 2; i >= 0; i--) {
105105
g1_dbl(u, u);
106106
if (bn_get_bit(n, i)) {
107-
g1_add(u, u, t);
107+
g1_add(u, u, a);
108108
}
109109
}
110-
g1_neg(v, a);
110+
ep_psi(v, a);
111111
r = g1_on_curve(a) && (g1_cmp(v, u) == RLC_EQ);
112112
break;
113113
default:
@@ -126,7 +126,6 @@ int g1_is_valid(const g1_t a) {
126126
RLC_THROW(ERR_CAUGHT);
127127
} RLC_FINALLY {
128128
bn_free(n);
129-
g1_free(t);
130129
g1_free(u);
131130
g1_free(v);
132131
}
@@ -183,17 +182,19 @@ int g2_is_valid(const g2_t a) {
183182
r = g2_on_curve(a) && (g2_cmp(u, v) == RLC_EQ);
184183
} else {
185184
switch (ep_curve_is_pairf()) {
186-
/* Formulas from "Faster Subgroup Checks for BLS12-381" by Bowe.
187-
* https://eprint.iacr.org/2019/814.pdf */
185+
/* Formulas from "Co-factor clearing and subgroup membership
186+
* testing on pairing-friendly curves" by El Housni, Guillevic,
187+
* Piellard. https://eprint.iacr.org/2022/352.pdf */
188188
case EP_B12:
189+
case EP_B24:
189190
#if FP_PRIME == 383
190191
/* Since p mod n = r, we can check instead that
191192
* psi^4(P) + P == \psi^2(P). */
192193
ep2_frb(u, a, 4);
193194
ep2_add(u, u, a);
194195
ep2_frb(v, a, 2);
195196
#else
196-
/* Check [z]psi^3(P) + P == \psi^2(P). */
197+
/* Check \psi(P) == z(P). */
197198
fp_prime_get_par(n);
198199
g2_copy(u, a);
199200
for (int i = bn_bits(n) - 2; i >= 0; i--) {
@@ -205,9 +206,7 @@ int g2_is_valid(const g2_t a) {
205206
if (bn_sign(n) == RLC_NEG) {
206207
g2_neg(u, u);
207208
}
208-
g2_frb(u, u, 3);
209-
g2_frb(v, a, 2);
210-
g2_add(u, u, a);
209+
g2_frb(v, a, 1);
211210
#endif
212211
r = g2_on_curve(a) && (g2_cmp(u, v) == RLC_EQ);
213212
break;
@@ -305,15 +304,13 @@ int gt_is_valid(const gt_t a) {
305304
#endif
306305
r &= fp12_test_cyc((void *)a);
307306
break;
308-
#if FP_PRIME == 315 || FP_PRIME == 317 || FP_PRIME == 509
309307
case EP_B24:
310308
/* Check that a^u = a^p. */
311309
gt_frb(u, a, 1);
312310
fp24_exp_cyc_sps((void *)v, (void *)a, b, l, bn_sign(n));
313311
r = (gt_cmp(u, v) == RLC_EQ);
314-
r = fp24_test_cyc((void *)a);
312+
r &= fp24_test_cyc((void *)a);
315313
break;
316-
#endif
317314
default:
318315
/* Common case. */
319316
bn_sub_dig(n, n, 1);

0 commit comments

Comments
 (0)