Proof

Struct Proof 

Source
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: i64

Implementations§

Source§

impl Proof

Source

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);
Source

pub fn log_state(&self)

Source

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),
}
Source

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),
}
Source

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. Pass None if not provided.
  • nonce - The nonce value.
  • rand_num - The random number to verify.
§Returns
  • Ok(valid) - true if the random number is valid, false otherwise.
  • 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),
}

Trait Implementations§

Source§

impl Debug for Proof

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Proof

§

impl RefUnwindSafe for Proof

§

impl Send for Proof

§

impl Sync for Proof

§

impl Unpin for Proof

§

impl UnwindSafe for Proof

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V