pub struct Proof {
pub client_seed: Vec<u8>,
pub server_seed: Vec<u8>,
pub blinded_server_seed: Vec<u8>,
pub nonce: i64,
}Expand description
Proof represents a provably fair random number generator (RNG) using HMAC-SHA512 hash.
This struct allows users to create a new RNG instance, log its state, roll the RNG, calculate random numbers, and verify the validity of generated numbers.
§Examples
use hmac_rng::Proof;
let mut proof = Proof::new(None, None, 0);
proof.log_state();
let result = proof.roll();
match result {
Ok(random_number) => println!("Random number: {}", random_number),
Err(err) => eprintln!("Error: {}", err),
}Fields§
§client_seed: Vec<u8>§server_seed: Vec<u8>§blinded_server_seed: Vec<u8>§nonce: i64Implementations§
Source§impl Proof
impl Proof
Sourcepub fn new(
client_seed: Option<Vec<u8>>,
server_seed: Option<Vec<u8>>,
nonce: i64,
) -> Self
pub fn new( client_seed: Option<Vec<u8>>, server_seed: Option<Vec<u8>>, nonce: i64, ) -> Self
Creates a new instance of the Proof struct with the provided or random server seed, client seed, and nonce value.
§Arguments
client_seed- An optional client seed.server_seed- An optional server seed.nonce- The nonce value.
§Examples
use hmac_rng::Proof;
let mut proof = Proof::new(None, None, 0);pub fn log_state(&self)
Sourcepub fn roll(&mut self) -> Result<f64, String>
pub fn roll(&mut self) -> Result<f64, String>
Increments the nonce value and calculates the random number for the current state of the Proof struct.
This method ensures that the first nonce used is 0.
§Returns
Ok(random_number)- The generated random number.Err(err)- An error message if the calculation fails.
§Examples
use hmac_rng::Proof;
let mut proof = Proof::new(None, None, 0);
let result = proof.roll();
match result {
Ok(random_number) => println!("Random number: {}", random_number),
Err(err) => eprintln!("Error: {}", err),
}Sourcepub fn calculate(&self) -> Result<f64, String>
pub fn calculate(&self) -> Result<f64, String>
Calculates the current value from the current state of the Proof struct.
This method does not advance the state in any way. Calling Calculate multiple times with the same nonce will always result in the same value.
§Returns
Ok(random_number)- The calculated random number.Err(err)- An error message if the calculation fails.
§Examples
use hmac_rng::Proof;
let proof = Proof::new(None, None, 0);
let result = proof.calculate();
match result {
Ok(random_number) => println!("Random number: {}", random_number),
Err(err) => eprintln!("Error: {}", err),
}Sourcepub fn verify(
client_seed: &[u8],
server_seed: Option<&[u8]>,
nonce: i64,
rand_num: f64,
) -> Result<bool, String>
pub fn verify( client_seed: &[u8], server_seed: Option<&[u8]>, nonce: i64, rand_num: f64, ) -> Result<bool, String>
Verifies that the given random number is valid for the given client seed, server seed, and nonce values by recreating the Proof instance and comparing the calculated random number with the provided random number.
§Arguments
client_seed- The client seed.server_seed- An optional server seed. PassNoneif not provided.nonce- The nonce value.rand_num- The random number to verify.
§Returns
Ok(valid)-trueif the random number is valid,falseotherwise.Err(err)- An error message if the verification fails.
§Examples
use hmac_rng::Proof;
let client_seed = vec![1, 2, 3];
let server_seed = vec![4, 5, 6];
let nonce = 0;
let random_number = 0.42;
let result = Proof::verify(&client_seed, Some(&server_seed), nonce, random_number);
match result {
Ok(valid) => println!("Valid: {}", valid),
Err(err) => eprintln!("Error: {}", err),
}