From 2a275a2f8770f4b84bb165fda2c5294aa1e8feab Mon Sep 17 00:00:00 2001 From: Yeastplume Date: Fri, 11 Aug 2017 22:05:59 +0100 Subject: [PATCH] POW Blake 2 changeover (#91) * playing around with changing cuckoo sizes on the fly * modifying tests to use global cuckoo parameters, and checking results * check for pow size * Changing global function names, and removing length from POW serialization * changes to use blake2 for cuckoo seed generation * updating tests to blake 2 --- chain/src/chain.rs | 3 +- core/src/genesis.rs | 2 +- core/src/global.rs | 14 +++++++++ core/src/pow/cuckoo.rs | 70 ++++++++++++++++++++++-------------------- grin/Cargo.toml | 2 +- 5 files changed, 54 insertions(+), 37 deletions(-) diff --git a/chain/src/chain.rs b/chain/src/chain.rs index 47ebc1d26..ed3724e86 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -85,7 +85,8 @@ impl Chain { let sz = global::sizeshift(); let proof_size = global::proofsize(); - let mut internal_miner = pow::cuckoo::Miner::new(consensus::EASINESS, sz as u32, proof_size); pow::pow_size(&mut internal_miner, &mut gen.header, diff, sz as u32).unwrap(); + let mut internal_miner = pow::cuckoo::Miner::new(consensus::EASINESS, sz as u32, proof_size); + pow::pow_size(&mut internal_miner, &mut gen.header, diff, sz as u32).unwrap(); chain_store.save_block(&gen)?; // saving a new tip based on genesis diff --git a/core/src/genesis.rs b/core/src/genesis.rs index b612b4ec7..8114af5f9 100644 --- a/core/src/genesis.rs +++ b/core/src/genesis.rs @@ -41,7 +41,7 @@ pub fn genesis() -> core::Block { utxo_merkle: [].hash(), tx_merkle: [].hash(), features: core::DEFAULT_BLOCK, - nonce: 0, + nonce: global::get_genesis_nonce(), pow: core::Proof::zero(proof_size), // TODO get actual PoW solution }, inputs: vec![], diff --git a/core/src/global.rs b/core/src/global.rs index 0dca2bc25..cf371b126 100644 --- a/core/src/global.rs +++ b/core/src/global.rs @@ -93,3 +93,17 @@ pub fn is_automated_testing_mode() -> bool { return false; } } + +/// Helper function to get a nonce known to create a valid POW on +/// the genesis block, to prevent it taking ages. Should be fine for now +/// as the genesis block POW solution turns out to be the same for every new block chain +/// at the moment + +pub fn get_genesis_nonce() -> u64 { + let param_ref=MINING_PARAMETER_MODE.read().unwrap(); + match *param_ref { + MiningParameterMode::AutomatedTesting => 0, //won't make a difference + MiningParameterMode::UserTesting => 22141, //Magic nonce for current genesis block at cuckoo16 + MiningParameterMode::Production => 0, //TBD + } +} diff --git a/core/src/pow/cuckoo.rs b/core/src/pow/cuckoo.rs index a604e0118..3e199ef1e 100644 --- a/core/src/pow/cuckoo.rs +++ b/core/src/pow/cuckoo.rs @@ -20,8 +20,7 @@ use std::collections::HashSet; use std::cmp; -use crypto::digest::Digest; -use crypto::sha2::Sha256; +use blake2; use core::Proof; use pow::siphash::siphash24; @@ -58,10 +57,8 @@ impl Cuckoo { /// serialized block header. pub fn new(header: &[u8], sizeshift: u32) -> Cuckoo { let size = 1 << sizeshift; - let mut hasher = Sha256::new(); - let mut hashed = [0; 32]; - hasher.input(header); - hasher.result(&mut hashed); + let hashed=blake2::blake2b::blake2b(32, &[], header); + let hashed=hashed.as_bytes(); let k0 = u8_to_u64(hashed, 0); let k1 = u8_to_u64(hashed, 8); @@ -320,7 +317,7 @@ impl Miner { /// Utility to transform a 8 bytes of a byte array into a u64. -fn u8_to_u64(p: [u8; 32], i: usize) -> u64 { +fn u8_to_u64(p:&[u8], i: usize) -> u64 { (p[i] as u64) | (p[i + 1] as u64) << 8 | (p[i + 2] as u64) << 16 | (p[i + 3] as u64) << 24 | (p[i + 4] as u64) << 32 | (p[i + 5] as u64) << 40 | (p[i + 6] as u64) << 48 | (p[i + 7] as u64) << 56 @@ -331,32 +328,32 @@ mod test { use super::*; use core::Proof; - static V1:[u32;42] = [0xe13, 0x410c, 0x7974, 0x8317, 0xb016, 0xb992, 0xe3c8, 0x1038a, - 0x116f0, 0x15ed2, 0x165a2, 0x17793, 0x17dd1, 0x1f885, 0x20932, - 0x20936, 0x2171b, 0x28968, 0x2b184, 0x30b8e, 0x31d28, 0x35782, - 0x381ea, 0x38321, 0x3b414, 0x3e14b, 0x43615, 0x49a51, 0x4a319, - 0x58271, 0x5dbb9, 0x5dbcf, 0x62db4, 0x653d2, 0x655f6, 0x66382, - 0x7057d, 0x765b0, 0x79c7c, 0x83167, 0x86e7b, 0x8a5f4]; - static V2:[u32;42] = [0x33b8, 0x3fd9, 0x8f2b, 0xba0d, 0x11e2d, 0x1d51d, 0x2786e, 0x29625, - 0x2a862, 0x2a972, 0x2e6d7, 0x319df, 0x37ce7, 0x3f771, 0x4373b, - 0x439b7, 0x48626, 0x49c7d, 0x4a6f1, 0x4a808, 0x4e518, 0x519e3, - 0x526bb, 0x54988, 0x564e9, 0x58a6c, 0x5a4dd, 0x63fa2, 0x68ad1, - 0x69e52, 0x6bf53, 0x70841, 0x76343, 0x763a4, 0x79681, 0x7d006, - 0x7d633, 0x7eebe, 0x7fe7c, 0x811fa, 0x863c1, 0x8b149]; - static V3:[u32;42] = [0x24ae, 0x5180, 0x9f3d, 0xd379, 0x102c9, 0x15787, 0x16df4, 0x19509, - 0x19a78, 0x235a0, 0x24210, 0x24410, 0x2567f, 0x282c3, 0x2d986, - 0x2efde, 0x319d7, 0x334d7, 0x336dd, 0x34296, 0x35809, 0x3ad40, - 0x46d81, 0x48c92, 0x4b374, 0x4c353, 0x4fe4c, 0x50e4f, 0x53202, - 0x5d167, 0x6527c, 0x6a8b5, 0x6c70d, 0x76d90, 0x794f4, 0x7c411, - 0x7c5d4, 0x7f59f, 0x7fead, 0x872d8, 0x875b4, 0x95c6b]; + + static V1:[u32;42] = [0x1fe9, 0x2050, 0x4581, 0x6322, 0x65ab, 0xb3c1, 0xc1a4, + 0xe257, 0x106ae, 0x17b11, 0x202d4, 0x2705d, 0x2deb2, 0x2f80e, + 0x32298, 0x34782, 0x35c5a, 0x37458, 0x38f28, 0x406b2, 0x40e34, + 0x40fc6, 0x42220, 0x42d13, 0x46c0f, 0x4fd47, 0x55ad2, 0x598f7, + 0x5aa8f, 0x62aa3, 0x65725, 0x65dcb, 0x671c7, 0x6eb20, 0x752fe, + 0x7594f, 0x79b9c, 0x7f775, 0x81635, 0x8401c, 0x844e5, 0x89fa8]; + static V2:[u32;42] = [0x2a37, 0x7557, 0xa3c3, 0xfce6, 0x1248e, 0x15837, 0x1827f, + 0x18a93, 0x1a7dd, 0x1b56b, 0x1ceb4, 0x1f962, 0x1fe2a, 0x29cb9, + 0x2f30e, 0x2f771, 0x336bf, 0x34355, 0x391d7, 0x39495, 0x3be0c, + 0x463be, 0x4d0c2, 0x4eead, 0x50214, 0x520de, 0x52a86, 0x53818, + 0x53b3b, 0x54c0b, 0x572fa, 0x5d79c, 0x5e3c2, 0x6769e, 0x6a0fe, + 0x6d835, 0x6fc7c, 0x70f03, 0x79d4a, 0x7b03e, 0x81e09, 0x9bd44]; + static V3:[u32;42] = [0x8158, 0x9f18, 0xc4ba, 0x108c7, 0x11caa, 0x13b82, 0x1618f, + 0x1c83b, 0x1ec89, 0x24354, 0x28864, 0x2a0fb, 0x2ce50, 0x2e8fa, + 0x32b36, 0x343e6, 0x34dc9, 0x36881, 0x3ffca, 0x40f79, 0x42721, + 0x43b8c, 0x44b9d, 0x47ed3, 0x4cd34, 0x5278a, 0x5ab64, 0x5b4d4, + 0x5d842, 0x5fa33, 0x6464e, 0x676ee, 0x685d6, 0x69df0, 0x6a5fd, + 0x6bda3, 0x72544, 0x77974, 0x7908c, 0x80e67, 0x81ef4, 0x8d882]; // cuckoo28 at 50% edges of letter 'u' - static V4:[u32;42] = [0x1abd16, 0x7bb47e, 0x860253, 0xfad0b2, 0x121aa4d, 0x150a10b, - 0x20605cb, 0x20ae7e3, 0x235a9be, 0x2640f4a, 0x2724c36, 0x2a6d38c, - 0x2c50b28, 0x30850f2, 0x309668a, 0x30c85bd, 0x345f42c, 0x3901676, - 0x432838f, 0x472158a, 0x4d04e9d, 0x4d6a987, 0x4f577bf, 0x4fbc49c, - 0x593978d, 0x5acd98f, 0x5e60917, 0x6310602, 0x6385e88, 0x64f149c, - 0x66d472e, 0x68e4df9, 0x6b4a89c, 0x6bb751d, 0x6e09792, 0x6e57e1d, - 0x6ecfcdd, 0x70abddc, 0x7291dfd, 0x788069e, 0x79a15b1, 0x7d1a1e9]; + static V4:[u32;42] = [0x1CBBFD, 0x2C5452, 0x520338, 0x6740C5, 0x8C6997, 0xC77150, 0xFD4972, + 0x1060FA7, 0x11BFEA0, 0x1343E8D, 0x14CE02A, 0x1533515, 0x1715E61, 0x1996D9B, + 0x1CB296B, 0x1FCA180, 0x209A367, 0x20AD02E, 0x23CD2E4, 0x2A3B360, 0x2DD1C0C, + 0x333A200, 0x33D77BC, 0x3620C78, 0x3DD7FB8, 0x3FBFA49, 0x41BDED2, 0x4A86FD9, + 0x570DE24, 0x57CAB86, 0x594B886, 0x5C74C94, 0x5DE7572, 0x60ADD6F, 0x635918B, + 0x6C9E120, 0x6EFA583, 0x7394ACA, 0x7556A23, 0x77F70AA, 0x7CF750A, 0x7F60790]; /// Find a 42-cycle on Cuckoo20 at 75% easiness and verifiy against a few /// known cycle proofs @@ -382,7 +379,9 @@ mod test { #[test] fn validate28_vectors() { - assert!(Cuckoo::new(&[117], 28).verify(Proof::new(V4.to_vec().clone()), 50)); + let mut test_header=[0;32]; + test_header[0]=24; + assert!(Cuckoo::new(&test_header, 28).verify(Proof::new(V4.to_vec().clone()), 50)); } #[test] @@ -392,7 +391,10 @@ mod test { assert!(!Cuckoo::new(&[49], 20).verify(Proof::new(vec![0xffff; 42]), 75)); // wrong data for proof assert!(!Cuckoo::new(&[50], 20).verify(Proof::new(V1.to_vec().clone()), 75)); - assert!(!Cuckoo::new(&[117], 20).verify(Proof::new(V4.to_vec().clone()), 50)); + let mut test_header=[0;32]; + test_header[0]=24; + assert!(!Cuckoo::new(&test_header, 20).verify(Proof::new(V4.to_vec().clone()), 50)); + } #[test] diff --git a/grin/Cargo.toml b/grin/Cargo.toml index 4fd2fb82c..66192df01 100644 --- a/grin/Cargo.toml +++ b/grin/Cargo.toml @@ -15,7 +15,7 @@ grin_util = { path = "../util" } grin_wallet = { path = "../wallet" } secp256k1zkp = { path = "../secp256k1zkp" } -cuckoo_miner = { git = "https://github.com/mimblewimble/cuckoo-miner", tag="grin_integration_4"} +cuckoo_miner = { git = "https://github.com/mimblewimble/cuckoo-miner", tag="grin_integration_5"} #cuckoo_miner = { path = "../../cuckoo-miner"} blake2-rfc = "~0.2.17"