Skip to content

Commit

Permalink
encode with highY
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Dec 10, 2019
1 parent 36eaaa7 commit 9ed58e5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
14 changes: 11 additions & 3 deletions libi2pd/Elligator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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) const
{
bool ret = true;
BN_CTX * ctx = BN_CTX_new ();
Expand All @@ -63,8 +63,16 @@ namespace crypto
if (Legendre (uxxA, ctx) != -1)
{
BIGNUM * r = BN_CTX_get (ctx);
BN_mod_inverse (r, xA, p, ctx);
BN_mod_mul (r, r, x, p, ctx);
if (highY)
{
BN_mod_inverse (r, x, p, ctx);
BN_mod_mul (r, r, xA, p, ctx);
}
else
{
BN_mod_inverse (r, xA, p, ctx);
BN_mod_mul (r, r, x, p, ctx);
}
BN_mod_mul (r, r, iu, p, ctx);

SquareRoot (r, r, ctx);
Expand Down
2 changes: 1 addition & 1 deletion libi2pd/Elligator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) const;
bool Decode (const uint8_t * encoded, uint8_t * key) const;

private:
Expand Down
12 changes: 10 additions & 2 deletions tests/test-elligator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const uint8_t encoded_key[32] =
0xef, 0x3a, 0xe4, 0x55, 0x33, 0xcd, 0x41, 0x0a, 0xa9, 0x1a, 0x41, 0x53, 0x31, 0xd8, 0x61, 0x2d
};

const uint8_t encoded_key_high_y[32] =
{
0x3c, 0xfb, 0x87, 0xc4, 0x6c, 0x0b, 0x45, 0x75, 0xca, 0x81, 0x75, 0xe0, 0xed, 0x1c, 0x0a, 0xe9,
0xda, 0xe7, 0x9d, 0xb7, 0x8d, 0xf8, 0x69, 0x97, 0xc4, 0x84, 0x7b, 0x9f, 0x20, 0xb2, 0x77, 0x18
};

const uint8_t encoded1[32] =
{
0xe7, 0x35, 0x07, 0xd3, 0x8b, 0xae, 0x63, 0x99, 0x2b, 0x3f, 0x57, 0xaa, 0xc4, 0x8c, 0x0a, 0xbc,
Expand Down Expand Up @@ -56,10 +62,12 @@ int main ()
{
uint8_t buf[32];
i2p::crypto::Elligator2 el;
// encoding test
// encoding tests
el.Encode (key, buf);
assert(memcmp (buf, encoded_key, 32) == 0);
// decoding test
el.Encode (key, buf, true); // with highY
assert(memcmp (buf, encoded_key_high_y, 32) == 0);
// decoding tests
el.Decode (encoded1, buf);
assert(memcmp (buf, key1, 32) == 0);
el.Decode (encoded2, buf);
Expand Down

0 comments on commit 9ed58e5

Please sign in to comment.