Skip to content

Commit 661a878

Browse files
committed
ECC: Binary field perf. opt.
1 parent ad2f7b8 commit 661a878

13 files changed

+543
-287
lines changed

crypto/src/math/ec/custom/sec/SecT113Field.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@ public static void Invert(ulong[] x, ulong[] z)
8787

8888
public static void Multiply(ulong[] x, ulong[] y, ulong[] z)
8989
{
90-
ulong[] tt = Nat128.CreateExt64();
90+
ulong[] tt = new ulong[8];
9191
ImplMultiply(x, y, tt);
9292
Reduce(tt, z);
9393
}
9494

9595
public static void MultiplyAddToExt(ulong[] x, ulong[] y, ulong[] zz)
9696
{
97-
ulong[] tt = Nat128.CreateExt64();
97+
ulong[] tt = new ulong[8];
9898
ImplMultiply(x, y, tt);
9999
AddExt(zz, tt, zz);
100100
}
@@ -180,11 +180,12 @@ protected static void ImplMultiply(ulong[] x, ulong[] y, ulong[] zz)
180180
g1 = ((g0 >> 57) ^ (g1 << 7)) & M57;
181181
g0 &= M57;
182182

183+
ulong[] u = zz;
183184
ulong[] H = new ulong[6];
184185

185-
ImplMulw(f0, g0, H, 0); // H(0) 57/56 bits
186-
ImplMulw(f1, g1, H, 2); // H(INF) 57/54 bits
187-
ImplMulw(f0 ^ f1, g0 ^ g1, H, 4); // H(1) 57/56 bits
186+
ImplMulw(u, f0, g0, H, 0); // H(0) 57/56 bits
187+
ImplMulw(u, f1, g1, H, 2); // H(INF) 57/54 bits
188+
ImplMulw(u, f0 ^ f1, g0 ^ g1, H, 4); // H(1) 57/56 bits
188189

189190
ulong r = H[1] ^ H[2];
190191
ulong z0 = H[0],
@@ -198,12 +199,11 @@ protected static void ImplMultiply(ulong[] x, ulong[] y, ulong[] zz)
198199
zz[3] = (z3 >> 21);
199200
}
200201

201-
protected static void ImplMulw(ulong x, ulong y, ulong[] z, int zOff)
202+
protected static void ImplMulw(ulong[] u, ulong x, ulong y, ulong[] z, int zOff)
202203
{
203204
Debug.Assert(x >> 57 == 0);
204205
Debug.Assert(y >> 57 == 0);
205206

206-
ulong[] u = new ulong[8];
207207
//u[0] = 0;
208208
u[1] = y;
209209
u[2] = u[1] << 1;
@@ -237,8 +237,7 @@ protected static void ImplMulw(ulong x, ulong y, ulong[] z, int zOff)
237237

238238
protected static void ImplSquare(ulong[] x, ulong[] zz)
239239
{
240-
Interleave.Expand64To128(x[0], zz, 0);
241-
Interleave.Expand64To128(x[1], zz, 2);
240+
Interleave.Expand64To128(x, 0, 2, zz, 0);
242241
}
243242
}
244243
}

crypto/src/math/ec/custom/sec/SecT131Field.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ public static void Invert(ulong[] x, ulong[] z)
9393

9494
public static void Multiply(ulong[] x, ulong[] y, ulong[] z)
9595
{
96-
ulong[] tt = Nat192.CreateExt64();
96+
ulong[] tt = new ulong[8];
9797
ImplMultiply(x, y, tt);
9898
Reduce(tt, z);
9999
}
100100

101101
public static void MultiplyAddToExt(ulong[] x, ulong[] y, ulong[] zz)
102102
{
103-
ulong[] tt = Nat192.CreateExt64();
103+
ulong[] tt = new ulong[8];
104104
ImplMultiply(x, y, tt);
105105
AddExt(zz, tt, zz);
106106
}
@@ -214,21 +214,22 @@ protected static void ImplMultiply(ulong[] x, ulong[] y, ulong[] zz)
214214
g1 = ((g0 >> 44) ^ (g1 << 20)) & M44;
215215
g0 &= M44;
216216

