From e584148ba1a3b21feab665da43d8ad69cd5488ee Mon Sep 17 00:00:00 2001 From: Giacomo Tazzari Date: Mon, 11 Aug 2014 11:06:32 +0200 Subject: [PATCH] Fixed implementation of EXP opcode (wrong results when exponent >= 2^32) --- libevm/VM.h | 4 ++-- liblll/Assembly.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libevm/VM.h b/libevm/VM.h index 15f05fd26..f014e2da8 100644 --- a/libevm/VM.h +++ b/libevm/VM.h @@ -241,9 +241,9 @@ template eth::bytesConstRef eth::VM::go(Ext& _ext, OnOpFunc const& _ { require(2); auto base = m_stack.back(); - unsigned expon = (unsigned)m_stack[m_stack.size() - 2]; + auto expon = m_stack[m_stack.size() - 2]; m_stack.pop_back(); - m_stack.back() = boost::multiprecision::pow(base, expon); + m_stack.back() = (u256)boost::multiprecision::powm((bigint)base, (bigint)expon, bigint(2) << 256); break; } case Instruction::NEG: diff --git a/liblll/Assembly.cpp b/liblll/Assembly.cpp index 78552f3c5..550f5c89e 100644 --- a/liblll/Assembly.cpp +++ b/liblll/Assembly.cpp @@ -230,7 +230,7 @@ Assembly& Assembly::optimise(bool _enable) { Instruction::SDIV, [](u256 a, u256 b)->u256{return s2u(u2s(a) / u2s(b));} }, { Instruction::MOD, [](u256 a, u256 b)->u256{return a % b;} }, { Instruction::SMOD, [](u256 a, u256 b)->u256{return s2u(u2s(a) % u2s(b));} }, - { Instruction::EXP, [](u256 a, u256 b)->u256{return boost::multiprecision::pow(a, (unsigned)b);} }, + { Instruction::EXP, [](u256 a, u256 b)->u256{return (u256)boost::multiprecision::powm((bigint)a, (bigint)b, bigint(2) << 256);} }, { Instruction::LT, [](u256 a, u256 b)->u256{return a < b ? 1 : 0;} }, { Instruction::GT, [](u256 a, u256 b)->u256{return a > b ? 1 : 0;} }, { Instruction::SLT, [](u256 a, u256 b)->u256{return u2s(a) < u2s(b) ? 1 : 0;} },