simplify by avoiding unnecessary conversions (#3343)

This commit is contained in:
John Tromp 2020-06-08 23:04:06 +02:00 committed by GitHub
parent 992d450e0a
commit e28e02ecd4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 30 deletions

View file

@ -76,6 +76,7 @@ where
let mut uvs = vec![0u64; 2 * proof.proof_size()]; let mut uvs = vec![0u64; 2 * proof.proof_size()];
let mut xor0: u64 = 0; let mut xor0: u64 = 0;
let mut xor1: u64 = 0; let mut xor1: u64 = 0;
let node_mask: u64 = to_u64!(self.params.edge_mask);
for n in 0..proof.proof_size() { for n in 0..proof.proof_size() {
if nonces[n] > to_u64!(self.params.edge_mask) { if nonces[n] > to_u64!(self.params.edge_mask) {
@ -85,13 +86,10 @@ where
return Err(ErrorKind::Verification("edges not ascending".to_owned()).into()); return Err(ErrorKind::Verification("edges not ascending".to_owned()).into());
} }
// 21 is standard siphash rotation constant // 21 is standard siphash rotation constant
let edge = to_edge!( let edge: u64 = siphash_block(&self.params.siphash_keys, nonces[n], 21, false);
T, uvs[2 * n] = edge & node_mask;
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);
xor0 ^= uvs[2 * n]; xor0 ^= uvs[2 * n];
uvs[2 * n + 1] = (edge >> 32) & node_mask;
xor1 ^= uvs[2 * n + 1]; xor1 ^= uvs[2 * n + 1];
} }
if xor0 | xor1 != 0 { if xor0 | xor1 != 0 {

View file

@ -76,7 +76,7 @@ where
let mut ndir = vec![0usize; 2]; let mut ndir = vec![0usize; 2];
let mut xor0: u64 = 0; let mut xor0: u64 = 0;
let mut xor1: 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() { for n in 0..proof.proof_size() {
let dir = (nonces[n] & 1) as usize; let dir = (nonces[n] & 1) as usize;
@ -89,14 +89,12 @@ where
if n > 0 && nonces[n] <= nonces[n - 1] { if n > 0 && nonces[n] <= nonces[n - 1] {
return Err(ErrorKind::Verification("edges not ascending".to_owned()).into()); return Err(ErrorKind::Verification("edges not ascending".to_owned()).into());
} }
let edge = to_edge!( // cuckarood uses a non-standard siphash rotation constant 25 as anti-ASIC tweak
T, let edge: u64 = siphash_block(&self.params.siphash_keys, nonces[n], 25, false);
siphash_block(&self.params.siphash_keys, nonces[n], 25, false)
);
let idx = 4 * ndir[dir] + 2 * dir; let idx = 4 * ndir[dir] + 2 * dir;
uvs[idx] = to_u64!(edge & nodemask); uvs[idx] = edge & node_mask;
uvs[idx + 1] = to_u64!((edge >> 32) & nodemask);
xor0 ^= uvs[idx]; xor0 ^= uvs[idx];
uvs[idx + 1] = (edge >> 32) & node_mask;
xor1 ^= uvs[idx + 1]; xor1 ^= uvs[idx + 1];
ndir[dir] += 1; ndir[dir] += 1;
} }

View file

@ -72,11 +72,11 @@ where
return Err(ErrorKind::Verification("wrong cycle length".to_owned()).into()); return Err(ErrorKind::Verification("wrong cycle length".to_owned()).into());
} }
let nonces = &proof.nonces; let nonces = &proof.nonces;
let mut from = vec![0u32; proofsize]; let mut from = vec![0u64; proofsize];
let mut to = vec![0u32; proofsize]; let mut to = vec![0u64; proofsize];
let mut xor_from: u32 = 0; let mut xor_from: u64 = 0;
let mut xor_to: u32 = 0; let mut xor_to: u64 = 0;
let nodemask = self.params.edge_mask >> 1; let node_mask: u64 = to_u64!(self.params.edge_mask) >> 1;
for n in 0..proofsize { for n in 0..proofsize {
if nonces[n] > to_u64!(self.params.edge_mask) { if nonces[n] > to_u64!(self.params.edge_mask) {
@ -85,13 +85,11 @@ where
if n > 0 && nonces[n] <= nonces[n - 1] { if n > 0 && nonces[n] <= nonces[n - 1] {
return Err(ErrorKind::Verification("edges not ascending".to_owned()).into()); return Err(ErrorKind::Verification("edges not ascending".to_owned()).into());
} }
let edge = to_edge!( // 21 is standard siphash rotation constant
T, let edge: u64 = siphash_block(&self.params.siphash_keys, nonces[n], 21, true);
siphash_block(&self.params.siphash_keys, nonces[n], 21, true) from[n] = edge & node_mask;
);
from[n] = to_u32!(edge & nodemask);
xor_from ^= from[n]; xor_from ^= from[n];
to[n] = to_u32!((edge >> 32) & nodemask); to[n] = (edge >> 32) & node_mask;
xor_to ^= to[n]; xor_to ^= to[n];
} }
if xor_from != xor_to { if xor_from != xor_to {

View file

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