217+
ulong[] u = zz;
217218
ulong[] H = new ulong[10];
218219

219-
ImplMulw(f0, g0, H, 0); // H(0) 44/43 bits
220-
ImplMulw(f2, g2, H, 2); // H(INF) 44/41 bits
220+
ImplMulw(u, f0, g0, H, 0); // H(0) 44/43 bits
221+
ImplMulw(u, f2, g2, H, 2); // H(INF) 44/41 bits
221222

222223
ulong t0 = f0 ^ f1 ^ f2;
223224
ulong t1 = g0 ^ g1 ^ g2;
224225

225-
ImplMulw(t0, t1, H, 4); // H(1) 44/43 bits
226+
ImplMulw(u, t0, t1, H, 4); // H(1) 44/43 bits
226227

227228
ulong t2 = (f1 << 1) ^ (f2 << 2);
228229
ulong t3 = (g1 << 1) ^ (g2 << 2);
229230

230-
ImplMulw(f0 ^ t2, g0 ^ t3, H, 6); // H(t) 44/45 bits
231-
ImplMulw(t0 ^ t2, t1 ^ t3, H, 8); // H(t + 1) 44/45 bits
231+
ImplMulw(u, f0 ^ t2, g0 ^ t3, H, 6); // H(t) 44/45 bits
232+
ImplMulw(u, t0 ^ t2, t1 ^ t3, H, 8); // H(t + 1) 44/45 bits
232233

233234
ulong t4 = H[6] ^ H[8];
234235
ulong t5 = H[7] ^ H[9];
@@ -301,12 +302,11 @@ protected static void ImplMultiply(ulong[] x, ulong[] y, ulong[] zz)
301302
ImplCompactExt(zz);
302303
}
303304

304-
protected static void ImplMulw(ulong x, ulong y, ulong[] z, int zOff)
305+
protected static void ImplMulw(ulong[] u, ulong x, ulong y, ulong[] z, int zOff)
305306
{
306307
Debug.Assert(x >> 45 == 0);
307308
Debug.Assert(y >> 45 == 0);
308309

309-
ulong[] u = new ulong[8];
310310
//u[0] = 0;
311311
u[1] = y;
312312
u[2] = u[1] << 1;
@@ -318,20 +318,23 @@ protected static void ImplMulw(ulong x, ulong y, ulong[] z, int zOff)
318318

319319
uint j = (uint)x;
320320
ulong g, h = 0, l = u[j & 7]
321-
^ u[(j >> 3) & 7] << 3
322-
^ u[(j >> 6) & 7] << 6;
323-
int k = 33;
321+
^ u[(j >> 3) & 7] << 3
322+
^ u[(j >> 6) & 7] << 6
323+
^ u[(j >> 9) & 7] << 9
324+
^ u[(j >> 12) & 7] << 12;
325+
int k = 30;
324326
do
325327
{
326328
j = (uint)(x >> k);
327329
g = u[j & 7]
328-
^ u[(j >> 3) & 7] << 3
329-
^ u[(j >> 6) & 7] << 6
330-
^ u[(j >> 9) & 7] << 9;
331-
l ^= (g << k);
330+
^ u[(j >> 3) & 7] << 3
331+
^ u[(j >> 6) & 7] << 6
332+
^ u[(j >> 9) & 7] << 9
333+
^ u[(j >> 12) & 7] << 12;
334+
l ^= (g << k);
332335
h ^= (g >> -k);
333336
}
334-
while ((k -= 12) > 0);
337+
while ((k -= 15) > 0);
335338

336339
Debug.Assert(h >> 25 == 0);
337340

@@ -341,8 +344,7 @@ protected static void ImplMulw(ulong x, ulong y, ulong[] z, int zOff)
341344

342345
protected static void ImplSquare(ulong[] x, ulong[] zz)
343346
{
344-
Interleave.Expand64To128(x[0], zz, 0);
345-
Interleave.Expand64To128(x[1], zz, 2);
347+
Interleave.Expand64To128(x, 0, 2, zz, 0);
346348
zz[4] = Interleave.Expand8to16((uint)x[2]);
347349
}
348350
}

