Skip to content

Commit

Permalink
feat: Use blake2b as the hash function uniformly
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed Mar 14, 2019
1 parent 43a9ad7 commit 6a42874
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 85 deletions.
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions chain/src/tests/find_fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,24 @@ fn test_find_fork_case1() {

let mut parent = genesis.clone();
for i in 0..3 {
let new_block = gen_block(&parent, i + 1, U256::from(100u64), vec![], vec![]);
let new_block = gen_block(&parent, i, U256::from(90u64), vec![], vec![]);
fork2.push(new_block.clone());
parent = new_block.header().clone();
}

// fork1 total_difficulty 400
for blk in &fork1 {
chain_service.process_block(Arc::new(blk.clone())).unwrap();
}

// fork2 total_difficulty 270
for blk in &fork2 {
chain_service.process_block(Arc::new(blk.clone())).unwrap();
}

let tip_number = { shared.chain_state().lock().tip_number() };

// fork2 total_difficulty 470
let new_block = gen_block(&parent, 100, U256::from(200u64), vec![], vec![]);
fork2.push(new_block.clone());

Expand Down Expand Up @@ -109,15 +112,17 @@ fn test_find_fork_case2() {

let mut parent = fork1[0].header().clone();
for i in 0..2 {
let new_block = gen_block(&parent, i + 1, U256::from(100u64), vec![], vec![]);
let new_block = gen_block(&parent, i, U256::from(90u64), vec![], vec![]);
fork2.push(new_block.clone());
parent = new_block.header().clone();
}

// fork2 total_difficulty 400
for blk in &fork1 {
chain_service.process_block(Arc::new(blk.clone())).unwrap();
}

// fork2 total_difficulty 280
for blk in &fork2 {
chain_service.process_block(Arc::new(blk.clone())).unwrap();
}
Expand Down Expand Up @@ -185,15 +190,17 @@ fn test_find_fork_case3() {

let mut parent = genesis.clone();
for i in 0..5 {
let new_block = gen_block(&parent, i + 1, U256::from(40u64), vec![], vec![]);
let new_block = gen_block(&parent, i, U256::from(40u64), vec![], vec![]);
fork2.push(new_block.clone());
parent = new_block.header().clone();
}

// fork2 total_difficulty 240
for blk in &fork1 {
chain_service.process_block(Arc::new(blk.clone())).unwrap();
}

// fork2 total_difficulty 200
for blk in &fork2 {
chain_service.process_block(Arc::new(blk.clone())).unwrap();
}
Expand Down Expand Up @@ -255,15 +262,17 @@ fn test_find_fork_case4() {

let mut parent = genesis.clone();
for i in 0..2 {
let new_block = gen_block(&parent, i + 1, U256::from(80u64), vec![], vec![]);
let new_block = gen_block(&parent, i, U256::from(80u64), vec![], vec![]);
fork2.push(new_block.clone());
parent = new_block.header().clone();
}

// fork2 total_difficulty 200
for blk in &fork1 {
chain_service.process_block(Arc::new(blk.clone())).unwrap();
}

// fork2 total_difficulty 160
for blk in &fork2 {
chain_service.process_block(Arc::new(blk.clone())).unwrap();
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/header.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bincode::{deserialize, serialize};
use faster_hex::hex_string;
use hash::sha3_256;
use hash::blake2b_256;
use numext_fixed_hash::H256;
use numext_fixed_uint::U256;
use serde_derive::{Deserialize, Serialize};
Expand Down Expand Up @@ -63,7 +63,7 @@ pub struct RawHeader {

impl RawHeader {
pub fn pow_hash(&self) -> H256 {
sha3_256(serialize(self).unwrap()).into()
blake2b_256(serialize(self).unwrap()).into()
}

pub fn with_seal(self, seal: Seal) -> Header {
Expand Down Expand Up @@ -149,7 +149,7 @@ impl Header {
}

pub fn hash(&self) -> H256 {
sha3_256(serialize(&self).unwrap()).into()
blake2b_256(serialize(&self).unwrap()).into()
}

pub fn pow_hash(&self) -> H256 {
Expand Down
44 changes: 42 additions & 2 deletions core/src/script.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use faster_hex::hex_encode;
use hash::sha3_256;
use hash::blake2b_256;
use numext_fixed_hash::H256;
use occupied_capacity::OccupiedCapacity;
use serde_derive::{Deserialize, Serialize};
Expand Down Expand Up @@ -167,7 +167,7 @@ impl Script {
for argument in &self.signed_args {
bytes.write_all(argument).unwrap();
}
sha3_256(bytes).into()
blake2b_256(bytes).into()
}
_ => H256::zero(),
}
Expand All @@ -183,3 +183,43 @@ impl OccupiedCapacity for Script {
+ self.signed_args.occupied_capacity()
}
}

#[cfg(test)]
mod tests {
use super::{Script, H256};

#[test]
fn empty_script_type_hash() {
let script = Script::new(0, vec![], None, None, vec![]);
let expect =
H256::from_hex_str("4b29eb5168ba6f74bff824b15146246109c732626abd3c0578cbf147d8e28479")
.unwrap();
assert_eq!(script.type_hash(), expect);
}

#[test]
fn always_success_script_type_hash() {
let always_success = include_bytes!("../../nodes_template/spec/cells/always_success");
let script = Script::new(0, vec![], None, Some(always_success.to_vec()), vec![]);
let expect =
H256::from_hex_str("9f94d2511b787387638faa4a5bfd448baf21aa5fde3afaa54bb791188b5cf002")
.unwrap();
assert_eq!(script.type_hash(), expect);
}

#[test]
fn one_script_type_hash() {
let one = Script::new(
0,
vec![vec![1]],
Some(H256::zero()),
Some(vec![1]),
vec![vec![1]],
);
let expect =
H256::from_hex_str("afb140d0673571ed5710d220d6146d41bd8bc18a3a4ff723dad4331da5af5bb6")
.unwrap();

assert_eq!(one.type_hash(), expect);
}
}
8 changes: 4 additions & 4 deletions core/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub use crate::Capacity;
use crate::{BlockNumber, Version};
use bincode::{deserialize, serialize};
use faster_hex::hex_string;
use hash::sha3_256;
use hash::blake2b_256;
use numext_fixed_hash::H256;
use occupied_capacity::OccupiedCapacity;
use serde_derive::{Deserialize, Serialize};
Expand Down Expand Up @@ -131,7 +131,7 @@ impl CellOutput {
}

pub fn data_hash(&self) -> H256 {
sha3_256(&self.data).into()
blake2b_256(&self.data).into()
}

pub fn destruct(self) -> (Capacity, Vec<u8>, H256, Option<Script>) {
Expand Down Expand Up @@ -221,7 +221,7 @@ impl ProposalShortId {
}

pub fn hash(&self) -> H256 {
sha3_256(serialize(self).unwrap()).into()
blake2b_256(serialize(self).unwrap()).into()
}

pub fn zero() -> Self {
Expand Down Expand Up @@ -255,7 +255,7 @@ impl Transaction {
}

pub fn hash(&self) -> H256 {
sha3_256(serialize(&self).unwrap()).into()
blake2b_256(serialize(&self).unwrap()).into()
}

pub fn out_points_iter(&self) -> impl Iterator<Item = &OutPoint> {
Expand Down
4 changes: 2 additions & 2 deletions core/src/uncle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::header::Header;
use crate::transaction::{ProposalShortId, Transaction};
use crate::BlockNumber;
use bincode::serialize;
use hash::sha3_256;
use hash::blake2b_256;
use numext_fixed_hash::H256;
use serde_derive::{Deserialize, Serialize};

Expand Down Expand Up @@ -62,6 +62,6 @@ pub fn uncles_hash(uncles: &[UncleBlock]) -> H256 {
if uncles.is_empty() {
H256::zero()
} else {
sha3_256(serialize(uncles).unwrap()).into()
blake2b_256(serialize(uncles).unwrap()).into()
}
}
42 changes: 24 additions & 18 deletions pow/src/cuckoo.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::PowEngine;
use byteorder::{ByteOrder, LittleEndian};
use ckb_core::header::BlockNumber;
use hash::blake2b;
use hash::blake2b_256;
use serde::{de, Deserialize};
use serde_derive::Deserialize;
use std::collections::HashMap;
Expand Down Expand Up @@ -136,7 +136,7 @@ impl CuckooSip {
}

fn message_to_keys(message: &[u8]) -> [u64; 4] {
let result = blake2b(message);
let result = blake2b_256(message);
[
LittleEndian::read_u64(&result[0..8]).to_le(),
LittleEndian::read_u64(&result[8..16]).to_le(),
Expand Down Expand Up @@ -304,7 +304,7 @@ mod test {
use proptest::{collection::size_range, prelude::*};

fn _cuckoo_solve(message: &[u8]) -> Result<(), TestCaseError> {
let cuckoo = Cuckoo::new(3, 6);
let cuckoo = Cuckoo::new(6, 8);
if let Some(proof) = cuckoo.solve(message) {
prop_assert!(cuckoo.verify(message, &proof));
}
Expand All @@ -318,44 +318,50 @@ mod test {
}
}

const TESTSET: [([u8; 80], [u32; 6]); 3] = [
const TESTSET: [([u8; 80], [u32; 8]); 3] = [
(
[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1c, 0, 0, 0,
238, 237, 143, 251, 211, 26, 16, 237, 158, 89, 77, 62, 49, 241, 85, 233, 49, 77,
230, 148, 177, 49, 129, 38, 152, 148, 40, 170, 1, 115, 145, 191, 44, 10, 206, 23,
226, 132, 186, 196, 204, 205, 133, 173, 209, 20, 116, 16, 159, 161, 117, 167, 151,
171, 246, 181, 209, 140, 189, 163, 206, 155, 209, 157, 110, 2, 79, 249, 34, 228,
252, 245, 141, 27, 9, 156, 85, 58, 121, 46,
],
[0, 1, 2, 4, 5, 6],
[1, 12, 23, 27, 31, 48, 50, 60],
),
(
[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x36, 0, 0, 0,
146, 101, 131, 178, 127, 39, 4, 255, 226, 74, 32, 146, 158, 0, 206, 120, 198, 96,
227, 140, 133, 121, 248, 27, 69, 136, 108, 226, 11, 47, 250, 27, 3, 94, 249, 46,
158, 71, 83, 205, 196, 206, 65, 31, 158, 62, 7, 45, 235, 234, 165, 137, 253, 210,
15, 224, 232, 233, 116, 214, 231, 234, 47, 3, 64, 250, 246, 80, 161, 51, 61, 153,
217, 101, 82, 189, 62, 247, 194, 3,
],
[0, 1, 2, 3, 4, 7],
[16, 26, 29, 33, 39, 43, 44, 54],
),
(
[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xf6, 0, 0, 0,
24, 75, 179, 121, 98, 241, 250, 124, 100, 197, 125, 237, 29, 128, 222, 12, 134, 5,
241, 148, 87, 86, 159, 53, 217, 6, 202, 87, 71, 169, 8, 6, 202, 47, 50, 214, 18,
68, 84, 248, 105, 201, 162, 182, 95, 189, 145, 108, 234, 173, 81, 191, 109, 56,
192, 59, 176, 113, 85, 75, 254, 237, 161, 177, 189, 22, 219, 131, 24, 67, 96, 12,
22, 192, 108, 1, 189, 243, 22, 31,
],
[0, 1, 2, 4, 5, 7],
[1, 15, 20, 22, 39, 41, 52, 56],
),
];

#[test]
fn solve_cuckoo() {
let cuckoo = Cuckoo::new(3, 6);
let cuckoo = Cuckoo::new(6, 8);
for (message, proof) in TESTSET.iter() {
assert_eq!(cuckoo.solve(message).unwrap(), proof);
}
}

#[test]
fn verify_cuckoo() {
let cuckoo = Cuckoo::new(3, 6);
let cuckoo = Cuckoo::new(6, 8);
for (message, proof) in TESTSET.iter() {
assert!(cuckoo.verify(message, proof));
}
Expand Down
16 changes: 8 additions & 8 deletions pow/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use byteorder::{ByteOrder, LittleEndian};
use ckb_core::difficulty::{boundary_to_difficulty, difficulty_to_boundary};
use ckb_core::header::{BlockNumber, Header, RawHeader, Seal};
use hash::blake2b;
use hash::blake2b_256;
use numext_fixed_hash::H256;
use serde_derive::Deserialize;
use std::sync::Arc;
Expand Down Expand Up @@ -40,7 +40,7 @@ pub trait PowEngine: Send + Sync {

#[allow(clippy::op_ref)]
fn verify_header(&self, header: &Header) -> bool {
let proof_hash: H256 = blake2b(&header.proof()).into();
let proof_hash: H256 = blake2b_256(&header.proof()).into();
if &boundary_to_difficulty(&proof_hash) < header.difficulty() {
return false;
}
Expand All @@ -53,7 +53,7 @@ pub trait PowEngine: Send + Sync {
let message = pow_message(&header.pow_hash()[..], nonce);

if let Some(proof) = self.solve(header.number(), &message) {
let result: H256 = blake2b(&proof).into();
let result: H256 = blake2b_256(&proof).into();
if result < difficulty_to_boundary(&header.difficulty()) {
return Some(Seal::new(nonce, proof));
}
Expand All @@ -70,18 +70,18 @@ pub trait PowEngine: Send + Sync {
#[cfg(test)]
mod test {
use super::*;
use hash::blake2b;
use hash::blake2b_256;
#[test]
fn test_pow_message() {
let zero_hash: H256 = blake2b(&[]).into();
let zero_hash: H256 = blake2b_256(&[]).into();
let nonce = u64::max_value();
let message = pow_message(zero_hash.as_bytes(), nonce);
assert_eq!(
message.to_vec(),
[
255, 255, 255, 255, 255, 255, 255, 255, 14, 87, 81, 192, 38, 229, 67, 178, 232,
171, 46, 176, 96, 153, 218, 161, 209, 229, 223, 71, 119, 143, 119, 135, 250, 171,
69, 205, 241, 47, 227, 168
255, 255, 255, 255, 255, 255, 255, 255, 68, 244, 198, 151, 68, 213, 248, 197, 93,
100, 32, 98, 148, 157, 202, 228, 155, 196, 231, 239, 67, 211, 136, 197, 161, 47,
66, 181, 99, 61, 22, 62
]
.to_vec()
);
Expand Down
Loading

0 comments on commit 6a42874

Please sign in to comment.