Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit 8c14000

Browse files
Tristan Debrunnersakridge
Tristan Debrunner
authored andcommitted
Add PoH SIMD
1 parent 92cfbf7 commit 8c14000

File tree

6 files changed

+354
-0
lines changed

6 files changed

+354
-0
lines changed

ci/build.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ echo --- Build SGX
4040
ci/docker-run.sh solanalabs/sgxsdk src/sgx/build.sh
4141
)
4242

43+
echo --- Build ISPC
44+
(
45+
set -x
46+
ci/docker-run.sh solanalabs/ispc src/poh-simd/build.sh
47+
)
48+
4349
echo --- Create tarball
4450
(
4551
set -x

ci/docker-ispc/Dockerfile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
FROM buildpack-deps:stretch
2+
3+
ARG ISPC_HOME=/usr/local/src/ispc
4+
ARG LLVM_HOME=/usr/local/src/llvm
5+
ARG LLVM_VERSION=8.0
6+
7+
ENV PATH=$LLVM_HOME/bin-$LLVM_VERSION/bin:$ISPC_HOME/bin/bin:$PATH
8+
9+
RUN set -x \
10+
&& apt-get update \
11+
&& apt purge -y --auto-remove cmake \
12+
&& apt-get install -y bison flex \
13+
&& wget https://cmake.org/files/v3.8/cmake-3.8.0-Linux-x86_64.sh \
14+
&& mkdir /opt/cmake \
15+
&& sh cmake-3.8.0-Linux-x86_64.sh --prefix=/opt/cmake --skip-license \
16+
&& ln -s /opt/cmake/bin/cmake /usr/local/bin/cmake \
17+
&& rm cmake-3.8.0-Linux-x86_64.sh \
18+
&& cmake --version \
19+
&& git clone git://github.com/ispc/ispc.git $ISPC_HOME \
20+
&& cd $ISPC_HOME \
21+
&& python alloy.py -b --version=$LLVM_VERSION --git --selfbuild \
22+
&& rm -rf $LLVM_HOME/build-$LLVM_VERSION $LLVM_HOME/llvm-$LLVM_VERSION $LLVM_HOME/bin-$LLVM_VERSION_temp $LLVM_HOME/build-$LLVM_VERSION_temp \
23+
&& mkdir build \
24+
&& cd build \
25+
&& echo $PATH \
26+
&& ls -la /usr/local/src/llvm/bin-8.0/bin \
27+
&& cmake -DCMAKE_INSTALL_PREFIX=$ISPC_HOME/bin -DCMAKE_CXX_COMPILER=clang++ $ISPC_HOME \
28+
&& make -j$(nproc) \
29+
&& make install \
30+
&& cd .. \
31+
&& rm -rf build \
32+
&& mv $LLVM_HOME/bin-$LLVM_VERSION / \
33+
&& rm -rf $LLVM_HOME \
34+
&& mkdir -p $LLVM_HOME \
35+
&& mv /bin-$LLVM_VERSION $LLVM_HOME \
36+
&& cd / \
37+
&& mv $ISPC_HOME/bin /ispcbin \
38+
&& rm -rf $ISPC_HOME \
39+
&& mkdir $ISPC_HOME \
40+
&& mv /ispcbin $ISPC_HOME/bin \
41+
&& ispc --version

src/poh-simd/Makefile

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
CC=ispc
2+
ISPC_FLAGS := -O2 --pic -I.
3+
DEPS := sha256.h
4+
5+
ISPC_OBJ := ispcobj
6+
ISPC_C_Objects := $(ISPC_OBJ)/poh-verify-sse2.o \
7+
$(ISPC_OBJ)/poh-verify-sse4.o \
8+
$(ISPC_OBJ)/poh-verify-avx1.o \
9+
$(ISPC_OBJ)/poh-verify-avx2.o \
10+
$(ISPC_OBJ)/poh-verify-avx512skx.o
11+
12+
OUT ?= libs
13+
14+
.PHONY: all run
15+
all: $(OUT)/libpoh-simd.so
16+
run: all
17+
18+
$(ISPC_OBJ)/poh-verify-sse2.o: poh-verify.ispc $(DEPS)
19+
@mkdir -p $(ISPC_OBJ)
20+
$(CC) --target=sse2-i32x4 -DNAME_SUFFIX=sse2 $(ISPC_FLAGS) $< -o $@
21+
22+
$(ISPC_OBJ)/poh-verify-sse4.o: poh-verify.ispc $(DEPS)
23+
@mkdir -p $(ISPC_OBJ)
24+
$(CC) --target=sse4-i32x4 -DNAME_SUFFIX=sse4 $(ISPC_FLAGS) $< -o $@
25+
26+
$(ISPC_OBJ)/poh-verify-avx1.o: poh-verify.ispc $(DEPS)
27+
@mkdir -p $(ISPC_OBJ)
28+
$(CC) --target=avx1-i32x8 -DNAME_SUFFIX=avx1 $(ISPC_FLAGS) $< -o $@
29+
30+
$(ISPC_OBJ)/poh-verify-avx2.o: poh-verify.ispc $(DEPS)
31+
@mkdir -p $(ISPC_OBJ)
32+
$(CC) --target=avx2-i32x8 -DNAME_SUFFIX=avx2 $(ISPC_FLAGS) $< -o $@
33+
34+
$(ISPC_OBJ)/poh-verify-avx512skx.o: poh-verify.ispc $(DEPS)
35+
@mkdir -p $(ISPC_OBJ)
36+
$(CC) --target=avx512skx-i32x16 -DNAME_SUFFIX=avx512skx $(ISPC_FLAGS) $< -o $@
37+
38+
$(OUT)/libpoh-simd.so: $(ISPC_C_Objects)
39+
@mkdir -p $(OUT)
40+
gcc -shared -o $@ $^
41+
42+
clean:
43+
@rm -rf $(ISPC_OBJ) $(OUT)

src/poh-simd/build.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
pwd=$PWD
5+
cd "$(dirname "$0")"
6+
7+
echo --- Build
8+
(
9+
set -x
10+
make OUT="$pwd"/dist
11+
)
12+

src/poh-simd/poh-verify.ispc

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
#include "sha256.h"
2+
3+
#define MAKE_FN_NAME(x) export void poh_verify_many_simd_ ## x (uniform u8 hashes[], uniform const unsigned int64 num_hashes_arr[])
4+
#define FUNCTION_NAME(signal) MAKE_FN_NAME(signal)
5+
6+
FUNCTION_NAME(NAME_SUFFIX)
7+
{
8+
foreach(i = 0 ... programCount) {
9+
u8* hash = &hashes[i * SHA256_BLOCK_SIZE];
10+
varying u32 s[8];
11+
varying u32 w[64];
12+
varying u32 T0;
13+
varying u32 T1;
14+
15+
// Load words
16+
for (int j = 0; j < SHA256_BLOCK_SIZE / 4; j++) {
17+
w[j] = (((u32)hash[j * 4] << 24) |
18+
((u32)hash[j * 4 + 1] << 16) |
19+
((u32)hash[j * 4 + 2] << 8) |
20+
((u32)hash[j * 4 + 3]));
21+
}
22+
23+
if (num_hashes_arr[i] > 0) {
24+
for (int j = 0; j < num_hashes_arr[i]; j++) {
25+
s[0] = 0x6a09e667;
26+
s[1] = 0xbb67ae85;
27+
s[2] = 0x3c6ef372;
28+
s[3] = 0xa54ff53a;
29+
s[4] = 0x510e527f;
30+
s[5] = 0x9b05688c;
31+
s[6] = 0x1f83d9ab;
32+
s[7] = 0x5be0cd19;
33+
34+
w[8] = 0x80000000;
35+
w[9] = 0;
36+
w[10] = 0;
37+
w[11] = 0;
38+
w[12] = 0;
39+
w[13] = 0;
40+
w[14] = 0;
41+
w[15] = 0x00000100;
42+
43+
SHA256ROUND(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], 0, w[0]);
44+
SHA256ROUND(s[7], s[0], s[1], s[2], s[3], s[4], s[5], s[6], 1, w[1]);
45+
SHA256ROUND(s[6], s[7], s[0], s[1], s[2], s[3], s[4], s[5], 2, w[2]);
46+
SHA256ROUND(s[5], s[6], s[7], s[0], s[1], s[2], s[3], s[4], 3, w[3]);
47+
SHA256ROUND(s[4], s[5], s[6], s[7], s[0], s[1], s[2], s[3], 4, w[4]);
48+
SHA256ROUND(s[3], s[4], s[5], s[6], s[7], s[0], s[1], s[2], 5, w[5]);
49+
SHA256ROUND(s[2], s[3], s[4], s[5], s[6], s[7], s[0], s[1], 6, w[6]);
50+
SHA256ROUND(s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[0], 7, w[7]);
51+
SHA256ROUND(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], 8, w[8]);
52+
SHA256ROUND(s[7], s[0], s[1], s[2], s[3], s[4], s[5], s[6], 9, w[9]);
53+
SHA256ROUND(s[6], s[7], s[0], s[1], s[2], s[3], s[4], s[5], 10, w[10]);
54+
SHA256ROUND(s[5], s[6], s[7], s[0], s[1], s[2], s[3], s[4], 11, w[11]);
55+
SHA256ROUND(s[4], s[5], s[6], s[7], s[0], s[1], s[2], s[3], 12, w[12]);
56+
SHA256ROUND(s[3], s[4], s[5], s[6], s[7], s[0], s[1], s[2], 13, w[13]);
57+
SHA256ROUND(s[2], s[3], s[4], s[5], s[6], s[7], s[0], s[1], 14, w[14]);
58+
SHA256ROUND(s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[0], 15, w[15]);
59+
w[16] = WSIGMA1(w[14]) + w[0] + w[9] + WSIGMA0(w[1]);
60+
SHA256ROUND(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], 16, w[16]);
61+
w[17] = WSIGMA1(w[15]) + w[1] + w[10] + WSIGMA0(w[2]);
62+
SHA256ROUND(s[7], s[0], s[1], s[2], s[3], s[4], s[5], s[6], 17, w[17]);
63+
w[18] = WSIGMA1(w[16]) + w[2] + w[11] + WSIGMA0(w[3]);
64+
SHA256ROUND(s[6], s[7], s[0], s[1], s[2], s[3], s[4], s[5], 18, w[18]);
65+
w[19] = WSIGMA1(w[17]) + w[3] + w[12] + WSIGMA0(w[4]);
66+
SHA256ROUND(s[5], s[6], s[7], s[0], s[1], s[2], s[3], s[4], 19, w[19]);
67+
w[20] = WSIGMA1(w[18]) + w[4] + w[13] + WSIGMA0(w[5]);
68+
SHA256ROUND(s[4], s[5], s[6], s[7], s[0], s[1], s[2], s[3], 20, w[20]);
69+
w[21] = WSIGMA1(w[19]) + w[5] + w[14] + WSIGMA0(w[6]);
70+
SHA256ROUND(s[3], s[4], s[5], s[6], s[7], s[0], s[1], s[2], 21, w[21]);
71+
w[22] = WSIGMA1(w[20]) + w[6] + w[15] + WSIGMA0(w[7]);
72+
SHA256ROUND(s[2], s[3], s[4], s[5], s[6], s[7], s[0], s[1], 22, w[22]);
73+
w[23] = WSIGMA1(w[21]) + w[7] + w[16] + WSIGMA0(w[8]);
74+
SHA256ROUND(s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[0], 23, w[23]);
75+
w[24] = WSIGMA1(w[22]) + w[8] + w[17] + WSIGMA0(w[9]);
76+
SHA256ROUND(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], 24, w[24]);
77+
w[25] = WSIGMA1(w[23]) + w[9] + w[18] + WSIGMA0(w[10]);
78+
SHA256ROUND(s[7], s[0], s[1], s[2], s[3], s[4], s[5], s[6], 25, w[25]);
79+
w[26] = WSIGMA1(w[24]) + w[10] + w[19] + WSIGMA0(w[11]);
80+
SHA256ROUND(s[6], s[7], s[0], s[1], s[2], s[3], s[4], s[5], 26, w[26]);
81+
w[27] = WSIGMA1(w[25]) + w[11] + w[20] + WSIGMA0(w[12]);
82+
SHA256ROUND(s[5], s[6], s[7], s[0], s[1], s[2], s[3], s[4], 27, w[27]);
83+
w[28] = WSIGMA1(w[26]) + w[12] + w[21] + WSIGMA0(w[13]);
84+
SHA256ROUND(s[4], s[5], s[6], s[7], s[0], s[1], s[2], s[3], 28, w[28]);
85+
w[29] = WSIGMA1(w[27]) + w[13] + w[22] + WSIGMA0(w[14]);
86+
SHA256ROUND(s[3], s[4], s[5], s[6], s[7], s[0], s[1], s[2], 29, w[29]);
87+
w[30] = WSIGMA1(w[28]) + w[14] + w[23] + WSIGMA0(w[15]);
88+
SHA256ROUND(s[2], s[3], s[4], s[5], s[6], s[7], s[0], s[1], 30, w[30]);
89+
w[31] = WSIGMA1(w[29]) + w[15] + w[24] + WSIGMA0(w[16]);
90+
SHA256ROUND(s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[0], 31, w[31]);
91+
w[32] = WSIGMA1(w[30]) + w[16] + w[25] + WSIGMA0(w[17]);
92+
SHA256ROUND(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], 32, w[32]);
93+
w[33] = WSIGMA1(w[31]) + w[17] + w[26] + WSIGMA0(w[18]);
94+
SHA256ROUND(s[7], s[0], s[1], s[2], s[3], s[4], s[5], s[6], 33, w[33]);
95+
w[34] = WSIGMA1(w[32]) + w[18] + w[27] + WSIGMA0(w[19]);
96+
SHA256ROUND(s[6], s[7], s[0], s[1], s[2], s[3], s[4], s[5], 34, w[34]);
97+
w[35] = WSIGMA1(w[33]) + w[19] + w[28] + WSIGMA0(w[20]);
98+
SHA256ROUND(s[5], s[6], s[7], s[0], s[1], s[2], s[3], s[4], 35, w[35]);
99+
w[36] = WSIGMA1(w[34]) + w[20] + w[29] + WSIGMA0(w[21]);
100+
SHA256ROUND(s[4], s[5], s[6], s[7], s[0], s[1], s[2], s[3], 36, w[36]);
101+
w[37] = WSIGMA1(w[35]) + w[21] + w[30] + WSIGMA0(w[22]);
102+
SHA256ROUND(s[3], s[4], s[5], s[6], s[7], s[0], s[1], s[2], 37, w[37]);
103+
w[38] = WSIGMA1(w[36]) + w[22] + w[31] + WSIGMA0(w[23]);
104+
SHA256ROUND(s[2], s[3], s[4], s[5], s[6], s[7], s[0], s[1], 38, w[38]);
105+
w[39] = WSIGMA1(w[37]) + w[23] + w[32] + WSIGMA0(w[24]);
106+
SHA256ROUND(s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[0], 39, w[39]);
107+
w[40] = WSIGMA1(w[38]) + w[24] + w[33] + WSIGMA0(w[25]);
108+
SHA256ROUND(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], 40, w[40]);
109+
w[41] = WSIGMA1(w[39]) + w[25] + w[34] + WSIGMA0(w[26]);
110+
SHA256ROUND(s[7], s[0], s[1], s[2], s[3], s[4], s[5], s[6], 41, w[41]);
111+
w[42] = WSIGMA1(w[40]) + w[26] + w[35] + WSIGMA0(w[27]);
112+
SHA256ROUND(s[6], s[7], s[0], s[1], s[2], s[3], s[4], s[5], 42, w[42]);
113+
w[43] = WSIGMA1(w[41]) + w[27] + w[36] + WSIGMA0(w[28]);
114+
SHA256ROUND(s[5], s[6], s[7], s[0], s[1], s[2], s[3], s[4], 43, w[43]);
115+
w[44] = WSIGMA1(w[42]) + w[28] + w[37] + WSIGMA0(w[29]);
116+
SHA256ROUND(s[4], s[5], s[6], s[7], s[0], s[1], s[2], s[3], 44, w[44]);
117+
w[45] = WSIGMA1(w[43]) + w[29] + w[38] + WSIGMA0(w[30]);
118+
SHA256ROUND(s[3], s[4], s[5], s[6], s[7], s[0], s[1], s[2], 45, w[45]);
119+
w[46] = WSIGMA1(w[44]) + w[30] + w[39] + WSIGMA0(w[31]);
120+
SHA256ROUND(s[2], s[3], s[4], s[5], s[6], s[7], s[0], s[1], 46, w[46]);
121+
w[47] = WSIGMA1(w[45]) + w[31] + w[40] + WSIGMA0(w[32]);
122+
SHA256ROUND(s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[0], 47, w[47]);
123+
w[48] = WSIGMA1(w[46]) + w[32] + w[41] + WSIGMA0(w[33]);
124+
SHA256ROUND(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], 48, w[48]);
125+
w[49] = WSIGMA1(w[47]) + w[33] + w[42] + WSIGMA0(w[34]);
126+
SHA256ROUND(s[7], s[0], s[1], s[2], s[3], s[4], s[5], s[6], 49, w[49]);
127+
w[50] = WSIGMA1(w[48]) + w[34] + w[43] + WSIGMA0(w[35]);
128+
SHA256ROUND(s[6], s[7], s[0], s[1], s[2], s[3], s[4], s[5], 50, w[50]);
129+
w[51] = WSIGMA1(w[49]) + w[35] + w[44] + WSIGMA0(w[36]);
130+
SHA256ROUND(s[5], s[6], s[7], s[0], s[1], s[2], s[3], s[4], 51, w[51]);
131+
w[52] = WSIGMA1(w[50]) + w[36] + w[45] + WSIGMA0(w[37]);
132+
SHA256ROUND(s[4], s[5], s[6], s[7], s[0], s[1], s[2], s[3], 52, w[52]);
133+
w[53] = WSIGMA1(w[51]) + w[37] + w[46] + WSIGMA0(w[38]);
134+
SHA256ROUND(s[3], s[4], s[5], s[6], s[7], s[0], s[1], s[2], 53, w[53]);
135+
w[54] = WSIGMA1(w[52]) + w[38] + w[47] + WSIGMA0(w[39]);
136+
SHA256ROUND(s[2], s[3], s[4], s[5], s[6], s[7], s[0], s[1], 54, w[54]);
137+
w[55] = WSIGMA1(w[53]) + w[39] + w[48] + WSIGMA0(w[40]);
138+
SHA256ROUND(s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[0], 55, w[55]);
139+
w[56] = WSIGMA1(w[54]) + w[40] + w[49] + WSIGMA0(w[41]);
140+
SHA256ROUND(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], 56, w[56]);
141+
w[57] = WSIGMA1(w[55]) + w[41] + w[50] + WSIGMA0(w[42]);
142+
SHA256ROUND(s[7], s[0], s[1], s[2], s[3], s[4], s[5], s[6], 57, w[57]);
143+
w[58] = WSIGMA1(w[56]) + w[42] + w[51] + WSIGMA0(w[43]);
144+
SHA256ROUND(s[6], s[7], s[0], s[1], s[2], s[3], s[4], s[5], 58, w[58]);
145+
w[59] = WSIGMA1(w[57]) + w[43] + w[52] + WSIGMA0(w[44]);
146+
SHA256ROUND(s[5], s[6], s[7], s[0], s[1], s[2], s[3], s[4], 59, w[59]);
147+
w[60] = WSIGMA1(w[58]) + w[44] + w[53] + WSIGMA0(w[45]);
148+
SHA256ROUND(s[4], s[5], s[6], s[7], s[0], s[1], s[2], s[3], 60, w[60]);
149+
w[61] = WSIGMA1(w[59]) + w[45] + w[54] + WSIGMA0(w[46]);
150+
SHA256ROUND(s[3], s[4], s[5], s[6], s[7], s[0], s[1], s[2], 61, w[61]);
151+
w[62] = WSIGMA1(w[60]) + w[46] + w[55] + WSIGMA0(w[47]);
152+
SHA256ROUND(s[2], s[3], s[4], s[5], s[6], s[7], s[0], s[1], 62, w[62]);
153+
w[63] = WSIGMA1(w[61]) + w[47] + w[56] + WSIGMA0(w[48]);
154+
SHA256ROUND(s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[0], 63, w[63]);
155+
156+
// Feed Forward
157+
s[0] = s[0] + 0x6a09e667;
158+
s[1] = s[1] + 0xbb67ae85;
159+
s[2] = s[2] + 0x3c6ef372;
160+
s[3] = s[3] + 0xa54ff53a;
161+
s[4] = s[4] + 0x510e527f;
162+
s[5] = s[5] + 0x9b05688c;
163+
s[6] = s[6] + 0x1f83d9ab;
164+
s[7] = s[7] + 0x5be0cd19;
165+
166+
// Store Hash value
167+
for (int k = 0; k < 8; k++) {
168+
w[k] = s[k];
169+
}
170+
}
171+
172+
// Store Hash value
173+
for (int j = 0; j < SHA256_BLOCK_SIZE / 4; j++) {
174+
hash[j * 4 + 3] = s[j] & 0xff;
175+
hash[j * 4 + 2] = (s[j] >> 8) & 0xff;
176+
hash[j * 4 + 1] = (s[j] >> 16) & 0xff;
177+
hash[j * 4 + 0] = (s[j] >> 24) & 0xff;
178+
}
179+
}
180+
}
181+
}

