Skip to content

Commit 6a42874

Browse files
committed
feat: Use blake2b as the hash function uniformly
1 parent 43a9ad7 commit 6a42874

13 files changed

Lines changed: 160 additions & 85 deletions

File tree

Cargo.lock

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chain/src/tests/find_fork.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,24 @@ fn test_find_fork_case1() {
4040

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

48+
// fork1 total_difficulty 400
4849
for blk in &fork1 {
4950
chain_service.process_block(Arc::new(blk.clone())).unwrap();
5051
}
5152

53+
// fork2 total_difficulty 270
5254
for blk in &fork2 {
5355
chain_service.process_block(Arc::new(blk.clone())).unwrap();
5456
}
5557

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

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

@@ -109,15 +112,17 @@ fn test_find_fork_case2() {
109112

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

120+
// fork2 total_difficulty 400
117121
for blk in &fork1 {
118122
chain_service.process_block(Arc::new(blk.clone())).unwrap();
119123
}
120124

125+
// fork2 total_difficulty 280
121126
for blk in &fork2 {
122127
chain_service.process_block(Arc::new(blk.clone())).unwrap();
123128
}
@@ -185,15 +190,17 @@ fn test_find_fork_case3() {
185190

186191
let mut parent = genesis.clone();
187192
for i in 0..5 {
188-
let new_block = gen_block(&parent, i + 1, U256::from(40u64), vec![], vec![]);
193+
let new_block = gen_block(&parent, i, U256::from(40u64), vec![], vec![]);
189194
fork2.push(new_block.clone());
190195
parent = new_block.header().clone();
191196
}
192197

198+
// fork2 total_difficulty 240
193199
for blk in &fork1 {
194200
chain_service.process_block(Arc::new(blk.clone())).unwrap();
195201
}
196202

203+
// fork2 total_difficulty 200
197204
for blk in &fork2 {
198205
chain_service.process_block(Arc::new(blk.clone())).unwrap();
199206
}
@@ -255,15 +262,17 @@ fn test_find_fork_case4() {
255262

256263
let mut parent = genesis.clone();
257264
for i in 0..2 {
258-
let new_block = gen_block(&parent, i + 1, U256::from(80u64), vec![], vec![]);
265+
let new_block = gen_block(&parent, i, U256::from(80u64), vec![], vec![]);
259266
fork2.push(new_block.clone());
260267
parent = new_block.header().clone();
261268
}
262269

270+
// fork2 total_difficulty 200
263271
for blk in &fork1 {
264272
chain_service.process_block(Arc::new(blk.clone())).unwrap();
265273
}
266274

275+
// fork2 total_difficulty 160
267276
for blk in &fork2 {
268277
chain_service.process_block(Arc::new(blk.clone())).unwrap();
269278
}

core/src/header.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bincode::{deserialize, serialize};
22
use faster_hex::hex_string;
3-
use hash::sha3_256;
3+
use hash::blake2b_256;
44
use numext_fixed_hash::H256;
55
use numext_fixed_uint::U256;
66
use serde_derive::{Deserialize, Serialize};
@@ -63,7 +63,7 @@ pub struct RawHeader {
6363

6464
impl RawHeader {
6565
pub fn pow_hash(&self) -> H256 {
66-
sha3_256(serialize(self).unwrap()).into()
66+
blake2b_256(serialize(self).unwrap()).into()
6767
}
6868

6969
pub fn with_seal(self, seal: Seal) -> Header {
@@ -149,7 +149,7 @@ impl Header {
149149
}
150150

151151
pub fn hash(&self) -> H256 {
152-
sha3_256(serialize(&self).unwrap()).into()
152+
blake2b_256(serialize(&self).unwrap()).into()
153153
}
154154

155155
pub fn pow_hash(&self) -> H256 {

core/src/script.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use faster_hex::hex_encode;
2-
use hash::sha3_256;
2+
use hash::blake2b_256;
33
use numext_fixed_hash::H256;
44
use occupied_capacity::OccupiedCapacity;
55
use serde_derive::{Deserialize, Serialize};
@@ -167,7 +167,7 @@ impl Script {
167167
for argument in &self.signed_args {
168168
bytes.write_all(argument).unwrap();
169169
}
170-
sha3_256(bytes).into()
170+
blake2b_256(bytes).into()
171171
}
172172
_ => H256::zero(),
173173
}
@@ -183,3 +183,43 @@ impl OccupiedCapacity for Script {
183183
+ self.signed_args.occupied_capacity()
184184
}
185185
}
186+
187+
#[cfg(test)]
188+
mod tests {
189+
use super::{Script, H256};
190+
191+
#[test]
192+
fn empty_script_type_hash() {
193+
let script = Script::new(0, vec![], None, None, vec![]);
194+
let expect =
195+
H256::from_hex_str("4b29eb5168ba6f74bff824b15146246109c732626abd3c0578cbf147d8e28479")
196+
.unwrap();
197+
assert_eq!(script.type_hash(), expect);
198+
}
199+
200+
#[test]
201+
fn always_success_script_type_hash() {
202+
let always_success = include_bytes!("../../nodes_template/spec/cells/always_success");
203+
let script = Script::new(0, vec![], None, Some(always_success.to_vec()), vec![]);
204+
let expect =
205+
H256::from_hex_str("9f94d2511b787387638faa4a5bfd448baf21aa5fde3afaa54bb791188b5cf002")
206+
.unwrap();
207+
assert_eq!(script.type_hash(), expect);
208+
}
209+
210+
#[test]
211+
fn one_script_type_hash() {
212+
let one = Script::new(
213+
0,
214+
vec![vec![1]],
215+
Some(H256::zero()),
216+
Some(vec![1]),
217+
vec![vec![1]],
218+
);
219+
let expect =
220+
H256::from_hex_str("afb140d0673571ed5710d220d6146d41bd8bc18a3a4ff723dad4331da5af5bb6")
221+
.unwrap();
222+
223+
assert_eq!(one.type_hash(), expect);
224+
}
225+
}

core/src/transaction.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub use crate::Capacity;
55
use crate::{BlockNumber, Version};
66
use bincode::{deserialize, serialize};
77
use faster_hex::hex_string;
8-
use hash::sha3_256;
8+
use hash::blake2b_256;
99
use numext_fixed_hash::H256;
1010
use occupied_capacity::OccupiedCapacity;
1111
use serde_derive::{Deserialize, Serialize};
@@ -131,7 +131,7 @@ impl CellOutput {
131131
}
132132

133133
pub fn data_hash(&self) -> H256 {
134-
sha3_256(&self.data).into()
134+
blake2b_256(&self.data).into()
135135
}
136136

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

223223
pub fn hash(&self) -> H256 {
224-
sha3_256(serialize(self).unwrap()).into()
224+
blake2b_256(serialize(self).unwrap()).into()
225225
}
226226

227227
pub fn zero() -> Self {
@@ -255,7 +255,7 @@ impl Transaction {
255255
}
256256

257257
pub fn hash(&self) -> H256 {
258-
sha3_256(serialize(&self).unwrap()).into()
258+
blake2b_256(serialize(&self).unwrap()).into()
259259
}
260260

261261
pub fn out_points_iter(&self) -> impl Iterator<Item = &OutPoint> {

core/src/uncle.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::header::Header;
33
use crate::transaction::{ProposalShortId, Transaction};
44
use crate::BlockNumber;
55
use bincode::serialize;
6-
use hash::sha3_256;
6+
use hash::blake2b_256;
77
use numext_fixed_hash::H256;
88
use serde_derive::{Deserialize, Serialize};
99

@@ -62,6 +62,6 @@ pub fn uncles_hash(uncles: &[UncleBlock]) -> H256 {
6262
if uncles.is_empty() {
6363
H256::zero()
6464
} else {
65-
sha3_256(serialize(uncles).unwrap()).into()
65+
blake2b_256(serialize(uncles).unwrap()).into()
6666
}
6767
}

pow/src/cuckoo.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::PowEngine;
22
use byteorder::{ByteOrder, LittleEndian};
33
use ckb_core::header::BlockNumber;
4-
use hash::blake2b;
4+
use hash::blake2b_256;
55
use serde::{de, Deserialize};
66
use serde_derive::Deserialize;
77
use std::collections::HashMap;
@@ -136,7 +136,7 @@ impl CuckooSip {
136136
}
137137

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

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

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

348354
#[test]
349355
fn solve_cuckoo() {
350-
let cuckoo = Cuckoo::new(3, 6);
356+
let cuckoo = Cuckoo::new(6, 8);
351357
for (message, proof) in TESTSET.iter() {
352358
assert_eq!(cuckoo.solve(message).unwrap(), proof);
353359
}
354360
}
355361

356362
#[test]
357363
fn verify_cuckoo() {
358-
let cuckoo = Cuckoo::new(3, 6);
364+
let cuckoo = Cuckoo::new(6, 8);
359365
for (message, proof) in TESTSET.iter() {
360366
assert!(cuckoo.verify(message, proof));
361367
}

pow/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use byteorder::{ByteOrder, LittleEndian};
22
use ckb_core::difficulty::{boundary_to_difficulty, difficulty_to_boundary};
33
use ckb_core::header::{BlockNumber, Header, RawHeader, Seal};
4-
use hash::blake2b;
4+
use hash::blake2b_256;
55
use numext_fixed_hash::H256;
66
use serde_derive::Deserialize;
77
use std::sync::Arc;
@@ -40,7 +40,7 @@ pub trait PowEngine: Send + Sync {
4040

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

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

0 commit comments

Comments
 (0)