X-Git-Url: https://git.distorted.org.uk/~mdw/secnet/blobdiff_plain/b7a5ecfcbac18c56d0b044975f6ed6835dd86ab4..0bcb8184cfce875a4dde57621139dd44c433f3a5:/f25519.c diff --git a/f25519.c b/f25519.c index 6dfc511..4e0d1cc 100644 --- a/f25519.c +++ b/f25519.c @@ -1,3 +1,44 @@ +/* + * f25519.c: arithmetic modulo 2^255 - 19 + */ +/* + * This file is Free Software. It has been modified to as part of its + * incorporation into secnet. + * + * Copyright 2017 Mark Wooding + * + * You may redistribute this file and/or modify it under the terms of + * the permissive licence shown below. + * + * You may redistribute secnet as a whole and/or modify it under the + * terms of the GNU General Public License as published by the Free + * Software Foundation; either version 3, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see + * https://www.gnu.org/licenses/gpl.html. + */ +/* + * Imported from Catacomb, and modified for Secnet (2017-04-30): + * + * * Use `fake-mLib-bits.h' in place of the real . + * + * * Remove the 16/32-bit implementation, since C99 always has 64-bit + * arithmetic. + * + * * Remove the test rig code: a replacement is in a separate source file. + * + * * Disable some of the operations which aren't needed for X25519. + * (They're used for Ed25519, which we don't need.) + * + * The file's original comment headers are preserved below. + */ /* -*-c-*- * * Arithmetic modulo 2^255 - 19 @@ -27,14 +68,10 @@ /*----- Header files ------------------------------------------------------*/ -#include "config.h" - -#include "ct.h" #include "f25519.h" /*----- Basic setup -------------------------------------------------------*/ -#if F25519_IMPL == 26 /* Elements x of GF(2^255 - 19) are represented by ten signed integers x_i: x * = SUM_{0<=i<10} x_i 2^ceil(51i/2), mostly following Bernstein's original * paper. @@ -68,30 +105,9 @@ typedef uint32 upiece; typedef uint64 udblpiece; (w)->P[8] = v##8; (w)->P[9] = v##9; \ } while (0) -#elif F25519_IMPL == 10 -/* Elements x of GF(2^255 - 19) are represented by 26 signed integers x_i: x - * = SUM_{0<=i<26} x_i 2^ceil(255i/26); i.e., most pieces are 10 bits wide, - * except for pieces 5, 10, 15, 20, and 25 which have 9 bits. - */ - -typedef int16 piece; typedef int32 dblpiece; -typedef uint16 upiece; typedef uint32 udblpiece; -#define P p10 -#define PIECEWD(i) \ - ((i) == 5 || (i) == 10 || (i) == 15 || (i) == 20 || (i) == 25 ? 9 : 10) -#define NPIECE 26 - -#define B10 0x0400 -#define B9 0x200 -#define B8 0x100 -#define M10 0x3ff -#define M9 0x1ff - -#endif - /*----- Debugging machinery -----------------------------------------------*/ -#if defined(F25519_DEBUG) || defined(TEST_RIG) +#if defined(F25519_DEBUG) #include @@ -134,8 +150,6 @@ DEF_FDUMP(fdump, piece, PIECEWD, NPIECE, 32, get_2p255m91()) void f25519_load(f25519 *z, const octet xv[32]) { -#if F25519_IMPL == 26 - uint32 xw0 = LOAD32_L(xv + 0), xw1 = LOAD32_L(xv + 4), xw2 = LOAD32_L(xv + 8), xw3 = LOAD32_L(xv + 12), xw4 = LOAD32_L(xv + 16), xw5 = LOAD32_L(xv + 20), @@ -199,50 +213,6 @@ void f25519_load(f25519 *z, const octet xv[32]) /* And with that, we're done. */ STASH(z, x); - -#elif F25519_IMPL == 10 - - piece x[NPIECE]; - unsigned i, j, n, wd; - uint32 a; - int b, c; - - /* First, just get the content out of the buffer. */ - for (i = j = a = n = 0, wd = 10; j < NPIECE; i++) { - a |= (uint32)xv[i] << n; n += 8; - if (n >= wd) { - x[j++] = a&MASK(wd); - a >>= wd; n -= wd; - wd = PIECEWD(j); - } - } - - /* There's a little bit left over from the top byte. Carry it into the low - * piece. - */ - x[0] += 19*(int)(a&MASK(n)); - - /* Next, convert the pieces into a roughly balanced signed representation. - * If a piece's top bit is set, lend a bit to the next piece over. For - * x_25, this needs to be carried around, which is a bit fiddly. - */ - b = x[NPIECE - 1]&B8; - c = 19&((b >> 3) - (b >> 8)); - x[NPIECE - 1] -= b << 1; - for (i = NPIECE - 2; i > 0; i--) { - wd = PIECEWD(i) - 1; - b = x[i]&BIT(wd); - x[i + 1] += b >> wd; - x[i] -= b << 1; - } - b = x[0]&B9; - x[1] += (b >> 9) + (x[0] >> 10); - x[0] = (x[0]&M10) - (b << 1) + c; - - /* And we're done. */ - for (i = 0; i < NPIECE; i++) z->P[i] = x[i]; - -#endif } /* --- @f25519_store@ --- * @@ -259,8 +229,6 @@ void f25519_load(f25519 *z, const octet xv[32]) void f25519_store(octet zv[32], const f25519 *x) { -#if F25519_IMPL == 26 - piece PIECES(x), PIECES(y), c, d; uint32 zw0, zw1, zw2, zw3, zw4, zw5, zw6, zw7; mask32 m; @@ -359,76 +327,6 @@ void f25519_store(octet zv[32], const f25519 *x) STORE32_L(zv + 8, zw2); STORE32_L(zv + 12, zw3); STORE32_L(zv + 16, zw4); STORE32_L(zv + 20, zw5); STORE32_L(zv + 24, zw6); STORE32_L(zv + 28, zw7); - -#elif F25519_IMPL == 10 - - piece y[NPIECE], yy[NPIECE], c, d; - unsigned i, j, n, wd; - uint32 m, a; - - /* Before we do anything, copy the input so we can hack on it. */ - for (i = 0; i < NPIECE; i++) y[i] = x->P[i]; - - /* First, propagate the carries throughout the pieces. - * - * It's worth paying careful attention to the bounds. We assume that we - * start out with |y_i| <= 2^14. We start by cutting off and reducing the - * carry c_25 from the topmost piece, y_25. This leaves 0 <= y_25 < 2^9; - * and we'll have |c_25| <= 2^5. We multiply this by 19 and we'll ad it - * onto y_0 and propagte the carries: but what bounds can we calculate on - * y before this? - * - * Let o_i = floor(255 i/26). We have Y_i = SUM_{0<=j>= 10; - for (i = 1; i < NPIECE; i++) { - wd = PIECEWD(i); - d += y[i] + (MASK(wd)&m); - yy[i] = d&MASK(wd); - d >>= wd; - } - - /* Choose which value to keep. */ - m = NONZEROP(c) | ~NONZEROP(d - 1); - for (i = 0; i < NPIECE; i++) y[i] = (yy[i]&m) | (y[i]&~m); - - /* Store the result as an octet string. */ - for (i = j = a = n = 0; i < NPIECE; i++) { - a |= (upiece)y[i] << n; n += PIECEWD(i); - while (n >= 8) { - zv[j++] = a&0xff; - a >>= 8; n -= 8; - } - } - zv[j++] = a; - -#endif } /* --- @f25519_set@ --- * @@ -463,16 +361,11 @@ void f25519_set(f25519 *x, int a) void f25519_add(f25519 *z, const f25519 *x, const f25519 *y) { -#if F25519_IMPL == 26 z->P[0] = x->P[0] + y->P[0]; z->P[1] = x->P[1] + y->P[1]; z->P[2] = x->P[2] + y->P[2]; z->P[3] = x->P[3] + y->P[3]; z->P[4] = x->P[4] + y->P[4]; z->P[5] = x->P[5] + y->P[5]; z->P[6] = x->P[6] + y->P[6]; z->P[7] = x->P[7] + y->P[7]; z->P[8] = x->P[8] + y->P[8]; z->P[9] = x->P[9] + y->P[9]; -#elif F25519_IMPL == 10 - unsigned i; - for (i = 0; i < NPIECE; i++) z->P[i] = x->P[i] + y->P[i]; -#endif } /* --- @f25519_sub@ --- * @@ -487,18 +380,15 @@ void f25519_add(f25519 *z, const f25519 *x, const f25519 *y) void f25519_sub(f25519 *z, const f25519 *x, const f25519 *y) { -#if F25519_IMPL == 26 z->P[0] = x->P[0] - y->P[0]; z->P[1] = x->P[1] - y->P[1]; z->P[2] = x->P[2] - y->P[2]; z->P[3] = x->P[3] - y->P[3]; z->P[4] = x->P[4] - y->P[4]; z->P[5] = x->P[5] - y->P[5]; z->P[6] = x->P[6] - y->P[6]; z->P[7] = x->P[7] - y->P[7]; z->P[8] = x->P[8] - y->P[8]; z->P[9] = x->P[9] - y->P[9]; -#elif F25519_IMPL == 10 - unsigned i; - for (i = 0; i < NPIECE; i++) z->P[i] = x->P[i] - y->P[i]; -#endif } +#ifndef F25519_TRIM_X25519 + /* --- @f25519_neg@ --- * * * Arguments: @f25519 *z@ = where to put the result (may alias @x@) @@ -511,20 +401,19 @@ void f25519_sub(f25519 *z, const f25519 *x, const f25519 *y) void f25519_neg(f25519 *z, const f25519 *x) { -#if F25519_IMPL == 26 z->P[0] = -x->P[0]; z->P[1] = -x->P[1]; z->P[2] = -x->P[2]; z->P[3] = -x->P[3]; z->P[4] = -x->P[4]; z->P[5] = -x->P[5]; z->P[6] = -x->P[6]; z->P[7] = -x->P[7]; z->P[8] = -x->P[8]; z->P[9] = -x->P[9]; -#elif F25519_IMPL == 10 - unsigned i; - for (i = 0; i < NPIECE; i++) z->P[i] = -x->P[i]; -#endif } +#endif + /*----- Constant-time utilities -------------------------------------------*/ +#ifndef F25519_TRIM_X25519 + /* --- @f25519_pick2@ --- * * * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@) @@ -542,7 +431,6 @@ void f25519_pick2(f25519 *z, const f25519 *x, const f25519 *y, uint32 m) { mask32 mm = FIX_MASK32(m); -#if F25519_IMPL == 26 z->P[0] = PICK2(x->P[0], y->P[0], mm); z->P[1] = PICK2(x->P[1], y->P[1], mm); z->P[2] = PICK2(x->P[2], y->P[2], mm); @@ -553,10 +441,6 @@ void f25519_pick2(f25519 *z, const f25519 *x, const f25519 *y, uint32 m) z->P[7] = PICK2(x->P[7], y->P[7], mm); z->P[8] = PICK2(x->P[8], y->P[8], mm); z->P[9] = PICK2(x->P[9], y->P[9], mm); -#elif F25519_IMPL == 10 - unsigned i; - for (i = 0; i < NPIECE; i++) z->P[i] = PICK2(x->P[i], y->P[i], mm); -#endif } /* --- @f25519_pickn@ --- * @@ -578,7 +462,6 @@ void f25519_pickn(f25519 *z, const f25519 *v, size_t n, size_t i) uint32 b = (uint32)1 << (31 - i); mask32 m; -#if F25519_IMPL == 26 z->P[0] = z->P[1] = z->P[2] = z->P[3] = z->P[4] = z->P[5] = z->P[6] = z->P[7] = z->P[8] = z->P[9] = 0; while (n--) { @@ -595,17 +478,9 @@ void f25519_pickn(f25519 *z, const f25519 *v, size_t n, size_t i) CONDPICK(z->P[9], v->P[9], m); v++; b <<= 1; } -#elif F25519_IMPL == 10 - unsigned j; +} - for (j = 0; j < NPIECE; j++) z->P[j] = 0; - while (n--) { - m = SIGN(b); - for (j = 0; j < NPIECE; j++) CONDPICK(z->P[j], v->P[j], m); - v++; b <<= 1; - } #endif -} /* --- @f25519_condswap@ --- * * @@ -623,7 +498,6 @@ void f25519_condswap(f25519 *x, f25519 *y, uint32 m) { mask32 mm = FIX_MASK32(m); -#if F25519_IMPL == 26 CONDSWAP(x->P[0], y->P[0], mm); CONDSWAP(x->P[1], y->P[1], mm); CONDSWAP(x->P[2], y->P[2], mm); @@ -634,12 +508,10 @@ void f25519_condswap(f25519 *x, f25519 *y, uint32 m) CONDSWAP(x->P[7], y->P[7], mm); CONDSWAP(x->P[8], y->P[8], mm); CONDSWAP(x->P[9], y->P[9], mm); -#elif F25519_IMPL == 10 - unsigned i; - for (i = 0; i < NPIECE; i++) CONDSWAP(x->P[i], y->P[i], mm); -#endif } +#ifndef F25519_TRIM_X25519 + /* --- @f25519_condneg@ --- * * * Arguments: @f25519 *z@ = where to put the result (may alias @x@) @@ -655,16 +527,10 @@ void f25519_condswap(f25519 *x, f25519 *y, uint32 m) void f25519_condneg(f25519 *z, const f25519 *x, uint32 m) { -#ifdef NEG_TWOC mask32 m_xor = FIX_MASK32(m); piece m_add = m&1; # define CONDNEG(x) (((x) ^ m_xor) + m_add) -#else - int s = PICK2(-1, +1, m); -# define CONDNEG(x) (s*(x)) -#endif -#if F25519_IMPL == 26 z->P[0] = CONDNEG(x->P[0]); z->P[1] = CONDNEG(x->P[1]); z->P[2] = CONDNEG(x->P[2]); @@ -675,17 +541,13 @@ void f25519_condneg(f25519 *z, const f25519 *x, uint32 m) z->P[7] = CONDNEG(x->P[7]); z->P[8] = CONDNEG(x->P[8]); z->P[9] = CONDNEG(x->P[9]); -#elif F25519_IMPL == 10 - unsigned i; - for (i = 0; i < NPIECE; i++) z->P[i] = CONDNEG(x->P[i]); -#endif #undef CONDNEG } -/*----- Multiplication ----------------------------------------------------*/ +#endif -#if F25519_IMPL == 26 +/*----- Multiplication ----------------------------------------------------*/ /* Let B = 2^63 - 1 be the largest value such that +B and -B can be * represented in a double-precision piece. On entry, it must be the case @@ -721,67 +583,6 @@ void f25519_condneg(f25519 *z, const f25519 *x, uint32 m) CARRYSTEP(z##9, _t9, M25, B24, 1, _t8, 26); \ } while (0) -#elif F25519_IMPL == 10 - -/* Perform carry propagation on X. */ -static void carry_reduce(dblpiece x[NPIECE]) -{ - /* Initial bounds: we assume |x_i| < 2^31 - 2^27. */ - - unsigned i, j; - dblpiece c; - - /* The result is nearly canonical, because we do sequential carry - * propagation, because smaller processors are more likely to prefer the - * smaller working set than the instruction-level parallelism. - * - * Start at x_23; truncate it to 10 bits, and propagate the carry to x_24. - * Truncate x_24 to 10 bits, and add the carry onto x_25. Truncate x_25 to - * 9 bits, and add 19 times the carry onto x_0. And so on. - * - * Let c_i be the portion of x_i to be carried onto x_{i+1}. I claim that - * |c_i| <= 2^22. Then the carry /into/ any x_i has magnitude at most - * 19*2^22 < 2^27 (allowing for the reduction as we carry from x_25 to - * x_0), and x_i after carry is bounded above by 2^31. Hence, the carry - * out is at most 2^22, as claimed. - * - * Once we reach x_23 for the second time, we start with |x_23| <= 2^9. - * The carry into x_23 is at most 2^27 as calculated above; so the carry - * out into x_24 has magnitude at most 2^17. In turn, |x_24| <= 2^9 before - * the carry, so is now no more than 2^18 in magnitude, and the carry out - * into x_25 is at most 2^8. This leaves |x_25| < 2^9 after carry - * propagation. - * - * Be careful with the bit hacking because the quantities involved are - * signed. - */ - - /*For each piece, we bias it so that floor division (as done by an - * arithmetic right shift) and modulus (as done by bitwise-AND) does the - * right thing. - */ -#define CARRY(i, wd, b, m) do { \ - x[i] += (b); \ - c = ASR(dblpiece, x[i], (wd)); \ - x[i] = (dblpiece)((udblpiece)x[i]&(m)) - (b); \ -} while (0) - - { CARRY(23, 10, B9, M10); } - { x[24] += c; CARRY(24, 10, B9, M10); } - { x[25] += c; CARRY(25, 9, B8, M9); } - { x[0] += 19*c; CARRY( 0, 10, B9, M10); } - for (i = 1; i < 21; ) { - for (j = i + 4; i < j; ) { x[i] += c; CARRY( i, 10, B9, M10); i++; } - { x[i] += c; CARRY( i, 9, B8, M9); i++; } - } - while (i < 25) { x[i] += c; CARRY( i, 10, B9, M10); i++; } - x[25] += c; - -#undef CARRY -} - -#endif - /* --- @f25519_mulconst@ --- * * * Arguments: @f25519 *z@ = where to put the result (may alias @x@) @@ -795,8 +596,6 @@ static void carry_reduce(dblpiece x[NPIECE]) void f25519_mulconst(f25519 *z, const f25519 *x, long a) { -#if F25519_IMPL == 26 - piece PIECES(x); dblpiece PIECES(z), aa = a; @@ -811,17 +610,6 @@ void f25519_mulconst(f25519 *z, const f25519 *x, long a) /* Following `CARRY_REDUCE', we'll have |z_i| <= 2^26. */ CARRY_REDUCE(z, z); STASH(z, z); - -#elif F25519_IMPL == 10 - - dblpiece y[NPIECE]; - unsigned i; - - for (i = 0; i < NPIECE; i++) y[i] = a*x->P[i]; - carry_reduce(y); - for (i = 0; i < NPIECE; i++) z->P[i] = y[i]; - -#endif } /* --- @f25519_mul@ --- * @@ -836,8 +624,6 @@ void f25519_mulconst(f25519 *z, const f25519 *x, long a) void f25519_mul(f25519 *z, const f25519 *x, const f25519 *y) { -#if F25519_IMPL == 26 - piece PIECES(x), PIECES(y); dblpiece PIECES(z); unsigned i; @@ -902,48 +688,6 @@ void f25519_mul(f25519 *z, const f25519 *x, const f25519 *y) */ for (i = 0; i < 2; i++) CARRY_REDUCE(z, z); STASH(z, z); - -#elif F25519_IMPL == 10 - - dblpiece u[NPIECE], t, tt, p; - unsigned i, j, k; - - /* This is unpleasant. Honestly, this table seems to be the best way of - * doing it. - */ - static const unsigned short off[NPIECE] = { - 0, 10, 20, 30, 40, 50, 59, 69, 79, 89, 99, 108, 118, - 128, 138, 148, 157, 167, 177, 187, 197, 206, 216, 226, 236, 246 - }; - - /* First pass: things we must multiply by 19 or 38. */ - for (i = 0; i < NPIECE - 1; i++) { - t = tt = 0; - for (j = i + 1; j < NPIECE; j++) { - k = NPIECE + i - j; p = (dblpiece)x->P[j]*y->P[k]; - if (off[i] < off[j] + off[k] - 255) tt += p; - else t += p; - } - u[i] = 19*(t + 2*tt); - } - u[NPIECE - 1] = 0; - - /* Second pass: things we must multiply by 1 or 2. */ - for (i = 0; i < NPIECE; i++) { - t = tt = 0; - for (j = 0; j <= i; j++) { - k = i - j; p = (dblpiece)x->P[j]*y->P[k]; - if (off[i] < off[j] + off[k]) tt += p; - else t += p; - } - u[i] += t + 2*tt; - } - - /* And we're done. */ - carry_reduce(u); - for (i = 0; i < NPIECE; i++) z->P[i] = u[i]; - -#endif } /* --- @f25519_sqr@ --- * @@ -958,8 +702,6 @@ void f25519_mul(f25519 *z, const f25519 *x, const f25519 *y) void f25519_sqr(f25519 *z, const f25519 *x) { -#if F25519_IMPL == 26 - piece PIECES(x); dblpiece PIECES(z); unsigned i; @@ -1003,10 +745,6 @@ void f25519_sqr(f25519 *z, const f25519 *x) /* See `f25519_mul' for details. */ for (i = 0; i < 2; i++) CARRY_REDUCE(z, z); STASH(z, z); - -#elif F25519_IMPL == 10 - f25519_mul(z, x, x); -#endif } /*----- More complicated things -------------------------------------------*/ @@ -1063,6 +801,8 @@ void f25519_inv(f25519 *z, const f25519 *x) #undef SQRN } +#ifndef F25519_TRIM_X25519 + /* --- @f25519_quosqrt@ --- * * * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@) @@ -1170,318 +910,6 @@ int f25519_quosqrt(f25519 *z, const f25519 *x, const f25519 *y) return (rc); } -/*----- Test rig ----------------------------------------------------------*/ - -#ifdef TEST_RIG - -#include -#include -#include - -static void fixdstr(dstr *d) -{ - if (d->len > 32) - die(1, "invalid length for f25519"); - else if (d->len < 32) { - dstr_ensure(d, 32); - memset(d->buf + d->len, 0, 32 - d->len); - d->len = 32; - } -} - -static void cvt_f25519(const char *buf, dstr *d) -{ - dstr dd = DSTR_INIT; - - type_hex.cvt(buf, &dd); fixdstr(&dd); - dstr_ensure(d, sizeof(f25519)); d->len = sizeof(f25519); - f25519_load((f25519 *)d->buf, (const octet *)dd.buf); - dstr_destroy(&dd); -} - -static void dump_f25519(dstr *d, FILE *fp) - { fdump(stderr, "???", (const piece *)d->buf); } - -static void cvt_f25519_ref(const char *buf, dstr *d) - { type_hex.cvt(buf, d); fixdstr(d); } - -static void dump_f25519_ref(dstr *d, FILE *fp) -{ - f25519 x; - - f25519_load(&x, (const octet *)d->buf); - fdump(stderr, "???", x.P); -} - -static int eq(const f25519 *x, dstr *d) - { octet b[32]; f25519_store(b, x); return (memcmp(b, d->buf, 32) == 0); } - -static const test_type - type_f25519 = { cvt_f25519, dump_f25519 }, - type_f25519_ref = { cvt_f25519_ref, dump_f25519_ref }; - -#define TEST_UNOP(op) \ - static int vrf_##op(dstr dv[]) \ - { \ - f25519 *x = (f25519 *)dv[0].buf; \ - f25519 z, zz; \ - int ok = 1; \ - \ - f25519_##op(&z, x); \ - if (!eq(&z, &dv[1])) { \ - ok = 0; \ - fprintf(stderr, "failed!\n"); \ - fdump(stderr, "x", x->P); \ - fdump(stderr, "calc", z.P); \ - f25519_load(&zz, (const octet *)dv[1].buf); \ - fdump(stderr, "z", zz.P); \ - } \ - \ - return (ok); \ - } - -TEST_UNOP(neg) -TEST_UNOP(sqr) -TEST_UNOP(inv) - -#define TEST_BINOP(op) \ - static int vrf_##op(dstr dv[]) \ - { \ - f25519 *x = (f25519 *)dv[0].buf, *y = (f25519 *)dv[1].buf; \ - f25519 z, zz; \ - int ok = 1; \ - \ - f25519_##op(&z, x, y); \ - if (!eq(&z, &dv[2])) { \ - ok = 0; \ - fprintf(stderr, "failed!\n"); \ - fdump(stderr, "x", x->P); \ - fdump(stderr, "y", y->P); \ - fdump(stderr, "calc", z.P); \ - f25519_load(&zz, (const octet *)dv[2].buf); \ - fdump(stderr, "z", zz.P); \ - } \ - \ - return (ok); \ - } - -TEST_BINOP(add) -TEST_BINOP(sub) -TEST_BINOP(mul) - -static int vrf_mulc(dstr dv[]) -{ - f25519 *x = (f25519 *)dv[0].buf; - long a = *(const long *)dv[1].buf; - f25519 z, zz; - int ok = 1; - - f25519_mulconst(&z, x, a); - if (!eq(&z, &dv[2])) { - ok = 0; - fprintf(stderr, "failed!\n"); - fdump(stderr, "x", x->P); - fprintf(stderr, "a = %ld\n", a); - fdump(stderr, "calc", z.P); - f25519_load(&zz, (const octet *)dv[2].buf); - fdump(stderr, "z", zz.P); - } - - return (ok); -} - -static int vrf_condneg(dstr dv[]) -{ - f25519 *x = (f25519 *)dv[0].buf; - uint32 m = *(uint32 *)dv[1].buf; - f25519 z; - int ok = 1; - - f25519_condneg(&z, x, m); - if (!eq(&z, &dv[2])) { - ok = 0; - fprintf(stderr, "failed!\n"); - fdump(stderr, "x", x->P); - fprintf(stderr, "m = 0x%08lx\n", (unsigned long)m); - fdump(stderr, "calc z", z.P); - f25519_load(&z, (const octet *)dv[1].buf); - fdump(stderr, "want z", z.P); - } - - return (ok); -} - -static int vrf_pick2(dstr dv[]) -{ - f25519 *x = (f25519 *)dv[0].buf, *y = (f25519 *)dv[1].buf; - uint32 m = *(uint32 *)dv[2].buf; - f25519 z; - int ok = 1; - - f25519_pick2(&z, x, y, m); - if (!eq(&z, &dv[3])) { - ok = 0; - fprintf(stderr, "failed!\n"); - fdump(stderr, "x", x->P); - fdump(stderr, "y", y->P); - fprintf(stderr, "m = 0x%08lx\n", (unsigned long)m); - fdump(stderr, "calc z", z.P); - f25519_load(&z, (const octet *)dv[3].buf); - fdump(stderr, "want z", z.P); - } - - return (ok); -} - -static int vrf_pickn(dstr dv[]) -{ - dstr d = DSTR_INIT; - f25519 v[32], z; - size_t i = *(uint32 *)dv[1].buf, j, n; - const char *p; - char *q; - int ok = 1; - - for (q = dv[0].buf, n = 0; (p = str_qword(&q, 0)) != 0; n++) - { cvt_f25519(p, &d); v[n] = *(f25519 *)d.buf; } - - f25519_pickn(&z, v, n, i); - if (!eq(&z, &dv[2])) { - ok = 0; - fprintf(stderr, "failed!\n"); - for (j = 0; j < n; j++) { - fprintf(stderr, "v[%2u]", (unsigned)j); - fdump(stderr, "", v[j].P); - } - fprintf(stderr, "i = %u\n", (unsigned)i); - fdump(stderr, "calc z", z.P); - f25519_load(&z, (const octet *)dv[2].buf); - fdump(stderr, "want z", z.P); - } - - dstr_destroy(&d); - return (ok); -} - -static int vrf_condswap(dstr dv[]) -{ - f25519 *x = (f25519 *)dv[0].buf, *y = (f25519 *)dv[1].buf; - f25519 xx = *x, yy = *y; - uint32 m = *(uint32 *)dv[2].buf; - int ok = 1; - - f25519_condswap(&xx, &yy, m); - if (!eq(&xx, &dv[3]) || !eq(&yy, &dv[4])) { - ok = 0; - fprintf(stderr, "failed!\n"); - fdump(stderr, "x", x->P); - fdump(stderr, "y", y->P); - fprintf(stderr, "m = 0x%08lx\n", (unsigned long)m); - fdump(stderr, "calc xx", xx.P); - fdump(stderr, "calc yy", yy.P); - f25519_load(&xx, (const octet *)dv[3].buf); - f25519_load(&yy, (const octet *)dv[4].buf); - fdump(stderr, "want xx", xx.P); - fdump(stderr, "want yy", yy.P); - } - - return (ok); -} - -static int vrf_quosqrt(dstr dv[]) -{ - f25519 *x = (f25519 *)dv[0].buf, *y = (f25519 *)dv[1].buf; - f25519 z, zz; - int rc; - int ok = 1; - - if (dv[2].len) { fixdstr(&dv[2]); fixdstr(&dv[3]); } - rc = f25519_quosqrt(&z, x, y); - if (!dv[2].len ? !rc : (rc || (!eq(&z, &dv[2]) && !eq(&z, &dv[3])))) { - ok = 0; - fprintf(stderr, "failed!\n"); - fdump(stderr, "x", x->P); - fdump(stderr, "y", y->P); - if (rc) fprintf(stderr, "calc: FAIL\n"); - else fdump(stderr, "calc", z.P); - if (!dv[2].len) - fprintf(stderr, "exp: FAIL\n"); - else { - f25519_load(&zz, (const octet *)dv[2].buf); - fdump(stderr, "z", zz.P); - f25519_load(&zz, (const octet *)dv[3].buf); - fdump(stderr, "z'", zz.P); - } - } - - return (ok); -} - -static int vrf_sub_mulc_add_sub_mul(dstr dv[]) -{ - f25519 *u = (f25519 *)dv[0].buf, *v = (f25519 *)dv[1].buf, - *w = (f25519 *)dv[3].buf, *x = (f25519 *)dv[4].buf, - *y = (f25519 *)dv[5].buf; - long a = *(const long *)dv[2].buf; - f25519 umv, aumv, wpaumv, xmy, z, zz; - int ok = 1; - - f25519_sub(&umv, u, v); - f25519_mulconst(&aumv, &umv, a); - f25519_add(&wpaumv, w, &aumv); - f25519_sub(&xmy, x, y); - f25519_mul(&z, &wpaumv, &xmy); - - if (!eq(&z, &dv[6])) { - ok = 0; - fprintf(stderr, "failed!\n"); - fdump(stderr, "u", u->P); - fdump(stderr, "v", v->P); - fdump(stderr, "u - v", umv.P); - fprintf(stderr, "a = %ld\n", a); - fdump(stderr, "a (u - v)", aumv.P); - fdump(stderr, "w + a (u - v)", wpaumv.P); - fdump(stderr, "x", x->P); - fdump(stderr, "y", y->P); - fdump(stderr, "x - y", xmy.P); - fdump(stderr, "(x - y) (w + a (u - v))", z.P); - f25519_load(&zz, (const octet *)dv[6].buf); fdump(stderr, "z", zz.P); - } - - return (ok); -} - -static test_chunk tests[] = { - { "add", vrf_add, { &type_f25519, &type_f25519, &type_f25519_ref } }, - { "sub", vrf_sub, { &type_f25519, &type_f25519, &type_f25519_ref } }, - { "neg", vrf_neg, { &type_f25519, &type_f25519_ref } }, - { "condneg", vrf_condneg, - { &type_f25519, &type_uint32, &type_f25519_ref } }, - { "mul", vrf_mul, { &type_f25519, &type_f25519, &type_f25519_ref } }, - { "mulconst", vrf_mulc, { &type_f25519, &type_long, &type_f25519_ref } }, - { "pick2", vrf_pick2, - { &type_f25519, &type_f25519, &type_uint32, &type_f25519_ref } }, - { "pickn", vrf_pickn, - { &type_string, &type_uint32, &type_f25519_ref } }, - { "condswap", vrf_condswap, - { &type_f25519, &type_f25519, &type_uint32, - &type_f25519_ref, &type_f25519_ref } }, - { "sqr", vrf_sqr, { &type_f25519, &type_f25519_ref } }, - { "inv", vrf_inv, { &type_f25519, &type_f25519_ref } }, - { "quosqrt", vrf_quosqrt, - { &type_f25519, &type_f25519, &type_hex, &type_hex } }, - { "sub-mulc-add-sub-mul", vrf_sub_mulc_add_sub_mul, - { &type_f25519, &type_f25519, &type_long, &type_f25519, - &type_f25519, &type_f25519, &type_f25519_ref } }, - { 0, 0, { 0 } } -}; - -int main(int argc, char *argv[]) -{ - test_run(argc, argv, tests, SRCDIR "/t/f25519"); - return (0); -} - #endif /*----- That's all, folks -------------------------------------------------*/