Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
Public Member Functions | List of all members
libzeth::chacha_rng Class Reference

#include <chacha_rng.hpp>

Public Member Functions

 chacha_rng (const void *seed, size_t seed_size)
 
void random (void *output, size_t output_size)
 

Detailed Description

Random number generator matching the implementation used by zcash powersoftau and phase2. Usage: https://github.com/clearmatics/powersoftau (See hash_to_g2 function) Implementation is based on: https://docs.rs/rand/0.4.6/src/rand/prng/chacha.rs.html (See description comment, in particular word layout)

Definition at line 22 of file chacha_rng.hpp.

Constructor & Destructor Documentation

◆ chacha_rng()

libzeth::chacha_rng::chacha_rng ( const void *  seed,
size_t  seed_size 
)

Definition at line 20 of file chacha_rng.cpp.

21  : data_used(sizeof(block))
22 {
23  // Copies behaviour of ChaChaRng::from_seed() from the referenced code.
24  // Use the first 8 words of seed (padding with 0 if necessary) as the key.
25  seed_size = std::min(seed_size, sizeof(key));
26  memcpy(key, seed, seed_size);
27  if (seed_size < sizeof(key)) {
28  memset(((uint8_t *)key) + seed_size, 0, sizeof(key) - seed_size);
29  }
30 
31  // Reset the counter to 0.
32  memset(counter, 0, sizeof(counter));
33 }

Member Function Documentation

◆ random()

void libzeth::chacha_rng::random ( void *  output,
size_t  output_size 
)

Definition at line 35 of file chacha_rng.cpp.

36 {
37  // Iteratively take any remaining data in the current block, populating the
38  // block with new data as required, until the output buffer is full.
39 
40  // Destination as a uint8_t pointer for easy incrementing.
41  uint8_t *target = (uint8_t *)output;
42  while (output_size > 0) {
43  if (data_used == sizeof(block)) {
44  update();
45  }
46 
47  const size_t data_remaining = sizeof(block) - data_used;
48  const size_t to_write = std::min(data_remaining, output_size);
49 
50  memcpy(target, &block[data_used], to_write);
51  data_used += to_write;
52  target += to_write;
53  output_size -= to_write;
54  }
55 }

The documentation for this class was generated from the following files: