Skip to content

Commit 2d8dc5e

Browse files
committed
add some comments and remove unused codes
1 parent 015df1c commit 2d8dc5e

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

src/hash/sha256.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
#![allow(non_snake_case)]
2-
use core::num;
3-
use std::collections::HashMap;
4-
5-
use bitcoin::opcodes::all::{OP_FROMALTSTACK, OP_TOALTSTACK};
62

73
use crate::treepp::{pushable, script, Script};
84
use crate::u32::u32_add::u32_add_drop;
9-
use crate::u32::u32_std::{u32_dup, u32_equalverify, u32_roll};
5+
use crate::u32::u32_std::{u32_dup, u32_roll};
106
use crate::u32::{
117
u32_and::u32_and,
128
u32_rrot::u32_rrot,
139
u32_std::{u32_drop, u32_fromaltstack, u32_pick, u32_push, u32_toaltstack},
1410
u32_xor::{u32_xor, u8_drop_xor_table, u8_push_xor_table},
15-
// unroll,
1611
};
1712

1813
const K: [u32; 64] = [
@@ -66,7 +61,6 @@ pub fn sha256(num_bytes: usize) -> Script {
6661
}
6762
}
6863

69-
/// TODO:
7064
/// reorder bytes for u32
7165
pub fn padding_add_roll(num_bytes: usize) -> Script {
7266
assert!(num_bytes < 512);
@@ -102,6 +96,7 @@ pub fn sha256_init() -> Vec<Script> {
10296
state.iter().map(|x: &u32| u32_push(*x)).collect::<Vec<_>>()
10397
}
10498

99+
/// Change byte order, because SHA uses big endian.
105100
pub fn sha256_final() -> Script {
106101
script! {
107102
for _ in 0..8 {
@@ -124,7 +119,7 @@ pub fn sha256_k() -> Vec<Script> {
124119
}
125120

126121
/// sha256 transform
127-
/// stack: [m[15], m[14], ..., m[0], state[7], state[6], ..., state[0]]
122+
/// intput: [m[15], m[14], ..., m[0], state[7], state[6], ..., state[0]]
128123
/// output: [state[7], state[6], ..., state[0]]
129124
pub fn sha256_transform(xor_depth: u32, k_depth: u32) -> Script {
130125
script! {
@@ -235,7 +230,7 @@ pub fn sha256_transform(xor_depth: u32, k_depth: u32) -> Script {
235230
}
236231
}
237232

238-
/// shift right the top u32
233+
/// Shift right the top u32 element
239234
pub fn u32_shr(rot_num: usize, stack_depth: u32) -> Script {
240235
script! {
241236
{u32_rrot(rot_num)}
@@ -295,6 +290,7 @@ pub fn sig1(stack_depth: u32) -> Script {
295290
}
296291
}
297292

293+
/// Change top element x to (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))
298294
pub fn ep0(stack_depth: u32) -> Script {
299295
script! {
300296
{u32_dup()}
@@ -319,6 +315,7 @@ pub fn ep0(stack_depth: u32) -> Script {
319315
}
320316
}
321317

318+
/// Change top element x to (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))
322319
pub fn ep1(stack_depth: u32) -> Script {
323320
script! {
324321
{u32_dup()}
@@ -343,6 +340,7 @@ pub fn ep1(stack_depth: u32) -> Script {
343340
}
344341
}
345342

343+
// A better u32_not is needed.
346344
pub fn u32_not(stack_depth: u32) -> Script {
347345
script! {
348346
{u32_push(0xffffffff)}
@@ -353,6 +351,7 @@ pub fn u32_not(stack_depth: u32) -> Script {
353351
}
354352
}
355353

354+
/// Push reversed bytes to the alt stack.
356355
pub fn push_reverse_bytes_to_alt(num_bytes: usize) -> Script {
357356
script! {
358357
for i in 1..=num_bytes {
@@ -429,9 +428,7 @@ mod tests {
429428
use crate::hash::sha256::*;
430429
use crate::treepp::pushable;
431430
use crate::treepp::{execute_script, script};
432-
use crate::u32::u32_std::u32_equal;
433-
use bitcoin::block;
434-
use bitcoin::opcodes::all::{OP_EQUALVERIFY, OP_ROLL, OP_SWAP};
431+
use crate::u32::u32_std::{u32_equal, u32_equalverify};
435432
use sha2::{Digest, Sha256};
436433

437434
fn rrot(x: u32, n: usize) -> u32 {
@@ -634,10 +631,6 @@ mod tests {
634631

635632
#[test]
636633
fn test_transform_data() {
637-
// push xor table
638-
639-
// push k
640-
641634
let input: [u32; 16] = [
642635
0x61626364, 0x62636465, 0x63646566, 0x64656667, 0x65666768, 0x66676869, 0x6768696a,
643636
0x68696a6b, 0x696a6b6c, 0x6a6b6c6d, 0x6b6c6d6e, 0x6c6d6e6f, 0x6d6e6f70, 0x6e6f7071,
@@ -651,8 +644,10 @@ mod tests {
651644

652645
let script = script! {
653646

647+
// push xor table
654648
{u8_push_xor_table()}
655649

650+
// push k
656651
for i in 0..64 {
657652
{u32_push(K[63-i])}
658653
}
@@ -679,7 +674,6 @@ mod tests {
679674
};
680675

681676
let res = execute_script(script);
682-
// println!("stack: {:100}", res);
683677
assert_eq!(res.final_stack.len(), 0);
684678
}
685679

0 commit comments

Comments
 (0)