This repository was archived by the owner on Nov 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathsha256.mini
More file actions
143 lines (132 loc) · 3.76 KB
/
sha256.mini
File metadata and controls
143 lines (132 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// Copyright 2020, Offchain Labs, Inc. All rights reserved.
//
use std::bytearray::ByteArray;
use std::bytearray::bytearray_size;
use std::bytearray::bytearray_get256;
use std::bytearray::bytearray_getByte;
type Sha256Hasher = struct {
accumulator: bytes32,
buf: [2]uint,
offset: uint,
totalSizeBits: uint,
};
public func sha256hasher_new() -> Sha256Hasher {
struct {
accumulator: bytes32(0x6a09e667bb67ae853c6ef372a54ff53a510e527f9b05688c1f83d9ab5be0cd19),
buf: unsafecast<[2]uint>((0,0)),
offset: 0,
totalSizeBits: 0,
}
}
public func sha256hasher_pushByte(h: Sha256Hasher, b: uint) -> Sha256Hasher {
let word = h.offset / 32;
let newBuffer = h.buf with {
[word] = h.buf[word] ^ (b&0xff << (248-8*(h.offset % 32)))
};
if h.offset >= 63 {
h with {
accumulator: asm(h.accumulator, newBuffer[0], newBuffer[1]) bytes32 { sha256f }
} with {
buf: unsafecast<[2]uint>((0,0))
} with {
offset: 0
} with {
totalSizeBits: 8 + h.totalSizeBits
}
} else {
h with {
buf: newBuffer
} with {
offset: h.offset+1
} with {
totalSizeBits: 8 + h.totalSizeBits
}
}
}
public func sha256hasher_push256(h: Sha256Hasher, val: uint) -> Sha256Hasher {
let offset = h.offset;
if offset == 0 {
h with {
buf: h.buf with { [0] = val }
} with {
offset: 32
} with {
totalSizeBits: 256+h.totalSizeBits
}
} else if offset == 32 {
h with {
accumulator: asm(h.accumulator, h.buf[0], val) bytes32 { sha256f }
} with {
buf: unsafecast<[2]uint>((0,0))
} with {
offset: 0
} with {
totalSizeBits: 256+h.totalSizeBits
}
} else if offset < 32 {
h with {
buf: h.buf with {
[0] = h.buf[0] | (val >> (8*offset))
} with {
[1] = h.buf[1] | (val << (8*(32-offset)))
}
} with {
offset: 32+offset
} with {
totalSizeBits: 256+h.totalSizeBits
}
} else {
offset = offset-32;
h with {
accumulator: asm(
h.accumulator,
h.buf[0],
h.buf[1] | (val >> (8*offset))
) bytes32 { sha256f }
} with {
buf: unsafecast<[2]uint>((
val << 8*(32-offset),
0
))
} with {
offset: offset
} with {
totalSizeBits: 256+h.totalSizeBits
}
}
}
public func sha256hasher_finish(h: Sha256Hasher) -> bytes32 {
// write the first padding byte
h = sha256hasher_pushByte(h, 0x80) with {
totalSizeBits: h.totalSizeBits // undo the +8 caused by sha256hasher_pushByte call
};
// make sure there is space for the 64-bit total length
if h.offset > 56 {
h = h with {
accumulator: asm(h.accumulator, h.buf[0], h.buf[1]) bytes32 { sha256f }
} with {
buf: unsafecast<[2]uint>((0,0))
};
}
// insert the total size, and invoke the compression function
asm(
h.accumulator,
h.buf[0],
h.buf[1] | (h.totalSizeBits & 0xffffffffffffffff),
) bytes32 { sha256f }
}
public func sha256_byteArray(ba: ByteArray) -> bytes32 {
let hasher = sha256hasher_new();
let i = 0;
let sz = bytearray_size(ba);
while i+32 < sz {
hasher = sha256hasher_push256(hasher, bytearray_get256(ba, i));
i = i+32;
}
while i < sz {
hasher = sha256hasher_pushByte(hasher, bytearray_getByte(ba, i));
i = i+1;
}
sha256hasher_finish(hasher)
}