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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use serde::{Deserialize, Serialize};
use sha2::{digest::FixedOutput, Digest, Sha256};
use std::marker::PhantomData;
const SALT: &str = "79ziepia7vhjgviiwjhnend3ofjqocsi2winc4ptqhmkvcajihywxcizewvckg9h6gs4j83v9";
#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
pub struct PoW<T> {
pub nonce: u64,
pub result: String,
_spook: PhantomData<T>,
}
impl<T: Serialize> PoW<T> {
pub fn prove_work(t: &T, difficulty: u128) -> bincode::Result<PoW<T>> {
bincode_cfg()
.serialize(t)
.map(|v| Self::prove_work_serialized(&v, difficulty))
}
pub fn prove_work_serialized(prefix: &[u8], difficulty: u128) -> PoW<T> {
let prefix_sha = Sha256::new().chain(SALT).chain(prefix);
let mut n = 0;
let mut result = 0;
while result < difficulty {
n += 1;
result = score(prefix_sha.clone(), n);
}
PoW {
nonce: n,
result: result.to_string(),
_spook: PhantomData,
}
}
pub fn calculate(&self, t: &T) -> bincode::Result<u128> {
bincode_cfg()
.serialize(t)
.map(|v| self.calculate_serialized(&v))
}
pub fn calculate_serialized(&self, target: &[u8]) -> u128 {
score(Sha256::new().chain(SALT).chain(target), self.nonce)
}
pub fn is_valid_proof(&self, t: &T) -> bool {
match self.calculate(t) {
Ok(res) => return if self.result == res.to_string() {true} else {false},
Err(_) => return false
}
}
pub fn is_sufficient_difficulty(&self, target_diff: u128) -> bool {
match self.result.parse::<u128>() {
Ok(res) => return res >= target_diff,
Err(_) => return false
}
}
}
fn score(prefix_sha: Sha256, nonce: u64) -> u128 {
first_bytes_as_u128(
prefix_sha
.chain(&nonce.to_be_bytes())
.fixed_result()
.as_slice(),
)
}
fn first_bytes_as_u128(inp: &[u8]) -> u128 {
bincode_cfg().deserialize(&inp).unwrap()
}
fn bincode_cfg() -> bincode::Config {
let mut cfg = bincode::config();
cfg.big_endian();
cfg
}
#[cfg(test)]
mod test {
use super::*;
const DIFFICULTY: u128 = 0xff000000000000000000000000000000;
#[test]
fn base_functionality() {
let phrase = b"Ex nihilo nihil fit.".to_vec();
let pw = PoW::prove_work(&phrase, DIFFICULTY).unwrap();
assert!(pw.calculate(&phrase).unwrap() >= DIFFICULTY);
assert!(pw.is_valid_proof(&phrase));
assert!(pw.is_sufficient_difficulty(DIFFICULTY));
}
#[test]
fn double_pow() {
let phrase = "Ex nihilo nihil fit.".to_owned();
let pw = PoW::prove_work(&phrase, DIFFICULTY).unwrap();
let pwpw: PoW<PoW<String>> = PoW::prove_work(&pw, DIFFICULTY).unwrap();
assert!(pw.calculate(&phrase).unwrap() >= DIFFICULTY);
assert!(pwpw.calculate(&pw).unwrap() >= DIFFICULTY);
assert!(pw.is_sufficient_difficulty(DIFFICULTY));
assert!(pwpw.is_sufficient_difficulty(DIFFICULTY));
assert!(pw.is_valid_proof(&phrase));
assert!(pwpw.is_valid_proof(&pw));
}
#[test]
fn is_not_valid_proof() {
let phrase = "Ex nihilo nihil fit.".to_owned();
let phrase2 = "Omne quod movetur ab alio movetur.".to_owned();
let pw = PoW::prove_work(&phrase, DIFFICULTY).unwrap();
let pw2 = PoW::prove_work(&phrase2, DIFFICULTY).unwrap();
assert!(!pw.is_valid_proof(&phrase2));
assert!(!pw2.is_valid_proof(&phrase));
}
#[test]
fn serialization_test() {
let target: u8 = 1;
let pw = PoW::prove_work(&target, DIFFICULTY).unwrap();
let message: (u8, PoW<u8>) = (target, pw);
let message_ser = bincode_cfg().serialize(&message).unwrap();
let recieved_message: (u8, PoW<u8>) = bincode_cfg().deserialize(&message_ser).unwrap();
assert_eq!(recieved_message, message);
assert!(message.1.is_sufficient_difficulty(DIFFICULTY));
assert!(message.1.is_valid_proof(&target));
}
}