Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify by avoiding unnecessary conversions #3343

Merged
merged 1 commit into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions core/src/pow/cuckaroo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ where
let mut uvs = vec![0u64; 2 * proof.proof_size()];
let mut xor0: u64 = 0;
let mut xor1: u64 = 0;
let node_mask: u64 = to_u64!(self.params.edge_mask);

for n in 0..proof.proof_size() {
if nonces[n] > to_u64!(self.params.edge_mask) {
Expand All @@ -85,13 +86,10 @@ where
return Err(ErrorKind::Verification("edges not ascending".to_owned()).into());
}
// 21 is standard siphash rotation constant
let edge = to_edge!(
T,
siphash_block(&self.params.siphash_keys, nonces[n], 21, false)
);
uvs[2 * n] = to_u64!(edge & self.params.edge_mask);
uvs[2 * n + 1] = to_u64!((edge >> 32) & self.params.edge_mask);
let edge: u64 = siphash_block(&self.params.siphash_keys, nonces[n], 21, false);
uvs[2 * n] = edge & node_mask;
xor0 ^= uvs[2 * n];
uvs[2 * n + 1] = (edge >> 32) & node_mask;
xor1 ^= uvs[2 * n + 1];
}
if xor0 | xor1 != 0 {
Expand Down
12 changes: 5 additions & 7 deletions core/src/pow/cuckarood.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ where
let mut ndir = vec![0usize; 2];
let mut xor0: u64 = 0;
let mut xor1: u64 = 0;
let nodemask = self.params.edge_mask >> 1;
let node_mask: u64 = to_u64!(self.params.edge_mask) >> 1;

for n in 0..proof.proof_size() {
let dir = (nonces[n] & 1) as usize;
Expand All @@ -89,14 +89,12 @@ where
if n > 0 && nonces[n] <= nonces[n - 1] {
return Err(ErrorKind::Verification("edges not ascending".to_owned()).into());
}
let edge = to_edge!(
T,
siphash_block(&self.params.siphash_keys, nonces[n], 25, false)
);
// cuckarood uses a non-standard siphash rotation constant 25 as anti-ASIC tweak
let edge: u64 = siphash_block(&self.params.siphash_keys, nonces[n], 25, false);
let idx = 4 * ndir[dir] + 2 * dir;
uvs[idx] = to_u64!(edge & nodemask);
uvs[idx + 1] = to_u64!((edge >> 32) & nodemask);
uvs[idx] = edge & node_mask;
xor0 ^= uvs[idx];
uvs[idx + 1] = (edge >> 32) & node_mask;
xor1 ^= uvs[idx + 1];
ndir[dir] += 1;
}
Expand Down
20 changes: 9 additions & 11 deletions core/src/pow/cuckaroom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ where
return Err(ErrorKind::Verification("wrong cycle length".to_owned()).into());
}
let nonces = &proof.nonces;
let mut from = vec![0u32; proofsize];
let mut to = vec![0u32; proofsize];
let mut xor_from: u32 = 0;
let mut xor_to: u32 = 0;
let nodemask = self.params.edge_mask >> 1;
let mut from = vec![0u64; proofsize];
let mut to = vec![0u64; proofsize];
let mut xor_from: u64 = 0;
let mut xor_to: u64 = 0;
let node_mask: u64 = to_u64!(self.params.edge_mask) >> 1;

for n in 0..proofsize {
if nonces[n] > to_u64!(self.params.edge_mask) {
Expand All @@ -85,13 +85,11 @@ where
if n > 0 && nonces[n] <= nonces[n - 1] {
return Err(ErrorKind::Verification("edges not ascending".to_owned()).into());
}
let edge = to_edge!(
T,
siphash_block(&self.params.siphash_keys, nonces[n], 21, true)
);
from[n] = to_u32!(edge & nodemask);
// 21 is standard siphash rotation constant
let edge: u64 = siphash_block(&self.params.siphash_keys, nonces[n], 21, true);
from[n] = edge & node_mask;
xor_from ^= from[n];
to[n] = to_u32!((edge >> 32) & nodemask);
to[n] = (edge >> 32) & node_mask;
xor_to ^= to[n];
}
if xor_from != xor_to {
Expand Down
10 changes: 4 additions & 6 deletions core/src/pow/cuckarooz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ where
let nonces = &proof.nonces;
let mut uvs = vec![0u64; 2 * proof.proof_size()];
let mut xoruv: u64 = 0;
let node_mask: u64 = to_u64!(self.params.edge_mask) << 1 | 1;

for n in 0..proof.proof_size() {
if nonces[n] > to_u64!(self.params.edge_mask) {
Expand All @@ -83,12 +84,9 @@ where
return Err(ErrorKind::Verification("edges not ascending".to_owned()).into());
}
// 21 is standard siphash rotation constant
let edge = to_edge!(
T,
siphash_block(&self.params.siphash_keys, nonces[n], 21, true)
);
uvs[2 * n] = to_u64!(edge & self.params.edge_mask);
uvs[2 * n + 1] = to_u64!((edge >> 32) & self.params.edge_mask);
let edge: u64 = siphash_block(&self.params.siphash_keys, nonces[n], 21, true);
uvs[2 * n] = edge & node_mask;
uvs[2 * n + 1] = (edge >> 32) & node_mask;
xoruv ^= uvs[2 * n] ^ uvs[2 * n + 1];
}
if xoruv != 0 {
Expand Down