src/poh-simd/sha256.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Adapted from kste's sha256 implementation, accessible at https://github.com/kste/sha256_avx
3+
*
4+
* MIT License
5+
*
6+
* Copyright (c) 2017
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
27+
#ifndef SHA256_H
28+
#define SHA256_H
29+
30+
#define u32 unsigned int32
31+
#define u8 unsigned int8
32+
33+
#define SHA256_BLOCK_SIZE 32
34+
35+
static const u32 RC[] = {
36+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
37+
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
38+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
39+
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
40+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
41+
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
42+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
43+
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
44+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
45+
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
46+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
47+
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
48+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
49+
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
50+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
51+
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
52+
};
53+
54+
#define MAJ(a, b, c) ((a&b) ^ (a&c) ^ (b&c))
55+
#define CH(a, b, c) ((a&b) ^ (~(a)&c))
56+
57+
#define ROTR32(x, r) ((x >> r) | (x << (SHA256_BLOCK_SIZE - r)))
58+
59+
#define SIGMA1(x) (ROTR32(x, 6) ^ ROTR32(x, 11) ^ ROTR32(x, 25))
60+
#define SIGMA0(x) (ROTR32(x, 2) ^ ROTR32(x, 13) ^ ROTR32(x, 22))
61+
62+
#define WSIGMA1(x) (ROTR32(x, 17) ^ ROTR32(x, 19) ^ (x >> 10))
63+
#define WSIGMA0(x) (ROTR32(x, 7) ^ ROTR32(x, 18) ^ (x >> 3))
64+
65+
#define SHA256ROUND(a, b, c, d, e, f, g, h, rc, w) \
66+
T0 = h + SIGMA1(e) + CH(e, f, g) + RC[rc] + w; \
67+
d = d + T0; \
68+
T1 = SIGMA0(a) + MAJ(a, b, c); \
69+
h = T0 + T1;
70+
71+
#endif

0 commit comments

Comments
 (0)