Merge pull request #5265 from ethereum/cleanupsha3

Simplify sha3.
This commit is contained in:
chriseth 2018-11-09 15:39:57 +01:00 committed by GitHub
commit 6bbedab383
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 36 additions and 98 deletions

View File

@ -22,7 +22,7 @@
#include <libdevcore/CommonData.h> #include <libdevcore/CommonData.h>
#include <libdevcore/Exceptions.h> #include <libdevcore/Exceptions.h>
#include <libdevcore/Assertions.h> #include <libdevcore/Assertions.h>
#include <libdevcore/SHA3.h> #include <libdevcore/Keccak256.h>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>

View File

@ -19,7 +19,8 @@
* @date 2014 * @date 2014
*/ */
#include "SHA3.h" #include <libdevcore/Keccak256.h>
#include <cstdint> #include <cstdint>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
@ -31,7 +32,7 @@ using namespace dev;
namespace dev namespace dev
{ {
namespace keccak namespace
{ {
/** libkeccak-tiny /** libkeccak-tiny
@ -43,26 +44,6 @@ namespace keccak
* but not liability. * but not liability.
*/ */
#define decshake(bits) \
int shake##bits(uint8_t*, size_t, const uint8_t*, size_t);
#define decsha3(bits) \
int sha3_##bits(uint8_t*, size_t, const uint8_t*, size_t);
#define deckeccak(bits) \
int keccak##bits(uint8_t*, size_t, const uint8_t*, size_t);
decshake(128)
decshake(256)
decsha3(224)
decsha3(256)
decsha3(384)
decsha3(512)
deckeccak(224)
deckeccak(256)
deckeccak(384)
deckeccak(512)
/******** The Keccak-f[1600] permutation ********/ /******** The Keccak-f[1600] permutation ********/
/*** Constants. ***/ /*** Constants. ***/
@ -164,13 +145,15 @@ mkapply_sd(setout, dst[i] = src[i]) // setout
} }
/** The sponge-based hash construction. **/ /** The sponge-based hash construction. **/
static inline int hash(uint8_t* out, size_t outlen, inline void hash(
const uint8_t* in, size_t inlen, uint8_t* out,
size_t rate, uint8_t delim) { size_t outlen,
if ((out == NULL) || ((in == NULL) && inlen != 0) || (rate >= Plen)) const uint8_t* in,
{ size_t inlen,
return -1; size_t rate,
} uint8_t delim
)
{
uint8_t a[Plen] = {0}; uint8_t a[Plen] = {0};
// Absorb input. // Absorb input.
foldP(in, inlen, xorin); foldP(in, inlen, xorin);
@ -185,58 +168,19 @@ static inline int hash(uint8_t* out, size_t outlen,
foldP(out, outlen, setout); foldP(out, outlen, setout);
setout(a, out, outlen); setout(a, out, outlen);
memset(a, 0, 200); memset(a, 0, 200);
return 0;
} }
/*** Helper macros to define SHA3 and SHAKE instances. ***/
#define defshake(bits) \
int shake##bits(uint8_t* out, size_t outlen, \
const uint8_t* in, size_t inlen) { \
return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x1f); \
}
#define defsha3(bits) \
int sha3_##bits(uint8_t* out, size_t outlen, \
const uint8_t* in, size_t inlen) { \
if (outlen > (bits/8)) { \
return -1; \
} \
return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x06); \
}
#define defkeccak(bits) \
int keccak##bits(uint8_t* out, size_t outlen, \
const uint8_t* in, size_t inlen) { \
if (outlen > (bits/8)) { \
return -1; \
} \
return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x01); \
}
/*** FIPS202 SHAKE VOFs ***/
defshake(128)
defshake(256)
/*** FIPS202 SHA3 FOFs ***/
defsha3(224)
defsha3(256)
defsha3(384)
defsha3(512)
/*** KECCAK FOFs ***/
defkeccak(224)
defkeccak(256)
defkeccak(384)
defkeccak(512)
} }
bool keccak256(bytesConstRef _input, bytesRef o_output) h256 keccak256(bytesConstRef _input)
{ {
// FIXME: What with unaligned memory? h256 output;
if (o_output.size() != 32) // Parameters used:
return false; // The 0x01 is the specific padding for keccak (sha3 uses 0x06) and
keccak::keccak256(o_output.data(), 32, _input.data(), _input.size()); // the way the round size (or window or whatever it was) is calculated.
// keccak::keccak(ret.data(), 32, (uint64_t const*)_input.data(), _input.size()); // 200 - (256 / 4) is the "rate"
return true; hash(output.data(), output.size, _input.data(), _input.size(), 200 - (256 / 4), 0x01);
return output;
} }
} }

View File

