Simplify sha3.

This commit is contained in:
chriseth 2018-10-18 00:48:28 +02:00
parent 7609e2871e
commit d9e6469811
2 changed files with 19 additions and 82 deletions

View File

@ -31,7 +31,7 @@ using namespace dev;
namespace dev namespace dev
{ {
namespace keccak namespace
{ {
/** libkeccak-tiny /** libkeccak-tiny
@ -43,26 +43,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 +144,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,
size_t rate,
uint8_t delim
)
{ {
return -1;
}
uint8_t a[Plen] = {0}; uint8_t a[Plen] = {0};
// Absorb input. // Absorb input.
foldP(in, inlen, xorin); foldP(in, inlen, xorin);
@ -185,58 +167,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)); }