This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsha256.mini
149 lines (138 loc) · 4.09 KB
/
sha256.mini
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
143
144
145
146
147
148
//
// Copyright 2020, Offchain Labs, Inc. All rights reserved.
//
use std::bytearray::ByteArray;
use std::bytearray::bytearray_new;
use std::bytearray::bytearray_size;
use std::bytearray::bytearray_get256;
use std::bytearray::bytearray_set256;
use std::bytearray::bytearray_get64;
use std::bytearray::bytearray_set64;
use std::bytearray::bytearray_getByte;
use std::bytearray::bytearray_setByte;
use std::bytearray::bytearray_copy;
type Sha256Hasher = struct {
accumulator: bytes32,
buf: [2]uint,
offset: uint,
totalSizeBits: uint,
}
public func sha256hasher_new() -> Sha256Hasher {
return 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] ^ asm(248-8*(h.offset % 32), b&0xff) uint { shl }
};
return xif (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;
return xif (offset == 0) {
h with {
buf: h.buf with { [0] = val }
} with {
offset: 32
} with {
totalSizeBits: 256+h.totalSizeBits
}
} elseif (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
}
} elseif (offset < 32) {
h with {
buf: h.buf with {
[0] = h.buf[0] | asm(8*offset, val) uint { shr }
} with {
[1] = h.buf[1] | asm(8*(32-offset), val) uint { shl }
}
} 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] | asm(8*offset, val) uint { shr }
) bytes32 { sha256f }
} with {
buf: unsafecast<[2]uint>((
asm(8*(32-offset), val) uint { shl },
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
return 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;
}
return sha256hasher_finish(hasher);
}