Skip to content

Commit e8412e4

Browse files
committed
WIP different C logic
1 parent 35ae499 commit e8412e4

File tree

1 file changed

+21
-74
lines changed

1 file changed

+21
-74
lines changed

src/field_5x64_impl.h

Lines changed: 21 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -220,95 +220,43 @@ void secp256k1_fe_sqr_inner(uint64_t *r, const uint64_t *a);
220220
} while(0)
221221

222222
/* Add a**2 to [c0,c1]. c0,c1 must all be 0 on input. */
223-
#define sqr2(c0,c1,a) do {\
224-
uint128_t t = (uint128_t)(a) * (a); \
225-
VERIFY_CHECK(c0 == 0); \
226-
VERIFY_CHECK(c1 == 0); \
227-
c0 = t; \
228-
c1 = t >> 64; \
229-
} while(0)
223+
#define sqr2(c0,c1,a) mul2(c0,c1,a,a)
230224

231225
/* Add a*b to [c0,c1,c2]. c2 must never overflow. */
232226
#define muladd3(c0,c1,c2,a,b) do {\
233-
uint64_t tl, th; \
234-
{ \
235-
uint128_t t = (uint128_t)(a) * (b); \
236-
th = t >> 64; /* at most 0xFFFFFFFFFFFFFFFE */ \
237-
tl = t; \
238-
} \
239-
c0 += tl; /* overflow is handled on the next line */ \
240-
th += (c0 < tl); /* at most 0xFFFFFFFFFFFFFFFF */ \
241-
c1 += th; /* overflow is handled on the next line */ \
242-
c2 += (c1 < th); /* never overflows by contract (verified in the next line) */ \
243-
VERIFY_CHECK((c1 >= th) || (c2 != 0)); \
227+
uint128_t t = (uint128_t)(a) * (b); \
228+
uint128_t acc = (uint128_t)c0 + (uint64_t)t; \
229+
c0 = acc; acc >>= 64; \
230+
acc += c1; acc += (t >> 64); \
231+
c1 = acc; c2 += (acc >> 64); \
232+
VERIFY_CHECK(c2 >= (acc >> 64)); \
244233
} while(0)
245234

246235
/* Add a**2 to [c0,c1,c2]. c2 must never overflow. */
247-
#define sqradd3(c0,c1,c2,a) do {\
248-
uint64_t tl, th; \
249-
{ \
250-
uint128_t t = (uint128_t)(a) * (a); \
251-
th = t >> 64; /* at most 0xFFFFFFFFFFFFFFFE */ \
252-
tl = t; \
253-
} \
254-
c0 += tl; /* overflow is handled on the next line */ \
255-
th += (c0 < tl); /* at most 0xFFFFFFFFFFFFFFFF */ \
256-
c1 += th; /* overflow is handled on the next line */ \
257-
c2 += (c1 < th); /* never overflows by contract (verified in the next line) */ \
258-
VERIFY_CHECK((c1 >= th) || (c2 != 0)); \
259-
} while(0)
236+
#define sqradd3(c0,c1,c2,a) muladd3(c0,c1,c2,a,a)
260237

