Browse Source

replaced radix-16 to radix-256

pull/306/head
orignal 9 years ago
parent
commit
a94a05fac9
  1. 59
      Signature.cpp

59
Signature.cpp

@ -55,16 +55,16 @@ namespace crypto
BN_mod (Bx, Bx, q, ctx); // % q BN_mod (Bx, Bx, q, ctx); // % q
BN_mod (By, By, q, ctx); // % q BN_mod (By, By, q, ctx); // % q
// precalculate Bi16 table // precalculate Bi256 table
Bi16Carry = { Bx, By }; // B Bi256Carry = { Bx, By }; // B
for (int i = 0; i < 64; i++) for (int i = 0; i < 32; i++)
{ {
Bi16[i][0] = Bi16Carry; Bi256[i][0] = Bi256Carry;
for (int j = 1; j < 8; j++) for (int j = 1; j < 128; j++)
Bi16[i][j] = Sum (Bi16[i][j-1], Bi16[i][0], ctx); // (16+j+1)^i*B Bi256[i][j] = Sum (Bi256[i][j-1], Bi256[i][0], ctx); // (256+j+1)^i*B
Bi16Carry = Bi16[i][7]; Bi256Carry = Bi256[i][127];
for (int j = 8; j < 16; j++) for (int j = 128; j < 256; j++)
Bi16Carry = Sum (Bi16Carry, Bi16[i][0], ctx); Bi256Carry = Sum (Bi256Carry, Bi256[i][0], ctx);
} }
BN_CTX_free (ctx); BN_CTX_free (ctx);
@ -72,11 +72,11 @@ namespace crypto
Ed25519 (const Ed25519& other): q (BN_dup (other.q)), l (BN_dup (other.l)), Ed25519 (const Ed25519& other): q (BN_dup (other.q)), l (BN_dup (other.l)),
d (BN_dup (other.d)), I (BN_dup (other.I)), two_252_2 (BN_dup (other.two_252_2)), d (BN_dup (other.d)), I (BN_dup (other.I)), two_252_2 (BN_dup (other.two_252_2)),
Bi16Carry (other.Bi16Carry) Bi256Carry (other.Bi256Carry)
{ {
for (int i = 0; i < 64; i++) for (int i = 0; i < 32; i++)
for (int j = 0; j < 8; j++) for (int j = 0; j < 128; j++)
Bi16[i][j] = other.Bi16[i][j]; Bi256[i][j] = other.Bi256[i][j];
} }
~Ed25519 () ~Ed25519 ()
@ -263,32 +263,29 @@ namespace crypto
bool carry = false; bool carry = false;
for (int i = 0; i < 32; i++) for (int i = 0; i < 32; i++)
{ {
uint8_t x = e[i] & 0x0F; // 4 low bits uint8_t x = e[i];
if (carry) { x++; if (x <= 15) carry = false; else x = 0; }; if (carry)
if (x > 0)
{ {
if (x <= 8) if (x < 255)
res = Sum (res, Bi16[i*2][x-1], ctx);
else
{ {
res = Sum (res, -Bi16[i*2][15-x], ctx); // -Bi[16-x] x++;
carry = true; carry = false;
} }
else
x = 0;
} }
x = e[i] >> 4; // 4 high bits
if (carry) { x++; if (x <= 15) carry = false; else x = 0; };
if (x > 0) if (x > 0)
{ {
if (x <= 8) if (x <= 128)
res = Sum (res, Bi16[i*2+1][x-1], ctx); res = Sum (res, Bi256[i][x-1], ctx);
else else
{ {
res = Sum (res, -Bi16[i*2+1][15-x], ctx); // -Bi[16-x] res = Sum (res, -Bi256[i][255-x], ctx); // -Bi[256-x]
carry = true; carry = true;
} }
} }
} }
if (carry) res = Sum (res, Bi16Carry, ctx); if (carry) res = Sum (res, Bi256Carry, ctx);
return res; return res;
} }
@ -415,10 +412,10 @@ namespace crypto
BIGNUM * q, * l, * d, * I; BIGNUM * q, * l, * d, * I;
// transient values // transient values
BIGNUM * two_252_2; // 2^252-2 BIGNUM * two_252_2; // 2^252-2
EDDSAPoint Bi16[64][8]; // per 4-bits, Bi16[i][j] = (16+j+1)^i*B, we don't store zeroes EDDSAPoint Bi256[32][128]; // per byte, Bi256[i][j] = (256+j+1)^i*B, we don't store zeroes
// if j > 8 we use 16 - j and carry 1 to next 4-bits // if j > 128 we use 256 - j and carry 1 to next byte
// Bi16[0][0] = B, base point // Bi256[0][0] = B, base point
EDDSAPoint Bi16Carry; // Bi16[64][0] EDDSAPoint Bi256Carry; // Bi256[32][0]
}; };
static std::unique_ptr<Ed25519> g_Ed25519; static std::unique_ptr<Ed25519> g_Ed25519;

Loading…
Cancel
Save