11Lib::KeccakF1600::KeccakF1600()
17Lib::KeccakF1600::reset ()
22static constexpr const uint8_t KECCAK_RHO_OFFSETS[25] = { 0, 1, 62, 28, 27, 36, 44, 6, 55, 20, 3, 10, 43,
23 25, 39, 41, 45, 15, 21, 8, 18, 2, 61, 56, 14 };
24static constexpr const uint64_t KECCAK_ROUND_CONSTANTS[255] = {
25 1, 32898, 0x800000000000808a, 0x8000000080008000, 32907, 0x80000001, 0x8000000080008081, 0x8000000000008009, 138, 136, 0x80008009,
26 0x8000000a, 0x8000808b, 0x800000000000008b, 0x8000000000008089, 0x8000000000008003, 0x8000000000008002, 0x8000000000000080, 32778,
27 0x800000008000000a, 0x8000000080008081, 0x8000000000008080, 0x80000001, 0x8000000080008008, 0x8000000080008082, 0x800000008000800a,
28 0x8000000000000003, 0x8000000080000009, 0x8000000000008082, 32777, 0x8000000000000080, 32899, 0x8000000000000081, 1, 32779,
29 0x8000000080008001, 128, 0x8000000000008000, 0x8000000080008001, 9, 0x800000008000808b, 129, 0x8000000000000082, 0x8000008b,
30 0x8000000080008009, 0x8000000080000000, 0x80000080, 0x80008003, 0x8000000080008082, 0x8000000080008083, 0x8000000080000088, 32905,
31 32777, 0x8000000000000009, 0x80008008, 0x80008001, 0x800000000000008a, 0x800000000000000b, 137, 0x80000002, 0x800000000000800b,
32 0x8000800b, 32907, 0x80000088, 0x800000000000800a, 0x80000089, 0x8000000000000001, 0x8000000000008088, 0x8000000000000081, 136,
33 0x80008080, 129, 0x800000000000000b, 0, 137, 0x8000008b, 0x8000000080008080, 0x800000000000008b, 0x8000000000008000,
34 0x8000000080008088, 0x80000082, 11, 0x800000000000000a, 32898, 0x8000000000008003, 0x800000000000808b, 0x800000008000000b,
35 0x800000008000008a, 0x80000081, 0x80000081, 0x80000008, 131, 0x8000000080008003, 0x80008088, 0x8000000080000088, 32768, 0x80008082,
36 0x80008089, 0x8000000080008083, 0x8000000080000001, 0x80008002, 0x8000000080000089, 130, 0x8000000080000008, 0x8000000000000089,
37 0x8000000080000008, 0x8000000000000000, 0x8000000000000083, 0x80008080, 8, 0x8000000080000080, 0x8000000080008080,
38 0x8000000000000002, 0x800000008000808b, 8, 0x8000000080000009, 0x800000000000800b, 0x80008082, 0x80008000, 0x8000000000008008, 32897,
39 0x8000000080008089, 0x80008089, 0x800000008000800a, 0x800000000000008a, 0x8000000000000082, 0x80000002, 0x8000000000008082, 32896,
40 0x800000008000000b, 0x8000000080000003, 10, 0x8000000000008001, 0x8000000080000083, 0x8000000000008083, 139, 32778,
41 0x8000000080000083, 0x800000000000800a, 0x80000000, 0x800000008000008a, 0x80000008, 10, 0x8000000000008088, 0x8000000000000008,
42 0x80000003, 0x8000000000000000, 0x800000000000000a, 32779, 0x8000000080008088, 0x8000000b, 0x80000080, 0x8000808a,
43 0x8000000000008009, 3, 0x80000003, 0x8000000000000089, 0x8000000080000081, 0x800000008000008b, 0x80008003, 0x800000008000800b,
44 0x8000000000008008, 32776, 0x8000000000008002, 0x8000000000000009, 0x80008081, 32906, 0x8000800a, 128, 0x8000000000008089,
45 0x800000000000808a, 0x8000000080008089, 0x80008000, 0x8000000000008081, 0x8000800a, 9, 0x8000000080008002, 0x8000000a, 0x80008002,
46 0x8000000080000000, 0x80000009, 32904, 2, 0x80008008, 0x80008088, 0x8000000080000001, 0x8000808b, 0x8000000000000002,
47 0x8000000080008002, 0x80000083, 32905, 32896, 0x8000000080000082, 0x8000000000000088, 0x800000008000808a, 32906, 0x80008083,
48 0x8000000b, 0x80000009, 32769, 0x80000089, 0x8000000000000088, 0x8000000080008003, 0x80008001, 0x8000000000000003,
49 0x8000000080000080, 0x8000000080008009, 0x8000000080000089, 11, 0x8000000000000083, 0x80008009, 0x80000083, 32768, 0x8000800b, 32770,
50 3, 0x8000008a, 0x8000000080000002, 32769, 0x80000000, 0x8000000080000003, 131, 0x800000008000808a, 32771, 32776, 0x800000000000808b,
51 0x8000000080000082, 0x8000000000000001, 0x8000000000008001, 0x800000008000000a, 0x8000000080008008, 0x800000008000800b,
52 0x8000000000008081, 0x80008083, 0x80000082, 130, 0x8000000080000081, 0x8000000080000002, 32904, 139, 32899, 0x8000000000000008,
53 0x8000008a, 0x800000008000008b, 0x8000808a, 0x8000000000008080, 0x80000088, 0x8000000000008083, 2, 0x80008081, 32771, 32897,
54 0x8000000080008000, 32770, 138,
59bit_rotate64 (uint64_t bits,
unsigned int offset)
62#if defined (__x86_64__) && 0
63 __asm__ (
"rolq %%cl, %0"
65 :
"0" (bits),
"c" (offset)
69 bits =
UNLIKELY (offset == 0) ? bits : (bits << offset) | (bits >> (64 - offset));
76Lib::KeccakF1600::permute (
const uint32_t n_rounds)
80 for (
size_t round_index = 0; round_index < n_rounds; round_index++)
84 for (
size_t x = 0; x < 5; x++)
87 for (
size_t y = 1; y < 5; y++)
90 for (
size_t x = 0; x < 5; x++)
92 const uint64_t D = C[(5 + x - 1) % 5] ^ bit_rotate64 (C[(x + 1) % 5], 1);
93 for (
size_t y = 0; y < 5; y++)
97 for (
size_t y = 0; y < 25; y += 5)
100 for (
size_t x = 0; x < 5; x++)
101 plane[x] = bit_rotate64 (plane[x], KECCAK_RHO_OFFSETS[x + y]);
104 const uint64_t a[25] = { A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[8], A[9], A[10], A[11], A[12],
105 A[13], A[14], A[15], A[16], A[17], A[18], A[19], A[20], A[21], A[22], A[23], A[24] };
106 for (
size_t y = 0; y < 5; y++)
107 for (
size_t x = 0; x < 5; x++)
109 const size_t X = (0 * x + 1 * y) % 5;
110 const size_t Y = (2 * x + 3 * y) % 5;
111 A[X + 5 * Y] = a[x + 5 * y];
114 for (
size_t y = 0; y < 25; y += 5)
117 for (
size_t x = 0; x < 5; x++)
118 C[x] = plane[x] ^ (~plane[(x + 1) % 5] & plane[(x + 2) % 5]);
119 for (
size_t x = 0; x < 5; x++)
123 A[0 + 5 * 0] ^= KECCAK_ROUND_CONSTANTS[round_index];
130KeccakRng::permute1600()
132 state_.permute (n_rounds_);
144 std::fill (&state_[0], &state_[256 / 64], 0);
158 const unsigned long long ull =
std::min ((
unsigned long long)
n_nums() - opos_, count);
179 for (i = 0; i < count; i++)
180 state_[i] ^= seeds[i];
200 std::fill (&state_[0], &state_[25], 0xaffeaffeaffeaffe);
206template<
size_t HASHBITS, u
int8_t DOMAINBITS>
208 Lib::KeccakF1600 state_;
215 xor_state (
size_t offset,
const uint8_t *input,
size_t n_in)
219 for (i = offset; i < offset + n_in; i++)
220 state_.byte (i) ^= input[i - offset];
234 state_.byte (offset) ^= trail;
236 const size_t lastbyte = byte_rate() - 1;
237 if (offset == lastbyte && trail >= 0x80)
239 state_.byte (lastbyte) ^= 0x80;
242 rate_ (rate), iopos_ (0), feeding_mode_ (true)
255 feeding_mode_ =
true;
258 update (
const uint8_t *data,
size_t length)
263 const size_t count =
std::min (byte_rate() - iopos_, length);
268 if (iopos_ >= byte_rate())
281 const uint8_t shake_delimiter = DOMAINBITS;
283 feeding_mode_ =
false;
287 const size_t count = HASHBITS / 8;
288 for (
size_t i = 0; i < count; i++)
289 hashvalue[i] = state_.byte (i);
301 const size_t count =
std::min (n_out, byte_rate() - iopos_);
302 for (
size_t i = 0; i < count; i++)
303 output[i] = state_.byte (iopos_ + i);
307 if (iopos_ >= byte_rate())
322 state_ (new (&mem_)
State())
324 static_assert (
sizeof (mem_) >=
sizeof (*state_),
"");
327SHA3_224::~SHA3_224 ()
335 state_->update (data, length);
341 state_->get_hash (hashvalue);
355 context.
digest (hashvalue);
364 state_ (new (&mem_)
State())
366 static_assert (
sizeof (mem_) >=
sizeof (*state_),
"");
369SHA3_256::~SHA3_256 ()
377 state_->update (data, length);
383 state_->get_hash (hashvalue);
397 context.
digest (hashvalue);
406 state_ (new (&mem_)
State())
408 static_assert (
sizeof (mem_) >=
sizeof (*state_),
"");
411SHA3_384::~SHA3_384 ()
419 state_->update (data, length);
425 state_->get_hash (hashvalue);
439 context.
digest (hashvalue);
448 state_ (new (&mem_)
State())
450 static_assert (
sizeof (mem_) >=
sizeof (*state_),
"");
453SHA3_512::~SHA3_512 ()
461 state_->update (data, length);
467 state_->get_hash (hashvalue);
481 context.
digest (hashvalue);
490 state_ (new (&mem_)
State())
492 static_assert (
sizeof (mem_) >=
sizeof (*state_),
"");
495SHAKE128::~SHAKE128 ()
503 state_->update (data, length);
509 state_->squeeze_digest (hashvalues, n);
519shake128_hash (
const void *data,
size_t data_length, uint8_t *hashvalues,
size_t n)
532 state_ (new (&mem_)
State())
534 static_assert (
sizeof (mem_) >=
sizeof (*state_),
"");
537SHAKE256::~SHAKE256 ()
545 state_->update (data, length);
551 state_->squeeze_digest (hashvalues, n);
561shake256_hash (
const void *data,
size_t data_length, uint8_t *hashvalues,
size_t n)
570 increment_ (0), accu_ (0)
576 increment_ (0), accu_ (0)
578 seed (offset, sequence);
592 increment_ = (sequence << 1) | 1;
594 accu_ = A * accu_ + increment_;
611 global_rng =
new (&mem)
KeccakRng (256, 8);
614 return global_rng->
random();
624 return global_random64();
636 const uint64_t quotient = 0xffffffffffffffffULL / range;
637 const uint64_t bound = quotient * range;
640 r = global_random64();
641 return begin + r / quotient;
653 r01 = global_random64() * 5.42101086242752217003726400434970855712890625e-20;
666 const double r01 = global_random64() * 5.42101086242752217003726400434970855712890625e-20;
667 return end * r01 + (1.0 - r01) * begin;
690 d = global_random64();
699Mwc256::Mwc256 (uint64_t x, uint64_t y, uint64_t z, uint64_t c)
716Mwc256::seed (uint64_t x, uint64_t y, uint64_t z, uint64_t c)
719 state[0] = x ^ 0xAAAAAAAAAAAAAAAA;
720 state[1] = y * 0x25363F651FC21EB5;
721 state[2] =
rotl (z, 37);
722 state[3] = c ^ 0x5555555555555555;
724 while (!state[3] || state[3] >= MWC_A3 - 1)
725 state[3] = state[3] / 2 | 1;
726 for (
size_t i = 0; i < 37; i++)
731static constexpr uint mwc256_MP_SIZE = 5;
735mpc_cmp (
const uint64_t *
const a,
const uint64_t *
const b)
737 for (
int i = mwc256_MP_SIZE; i-- != 0; ) {
738 if (a[i] < b[i])
return -1;
739 if (a[i] > b[i])
return 1;
745mpc_bsub (uint64_t *
const a,
const uint64_t *
const b)
749 for (
int i = 0; i < mwc256_MP_SIZE; i++)
751 __int128_t d = (__int128_t) a[i] - (__int128_t) b[i] - (__int128_t) borrow;
753 a[i] = ((__int128_t) UINT64_MAX + 1) + d;
758mpc_rem (uint64_t *
const a,
const uint64_t *
const m)
762 if (mpc_cmp (a, m) < 0)
769mpc_add (uint64_t *
const a,
const uint64_t *
const b,
const uint64_t *
const m)
772 for (
int i = 0; i < mwc256_MP_SIZE; i++)
774 __uint128_t s = (__uint128_t) a[i] + (__uint128_t) b[i] + (__uint128_t) carry;
783mpc_mul (uint64_t *
const a,
const uint64_t *
const b,
const uint64_t *
const m)
785 uint64_t r[mwc256_MP_SIZE] = {}, t[mwc256_MP_SIZE];
789 for (d = mwc256_MP_SIZE; d-- != 0 && b[d] == 0;)
792 for (
int i = 0; i < d * 64; i++)
794 if (b[i >> 6] & (UINT64_C (1) << (i & 63)))
802static uint64_t mwc256_mod[mwc256_MP_SIZE] = { 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, Mwc256::MWC_A3 - 1 };
807 static uint64_t jump128[mwc256_MP_SIZE] = { 0x49ffebb8aed35da, 0x8aeb90fc17d34f8c, 0x3e78ff9958b436d9, 0x377fc42deaad8b46 };
808 uint64_t newstate[mwc256_MP_SIZE] = { state[0], state[1], state[2], state[3] };
809 mpc_mul (newstate, jump128, mwc256_mod);
811 gen.state[0] = newstate[0];
812 gen.state[1] = newstate[1];
813 gen.state[2] = newstate[2];
814 gen.state[3] = newstate[3];
821 static uint64_t jump192[mwc256_MP_SIZE] = { 0x7cbd7641a0db932f, 0x1eafd94d7d3ac65c, 0xf4fc97e3b80db1b, 0x630e9c671e238c8a };
822 uint64_t newstate[mwc256_MP_SIZE] = { state[0], state[1], state[2], state[3] };
823 mpc_mul (newstate, jump192, mwc256_mod);
825 gen.state[0] = newstate[0];
826 gen.state[1] = newstate[1];
827 gen.state[2] = newstate[2];
828 gen.state[3] = newstate[3];
847 w = fnv1a_consthash64 (s.
c_str()); v = fnv1a_consthash64 (s.
data(), s.
size());
TASSERT (w == v);
850 TASSERT (a.next() == b.next());
851 TASSERT (a.next() == b.next());
852 TASSERT (a.next() == b.next());
853 TASSERT (a.next() == b.next());
856 TASSERT (c.next() != d.next());
857 TASSERT (c.next() != d.next());
858 TASSERT (c.next() != d.next());
859 TASSERT (c.next() != d.next());
862 TASSERT (c.next() == d.next());
863 TASSERT (c.next() == d.next());
864 TASSERT (c.next() == d.next());
865 TASSERT (c.next() == d.next());
866 a = a.clone128().clone192();
867 TASSERT (a.next() != a.next());
868 TASSERT (a.next() != a.next());
869 TASSERT (a.next() != a.next());
870 TASSERT (a.next() != a.next());
871 TASSERT (a.next() == c.next()); d.next();
872 TASSERT (a.next() == d.next()); c.next();
AutoSeeder provides non-deterministic seeding entropy.
void auto_seed()
Seed the generator from a system specific nondeterministic random source.
void discard(unsigned long long count)
void seed(uint64_t seed_value=1)
Reinitialize the generator state using a 64 bit seed_value.
size_t n_nums() const
Amount of 64 bit random numbers per generated block.
void xor_seed(const uint64_t *seeds, size_t n_seeds)
~KeccakRng()
The destructor resets the generator state to avoid leaving memory trails.
Marsaglia multiply-with-carry generator, period ca 2^255.
void auto_seed()
Seed the generator from a system specific nondeterministic random source.
Pcg32Rng()
Initialize and seed the generator from a system specific nondeterministic random source.
void seed(uint64_t offset, uint64_t sequence)
Seed by seeking to position offset within stream sequence.
size_t get_hash(uint8_t hashvalue[HASHBITS/8])
Switch from absorbing into squeezing mode and return digest.
void absorb_padding(size_t offset, uint8_t trail=0x01)
void squeeze_digest(uint8_t *output, size_t n_out)
Read out the current Keccak state and permute as needed.
size_t xor_state(size_t offset, const uint8_t *input, size_t n_in)
Add stream data up to block size into Keccak state via XOR.
#define ASE_ASSERT_RETURN(expr,...)
Return from the current function if expr evaluates to false and issue an assertion warning.
#define ASE_UNLIKELY(expr)
Compiler hint to optimize for expr evaluating to false.
#define return_unless(cond,...)
Return silently if cond does not evaluate to true with return value ...
#define ARRAY_SIZE(array)
Yield the number of C array elements.
#define UNLIKELY(cond)
Hint to the compiler to optimize for cond == FALSE.
#define TEST_INTEGRITY(FUNC)
Register func as an integrity test.
The Anklang C++ API namespace.
void sha3_224_hash(const void *data, size_t data_length, uint8_t hashvalue[28])
Calculate 224 bit SHA3 digest from data, see also class SHA3_224.
uint64_t uint64
A 64-bit unsigned integer, use PRI*64 in format strings.
void collect_runtime_entropy(uint64 *data, size_t n)
Collect entropy from the current process, usually quicker than collect_system_entropy().
void shake256_hash(const void *data, size_t data_length, uint8_t *hashvalues, size_t n)
Calculate SHA3 extendable output digest for 256 bit security strength, see also class SHAKE256.
void sha3_384_hash(const void *data, size_t data_length, uint8_t hashvalue[48])
Calculate 384 bit SHA3 digest from data, see also class SHA3_384.
uint64_t cached_hash_secret
Use hash_secret() for access.
void sha3_256_hash(const void *data, size_t data_length, uint8_t hashvalue[32])
Calculate 256 bit SHA3 digest from data, see also class SHA3_256.
int64_t random_irange(int64_t begin, int64_t end)
void sha3_512_hash(const void *data, size_t data_length, uint8_t hashvalue[64])
Calculate 512 bit SHA3 digest from data, see also class SHA3_512.
uint64_t random_nonce()
Provide a unique 64 bit identifier that is not 0, see also random_int64().
void shake128_hash(const void *data, size_t data_length, uint8_t *hashvalues, size_t n)
Calculate SHA3 extendable output digest for 128 bit security strength, see also class SHAKE128.
uint32_t uint32
A 32-bit unsigned integer.
uint32_t uint
Provide 'uint' as convenience type.
double random_frange(double begin, double end)
void memset4(uint32 *mem, uint32 filler, uint length)
Fill a memory area with a 32-bit quantitiy.
void random_secret(uint64_t *secret_var)
Generate a secret non-zero nonce in secret_var, unless it has already been assigned.
SHA3_224()
Create context to calculate a 224 bit SHA3 hash digest.
void update(const uint8_t *data, size_t length)
Feed data to be hashed.
void digest(uint8_t hashvalue[28])
Retrieve the resulting hash value.
void reset()
Reset state to feed and retrieve a new hash value.
SHA3_256()
Create context to calculate a 256 bit SHA3 hash digest.
void update(const uint8_t *data, size_t length)
Feed data to be hashed.
void digest(uint8_t hashvalue[32])
Retrieve the resulting hash value.
void reset()
Reset state to feed and retrieve a new hash value.
void reset()
Reset state to feed and retrieve a new hash value.
SHA3_384()
Create context to calculate a 384 bit SHA3 hash digest.
void update(const uint8_t *data, size_t length)
Feed data to be hashed.
void digest(uint8_t hashvalue[48])
Retrieve the resulting hash value.
void digest(uint8_t hashvalue[64])
Retrieve the resulting hash value.
SHA3_512()
Create context to calculate a 512 bit SHA3 hash digest.
void update(const uint8_t *data, size_t length)
Feed data to be hashed.
void reset()
Reset state to feed and retrieve a new hash value.
SHAKE128()
Create context to calculate an unbounded SHAKE128 hash digest.
void update(const uint8_t *data, size_t length)
Feed data to be hashed.
void reset()
Reset state to feed and retrieve a new hash value.
void squeeze_digest(uint8_t *hashvalues, size_t n)
Retrieve an arbitrary number of hash value bytes.
void reset()
Reset state to feed and retrieve a new hash value.
SHAKE256()
Create context to calculate an unbounded SHAKE256 hash digest.
void update(const uint8_t *data, size_t length)
Feed data to be hashed.
void squeeze_digest(uint8_t *hashvalues, size_t n)
Retrieve an arbitrary number of hash value bytes.
#define TASSERT(cond)
Unconditional test assertion, enters breakpoint if not fullfilled.