crypto/src/math/ec/custom/sec/SecT163Field.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ public static void Invert(ulong[] x, ulong[] z)
106106

107107
public static void Multiply(ulong[] x, ulong[] y, ulong[] z)
108108
{
109-
ulong[] tt = Nat192.CreateExt64();
109+
ulong[] tt = new ulong[8];
110110
ImplMultiply(x, y, tt);
111111
Reduce(tt, z);
112112
}
113113

114114
public static void MultiplyAddToExt(ulong[] x, ulong[] y, ulong[] zz)
115115
{
116-
ulong[] tt = Nat192.CreateExt64();
116+
ulong[] tt = new ulong[8];
117117
ImplMultiply(x, y, tt);
118118
AddExt(zz, tt, zz);
119119
}
@@ -225,21 +225,22 @@ protected static void ImplMultiply(ulong[] x, ulong[] y, ulong[] zz)
225225
g1 = ((g0 >> 55) ^ (g1 << 9)) & M55;
226226
g0 &= M55;
227227

228+
ulong[] u = zz;
228229
ulong[] H = new ulong[10];
229230

230-
ImplMulw(f0, g0, H, 0); // H(0) 55/54 bits
231-
ImplMulw(f2, g2, H, 2); // H(INF) 55/50 bits
231+
ImplMulw(u, f0, g0, H, 0); // H(0) 55/54 bits
232+
ImplMulw(u, f2, g2, H, 2); // H(INF) 55/50 bits
232233

233234
ulong t0 = f0 ^ f1 ^ f2;
234235
ulong t1 = g0 ^ g1 ^ g2;
235236

236-
ImplMulw(t0, t1, H, 4); // H(1) 55/54 bits
237+
ImplMulw(u, t0, t1, H, 4); // H(1) 55/54 bits
237238

238239
ulong t2 = (f1 << 1) ^ (f2 << 2);
239240
ulong t3 = (g1 << 1) ^ (g2 << 2);
240241

241-
ImplMulw(f0 ^ t2, g0 ^ t3, H, 6); // H(t) 55/56 bits
242-
ImplMulw(t0 ^ t2, t1 ^ t3, H, 8); // H(t + 1) 55/56 bits
242+
ImplMulw(u, f0 ^ t2, g0 ^ t3, H, 6); // H(t) 55/56 bits
243+
ImplMulw(u, t0 ^ t2, t1 ^ t3, H, 8); // H(t + 1) 55/56 bits
243244

244245
ulong t4 = H[6] ^ H[8];
245246
ulong t5 = H[7] ^ H[9];
@@ -312,12 +313,11 @@ protected static void ImplMultiply(ulong[] x, ulong[] y, ulong[] zz)
312313
ImplCompactExt(zz);
313314
}
314315

315-
protected static void ImplMulw(ulong x, ulong y, ulong[] z, int zOff)
316+
protected static void ImplMulw(ulong[] u, ulong x, ulong y, ulong[] z, int zOff)
316317
{
317318
Debug.Assert(x >> 56 == 0);
318319
Debug.Assert(y >> 56 == 0);
319320

320-
ulong[] u = new ulong[8];
321321
//u[0] = 0;
322322
u[1] = y;
323323
u[2] = u[1] << 1;
@@ -349,9 +349,7 @@ protected static void ImplMulw(ulong x, ulong y, ulong[] z, int zOff)
349349

350350
protected static void ImplSquare(ulong[] x, ulong[] zz)
351351
{
352-
Interleave.Expand64To128(x[0], zz, 0);
353-
Interleave.Expand64To128(x[1], zz, 2);
354-
Interleave.Expand64To128(x[2], zz, 4);
352+
Interleave.Expand64To128(x, 0, 3, zz, 0);
355353
}
356354
}
357355
}