@ -30,14 +30,8 @@
namespace dev namespace dev
{ {
// Keccak-256 convenience routines.
/// Calculate Keccak-256 hash of the given input and load it into the given output.
/// @returns false if o_output.size() != 32.
bool keccak256(bytesConstRef _input, bytesRef o_output);
/// Calculate Keccak-256 hash of the given input, returning as a 256-bit hash. /// Calculate Keccak-256 hash of the given input, returning as a 256-bit hash.
inline h256 keccak256(bytesConstRef _input) { h256 ret; keccak256(_input, ret.ref()); return ret; } h256 keccak256(bytesConstRef _input);
/// Calculate Keccak-256 hash of the given input, returning as a 256-bit hash. /// Calculate Keccak-256 hash of the given input, returning as a 256-bit hash.
inline h256 keccak256(bytes const& _input) { return keccak256(bytesConstRef(&_input)); } inline h256 keccak256(bytes const& _input) { return keccak256(bytesConstRef(&_input)); }

View File

@ -19,7 +19,7 @@
#include <libdevcore/SwarmHash.h> #include <libdevcore/SwarmHash.h>
#include <libdevcore/SHA3.h> #include <libdevcore/Keccak256.h>
using namespace std; using namespace std;
using namespace dev; using namespace dev;

View File

@ -27,7 +27,7 @@
#include <libdevcore/Common.h> #include <libdevcore/Common.h>
#include <libdevcore/Assertions.h> #include <libdevcore/Assertions.h>
#include <libdevcore/SHA3.h> #include <libdevcore/Keccak256.h>
#include <json/json.h> #include <json/json.h>

View File

@ -23,7 +23,7 @@
#include <functional> #include <functional>
#include <boost/range/adaptor/reversed.hpp> #include <boost/range/adaptor/reversed.hpp>
#include <libdevcore/SHA3.h> #include <libdevcore/Keccak256.h>
#include <libevmasm/CommonSubexpressionEliminator.h> #include <libevmasm/CommonSubexpressionEliminator.h>
#include <libevmasm/AssemblyItem.h> #include <libevmasm/AssemblyItem.h>

View File

@ -23,7 +23,7 @@
#include "KnownState.h" #include "KnownState.h"
#include <functional> #include <functional>
#include <libdevcore/SHA3.h> #include <libdevcore/Keccak256.h>
#include <libevmasm/AssemblyItem.h> #include <libevmasm/AssemblyItem.h>
using namespace std; using namespace std;

View File

@ -21,7 +21,7 @@
#include <libevmasm/LinkerObject.h> #include <libevmasm/LinkerObject.h>
#include <libdevcore/CommonData.h> #include <libdevcore/CommonData.h>
#include <libdevcore/SHA3.h> #include <libdevcore/Keccak256.h>
using namespace dev; using namespace dev;
using namespace dev::eth; using namespace dev::eth;

View File

@ -24,7 +24,7 @@
#include <libsolidity/ast/ASTVisitor.h> #include <libsolidity/ast/ASTVisitor.h>
#include <libsolidity/ast/AST_accept.h> #include <libsolidity/ast/AST_accept.h>
#include <libdevcore/SHA3.h> #include <libdevcore/Keccak256.h>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>

View File

@ -26,7 +26,7 @@
#include <libdevcore/CommonIO.h> #include <libdevcore/CommonIO.h>
#include <libdevcore/CommonData.h> #include <libdevcore/CommonData.h>
#include <libdevcore/SHA3.h> #include <libdevcore/Keccak256.h>
#include <libdevcore/UTF8.h> #include <libdevcore/UTF8.h>
#include <libdevcore/Algorithms.h> #include <libdevcore/Algorithms.h>

View File

@ -25,7 +25,7 @@
#include <boost/range/adaptor/reversed.hpp> #include <boost/range/adaptor/reversed.hpp>
#include <boost/algorithm/string/replace.hpp> #include <boost/algorithm/string/replace.hpp>
#include <libdevcore/Common.h> #include <libdevcore/Common.h>
#include <libdevcore/SHA3.h> #include <libdevcore/Keccak256.h>
#include <libsolidity/ast/AST.h> #include <libsolidity/ast/AST.h>
#include <libsolidity/codegen/ExpressionCompiler.h> #include <libsolidity/codegen/ExpressionCompiler.h>
#include <libsolidity/codegen/CompilerContext.h> #include <libsolidity/codegen/CompilerContext.h>

View File

@ -24,7 +24,7 @@
#include <map> #include <map>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <libdevcore/SHA3.h> #include <libdevcore/Keccak256.h>
#include <libevmasm/ControlFlowGraph.h> #include <libevmasm/ControlFlowGraph.h>
#include <libevmasm/KnownState.h> #include <libevmasm/KnownState.h>
#include <libevmasm/PathGasMeter.h> #include <libevmasm/PathGasMeter.h>

View File

@ -25,7 +25,7 @@
#include <libsolidity/ast/ASTJsonConverter.h> #include <libsolidity/ast/ASTJsonConverter.h>
#include <libevmasm/Instruction.h> #include <libevmasm/Instruction.h>
#include <libdevcore/JSON.h> #include <libdevcore/JSON.h>
#include <libdevcore/SHA3.h> #include <libdevcore/Keccak256.h>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>

View File

@ -28,7 +28,7 @@
#include <libsolidity/interface/EVMVersion.h> #include <libsolidity/interface/EVMVersion.h>
#include <libdevcore/FixedHash.h> #include <libdevcore/FixedHash.h>
#include <libdevcore/SHA3.h> #include <libdevcore/Keccak256.h>
#include <functional> #include <functional>

View File

@ -29,7 +29,7 @@
#include <libsolidity/parsing/Scanner.h> #include <libsolidity/parsing/Scanner.h>
#include <libdevcore/SHA3.h> #include <libdevcore/Keccak256.h>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>

View File

@ -26,7 +26,7 @@
#include <libsolidity/ast/AST.h> #include <libsolidity/ast/AST.h>
#include <libdevcore/SHA3.h> #include <libdevcore/Keccak256.h>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>

View File

@ -22,7 +22,7 @@
#include <libsolidity/ast/Types.h> #include <libsolidity/ast/Types.h>
#include <libsolidity/ast/AST.h> #include <libsolidity/ast/AST.h>
#include <libdevcore/SHA3.h> #include <libdevcore/Keccak256.h>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
using namespace std; using namespace std;