Skip to content

Commit 0b6f740

Browse files
committed
Optimizing xDBLADD, some minor editing
1 parent fa0a77d commit 0b6f740

File tree

3 files changed

+29
-37
lines changed

3 files changed

+29
-37
lines changed

src/compression/dlog.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ void from_base(int *D, digit_t *r, int Dlen, int base)
3232
digit[0] = (digit_t)(-D[i]);
3333
if ((base & 1) == 0) {
3434
Montgomery_neg(digit, (digit_t*)Alice_order);
35-
}
36-
else {
35+
} else {
3736
mp_sub((digit_t*)Bob_order, digit, digit, NWORDS_ORDER);
3837
}
3938
} else {
@@ -70,8 +69,7 @@ void from_base(int *D, digit_t *r, int Dlen, int base)
7069
digit[0] = (digit_t)(-D[0]);
7170
if ((base & 1) == 0) {
7271
Montgomery_neg(digit, (digit_t*)Alice_order);
73-
}
74-
else {
72+
} else {
7573
mp_sub((digit_t*)Bob_order, digit, digit, NWORDS_ORDER);
7674
}
7775
} else {
@@ -88,7 +86,6 @@ void from_base(int *D, digit_t *r, int Dlen, int base)
8886

8987
#ifdef COMPRESSED_TABLES
9088

91-
9289
#ifdef ELL2_TORUS
9390

9491
int ord2w_dlog(const felm_t *r, const int *logT, const felm_t *Texp)
@@ -97,11 +94,9 @@ int ord2w_dlog(const felm_t *r, const int *logT, const felm_t *Texp)
9794
// Output: corresponding digit d in [-2^{w1-1},2^{w1-1}]
9895
felm_t x, y;
9996
felm_t sum = {0}, prods[1<<(W_2_1-1)] = {0};
100-
f2elm_t tmp;
10197

10298
fpcopy(r[0], x);
10399
fpcopy(r[1], y);
104-
105100
fpcorrection(x);
106101
fpcorrection(y);
107102

@@ -112,11 +107,10 @@ int ord2w_dlog(const felm_t *r, const int *logT, const felm_t *Texp)
112107
fpneg(sum);
113108
fpcorrection(sum);
114109
if (memcmp(x, sum, NBITS_TO_NBYTES(NBITS_FIELD)) == 0) return logT[2];
115-
for (int j = 2; j < W_2; ++j)
116-
{
117-
for (int i = 0; i < (1<<(j-1)); ++i)
118-
{
119-
if ((i % 2) == 0) fpmul_mont(y, Texp[(1<<(j-2)) + (i/2) - 1], prods[(1<<(j-2)) + (i/2) - 1]);
110+
for (int j = 2; j < W_2; ++j) {
111+
for (int i = 0; i < (1<<(j-1)); ++i) {
112+
if ((i % 2) == 0)
113+
fpmul_mont(y, Texp[(1<<(j-2)) + (i/2) - 1], prods[(1<<(j-2)) + (i/2) - 1]);
120114
fpcopy(y, sum);
121115
for (int k = 0; k <= j-2; ++k) {
122116
if (((i>>(j-k-2)) % 2) == 0)
@@ -142,18 +136,17 @@ int ord2w_dlog(const felm_t *r, const int *logT, const felm_t *Texp)
142136
// Output: The signed digit D in {-ell^(w-1), ..., ell^(w-1)}
143137
int ord2w_dloghyb(const felm_t *h, const int *logT, const felm_t *Texp, const felm_t *G)
144138
{
145-
int k = 0, d = 0, index = 0, ord = 0, tmp = 0, w = W_2, w2 = w - W_2_1, i_j = 0, t, pow0, pow1;
139+
int k = 0, d = 0, index = 0, ord = 0, tmp = 0, w = W_2, w2 = w - W_2_1, i_j = 0, t;
146140
uint8_t inv = 0, flag = 0;
147-
f2elm_t H[W_2_1] = {0}, tmp2; // Size of H should be max of {W_2_1, W_2 - W_2_1}
141+
f2elm_t H[W_2_1] = {0}; // Size of H should be max of {W_2_1, W_2 - W_2_1}
148142
felm_t one = {0};
149143

150144
fpcopy((digit_t*)&Montgomery_one, one);
151145
fp2copy(h, (felm_t*)&H[0]);
152146
fpcorrection(H[0][0]);
153147
fpcorrection(H[0][1]);
154148

155-
for (int i = 1; i <= w2; ++i)
156-
{
149+
for (int i = 1; i <= w2; ++i) {
157150
if (!is_felm_zero(H[0][1])) { // check if first compressed Fp2 element in H is NOT the identity
158151
for (int j = k; j >= 0; j--) fp2copy(H[j], H[j+1]);
159152
sqr_Fp2_cycl_proj(H[0]);

src/ec_isogeny.c

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -273,26 +273,25 @@ void xDBLADD(point_proj_t P, point_proj_t Q, const f2elm_t XPQ, const f2elm_t ZP
273273
// Output: projective Montgomery points P <- 2*P = (X2P:Z2P) such that x(2P)=X2P/Z2P, and Q <- P+Q = (XQP:ZQP) such that = x(Q+P)=XQP/ZQP.
274274
f2elm_t t0, t1, t2;
275275

276-
fp2add(P->X, P->Z, t0); // t0 = XP+ZP
277-
fp2sub(P->X, P->Z, t1); // t1 = XP-ZP
278-
fp2sqr_mont(t0, P->X); // XP = (XP+ZP)^2
279-
fp2sub(Q->X, Q->Z, t2); // t2 = XQ-ZQ
280-
fp2correction(t2);
281-
fp2add(Q->X, Q->Z, Q->X); // XQ = XQ+ZQ
282-
fp2mul_mont(t0, t2, t0); // t0 = (XP+ZP)*(XQ-ZQ)
283-
fp2sqr_mont(t1, P->Z); // ZP = (XP-ZP)^2
284-
fp2mul_mont(t1, Q->X, t1); // t1 = (XP-ZP)*(XQ+ZQ)
285-
fp2sub(P->X, P->Z, t2); // t2 = (XP+ZP)^2-(XP-ZP)^2
286-
fp2mul_mont(P->X, P->Z, P->X); // XP = (XP+ZP)^2*(XP-ZP)^2
287-
fp2mul_mont(t2, A24, Q->X); // XQ = A24*[(XP+ZP)^2-(XP-ZP)^2]
288-
fp2sub(t0, t1, Q->Z); // ZQ = (XP+ZP)*(XQ-ZQ)-(XP-ZP)*(XQ+ZQ)
289-
fp2add(Q->X, P->Z, P->Z); // ZP = A24*[(XP+ZP)^2-(XP-ZP)^2]+(XP-ZP)^2
290-
fp2add(t0, t1, Q->X); // XQ = (XP+ZP)*(XQ-ZQ)+(XP-ZP)*(XQ+ZQ)
291-
fp2mul_mont(P->Z, t2, P->Z); // ZP = [A24*[(XP+ZP)^2-(XP-ZP)^2]+(XP-ZP)^2]*[(XP+ZP)^2-(XP-ZP)^2]
292-
fp2sqr_mont(Q->Z, Q->Z); // ZQ = [(XP+ZP)*(XQ-ZQ)-(XP-ZP)*(XQ+ZQ)]^2
293-
fp2sqr_mont(Q->X, Q->X); // XQ = [(XP+ZP)*(XQ-ZQ)+(XP-ZP)*(XQ+ZQ)]^2
294-
fp2mul_mont(Q->X, ZPQ, Q->X); // XQ = ZPQ*[(XP+ZP)*(XQ-ZQ)+(XP-ZP)*(XQ+ZQ)]^2
295-
fp2mul_mont(Q->Z, XPQ, Q->Z); // ZQ = XPQ*[(XP+ZP)*(XQ-ZQ)-(XP-ZP)*(XQ+ZQ)]^2
276+
mp2_add(P->X, P->Z, t0); // t0 = XP+ZP
277+
mp2_sub_p2(P->X, P->Z, t1); // t1 = XP-ZP
278+
fp2sqr_mont(t0, P->X); // XP = (XP+ZP)^2
279+
mp2_sub_p2(Q->X, Q->Z, t2); // t2 = XQ-ZQ
280+
mp2_add(Q->X, Q->Z, Q->X); // XQ = XQ+ZQ
281+
fp2mul_mont(t0, t2, t0); // t0 = (XP+ZP)*(XQ-ZQ)
282+
fp2sqr_mont(t1, P->Z); // ZP = (XP-ZP)^2
283+
fp2mul_mont(t1, Q->X, t1); // t1 = (XP-ZP)*(XQ+ZQ)
284+
mp2_sub_p2(P->X, P->Z, t2); // t2 = (XP+ZP)^2-(XP-ZP)^2
285+
fp2mul_mont(P->X, P->Z, P->X); // XP = (XP+ZP)^2*(XP-ZP)^2
286+
fp2mul_mont(A24, t2, Q->X); // XQ = A24*[(XP+ZP)^2-(XP-ZP)^2]
287+
mp2_sub_p2(t0, t1, Q->Z); // ZQ = (XP+ZP)*(XQ-ZQ)-(XP-ZP)*(XQ+ZQ)
288+
mp2_add(Q->X, P->Z, P->Z); // ZP = A24*[(XP+ZP)^2-(XP-ZP)^2]+(XP-ZP)^2
289+
mp2_add(t0, t1, Q->X); // XQ = (XP+ZP)*(XQ-ZQ)+(XP-ZP)*(XQ+ZQ)
290+
fp2mul_mont(P->Z, t2, P->Z); // ZP = [A24*[(XP+ZP)^2-(XP-ZP)^2]+(XP-ZP)^2]*[(XP+ZP)^2-(XP-ZP)^2]
291+
fp2sqr_mont(Q->Z, Q->Z); // ZQ = [(XP+ZP)*(XQ-ZQ)-(XP-ZP)*(XQ+ZQ)]^2
292+
fp2sqr_mont(Q->X, Q->X); // XQ = [(XP+ZP)*(XQ-ZQ)+(XP-ZP)*(XQ+ZQ)]^2
293+
fp2mul_mont(Q->Z, XPQ, Q->Z); // ZQ = xPQ*[(XP+ZP)*(XQ-ZQ)-(XP-ZP)*(XQ+ZQ)]^2
294+
fp2mul_mont(Q->X, ZPQ, Q->X); // XQ = ZPQ*[(XP+ZP)*(XQ-ZQ)+(XP-ZP)*(XQ+ZQ)]^2
296295
}
297296

298297

src/fpx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,7 @@ int reverse_bits(int t, unsigned int nbits)
15581558
x >>= 1;
15591559
bits++;
15601560
}
1561-
while (bits < nbits) {
1561+
while ((unsigned int)bits < nbits) {
15621562
r <<= 1;
15631563
bits++;
15641564
}

0 commit comments

Comments
 (0)