|
|
@ -40,7 +40,7 @@ namespace crypto |
|
|
|
BN_free (u); BN_free (iu); |
|
|
|
BN_free (u); BN_free (iu); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool Elligator2::Encode (const uint8_t * key, uint8_t * encoded) const |
|
|
|
bool Elligator2::Encode (const uint8_t * key, uint8_t * encoded, bool highY, bool random) const |
|
|
|
{ |
|
|
|
{ |
|
|
|
bool ret = true; |
|
|
|
bool ret = true; |
|
|
|
BN_CTX * ctx = BN_CTX_new (); |
|
|
|
BN_CTX * ctx = BN_CTX_new (); |
|
|
@ -63,9 +63,12 @@ namespace crypto |
|
|
|
|
|
|
|
|
|
|
|
if (Legendre (uxxA, ctx) != -1) |
|
|
|
if (Legendre (uxxA, ctx) != -1) |
|
|
|
{ |
|
|
|
{ |
|
|
|
uint8_t randByte; // random highest bits and high y
|
|
|
|
uint8_t randByte = 0; // random highest bits and high y
|
|
|
|
|
|
|
|
if (random) |
|
|
|
|
|
|
|
{ |
|
|
|
RAND_bytes (&randByte, 1); |
|
|
|
RAND_bytes (&randByte, 1); |
|
|
|
bool highY = randByte & 0x01; |
|
|
|
highY = randByte & 0x01; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
BIGNUM * r = BN_CTX_get (ctx); |
|
|
|
BIGNUM * r = BN_CTX_get (ctx); |
|
|
|
if (highY) |
|
|
|
if (highY) |
|
|
@ -83,6 +86,7 @@ namespace crypto |
|
|
|
SquareRoot (r, r, ctx); |
|
|
|
SquareRoot (r, r, ctx); |
|
|
|
bn2buf (r, encoded, 32); |
|
|
|
bn2buf (r, encoded, 32); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (random) |
|
|
|
encoded[0] |= (randByte & 0xC0); // copy two highest bits from randByte
|
|
|
|
encoded[0] |= (randByte & 0xC0); // copy two highest bits from randByte
|
|
|
|
for (size_t i = 0; i < 16; i++) // To Little Endian
|
|
|
|
for (size_t i = 0; i < 16; i++) // To Little Endian
|
|
|
|
{ |
|
|
|
{ |
|
|
|