Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions opt-lib/sha256/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/main
/*.o
33 changes: 33 additions & 0 deletions opt-lib/sha256/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
CC = clang-19
LD = ld.lld-19

# -flto enables Link Time Optimization for better cross-module optimization (~4% cycle reduction)
# May increase compile time and can occasionally cause issues with specific toolchain configurations

CFLAGS = --target=riscv64 -march=rv64imc_zba_zbb_zbc_zbs -flto \
-O3 -g \
-nodefaultlibs \
-isystem ../../deps/musl/release/include \
-fdata-sections -ffunction-sections

LDFLAGS = -static --gc-sections -L../../deps/musl/release/lib -L../../deps/compiler-rt-builtins-riscv/build \
-lc -lgcc -lcompiler-rt

%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<

all: main.o sha256.o
$(LD) $(LDFLAGS) -o main $^
llvm-strip -s main

run:
ckb-debugger --bin main

asm: sha256.o
llvm-objdump-19 -d -S sha256.o

fmt:
clang-format --style="{BasedOnStyle: Google, IndentWidth: 4}" -i *.c *.h

clean:
rm -f main.o sha256.o main
9 changes: 9 additions & 0 deletions opt-lib/sha256/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Setup

```sh
$ cd $ROOT/deps/musl
$ CLANG=clang-19 bash ckb/build.sh

$ cd $ROOT/deps/compiler-rt-builtins-riscv
$ make CC=clang-19 AR=llvm-ar-19
```
199 changes: 199 additions & 0 deletions opt-lib/sha256/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
#include <stdio.h>
#include <string.h>

#include "sha256.h"

int test_ax1000000() {
SHA256_CTX ctx;
SHA256_BYTE hash[32];
const char *message = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

sha256_init(&ctx);
for (int i = 0; i < 25000; i++) {
sha256_update(&ctx, (const SHA256_BYTE *)message, strlen(message));
}
sha256_final(&ctx, hash);

SHA256_BYTE want[32] = {
0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92, 0x81, 0xa1, 0xc7,
0xe2, 0x84, 0xd7, 0x3e, 0x67, 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97,
0x20, 0x0e, 0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0,
};
for (int i = 0; i < 32; i++) {
if (hash[i] != want[i]) {
return 1;
}
}
return 0;
}

int test_chain() {
SHA256_BYTE hash[32] = {};
for (int i = 0; i < 25000; i++) {
SHA256_CTX ctx;
sha256_init(&ctx);
sha256_update(&ctx, hash, 32);
sha256_final(&ctx, hash);
}
SHA256_BYTE want[32] = {
0xde, 0x49, 0x2c, 0x0e, 0x28, 0xd3, 0x77, 0x06, 0xda, 0xc0, 0x48,
0x97, 0xe9, 0xc8, 0x25, 0xee, 0x01, 0x4b, 0x1e, 0x85, 0xbc, 0x27,
0x7b, 0x3c, 0xdd, 0xbd, 0x9a, 0x56, 0x64, 0x51, 0xd7, 0xb6,
};
for (int i = 0; i < 32; i++) {
if (hash[i] != want[i]) {
return 1;
}
}
return 0;
}

int test_empty() {
SHA256_CTX ctx;
SHA256_BYTE hash[32];
const char *message = "";

sha256_init(&ctx);
sha256_update(&ctx, (const SHA256_BYTE *)message, strlen(message));
sha256_final(&ctx, hash);

SHA256_BYTE want[32] = {
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4,
0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b,
0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
};
for (int i = 0; i < 32; i++) {
if (hash[i] != want[i]) {
return 1;
}
}
return 0;
}

int test_hello_world() {
SHA256_CTX ctx;
SHA256_BYTE hash[32];
const char *message = "Hello, World!";

sha256_init(&ctx);
sha256_update(&ctx, (const SHA256_BYTE *)message, strlen(message));
sha256_final(&ctx, hash);

SHA256_BYTE want[32] = {
0xdf, 0xfd, 0x60, 0x21, 0xbb, 0x2b, 0xd5, 0xb0, 0xaf, 0x67, 0x62,
0x90, 0x80, 0x9e, 0xc3, 0xa5, 0x31, 0x91, 0xdd, 0x81, 0xc7, 0xf7,
0x0a, 0x4b, 0x28, 0x68, 0x8a, 0x36, 0x21, 0x82, 0x98, 0x6f,
};
for (int i = 0; i < 32; i++) {
if (hash[i] != want[i]) {
return 1;
}
}
return 0;
}

int test_len_57() {
SHA256_CTX ctx;
SHA256_BYTE hash[32];
const SHA256_BYTE message[57] = {
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
};

sha256_init(&ctx);
sha256_update(&ctx, (const SHA256_BYTE *)message, 57);
sha256_final(&ctx, hash);

SHA256_BYTE want[32] = {
0xb6, 0xd6, 0xbc, 0xbe, 0x1a, 0xca, 0x25, 0x0c, 0xc8, 0x44, 0xa9,
0xc5, 0x63, 0x3a, 0xb9, 0x0c, 0x3f, 0x74, 0xf5, 0x3e, 0xc3, 0xcd,
0xa5, 0x95, 0x0a, 0x8d, 0x9a, 0x4a, 0x77, 0xb0, 0x86, 0xb4,
};
for (int i = 0; i < 32; i++) {
if (hash[i] != want[i]) {
return 1;
}
}
return 0;
}

int test_rc4_16() {
SHA256_CTX ctx;
SHA256_BYTE hash[32];
const SHA256_BYTE message[16] = {
0xde, 0x18, 0x89, 0x41, 0xa3, 0x37, 0x5d, 0x3a,
0x8a, 0x06, 0x1e, 0x67, 0x57, 0x6e, 0x92, 0x6d,
};

sha256_init(&ctx);
sha256_update(&ctx, (const SHA256_BYTE *)message, 16);
sha256_final(&ctx, hash);

SHA256_BYTE want[32] = {
0x06, 0x7c, 0x53, 0x12, 0x69, 0x73, 0x5c, 0xa7, 0xf5, 0x41, 0xfd,
0xac, 0xa8, 0xf0, 0xdc, 0x76, 0x30, 0x5d, 0x3c, 0xad, 0xa1, 0x40,
0xf8, 0x93, 0x72, 0xa4, 0x10, 0xfe, 0x5e, 0xff, 0x6e, 0x4d,
};
for (int i = 0; i < 32; i++) {
if (hash[i] != want[i]) {
return 1;
}
}
return 0;
}

int test_rc4_55() {
SHA256_CTX ctx;
SHA256_BYTE hash[32];
const SHA256_BYTE message[55] = {
0xde, 0x18, 0x89, 0x41, 0xa3, 0x37, 0x5d, 0x3a, 0x8a, 0x06, 0x1e,
0x67, 0x57, 0x6e, 0x92, 0x6d, 0xc7, 0x1a, 0x7f, 0xa3, 0xf0, 0xcc,
0xeb, 0x97, 0x45, 0x2b, 0x4d, 0x32, 0x27, 0x96, 0x5f, 0x9e, 0xa8,
0xcc, 0x75, 0x07, 0x6d, 0x9f, 0xb9, 0xc5, 0x41, 0x7a, 0xa5, 0xcb,
0x30, 0xfc, 0x22, 0x19, 0x8b, 0x34, 0x98, 0x2d, 0xbb, 0x62, 0x9e,
};

sha256_init(&ctx);
sha256_update(&ctx, (const SHA256_BYTE *)message, 55);
sha256_final(&ctx, hash);

SHA256_BYTE want[32] = {
0x03, 0x80, 0x51, 0xe9, 0xc3, 0x24, 0x39, 0x3b, 0xd1, 0xca, 0x19,
0x78, 0xdd, 0x09, 0x52, 0xc2, 0xaa, 0x37, 0x42, 0xca, 0x4f, 0x1b,
0xd5, 0xcd, 0x46, 0x11, 0xce, 0xa8, 0x38, 0x92, 0xd3, 0x82,
};
for (int i = 0; i < 32; i++) {
if (hash[i] != want[i]) {
return 1;
}
}
return 0;
}

int main() {
if (test_ax1000000() != 0) {
return 1;
}
if (test_chain() != 0) {
return 1;
}
if (test_empty() != 0) {
return 1;
}
if (test_hello_world() != 0) {
return 1;
}
if (test_len_57() != 0) {
return 1;
}
if (test_rc4_16() != 0) {
return 1;
}
if (test_rc4_55() != 0) {
return 1;
}
return 0;
}
Loading