From 0d2d7e5e716ab0a1a9da1b1595890a43444b561b Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 21 Jan 2020 17:53:48 -0500 Subject: [PATCH] fixed Elligator tests --- libi2pd/Elligator.cpp | 18 +++++++++++------- libi2pd/Elligator.h | 2 +- tests/test-elligator.cpp | 4 ++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/libi2pd/Elligator.cpp b/libi2pd/Elligator.cpp index bd9c382f..48a5a7ac 100644 --- a/libi2pd/Elligator.cpp +++ b/libi2pd/Elligator.cpp @@ -40,7 +40,7 @@ namespace crypto 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; BN_CTX * ctx = BN_CTX_new (); @@ -63,10 +63,13 @@ namespace crypto if (Legendre (uxxA, ctx) != -1) { - uint8_t randByte; // random highest bits and high y - RAND_bytes (&randByte, 1); - bool highY = randByte & 0x01; - + uint8_t randByte = 0; // random highest bits and high y + if (random) + { + RAND_bytes (&randByte, 1); + highY = randByte & 0x01; + } + BIGNUM * r = BN_CTX_get (ctx); if (highY) { @@ -82,8 +85,9 @@ namespace crypto SquareRoot (r, r, ctx); bn2buf (r, encoded, 32); - - encoded[0] |= (randByte & 0xC0); // copy two highest bits from randByte + + if (random) + encoded[0] |= (randByte & 0xC0); // copy two highest bits from randByte for (size_t i = 0; i < 16; i++) // To Little Endian { uint8_t tmp = encoded[i]; diff --git a/libi2pd/Elligator.h b/libi2pd/Elligator.h index 6f9eaf2a..7cdcbbfe 100644 --- a/libi2pd/Elligator.h +++ b/libi2pd/Elligator.h @@ -17,7 +17,7 @@ namespace crypto Elligator2 (); ~Elligator2 (); - bool Encode (const uint8_t * key, uint8_t * encoded) const; + bool Encode (const uint8_t * key, uint8_t * encoded, bool highY = false, bool random = true) const; bool Decode (const uint8_t * encoded, uint8_t * key) const; private: diff --git a/tests/test-elligator.cpp b/tests/test-elligator.cpp index e73eb8ab..48c9e31a 100644 --- a/tests/test-elligator.cpp +++ b/tests/test-elligator.cpp @@ -69,9 +69,9 @@ int main () uint8_t buf[32]; i2p::crypto::Elligator2 el; // encoding tests - el.Encode (key, buf); + el.Encode (key, buf, false, false); assert(memcmp (buf, encoded_key, 32) == 0); - el.Encode (key, buf, true); // with highY + el.Encode (key, buf, true, false); // with highY assert(memcmp (buf, encoded_key_high_y, 32) == 0); // decoding tests el.Decode (encoded1, buf);