This commit is contained in:
y4my4my4m 2023-12-28 00:08:41 -05:00
parent ef36afa82b
commit 44e4368491

View file

@ -351,22 +351,22 @@ U0 Set25519(I64 *r, I64 *a) {
} }
} }
U0 Car25519(I64 *o) { U0 Car25519(gf o) {
I64 i, c; I64 i, c;
for (i = 0; i < 16; ++i) { for (i = 0; i < 16; ++i) {
o[i] += (1 << 16); o.data[i] += (1 << 16);
c = o[i] >> 16; c = o.data[i] >> 16;
o[(i + 1) * (i < 15)] += c - 1 + 37 * (c - 1) * (i == 15); o.data[(i + 1) * (i < 15)] += c - 1 + 37 * (c - 1) * (i == 15);
o[i] -= c << 16; o.data[i] -= c << 16;
} }
} }
U0 Sel25519(I64 *p, I64 *q, I64 b) { U0 Sel25519(gf p, gf q, I64 b) {
I64 t, i, c = ~(b - 1); I64 t, i, c = ~(b - 1);
for (i = 0; i < 16; ++i) { for (i = 0; i < 16; ++i) {
t = c & (p[i] ^ q[i]); t = c & (p.data[i] ^ q.data[i]);
p[i] ^= t; p.data[i] ^= t;
q[i] ^= t; q.data[i] ^= t;
} }
} }
@ -412,7 +412,7 @@ U8 Par25519(gf a) {
return d[0] & 1; return d[0] & 1;
} }
U0 Unpack25519(gf *o, U8 *n) { U0 Unpack25519(gf o, U8 *n) {
I64 i; I64 i;
for(i=0;i<16;++i) { for(i=0;i<16;++i) {
o->data[i] = n[2 * i] + (n[2 * i + 1] << 8); o->data[i] = n[2 * i] + (n[2 * i + 1] << 8);
@ -424,21 +424,21 @@ U0 Unpack25519(gf *o, U8 *n) {
U0 A(gf o, gf a, gf b) U0 A(gf o, gf a, gf b)
{ {
I64 i; I64 i;
for (i = 0;i < 16;++i) o[i] = a[i] + b[i]; for (i = 0;i < 16;++i) o.data[i] = a.data[i] + b.data[i];
} }
// substraction // substraction
U0 Z(gf o, gf a, gf b) { U0 Z(gf o, gf a, gf b) {
I64 i; I64 i;
for (i = 0;i < 16;++i) o[i] = a[i] - b[i]; for (i = 0;i < 16;++i) o.data[i] = a.data[i] - b.data[i];
} }
// multiply // multiply
U0 M(gf o, gf a, gf b) { U0 M(gf o, gf a, gf b) {
I64 i, j, t[31]; I64 i, j, t[31];
for (i = 0;i < 31;++i) t[i] = 0; for (i = 0;i < 31;++i) t[i] = 0;
for (i = 0;i < 16;++i) { for (j = 0;j < 16;++j) {t[i + j] += a[i] * b[j];} } for (i = 0;i < 16;++i) { for (j = 0;j < 16;++j) {t[i + j] += a.data[i] * b.data[j];} }
for (i = 0;i < 15;++i) t[i] += 38 * t[i + 16]; for (i = 0;i < 15;++i) t[i] += 38 * t[i + 16];
for (i = 0;i < 16;++i) o[i] = t[i]; for (i = 0;i < 16;++i) o.data[i] = t[i];
Car25519(o); Car25519(o);
Car25519(o); Car25519(o);
} }
@ -474,25 +474,25 @@ U0 Pow2523(gf *o, gf i) {
U0 Add(gf *p, gf *q) { U0 Add(gf *p, gf *q) {
gf a, b, c, d, t, e, f, g, h; gf a, b, c, d, t, e, f, g, h;
Z(a.data, p[1].data, p[0].data); Z(a, p[1], p[0]);
Z(t.data, q[1].data, q[0].data); Z(t, q[1], q[0]);
M(a.data, a.data, t.data); M(a, a, t);
A(b.data, p[0].data, p[1].data); A(b, p[0], p[1]);
A(t.data, q[0].data, q[1].data); A(t, q[0], q[1]);
M(b.data, b.data, t.data); M(b, b, t);
M(c.data, p[3].data, q[3].data); M(c, p[3], q[3]);
M(c.data, c.data, D2.data); M(c, c, D2);
M(d.data, p[2].data, q[2].data); M(d, p[2], q[2]);
A(d.data, d.data, d.data); A(d, d, d);
Z(e.data, b.data, a.data); Z(e, b, a);
Z(f.data, d.data, c.data); Z(f, d, c);
A(g.data, d.data, c.data); A(g, d, c);
A(h.data, b.data, a.data); A(h, b, a);
M(p[0].data, e.data, f.data); M(p[0], e, f);
M(p[1].data, h.data, g.data); M(p[1], h, g);
M(p[2].data, g.data, f.data); M(p[2], g, f);
M(p[3].data, e.data, h.data); M(p[3], e, h);
} }
U0 Cswap(gf *p, gf *q, U8 b) { U0 Cswap(gf *p, gf *q, U8 b) {
@ -504,11 +504,11 @@ U0 Cswap(gf *p, gf *q, U8 b) {
U0 Pack(U8 *r, gf *p) { U0 Pack(U8 *r, gf *p) {
gf tx, ty, zi; gf tx, ty, zi;
Inv25519(zi.data, p[2].data); Inv25519(zi, p[2]);
M(tx.data, p[0].data, zi.data); M(tx, p[0], zi);
M(ty.data, p[1].data, zi.data); M(ty, p[1], zi);
Pack25519(r, ty.data); Pack25519(r, ty);
r[31] ^= Par25519(tx.data) << 7; r[31] ^= Par25519(tx) << 7;
} }
U0 Scalarmult(gf *p, gf *q, U8 *s) { U0 Scalarmult(gf *p, gf *q, U8 *s) {
@ -526,15 +526,6 @@ U0 Scalarmult(gf *p, gf *q, U8 *s) {
} }
} }
U0 Scalarbase(gf *p, U8 *s) {
gf q[4];
Set25519(q[0].data, X.data);
Set25519(q[1].data, Y.data);
Set25519(q[2].data, gf1.data);
M(q[3].data, X.data, Y.data);
Scalarmult(p, q, s);
}
I64 crypto_sign_keypair(U8 *pk, U8 *sk) I64 crypto_sign_keypair(U8 *pk, U8 *sk)
{ {
U8 d[64]; U8 d[64];
@ -556,56 +547,57 @@ I64 crypto_sign_keypair(U8 *pk, U8 *sk)
I64 crypto_Scalarmult(U8 *q, U8 *n, U8 *p) { I64 crypto_Scalarmult(U8 *q, U8 *n, U8 *p) {
U8 z[32]; U8 z[32];
I64 x[80], r, i; I64 x[80], r, i;
gf a, b, c, d, e, f; gf a, b, c, d, e, f;
for (i = 0;i < 31;++i) z[i] = n[i]; for (i = 0; i < 31; ++i) {
z[31] = (n[31] & 127) | 64; z[i] = n[i];
z[0] &= 248; }
Unpack25519(x, p); z[31] = (n[31] & 127) | 64;
for (i = 0;i < 16;++i) { z[0] &= 248;
b[i] = x[i];
d[i] = a[i] = c[i] = 0; Unpack25519(x, p);
}
a[0] = d[0] = 1; for (i = 0; i < 16; ++i) {
for (i = 254; i >= 0; --i) { d.data[i] = a.data[i] = c.data[i] = 0;
r = (z[i >> 3] >> (i & 7)) & 1; }
Cswap(a, b, r); a.data[0] = d.data[0] = 1;
Cswap(c, d, r);
A(e, a, c); for (i = 254; i >= 0; --i) {
Z(a, a, c); r = (z[i >> 3] >> (i & 7)) & 1;
A(c, b, d); Sel25519(a.data,b.data,r);
Z(b, b, d); Sel25519(c.data,d.data,r);
S(d, e); A(e,a,c);
S(f, a); Z(a,a,c);
M(a, c, a); A(c,b,d);
M(c, b, e); Z(b,b,d);
A(e, a, c); S(d,e);
Z(a, a, c); S(f,a);
S(b, a); M(a,c,a);
Z(c, d, f); M(c,b,e);
M(a, c, _121665); A(e,a,c);
A(a, a, d); Z(a,a,c);
M(c, c, a); S(b,a);
M(a, d, f); Z(c,d,f);
M(d, b, x); M(a,c,_121665);
S(b, e); A(a,a,d);
Cswap(a, b, r); M(c,c,a);
Cswap(c, d, r); M(a,d,f);
} M(d,b,x);
for (i = 0;i < 16;++i) { S(b,e);
x[i + 16] = a[i]; Sel25519(a.data,b.data,r);
x[i + 32] = c[i]; Sel25519(c.data,d.data,r);
x[i + 48] = b[i]; }
x[i + 64] = d[i];
} Inv25519(x+32, c+32);
Inv25519(x + 32, x + 32); M(x+16, a+16, x+32);
M(x + 16, x + 16, x + 32); Pack25519(q, x+16);
Pack25519(q, x + 16);
return 0; return 0;
} }
static U64 R(U64 x, I64 c) { return (x >> c) | (x << (64 - c)); } static U64 R(U64 x, I64 c) { return (x >> c) | (x << (64 - c)); }
static U64 Ch(U64 x, U64 y, U64 z) { return (x & y) ^ (~x & z); } static U64 Ch(U64 x, U64 y, U64 z) { return (x & y) ^ (~x & z); }
static U64 Maj(U64 x, U64 y, U64 z) { return (x & y) ^ (x & z) ^ (y & z); } static U64 Maj(U64 x, U64 y, U64 z) { return (x & y) ^ (x & z) ^ (y & z); }
@ -708,64 +700,33 @@ I64 crypto_hash(U8 *out, U8 *m, U64 n) {
U0 Add(gf *p, gf *q) { U0 Add(gf *p, gf *q) {
gf a, b, c, d, t, e, f, g, h; gf a, b, c, d, t, e, f, g, h;
Z(a.data, p[1].data, p[0].data); Z(a, p[1], p[0]);
Z(t.data, q[1].data, q[0].data); Z(t, q[1], q[0]);
M(a.data, a.data, t.data); M(a, a, t);
A(b.data, p[0].data, p[1].data); A(b, p[0], p[1]);
A(t.data, q[0].data, q[1].data); A(t, q[0], q[1]);
M(b.data, b.data, t.data); M(b, b, t);
M(c.data, p[3].data, q[3].data); M(c, p[3], q[3]);
M(c.data, c.data, D2.data); M(c, c, D2);
M(d.data, p[2].data, q[2].data); M(d, p[2], q[2]);
A(d.data, d.data, d.data); A(d, d, d);
Z(e.data, b.data, a.data); Z(e, b, a);
Z(f.data, d.data, c.data); Z(f, d, c);
A(g.data, d.data, c.data); A(g, d, c);
A(h.data, b.data, a.data); A(h, b, a);
M(p[0].data, e.data, f.data); M(p[0], e, f);
M(p[1].data, h.data, g.data); M(p[1], h, g);
M(p[2].data, g.data, f.data); M(p[2], g, f);
M(p[3].data, e.data, h.data); M(p[3], e, h);
} }
U0 Cswap(gf *p, gf *q, U8 b) { U0 Scalarbase(gf p, U8 *s) {
I64 i; gf q[4]; // need to create p[4] too?
for (i = 0; i < 4; ++i) {
Sel25519(p[i].data, q[i].data, b);
}
}
U0 Pack(U8 *r, gf *p) {
gf tx, ty, zi;
Inv25519(&zi, p[2].data);
M(tx.data, p[0].data, zi.data);
M(ty.data, p[1].data, zi.data);
Pack25519(r, ty.data);
r[31] ^= Par25519(tx.data) << 7;
}
U0 Scalarmult(gf *p, gf *q, U8 *s) {
I64 i;
Set25519(p[0].data, gf0.data);
Set25519(p[1].data, gf1.data);
Set25519(p[2].data, gf1.data);
Set25519(p[3].data, gf0.data);
for (i = 255; i >= 0; --i) {
U8 b = (s[i / 8] >> (i & 7)) & 1;
Cswap(p, q, b);
Add(p, q);
Add(p, p);
Cswap(p, q, b);
}
}
U0 Scalarbase(gf *p, U8 *s) {
gf q[4];
Set25519(q[0].data, X.data); Set25519(q[0].data, X.data);
Set25519(q[1].data, Y.data); Set25519(q[1].data, Y.data);
Set25519(q[2].data, gf1.data); Set25519(q[2].data, gf1.data);
M(q[3].data, X.data, Y.data); M(q[3], X, Y);
Scalarmult(p, q, s); Scalarmult(p, q, s);
} }
@ -838,38 +799,38 @@ I64 crypto_sign(U8 *sm, U64 *smlen, U8 *m, U64 n, U8 *sk)
return 0; return 0;
} }
I64 UnpackNeg(gf *r, U8 *p) { I64 UnpackNeg(gf r, U8 *p) {
gf t, chk, num, den, den2, den4, den6; gf t, chk, num, den, den2, den4, den6;
Set25519(r[2].data, gf1.data); Set25519(r.data[2], gf1.data);
Unpack25519(r[1].data, p); Unpack25519(r[1], p.data);
S(num.data, r[1].data); S(num, r[1]);
M(den.data, num.data, D.data); M(den, num, D);
Z(num.data, num.data, r[2].data); Z(num, num, r[2]);
A(den.data, r[2].data, den.data); A(den, r[2], den);
S(den2.data, den.data); S(den2, den);
S(den4.data, den2.data); S(den4, den2);
M(den6.data, den4.data, den2.data); M(den6, den4, den2);
M(t.data, den6.data, num.data); M(t, den6, num);
M(t.data, t.data, den.data); M(t, t, den);
Pow2523(t.data, t.data); Pow2523(t, t);
M(t.data, t.data, num.data); M(t, t, num);
M(t.data, t.data, den.data); M(t, t, den);
M(t.data, t.data, den.data); M(t, t, den);
M(r[0].data, t.data, den.data); M(r[0], t, den);
S(chk.data, r[0].data); S(chk, r[0]);
M(chk.data, chk.data, den.data); M(chk, chk, den);
if (Neq25519(chk.data, num.data)) M(r[0].data, r[0].data, I.data); if (Neq25519(chk, num)) M(r[0], r[0], I);
S(chk.data, r[0].data); S(chk, r[0]);
M(chk.data, chk.data, den.data); M(chk, chk, den);
if (Neq25519(chk.data, num.data)) return -1; if (Neq25519(chk, num)) return -1;
if (Par25519(r[0].data) == (p[31] >> 7)) Z(r[0].data, gf0.data, r[0].data); if (Par25519(r[0]) == (p[31] >> 7)) Z(r[0], gf0, r[0]);
M(r[3].data, r[0].data, r[1].data); M(r[3], r[0], r[1]);
return 0; return 0;
} }