Browse Source

Elligator's encode

pull/1491/head
orignal 5 years ago
parent
commit
df1aa52e08
  1. 68
      libi2pd/Elligator.cpp
  2. 8
      libi2pd/Elligator.h

68
libi2pd/Elligator.cpp

@ -1,15 +1,83 @@
#include "Crypto.h"
#include "Elligator.h" #include "Elligator.h"
namespace i2p namespace i2p
{ {
namespace crypto 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 () 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 () 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<Elligator2> g_Elligator; static std::unique_ptr<Elligator2> g_Elligator;

8
libi2pd/Elligator.h

@ -1,7 +1,9 @@
#ifndef ELLIGATOR_H__ #ifndef ELLIGATOR_H__
#define ELLIGATOR_H__ #define ELLIGATOR_H__
#include <inttypes.h>
#include <memory> #include <memory>
#include <openssl/bn.h>
namespace i2p namespace i2p
{ {
@ -14,6 +16,12 @@ namespace crypto
Elligator2 (); Elligator2 ();
~Elligator2 (); ~Elligator2 ();
void Encode (const uint8_t * key, uint8_t * encoded) const;
private:
BIGNUM * p, * n1, * n2, * A, * u, * iu;
}; };
std::unique_ptr<Elligator2>& GetElligator (); std::unique_ptr<Elligator2>& GetElligator ();

Loading…
Cancel
Save