crypto/src/math/ec/custom/sec/SecT193Field.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -239,19 +239,21 @@ protected static void ImplMultiply(ulong[] x, ulong[] y, ulong[] zz)
239239
ImplExpand(x, f);
240240
ImplExpand(y, g);
241241

242-
ImplMulwAcc(f[0], g[0], zz, 0);
243-
ImplMulwAcc(f[1], g[1], zz, 1);
244-
ImplMulwAcc(f[2], g[2], zz, 2);
245-
ImplMulwAcc(f[3], g[3], zz, 3);
242+
ulong[] u = new ulong[8];
243+
244+
ImplMulwAcc(u, f[0], g[0], zz, 0);
245+
ImplMulwAcc(u, f[1], g[1], zz, 1);
246+
ImplMulwAcc(u, f[2], g[2], zz, 2);
247+
ImplMulwAcc(u, f[3], g[3], zz, 3);
246248

247249
// U *= (1 - t^n)
248250
for (int i = 5; i > 0; --i)
249251
{
250252
zz[i] ^= zz[i - 1];
251253
}
252254

253-
ImplMulwAcc(f[0] ^ f[1], g[0] ^ g[1], zz, 1);
254-
ImplMulwAcc(f[2] ^ f[3], g[2] ^ g[3], zz, 3);
255+
ImplMulwAcc(u, f[0] ^ f[1], g[0] ^ g[1], zz, 1);
256+
ImplMulwAcc(u, f[2] ^ f[3], g[2] ^ g[3], zz, 3);
255257

256258
// V *= (1 - t^2n)
257259
for (int i = 7; i > 1; --i)
@@ -263,10 +265,10 @@ protected static void ImplMultiply(ulong[] x, ulong[] y, ulong[] zz)
263265
{
264266
ulong c0 = f[0] ^ f[2], c1 = f[1] ^ f[3];
265267
ulong d0 = g[0] ^ g[2], d1 = g[1] ^ g[3];
266-
ImplMulwAcc(c0 ^ c1, d0 ^ d1, zz, 3);
268+
ImplMulwAcc(u, c0 ^ c1, d0 ^ d1, zz, 3);
267269
ulong[] t = new ulong[3];
268-
ImplMulwAcc(c0, d0, t, 0);
269-
ImplMulwAcc(c1, d1, t, 1);
270+
ImplMulwAcc(u, c0, d0, t, 0);
271+
ImplMulwAcc(u, c1, d1, t, 1);
270272
ulong t0 = t[0], t1 = t[1], t2 = t[2];
271273
zz[2] ^= t0;
272274
zz[3] ^= t0 ^ t1;
@@ -277,12 +279,11 @@ protected static void ImplMultiply(ulong[] x, ulong[] y, ulong[] zz)
277279
ImplCompactExt(zz);
278280
}
279281

280-
protected static void ImplMulwAcc(ulong x, ulong y, ulong[] z, int zOff)
282+
protected static void ImplMulwAcc(ulong[] u, ulong x, ulong y, ulong[] z, int zOff)
281283
{
282284
Debug.Assert(x >> 49 == 0);
283285
Debug.Assert(y >> 49 == 0);
284286

285-
ulong[] u = new ulong[8];
286287
//u[0] = 0;
287288
u[1] = y;
288289
u[2] = u[1] << 1;
@@ -317,9 +318,7 @@ protected static void ImplMulwAcc(ulong x, ulong y, ulong[] z, int zOff)
317318

318319
protected static void ImplSquare(ulong[] x, ulong[] zz)
319320
{
320-
Interleave.Expand64To128(x[0], zz, 0);
321-
Interleave.Expand64To128(x[1], zz, 2);
322-
Interleave.Expand64To128(x[2], zz, 4);
321+
Interleave.Expand64To128(x, 0, 3, zz, 0);
323322
zz[6] = (x[3] & M01);
324323
}
325324
}

