MCPcopy Create free account
hub / github.com/BitVM/BitVM / sha256

Function sha256

bitvm/src/hash/sha256.rs:30–69  ·  view source on GitHub ↗

sha256 take indefinite length input on the top of stack and return 256 bit (64 byte)

(num_bytes: usize)

Source from the content-addressed store, hash-verified

28
29/// sha256 take indefinite length input on the top of stack and return 256 bit (64 byte)
30pub fn sha256(num_bytes: usize) -> Script {
31 if num_bytes == 32 {
32 return sha256_32bytes();
33 }
34 if num_bytes == 80 {
35 return sha256_80bytes();
36 }
37 let mut chunks_size: usize = num_bytes / 64 + 1;
38 if (num_bytes % 64) > 55 {
39 chunks_size += 1;
40 }
41
42 script! {
43 {push_reverse_bytes_to_alt(num_bytes)}
44
45 // top of stack: [ [n bytes input] ]
46 {u8_push_xor_table()}
47 {sha256_k()}
48 // top of stack: [ [64 byte chunks]... ]
49 {padding_add_roll(num_bytes)}
50 {sha256_init()}
51 // top of stack: [ [64 byte chunks]..., state[0-7]]
52 for i in 0..chunks_size {
53 {sha256_transform(8 + ((chunks_size as u32) - i as u32) *16 + 64 + 1, 8 + ((chunks_size as u32) - i as u32) *16)}
54 }
55
56 {sha256_final()}
57 for _ in 0..8 {
58 {u32_toaltstack()}
59 }
60 for _ in 0..64 {
61 {u32_drop()}
62 }
63 {u8_drop_xor_table()}
64
65 for _ in 0..8 {
66 {u32_fromaltstack()}
67 }
68 }
69}
70
71pub fn sha256_32bytes() -> Script {
72 script! {

Callers

nothing calls this directly

Calls 2

sha256_32bytesFunction · 0.85
sha256_80bytesFunction · 0.85

Tested by

no test coverage detected