Repository navigation
Expand file tree
/
Copy pathproof_of_work.rs
More file actions
117 lines (100 loc) · 3.62 KB
/
Copy pathproof_of_work.rs
File metadata and controls
117 lines (100 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use super::channel::Blake2sChannel;
use crate::commitment_scheme::blake2_hash::{Blake2sHash, Blake2sHasher};
use crate::commitment_scheme::hasher::Hasher;
use crate::core::channel::Channel;
// TODO(ShaharS): generalize to more channels and create a from function in the hash traits.
pub struct ProofOfWork {
// Proof of work difficulty.
pub n_bits: u32,
}
#[derive(Clone, Debug)]
pub struct ProofOfWorkProof {
pub nonce: u64,
}
impl ProofOfWork {
pub fn new(n_bits: u32) -> Self {
Self { n_bits }
}
pub fn prove(&self, channel: &mut Blake2sChannel) -> ProofOfWorkProof {
let seed = channel.get_digest().as_ref().to_vec();
let proof = self.grind(seed);
channel.mix_nonce(proof.nonce);
proof
}
pub fn verify(&self, channel: &mut Blake2sChannel, proof: &ProofOfWorkProof) -> bool {
let seed = channel.get_digest().as_ref().to_vec();
let verified = check_leading_zeros(
self.hash_with_nonce(&seed, proof.nonce).as_ref(),
self.n_bits,
);
if verified {
channel.mix_nonce(proof.nonce);
}
verified
}
fn grind(&self, seed: Vec<u8>) -> ProofOfWorkProof {
let mut nonce = 0u64;
// TODO(ShaharS): naive implementation, should be replaced with a parallel one.
loop {
let hash = self.hash_with_nonce(&seed, nonce);
if check_leading_zeros(hash.as_ref(), self.n_bits) {
return ProofOfWorkProof { nonce };
}
nonce += 1;
}
}
fn hash_with_nonce(&self, seed: &[u8], nonce: u64) -> Blake2sHash {
let hash_input = seed
.iter()
.chain(nonce.to_le_bytes().iter())
.cloned()
.collect::<Vec<_>>();
Blake2sHasher::hash(&hash_input)
}
}
/// Check that the prefix leading zeros is greater than `bound_bits`.
fn check_leading_zeros(bytes: &[u8], bound_bits: u32) -> bool {
let mut n_bits = 0;
// bytes are in little endian order.
for byte in bytes.iter().rev() {
if *byte == 0 {
n_bits += 8;
} else {
n_bits += byte.leading_zeros();
break;
}
}
n_bits >= bound_bits
}
#[cfg(test)]
mod tests {
use crate::commitment_scheme::blake2_hash::Blake2sHash;
use crate::core::channel::{Blake2sChannel, Channel};
use crate::core::proof_of_work::{ProofOfWork, ProofOfWorkProof};
#[test]
fn test_verify_proof_of_work_success() {
let mut channel = Blake2sChannel::new(Blake2sHash::from(vec![0; 32]));
let proof_of_work_prover = ProofOfWork { n_bits: 11 };
let proof = ProofOfWorkProof { nonce: 133 };
assert!(proof_of_work_prover.verify(&mut channel, &proof));
}
#[test]
fn test_verify_proof_of_work_fail() {
let mut channel = Blake2sChannel::new(Blake2sHash::from(vec![0; 32]));
let proof_of_work_prover = ProofOfWork { n_bits: 1 };
let invalid_proof = ProofOfWorkProof { nonce: 0 };
assert!(!proof_of_work_prover.verify(&mut channel, &invalid_proof));
}
#[test]
fn test_proof_of_work() {
let n_bits = 12;
let mut prover_channel = Blake2sChannel::new(Blake2sHash::default());
let mut verifier_channel = Blake2sChannel::new(Blake2sHash::default());
let prover = ProofOfWork::new(n_bits);
let verifier = ProofOfWork::new(n_bits);
let proof = prover.prove(&mut prover_channel);
let verified = verifier.verify(&mut verifier_channel, &proof);
assert!(verified);
assert_eq!(prover_channel.get_digest(), verifier_channel.get_digest());
}
}