261238
/* Add 2*a*b to [c0,c1,c2]. c2 must never overflow. */
262239
#define mul2add3(c0,c1,c2,a,b) do {\
263-
uint64_t tl, th, th2, tl2; \
264-
{ \
265-
uint128_t t = (uint128_t)(a) * (b); \
266-
th = t >> 64; /* at most 0xFFFFFFFFFFFFFFFE */ \
267-
tl = t; \
268-
} \
269-
th2 = th + th; /* at most 0xFFFFFFFFFFFFFFFE (in case th was 0x7FFFFFFFFFFFFFFF) */ \
270-
c2 += (th2 < th); /* never overflows by contract (verified the next line) */ \
271-
VERIFY_CHECK((th2 >= th) || (c2 != 0)); \
272-
tl2 = tl + tl; /* at most 0xFFFFFFFFFFFFFFFE (in case the lowest 63 bits of tl were 0x7FFFFFFFFFFFFFFF) */ \
273-
th2 += (tl2 < tl); /* at most 0xFFFFFFFFFFFFFFFF */ \
274-
c0 += tl2; /* overflow is handled on the next line */ \
275-
th2 += (c0 < tl2); /* second overflow is handled on the next line */ \
276-
c2 += (c0 < tl2) & (th2 == 0); /* never overflows by contract (verified the next line) */ \
277-
VERIFY_CHECK((c0 >= tl2) || (th2 != 0) || (c2 != 0)); \
278-
c1 += th2; /* overflow is handled on the next line */ \
279-
c2 += (c1 < th2); /* never overflows by contract (verified the next line) */ \
280-
VERIFY_CHECK((c1 >= th2) || (c2 != 0)); \
240+
uint128_t t = (uint128_t)(a) * (b); \
241+
uint128_t acc = (uint128_t)c0 + (((uint128_t)((uint64_t)t)) << 1); \
242+
c0 = acc; acc >>= 64; \
243+
acc += c1; acc += ((t >> 64) << 1); \
244+
c1 = acc; c2 += (acc >> 64); \
245+
VERIFY_CHECK(c2 >= (acc >> 64)); \
281246
} while(0)
282247

283248
/* Add a*b to [c0,c1]. c1 must never overflow. */
284249
#define muladd2(c0,c1,a,b) do {\
285-
uint64_t tl, th; \
286-
ON_VERIFY(uint64_t old_c1 = c1;) \
287-
{ \
288-
uint128_t t = (uint128_t)(a) * (b); \
289-
th = t >> 64; /* at most 0xFFFFFFFFFFFFFFFE */ \
290-
tl = t; \
291-
} \
292-
c0 += tl; /* overflow is handled on the next line */ \
293-
th += (c0 < tl); /* at most 0xFFFFFFFFFFFFFFFF */ \
294-
c1 += th; /* overflow is handled on the next line */ \
295-
ON_VERIFY(VERIFY_CHECK(c1 >= old_c1);) \
250+
uint128_t t = (uint128_t)(a) * (b); \
251+
uint128_t acc = (uint128_t)c0 + (uint64_t)t; \
252+
c0 = acc; acc >>= 64; \
253+
acc += c1; acc += (t >> 64); \
254+
c1 = acc; \
255+
VERIFY_CHECK((acc >> 64) == 0); \
296256
} while(0)
297257

298258
/* Add a**2 to [c0,c1. c1 must never overflow. */
299-
#define sqradd2(c0,c1,a) do {\
300-
uint64_t tl, th; \
301-
ON_VERIFY(uint64_t old_c1 = c1;) \
302-
{ \
303-
uint128_t t = (uint128_t)(a) * (a); \
304-
th = t >> 64; /* at most 0xFFFFFFFFFFFFFFFE */ \
305-
tl = t; \
306-
} \
307-
c0 += tl; /* overflow is handled on the next line */ \
308-
th += (c0 < tl); /* at most 0xFFFFFFFFFFFFFFFF */ \
309-
c1 += th; /* overflow is handled on the next line */ \
310-
ON_VERIFY(VERIFY_CHECK(c1 >= old_c1);) \
311-
} while(0)
259+
#define sqradd2(c0,c1,a) muladd2(c0,c1,a,a)
312260

313261
/* Add [a0,a1,a2,a3,a4] t0 [c0,c1,c2,c3,c4]. C4 cannot overflow. */
314262
#define add5x5(c0,c1,c2,c3,c4,a0,a1,a2,a3,a4) do {\
@@ -350,7 +298,6 @@ void secp256k1_fe_sqr_inner(uint64_t *r, const uint64_t *a);
350298
c3 = tmp; \
351299
} while(0)
352300

353-
354301
/* Add a to [c0,c1,c2]. c2 must never overflow. */
355302
#define add3(c0,c1,c2,a) do {\
356303
uint128_t tmp = (uint128_t)c0 + (a); \

0 commit comments

Comments
 (0)