bulletproofs.cc: ported mooo's removal of openssl dep from bulletproofs via local invert impl

pull/2/head
Paul Shapiro 6 years ago
parent e7d1ce718a
commit e089dec813

@ -29,8 +29,6 @@
// Adapted from Java code by Sarang Noether
#include <stdlib.h>
#include <openssl/ssl.h>
#include <openssl/bn.h>
#include <boost/thread/mutex.hpp>
#include "misc_log_ex.h"
// #include "common/perf_timer.h"
@ -244,44 +242,20 @@ static rct::keyV vector_scalar2(const rct::keyV &a, const rct::key &x)
return res;
}
static rct::key switch_endianness(rct::key k)
{
std::reverse(k.bytes, k.bytes + sizeof(k));
return k;
}
/* Compute the inverse of a scalar, the stupid way */
/* Compute the inverse of a scalar, the naive way */
static rct::key invert(const rct::key &x)
{
rct::key inv;
BN_CTX *ctx = BN_CTX_new();
BIGNUM *X = BN_new();
BIGNUM *L = BN_new();
BIGNUM *I = BN_new();
BN_bin2bn(switch_endianness(x).bytes, sizeof(rct::key), X);
BN_bin2bn(switch_endianness(rct::curveOrder()).bytes, sizeof(rct::key), L);
CHECK_AND_ASSERT_THROW_MES(BN_mod_inverse(I, X, L, ctx), "Failed to invert");
const int len = BN_num_bytes(I);
CHECK_AND_ASSERT_THROW_MES((size_t)len <= sizeof(rct::key), "Invalid number length");
inv = rct::zero();
BN_bn2bin(I, inv.bytes);
std::reverse(inv.bytes, inv.bytes + len);
static const rct::key l_minus_2 = { {0xeb, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 } };
BN_free(I);
BN_free(L);
BN_free(X);
BN_CTX_free(ctx);
#ifdef DEBUG_BP
rct::key tmp;
sc_mul(tmp.bytes, inv.bytes, x.bytes);
CHECK_AND_ASSERT_THROW_MES(tmp == rct::identity(), "invert failed");
#endif
return inv;
rct::key inv = rct::identity();
rct::key tmp = x;
for (int n = 0; n < 256; ++n)
{
if (l_minus_2[n>>3] & (1<<(n&7)))
sc_mul(inv.bytes, inv.bytes, tmp.bytes);
sc_mul(tmp.bytes, tmp.bytes, tmp.bytes);
}
return inv;
}
/* Compute the slice of a vector */

Loading…
Cancel
Save