Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
io
Browse files Browse the repository at this point in the history
WizardOfMenlo committed May 29, 2024
1 parent 9d50311 commit a36ee07
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ rand_chacha = { version = "0.3", optional=true }


[features]
default = []
default = [ "pow" ]
ark = ["dep:ark-ff", "dep:ark-ec", "dep:ark-serialize"]
group = ["dep:group"]
pow = ["dep:rand_chacha"]
25 changes: 24 additions & 1 deletion src/plugins/proof_of_work/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use rand::{Rng, SeedableRng};

use crate::{Arthur, ByteChallenges, ByteReader, ByteWriter, Merlin, ProofError, ProofResult};
use crate::{
Arthur, ByteChallenges, ByteIOPattern, ByteReader, ByteWriter, IOPattern, Merlin, ProofError,
ProofResult,
};

/// Struct describing the number of bits of proof of work required
/// Must be between 0 and 128 bits
@@ -18,6 +21,18 @@ impl POWBits {
/// The nonce for a proof-of-work-challenge
pub struct POWNonce(pub [u8; 16]);

pub trait POWIOPatter {
// TODO: Do we want to add bits in the label at trait level?
fn challenge_pow(self, label: &str) -> Self;
}

impl POWIOPatter for IOPattern {
fn challenge_pow(self, label: &str) -> Self {
// 16 bytes challenge and 16 bytes nonce (that will be written)
self.challenge_bytes(16, label).add_bytes(16, label)
}
}

pub trait POWChallenge {
fn challenge_pow(&mut self, bits: POWBits) -> ProofResult<POWNonce>;
}
@@ -33,14 +48,19 @@ where
Merlin: ByteWriter,
{
fn challenge_pow(&mut self, bits: POWBits) -> ProofResult<POWNonce> {
// Squeeze 16 bytes as a challenge from the spong
let mut seed = [0u8; 32];
self.fill_challenge_bytes(&mut seed[..16])?;

// Loop to find a 16-byte nonce
let mut counter = [0u8; 16];
loop {
// Seed rng with the 32-byte (challenge + nonce) seed
seed[16..].copy_from_slice(&counter);
let mut rng = rand_chacha::ChaCha20Rng::from_seed(seed);
let num: u64 = rng.gen();
if num < (1 << bits.0) {
// Add to the transcript the nonce
self.add_bytes(&counter)?;
return Ok(POWNonce(counter));
}
@@ -54,10 +74,13 @@ where
Arthur<'a>: ByteReader,
{
fn challenge_pow(&mut self, bits: POWBits) -> ProofResult<POWNonce> {
// Get the 32 byte seed
let mut seed = [0u8; 32];
self.fill_challenge_bytes(&mut seed[..16])?;
let counter: [u8; 16] = self.next_bytes()?;
seed[16..].copy_from_slice(&counter);

// Instantiate rng and verify.
let mut rng = rand_chacha::ChaCha20Rng::from_seed(seed);
let num: u64 = rng.gen();

0 comments on commit a36ee07

Please sign in to comment.