From df1aa52e087135a25602ed2935fe8a785ff7cd21 Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 5 Dec 2019 16:03:11 -0500 Subject: [PATCH] Elligator's encode --- libi2pd/Elligator.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++ libi2pd/Elligator.h | 8 +++++ 2 files changed, 76 insertions(+) diff --git a/libi2pd/Elligator.cpp b/libi2pd/Elligator.cpp index cc49adbf..cc38672a 100644 --- a/libi2pd/Elligator.cpp +++ b/libi2pd/Elligator.cpp @@ -1,15 +1,83 @@ +#include "Crypto.h" #include "Elligator.h" namespace i2p { namespace crypto { + static const uint8_t p_[32]= + { + 0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f + }; + + static const uint8_t n1_[32] = + { + 0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f + }; + + static const uint8_t n2_[32] = + { + 0xeb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f + }; + + static const uint8_t A_[32] = + { + 0x06, 0x6d, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + + static const uint8_t u_[32] = + { + 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + + #define decode_bytes(x) { x = BN_new (); BN_bin2bn (x##_, 32, x); } Elligator2::Elligator2 () { + decode_bytes (p); + decode_bytes (n1); + decode_bytes (n2); + decode_bytes (A); + decode_bytes (u); + + BN_CTX * ctx = BN_CTX_new (); + BN_mod_inverse (iu, u, p, ctx); + BN_CTX_free (ctx); } Elligator2::~Elligator2 () { + BN_free (p); + BN_free (n1); + BN_free (n2); + BN_free (A); + } + + void Elligator2::Encode (const uint8_t * key, uint8_t * encoded) const + { + BN_CTX * ctx = BN_CTX_new (); + BN_CTX_start (ctx); + + BIGNUM * a = BN_CTX_get (ctx); BN_bin2bn (key, 32, a); + BIGNUM * b = BN_CTX_get (ctx); + BN_add (a, A, b); + BIGNUM * c = BN_CTX_get (ctx); + BN_mod_exp (c, b, n2, p, ctx); + BN_mod_mul (b, c, a, p, ctx); + BN_sub (b, p, b); + + //BN_mod_exp (c, b, n2, p, ctx); + + BN_mod_mul (c, b, iu, p, ctx); + // TODO: + bn2buf (b, encoded, 32); + + BN_CTX_end (ctx); + BN_CTX_free (ctx); } static std::unique_ptr g_Elligator; diff --git a/libi2pd/Elligator.h b/libi2pd/Elligator.h index 7993ccb9..09f46888 100644 --- a/libi2pd/Elligator.h +++ b/libi2pd/Elligator.h @@ -1,7 +1,9 @@ #ifndef ELLIGATOR_H__ #define ELLIGATOR_H__ +#include #include +#include namespace i2p { @@ -14,6 +16,12 @@ namespace crypto Elligator2 (); ~Elligator2 (); + + void Encode (const uint8_t * key, uint8_t * encoded) const; + + private: + + BIGNUM * p, * n1, * n2, * A, * u, * iu; }; std::unique_ptr& GetElligator ();