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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use crate::global;
use crate::pow::common::CuckooParams;
use crate::pow::error::{Error, ErrorKind};
use crate::pow::siphash::siphash_block;
use crate::pow::{PoWContext, Proof};
pub fn new_cuckarood_ctx(edge_bits: u8, proof_size: usize) -> Result<Box<dyn PoWContext>, Error> {
let params = CuckooParams::new(edge_bits, edge_bits - 1, proof_size)?;
Ok(Box::new(CuckaroodContext { params }))
}
pub struct CuckaroodContext {
params: CuckooParams,
}
impl PoWContext for CuckaroodContext {
fn set_header_nonce(
&mut self,
header: Vec<u8>,
nonce: Option<u32>,
_solve: bool,
) -> Result<(), Error> {
self.params.reset_header_nonce(header, nonce)
}
fn find_cycles(&mut self) -> Result<Vec<Proof>, Error> {
unimplemented!()
}
fn verify(&self, proof: &Proof) -> Result<(), Error> {
let size = proof.proof_size();
if size != global::proofsize() {
return Err(ErrorKind::Verification("wrong cycle length".to_owned()).into());
}
let nonces = &proof.nonces;
let mut uvs = vec![0u64; 2 * size];
let mut ndir = vec![0usize; 2];
let mut xor0: u64 = 0;
let mut xor1: u64 = 0;
let mask = u64::MAX >> size.leading_zeros();
let mut headu = vec![2 * size; 1 + mask as usize];
let mut headv = vec![2 * size; 1 + mask as usize];
let mut prev = vec![0usize; 2 * size];
for n in 0..size {
let dir = (nonces[n] & 1) as usize;
if ndir[dir] >= size / 2 {
return Err(ErrorKind::Verification("edges not balanced".to_owned()).into());
}
if nonces[n] > self.params.edge_mask {
return Err(ErrorKind::Verification("edge too big".to_owned()).into());
}
if n > 0 && nonces[n] <= nonces[n - 1] {
return Err(ErrorKind::Verification("edges not ascending".to_owned()).into());
}
let edge: u64 = siphash_block(&self.params.siphash_keys, nonces[n], 25, false);
let idx = 4 * ndir[dir] + 2 * dir;
let u = edge & self.params.node_mask;
let v = (edge >> 32) & self.params.node_mask;
uvs[idx] = u;
let ubits = ((u << 1 | dir as u64) & mask) as usize;
prev[idx] = headu[ubits];
headu[ubits] = idx;
uvs[idx + 1] = v;
let vbits = ((v << 1 | dir as u64) & mask) as usize;
prev[idx + 1] = headv[vbits];
headv[vbits] = idx + 1;
xor0 ^= u;
xor1 ^= v;
ndir[dir] += 1;
}
if xor0 | xor1 != 0 {
return Err(ErrorKind::Verification("endpoints don't match up".to_owned()).into());
}
let mut n = 0;
let mut i = 0;
let mut j;
loop {
j = i;
let mut k = if i & 1 == 0 {
headu[((uvs[i] << 1 | 1) & mask) as usize]
} else {
headv[((uvs[i] << 1 | 0) & mask) as usize]
};
while k != 2 * size {
if uvs[k] == uvs[i] {
if j != i {
return Err(ErrorKind::Verification("branch in cycle".to_owned()).into());
}
j = k;
}
k = prev[k];
}
if j == i {
return Err(ErrorKind::Verification("cycle dead ends".to_owned()).into());
}
i = j ^ 1;
n += 1;
if i == 0 {
break;
}
}
if n == size {
Ok(())
} else {
Err(ErrorKind::Verification("cycle too short".to_owned()).into())
}
}
}
#[cfg(test)]
mod test {
use super::*;
static V1_19_HASH: [u64; 4] = [
0x89f81d7da5e674df,
0x7586b93105a5fd13,
0x6fbe212dd4e8c001,
0x8800c93a8431f938,
];
static V1_19_SOL: [u64; 42] = [
0xa00, 0x3ffb, 0xa474, 0xdc27, 0x182e6, 0x242cc, 0x24de4, 0x270a2, 0x28356, 0x2951f,
0x2a6ae, 0x2c889, 0x355c7, 0x3863b, 0x3bd7e, 0x3cdbc, 0x3ff95, 0x430b6, 0x4ba1a, 0x4bd7e,
0x4c59f, 0x4f76d, 0x52064, 0x5378c, 0x540a3, 0x5af6b, 0x5b041, 0x5e9d3, 0x64ec7, 0x6564b,
0x66763, 0x66899, 0x66e80, 0x68e4e, 0x69133, 0x6b20a, 0x6c2d7, 0x6fd3b, 0x79a8a, 0x79e29,
0x7ae52, 0x7defe,
];
static V2_29_HASH: [u64; 4] = [
0xe2f917b2d79492ed,
0xf51088eaaa3a07a0,
0xaf4d4288d36a4fa8,
0xc8cdfd30a54e0581,
];
static V2_29_SOL: [u64; 42] = [
0x1a9629, 0x1fb257, 0x5dc22a, 0xf3d0b0, 0x200c474, 0x24bd68f, 0x48ad104, 0x4a17170,
0x4ca9a41, 0x55f983f, 0x6076c91, 0x6256ffc, 0x63b60a1, 0x7fd5b16, 0x985bff8, 0xaae71f3,
0xb71f7b4, 0xb989679, 0xc09b7b8, 0xd7601da, 0xd7ab1b6, 0xef1c727, 0xf1e702b, 0xfd6d961,
0xfdf0007, 0x10248134, 0x114657f6, 0x11f52612, 0x12887251, 0x13596b4b, 0x15e8d831,
0x16b4c9e5, 0x17097420, 0x1718afca, 0x187fc40c, 0x19359788, 0x1b41d3f1, 0x1bea25a7,
0x1d28df0f, 0x1ea6c4a0, 0x1f9bf79f, 0x1fa005c6,
];
#[test]
fn cuckarood19_29_vectors() {
global::set_local_chain_type(global::ChainTypes::Mainnet);
let mut ctx19 = new_impl(19, 42);
ctx19.params.siphash_keys = V1_19_HASH;
assert!(ctx19.verify(&Proof::new(V1_19_SOL.to_vec())).is_ok());
assert!(ctx19.verify(&Proof::zero(42)).is_err());
let mut ctx29 = new_impl(29, 42);
ctx29.params.siphash_keys = V2_29_HASH;
assert!(ctx29.verify(&Proof::new(V2_29_SOL.to_vec())).is_ok());
assert!(ctx29.verify(&Proof::zero(42)).is_err());
}
fn new_impl(edge_bits: u8, proof_size: usize) -> CuckaroodContext {
let params = CuckooParams::new(edge_bits, edge_bits - 1, proof_size).unwrap();
CuckaroodContext { params }
}
}