crypto/src/math/ec/custom/sec/SecT233Field.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -251,19 +251,21 @@ protected static void ImplMultiply(ulong[] x, ulong[] y, ulong[] zz)
251251
ImplExpand(x, f);
252252
ImplExpand(y, g);
253253

254-
ImplMulwAcc(f[0], g[0], zz, 0);
255-
ImplMulwAcc(f[1], g[1], zz, 1);
256-
ImplMulwAcc(f[2], g[2], zz, 2);
257-
ImplMulwAcc(f[3], g[3], zz, 3);
254+
ulong[] u = new ulong[8];
255+
256+
ImplMulwAcc(u, f[0], g[0], zz, 0);
257+
ImplMulwAcc(u, f[1], g[1], zz, 1);
258+
ImplMulwAcc(u, f[2], g[2], zz, 2);
259+
ImplMulwAcc(u, f[3], g[3], zz, 3);
258260

259261
// U *= (1 - t^n)
260262
for (int i = 5; i > 0; --i)
261263
{
262264
zz[i] ^= zz[i - 1];
263265
}
264266

265-
ImplMulwAcc(f[0] ^ f[1], g[0] ^ g[1], zz, 1);
266-
ImplMulwAcc(f[2] ^ f[3], g[2] ^ g[3], zz, 3);
267+
ImplMulwAcc(u, f[0] ^ f[1], g[0] ^ g[1], zz, 1);
268+
ImplMulwAcc(u, f[2] ^ f[3], g[2] ^ g[3], zz, 3);
267269

268270
// V *= (1 - t^2n)
269271
for (int i = 7; i > 1; --i)
@@ -275,10 +277,10 @@ protected static void ImplMultiply(ulong[] x, ulong[] y, ulong[] zz)
275277
{
276278
ulong c0 = f[0] ^ f[2], c1 = f[1] ^ f[3];
277279
ulong d0 = g[0] ^ g[2], d1 = g[1] ^ g[3];
278-
ImplMulwAcc(c0 ^ c1, d0 ^ d1, zz, 3);
280+
ImplMulwAcc(u, c0 ^ c1, d0 ^ d1, zz, 3);
279281
ulong[] t = new ulong[3];
280-
ImplMulwAcc(c0, d0, t, 0);
281-
ImplMulwAcc(c1, d1, t, 1);
282+
ImplMulwAcc(u, c0, d0, t, 0);
283+
ImplMulwAcc(u, c1, d1, t, 1);
282284
ulong t0 = t[0], t1 = t[1], t2 = t[2];
283285
zz[2] ^= t0;
284286
zz[3] ^= t0 ^ t1;
@@ -289,12 +291,11 @@ protected static void ImplMultiply(ulong[] x, ulong[] y, ulong[] zz)
289291
ImplCompactExt(zz);
290292
}
291293

292-
protected static void ImplMulwAcc(ulong x, ulong y, ulong[] z, int zOff)
294+
protected static void ImplMulwAcc(ulong[] u, ulong x, ulong y, ulong[] z, int zOff)
293295
{
294296
Debug.Assert(x >> 59 == 0);
295297
Debug.Assert(y >> 59 == 0);
296298

297-
ulong[] u = new ulong[8];
298299
//u[0] = 0;
299300
u[1] = y;
300301
u[2] = u[1] << 1;
@@ -326,10 +327,7 @@ protected static void ImplMulwAcc(ulong x, ulong y, ulong[] z, int zOff)
326327

327328
protected static void ImplSquare(ulong[] x, ulong[] zz)
328329
{
329-
Interleave.Expand64To128(x[0], zz, 0);
330-
Interleave.Expand64To128(x[1], zz, 2);
331-
Interleave.Expand64To128(x[2], zz, 4);
332-
Interleave.Expand64To128(x[3], zz, 6);
330+
Interleave.Expand64To128(x, 0, 4, zz, 0);
333331
}
334332
}
335333
}

0 commit comments

Comments
 (0)