From e28e02ecd40dadf70caae78e18bc2ca9adba2169 Mon Sep 17 00:00:00 2001 From: John Tromp Date: Mon, 8 Jun 2020 23:04:06 +0200 Subject: [PATCH] simplify by avoiding unnecessary conversions (#3343) --- core/src/pow/cuckaroo.rs | 10 ++++------ core/src/pow/cuckarood.rs | 12 +++++------- core/src/pow/cuckaroom.rs | 20 +++++++++----------- core/src/pow/cuckarooz.rs | 10 ++++------ 4 files changed, 22 insertions(+), 30 deletions(-) diff --git a/core/src/pow/cuckaroo.rs b/core/src/pow/cuckaroo.rs index 88311dfaff..2f4f62fc7b 100644 --- a/core/src/pow/cuckaroo.rs +++ b/core/src/pow/cuckaroo.rs @@ -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) { @@ -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 { diff --git a/core/src/pow/cuckarood.rs b/core/src/pow/cuckarood.rs index c5fb8c9246..a4e1e00dc3 100644 --- a/core/src/pow/cuckarood.rs +++ b/core/src/pow/cuckarood.rs @@ -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; @@ -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; } diff --git a/core/src/pow/cuckaroom.rs b/core/src/pow/cuckaroom.rs index 7600250931..82f9f36bb7 100644 --- a/core/src/pow/cuckaroom.rs +++ b/core/src/pow/cuckaroom.rs @@ -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) { @@ -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 { diff --git a/core/src/pow/cuckarooz.rs b/core/src/pow/cuckarooz.rs index 6781be8a2a..74d73e0c15 100644 --- a/core/src/pow/cuckarooz.rs +++ b/core/src/pow/cuckarooz.rs @@ -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) { @@ -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 {