This commit is contained in:
y4my4my4m 2023-12-27 23:16:42 -05:00
parent 835a239920
commit ef36afa82b

View file

@ -600,7 +600,7 @@ I64 crypto_Scalarmult(U8 *q, U8 *n, U8 *p) {
x[i + 48] = b[i]; x[i + 48] = b[i];
x[i + 64] = d[i]; x[i + 64] = d[i];
} }
inv25519(x + 32, x + 32); Inv25519(x + 32, x + 32);
M(x + 16, x + 16, x + 32); M(x + 16, x + 16, x + 32);
Pack25519(q, x + 16); Pack25519(q, x + 16);
return 0; return 0;
@ -704,82 +704,82 @@ I64 crypto_hash(U8 *out, U8 *m, U64 n) {
// FIXME: cant predefine p[4][16] like that!!! // FIXME: cant predefine p[4][16] like that!!!
U0 add(U16 p[4][16], U16 q[4][16])
{
U16 a[16], b[16], c[16], d[16], t[16], e[16], f[16], g[16], h[16];
Z(a, p[1], p[0]); U0 Add(gf *p, gf *q) {
Z(t, q[1], q[0]); gf a, b, c, d, t, e, f, g, h;
M(a, a, t);
A(b, p[0], p[1]);
A(t, q[0], q[1]);
M(b, b, t);
M(c, p[3], q[3]);
M(c, c, D2);
M(d, p[2], q[2]);
A(d, d, d);
Z(e, b, a);
Z(f, d, c);
A(g, d, c);
A(h, b, a);
M(p[0], e, f); Z(a.data, p[1].data, p[0].data);
M(p[1], h, g); Z(t.data, q[1].data, q[0].data);
M(p[2], g, f); M(a.data, a.data, t.data);
M(p[3], e, h); A(b.data, p[0].data, p[1].data);
A(t.data, q[0].data, q[1].data);
M(b.data, b.data, t.data);
M(c.data, p[3].data, q[3].data);
M(c.data, c.data, D2.data);
M(d.data, p[2].data, q[2].data);
A(d.data, d.data, d.data);
Z(e.data, b.data, a.data);
Z(f.data, d.data, c.data);
A(g.data, d.data, c.data);
A(h.data, b.data, a.data);
M(p[0].data, e.data, f.data);
M(p[1].data, h.data, g.data);
M(p[2].data, g.data, f.data);
M(p[3].data, e.data, h.data);
} }
U0 Cswap(U16 p[4][16], U16 q[4][16], U8 b) U0 Cswap(gf *p, gf *q, U8 b) {
{
I64 i; I64 i;
for (i = 0;i < 4;++i) Sel25519(p[i],q[i],b); for (i = 0; i < 4; ++i) {
Sel25519(p[i].data, q[i].data, b);
}
} }
U0 Pack(U8 *r, U16 p[4][16]) U0 Pack(U8 *r, gf *p) {
{ gf tx, ty, zi;
U16 tx[16], ty[16], zi[16]; Inv25519(&zi, 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(U16 p[4][16], U16 q[4][16], U8 *s) U0 Scalarmult(gf *p, gf *q, U8 *s) {
{
I64 i; I64 i;
Set25519(p[0],gf0); Set25519(p[0].data, gf0.data);
Set25519(p[1],gf1); Set25519(p[1].data, gf1.data);
Set25519(p[2],gf1); Set25519(p[2].data, gf1.data);
Set25519(p[3],gf0); Set25519(p[3].data, gf0.data);
for (i = 255; i >= 0; --i) { for (i = 255; i >= 0; --i) {
U8 b = (s[i / 8] >> (i & 7)) & 1; U8 b = (s[i / 8] >> (i & 7)) & 1;
Cswap(p, q, b); Cswap(p, q, b);
add(q,p); Add(p, q);
add(p,p); Add(p, p);
Cswap(p, q, b); Cswap(p, q, b);
} }
} }
U0 Scalarbase(U16 p[4][16], U8 *s) U0 Scalarbase(gf *p, U8 *s) {
{ gf q[4];
U16 q[4][16]; Set25519(q[0].data, X.data);
Set25519(q[0],X); Set25519(q[1].data, Y.data);
Set25519(q[1],Y); Set25519(q[2].data, gf1.data);
Set25519(q[2],gf1); M(q[3].data, X.data, Y.data);
M(q[3],X,Y);
Scalarmult(p, q, s); Scalarmult(p, q, s);
} }
U64 L[] = {0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10}; U64 L[32] = {0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10};
U0 modL(U8 *r, I64 x[64]) U0 modL(U8 *r, I64 *x) {
{ I64 carry, i, j, index;
I64 carry, i, j;
for (i = 63; i >= 32; --i) { for (i = 63; i >= 32; --i) {
carry = 0; carry = 0;
for (j = i - 32; j < i - 12; ++j) { for (j = i - 32; j < i - 12; ++j) {
x[j] += carry - 16 * x[i] * L[j - (i - 32)]; index = j - (i - 32);
if (index >= 0 && index < 32) {
x[j] += carry - 16 * x[i] * L[index];
}
carry = (x[j] + 128) >> 8; carry = (x[j] + 128) >> 8;
x[j] -= carry << 8; x[j] -= carry << 8;
} }
@ -891,7 +891,7 @@ I64 crypto_sign_open(U8 *m, U64 *mlen, U8 *sm, U64 n, U8 *pk)
Scalarmult(p, q, h); Scalarmult(p, q, h);
Scalarbase(q, sm + 32); Scalarbase(q, sm + 32);
add(p, q); Add(p, q);
Pack(t, p); Pack(t, p);
n -= 64; n -= 64;