Struct benaloh_challenge::Challenge
source · pub struct Challenge<'a, R: RngCore + CryptoRng, C>where
C: Fn(&mut RecordingRng<'a, R>) -> Vec<u8>,{ /* private fields */ }
Expand description
A benaloh challenge that wraps untrusted computation in a way that can be challanged.
Implementations§
source§impl<'a, R: RngCore + CryptoRng, C> Challenge<'a, R, C>where
C: Fn(&mut RecordingRng<'a, R>) -> Vec<u8>,
impl<'a, R: RngCore + CryptoRng, C> Challenge<'a, R, C>where
C: Fn(&mut RecordingRng<'a, R>) -> Vec<u8>,
sourcepub fn new(rng: &'a mut R, untrusted_computation: C) -> Self
pub fn new(rng: &'a mut R, untrusted_computation: C) -> Self
Create a new benaloh challenge with the given RNG and untrusted computation.
While this method takes a closure, it is generally recommended to create a separate untrusted_computation
function and wrap it in the closure.
Example:
fn untrusted_computation<R: Rng>(rng: &mut R, some_data: foo, other_data: bar) -> Vec<u8> {
// Some unstrusted computation that uses an RNG and other data.
// The results of this computation must be a vector of bytes.
};
let mut rng = rand::thread_rng();
let mut hasher = Sha256::new();
let foo = "foo";
let bar = "bar";
let mut challenge = benaloh_challenge::Challenge::new(&mut rng, |rng: _| {
untrusted_computation(rng, &foo, &bar)
});
Note that in this example untrusted_computation
is not given the original rng direcly.
The RNG is first wrapped in a RecordingRNG
befor being passed to untrusted_computation
.
sourcepub fn commit<H: Digest + FixedOutputReset>(&mut self, hasher: &mut H) -> Vec<u8>
pub fn commit<H: Digest + FixedOutputReset>(&mut self, hasher: &mut H) -> Vec<u8>
Commit the results and get the commitment
This method generates both the results and the commitment, so must be called before into_results()
is called.
sourcepub fn challenge(&mut self) -> Vec<u8>
pub fn challenge(&mut self) -> Vec<u8>
Challange the results, revealing the random factors and invalidating the results of the computaton.
The revealing random factors must be given to the challenging device so it may validate the commitment.
sourcepub fn into_results(self) -> Vec<u8>
pub fn into_results(self) -> Vec<u8>
Get the results of the untrusted computation, discarding (zeroing) the secret random factors.
This method will panic if called before commit()
is called (since commit()
generates the results).