|
@ -31,8 +31,6 @@ Arith256::Arith256(llvm::IRBuilder<>& _builder) : |
|
|
llvm::Type* arg3Types[] = {Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr}; |
|
|
llvm::Type* arg3Types[] = {Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr}; |
|
|
|
|
|
|
|
|
m_mul = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_mul", getModule()); |
|
|
m_mul = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_mul", getModule()); |
|
|
m_sdiv = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_sdiv", getModule()); |
|
|
|
|
|
m_smod = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_smod", getModule()); |
|
|
|
|
|
m_addmod = Function::Create(FunctionType::get(Type::Void, arg3Types, false), Linkage::ExternalLinkage, "arith_addmod", getModule()); |
|
|
m_addmod = Function::Create(FunctionType::get(Type::Void, arg3Types, false), Linkage::ExternalLinkage, "arith_addmod", getModule()); |
|
|
m_mulmod = Function::Create(FunctionType::get(Type::Void, arg3Types, false), Linkage::ExternalLinkage, "arith_mulmod", getModule()); |
|
|
m_mulmod = Function::Create(FunctionType::get(Type::Void, arg3Types, false), Linkage::ExternalLinkage, "arith_mulmod", getModule()); |
|
|
} |
|
|
} |
|
@ -230,24 +228,34 @@ llvm::Value* Arith256::mul(llvm::Value* _arg1, llvm::Value* _arg2) |
|
|
return binaryOp(m_mul, _arg1, _arg2); |
|
|
return binaryOp(m_mul, _arg1, _arg2); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
llvm::Value* Arith256::div(llvm::Value* _arg1, llvm::Value* _arg2) |
|
|
std::pair<llvm::Value*, llvm::Value*> Arith256::div(llvm::Value* _arg1, llvm::Value* _arg2) |
|
|
{ |
|
|
{ |
|
|
return m_builder.CreateExtractValue(createCall(getDivFunc(), {_arg1, _arg2}), 0, "div"); |
|
|
auto div = m_builder.CreateExtractValue(createCall(getDivFunc(), {_arg1, _arg2}), 0, "div"); |
|
|
|
|
|
auto mod = m_builder.CreateExtractValue(createCall(getDivFunc(), {_arg1, _arg2}), 1, "mod"); |
|
|
|
|
|
return std::make_pair(div, mod); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
llvm::Value* Arith256::mod(llvm::Value* _arg1, llvm::Value* _arg2) |
|
|
std::pair<llvm::Value*, llvm::Value*> Arith256::sdiv(llvm::Value* _x, llvm::Value* _y) |
|
|
{ |
|
|
{ |
|
|
return m_builder.CreateExtractValue(createCall(getDivFunc(), {_arg1, _arg2}), 1, "mod"); |
|
|
auto xIsNeg = m_builder.CreateICmpSLT(_x, Constant::get(0)); |
|
|
} |
|
|
auto xNeg = m_builder.CreateSub(Constant::get(0), _x); |
|
|
|
|
|
auto xAbs = m_builder.CreateSelect(xIsNeg, xNeg, _x); |
|
|
|
|
|
|
|
|
llvm::Value* Arith256::sdiv(llvm::Value* _arg1, llvm::Value* _arg2) |
|
|
auto yIsNeg = m_builder.CreateICmpSLT(_y, Constant::get(0)); |
|
|
{ |
|
|
auto yNeg = m_builder.CreateSub(Constant::get(0), _y); |
|
|
return binaryOp(m_sdiv, _arg1, _arg2); |
|
|
auto yAbs = m_builder.CreateSelect(yIsNeg, yNeg, _y); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
llvm::Value* Arith256::smod(llvm::Value* _arg1, llvm::Value* _arg2) |
|
|
auto res = div(xAbs, yAbs); |
|
|
{ |
|
|
|
|
|
return binaryOp(m_smod, _arg1, _arg2); |
|
|
// the reminder has the same sign as dividend
|
|
|
|
|
|
auto rNeg = m_builder.CreateSub(Constant::get(0), res.second); |
|
|
|
|
|
res.second = m_builder.CreateSelect(xIsNeg, rNeg, res.second); |
|
|
|
|
|
|
|
|
|
|
|
auto qNeg = m_builder.CreateSub(Constant::get(0), res.first); |
|
|
|
|
|
auto xyOpposite = m_builder.CreateXor(xIsNeg, yIsNeg); |
|
|
|
|
|
res.first = m_builder.CreateSelect(xyOpposite, qNeg, res.first); |
|
|
|
|
|
|
|
|
|
|
|
return res; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
llvm::Value* Arith256::exp(llvm::Value* _arg1, llvm::Value* _arg2) |
|
|
llvm::Value* Arith256::exp(llvm::Value* _arg1, llvm::Value* _arg2) |
|
@ -331,14 +339,6 @@ namespace |
|
|
|
|
|
|
|
|
const auto nLimbs = sizeof(i256) / sizeof(mp_limb_t); |
|
|
const auto nLimbs = sizeof(i256) / sizeof(mp_limb_t); |
|
|
|
|
|
|
|
|
// FIXME: Not thread-safe
|
|
|
|
|
|
static mp_limb_t mod_limbs[] = {0, 0, 0, 0, 1}; |
|
|
|
|
|
static_assert(sizeof(mod_limbs) / sizeof(mod_limbs[0]) == nLimbs + 1, "mp_limb_t size mismatch"); |
|
|
|
|
|
static const mpz_t mod{nLimbs + 1, nLimbs + 1, &mod_limbs[0]}; |
|
|
|
|
|
|
|
|
|
|
|
static mp_limb_t tmp_limbs[nLimbs + 2]; |
|
|
|
|
|
static mpz_t tmp{nLimbs + 2, 0, &tmp_limbs[0]}; |
|
|
|
|
|
|
|
|
|
|
|
int countLimbs(i256 const* _n) |
|
|
int countLimbs(i256 const* _n) |
|
|
{ |
|
|
{ |
|
|
static const auto limbsInWord = sizeof(_n->a) / sizeof(mp_limb_t); |
|
|
static const auto limbsInWord = sizeof(_n->a) / sizeof(mp_limb_t); |
|
@ -354,25 +354,6 @@ namespace |
|
|
if (_n->a != 0) return l; |
|
|
if (_n->a != 0) return l; |
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void u2s(mpz_t _u) |
|
|
|
|
|
{ |
|
|
|
|
|
if (static_cast<std::make_signed<mp_limb_t>::type>(_u->_mp_d[nLimbs - 1]) < 0) |
|
|
|
|
|
{ |
|
|
|
|
|
mpz_sub(tmp, mod, _u); |
|
|
|
|
|
mpz_set(_u, tmp); |
|
|
|
|
|
_u->_mp_size = -_u->_mp_size; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void s2u(mpz_t _s) |
|
|
|
|
|
{ |
|
|
|
|
|
if (_s->_mp_size < 0) |
|
|
|
|
|
{ |
|
|
|
|
|
mpz_add(tmp, mod, _s); |
|
|
|
|
|
mpz_set(_s, tmp); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
@ -395,51 +376,6 @@ extern "C" |
|
|
*o_result = mul(*_arg1, *_arg2); |
|
|
*o_result = mul(*_arg1, *_arg2); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
EXPORT void arith_sdiv(i256* _arg1, i256* _arg2, i256* o_result) |
|
|
|
|
|
{ |
|
|
|
|
|
*o_result = {}; |
|
|
|
|
|
if (isZero(_arg2)) |
|
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
mpz_t x{nLimbs, countLimbs(_arg1), reinterpret_cast<mp_limb_t*>(_arg1)}; |
|
|
|
|
|
mpz_t y{nLimbs, countLimbs(_arg2), reinterpret_cast<mp_limb_t*>(_arg2)}; |
|
|
|
|
|
mpz_t z{nLimbs, 0, reinterpret_cast<mp_limb_t*>(o_result)}; |
|
|
|
|
|
u2s(x); |
|
|
|
|
|
u2s(y); |
|
|
|
|
|
mpz_tdiv_q(z, x, y); |
|
|
|
|
|
s2u(z); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
EXPORT void arith_smod(i256* _arg1, i256* _arg2, i256* o_result) |
|
|
|
|
|
{ |
|
|
|
|
|
*o_result = {}; |
|
|
|
|
|
if (isZero(_arg2)) |
|
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
mpz_t x{nLimbs, countLimbs(_arg1), reinterpret_cast<mp_limb_t*>(_arg1)}; |
|
|
|
|
|
mpz_t y{nLimbs, countLimbs(_arg2), reinterpret_cast<mp_limb_t*>(_arg2)}; |
|
|
|
|
|
mpz_t z{nLimbs, 0, reinterpret_cast<mp_limb_t*>(o_result)}; |
|
|
|
|
|
u2s(x); |
|
|
|
|
|
u2s(y); |
|
|
|
|
|
mpz_tdiv_r(z, x, y); |
|
|
|
|
|
s2u(z); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
EXPORT void arith_exp(i256* _arg1, i256* _arg2, i256* o_result) |
|
|
|
|
|
{ |
|
|
|
|
|
*o_result = {}; |
|
|
|
|
|
|
|
|
|
|
|
static mp_limb_t mod_limbs[nLimbs + 1] = {}; |
|
|
|
|
|
mod_limbs[nLimbs] = 1; |
|
|
|
|
|
static const mpz_t mod{nLimbs + 1, nLimbs + 1, &mod_limbs[0]}; |
|
|
|
|
|
|
|
|
|
|
|
mpz_t x{nLimbs, countLimbs(_arg1), reinterpret_cast<mp_limb_t*>(_arg1)}; |
|
|
|
|
|
mpz_t y{nLimbs, countLimbs(_arg2), reinterpret_cast<mp_limb_t*>(_arg2)}; |
|
|
|
|
|
mpz_t z{nLimbs, 0, reinterpret_cast<mp_limb_t*>(o_result)}; |
|
|
|
|
|
|
|
|
|
|
|
mpz_powm(z, x, y, mod); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
EXPORT void arith_mulmod(i256* _arg1, i256* _arg2, i256* _arg3, i256* o_result) |
|
|
EXPORT void arith_mulmod(i256* _arg1, i256* _arg2, i256* _arg3, i256* o_result) |
|
|
{ |
|
|
{ |
|
|
*o_result = {}; |
|
|
*o_result = {}; |
|
|