Skip to content

Commit 9e02bb4

Browse files
committed
Fix existing tests so CI can resume running
1 parent dd5766a commit 9e02bb4

File tree

29 files changed

+528
-348
lines changed

29 files changed

+528
-348
lines changed

.github/workflows/rust-with-clang.yml

Lines changed: 0 additions & 17 deletions
This file was deleted.

.github/workflows/rust.yml

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@ on: [push, pull_request]
55
jobs:
66
build:
77

8-
runs-on: ubuntu-latest
8+
strategy:
9+
matrix:
10+
os: [ubuntu-latest, ubuntu-24.04-arm]
11+
12+
runs-on: ${{ matrix.os }}
913

1014
steps:
11-
- uses: actions/checkout@v1
12-
- name: Update submodules
13-
run: git submodule update --init
14-
- name: Run sphincs+ tests
15-
run: ./tests/sphincsplus/all_run.sh
16-
- name: Run sphincs+ rust tests
17-
run: ./tests/sphincsplus_rust/run_rust.sh
18-
- name: Run Tools tests
19-
run: cd tools/ckb-sphincs-tools && cargo test
15+
- uses: actions/checkout@v4
16+
with:
17+
submodules: "true"
18+
- name: Install llvm
19+
run: sudo apt update && sudo apt install -y clang llvm clang-format lld
20+
- uses: actions-rust-lang/setup-rust-toolchain@v1
21+
- name: Prepare, build, test, clippy, format
22+
run: make prepare build test clippy fmt
23+
- name: git diff
24+
run: git diff --exit-code

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# We cannot use $(shell pwd), which will return unix path format on Windows,
22
# making it hard to use.
3-
cur_dir = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
3+
cur_dir = $(dir $(abspath $(firstword $(MAKEFILE_LIST))))
44

55
TOP := $(cur_dir)
66
# RUSTFLAGS that are likely to be tweaked by developers. For example,
@@ -51,6 +51,9 @@ build:
5151
for contract in $(wildcard contracts/*); do \
5252
$(MAKE) -e -C $$contract build; \
5353
done; \
54+
for crate in $(wildcard tools/*); do \
55+
cargo build -p $$(basename $$crate | tr '-' '_') $(MODE_ARGS) $(CARGO_ARGS); \
56+
done; \
5457
else \
5558
$(MAKE) -e -C contracts/$(CONTRACT) build; \
5659
cargo build -p $(CONTRACT)-sim; \
@@ -63,8 +66,9 @@ TASK :=
6366
run:
6467
$(MAKE) -e -C contracts/$(CONTRACT) $(TASK)
6568

66-
test:
69+
test:
6770
bash tests/sphincsplus/all_run.sh
71+
bash tests/sphincsplus_rust/all_run.sh
6872
# cargo test $(CARGO_ARGS)
6973

7074
# check, clippy and fmt here are provided for completeness,

build-tools/build-params-finder/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn main() {
1818
.map(|line| {
1919
let parts: Vec<&str> = line.split(" ").collect();
2020
(
21-
usize::from_str_radix(parts[0], 10).expect("parse number"),
21+
parts[0].parse::<usize>().expect("parse number"),
2222
parts[1].to_string(),
2323
)
2424
})

build-tools/script-merge-tool/src/main.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,31 +88,31 @@ fn main() {
8888
writeln!(
8989
&mut file,
9090
"extern const uint32_t {};",
91-
offset_sname(&leaf_name, &cli.prefix)
91+
offset_sname(leaf_name, &cli.prefix)
9292
)
9393
.expect("write");
9494
writeln!(
9595
&mut file,
9696
"extern const uint32_t {};",
97-
length_sname(&leaf_name, &cli.prefix)
97+
length_sname(leaf_name, &cli.prefix)
9898
)
9999
.expect("write");
100100
}
101-
writeln!(&mut file, "").expect("write");
101+
writeln!(&mut file).expect("write");
102102

103103
// Build actual definitions, but under a ifdef guard
104104
writeln!(&mut file, "#ifdef CKB_SCRIPT_MERGE_TOOL_DEFINE_VARS").expect("write");
105105
for leaf_name in &leaf_names {
106106
writeln!(
107107
&mut file,
108108
"__attribute__ ((visibility (\"default\"))) const uint32_t {} = 0xFFFFFFFF;",
109-
offset_sname(&leaf_name, &cli.prefix)
109+
offset_sname(leaf_name, &cli.prefix)
110110
)
111111
.expect("write");
112112
writeln!(
113113
&mut file,
114114
"__attribute__ ((visibility (\"default\"))) const uint32_t {} = 1;",
115-
length_sname(&leaf_name, &cli.prefix)
115+
length_sname(leaf_name, &cli.prefix)
116116
)
117117
.expect("write");
118118
}
@@ -139,7 +139,7 @@ fn main() {
139139
length_sname(&leaf_name, &cli.prefix)
140140
)
141141
.expect("write");
142-
writeln!(&mut file, "").expect("write");
142+
writeln!(&mut file).expect("write");
143143
}
144144
}
145145
Commands::Merge {

contracts/c-sphincs-all-in-one-lock/Makefile

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# We cannot use $(shell pwd), which will return unix path format on Windows,
22
# making it hard to use.
3-
cur_dir = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
3+
cur_dir = $(dir $(abspath $(firstword $(MAKEFILE_LIST))))
44

55
TOP := $(cur_dir)
66
MODE := release
@@ -27,8 +27,13 @@ CLANG_FORMAT := $(subst clang,clang-format,$(CLANG))
2727

2828
# Local build directory
2929
CUR_BUILD := $(cur_dir)/build
30+
# FIPS 205 only approves simple THASH type
31+
THASH = simple
32+
LOCK_DIR := $(cur_dir)
3033
SPHINCS_PLUS_DIR := $(TOP)/deps/sphincsplus/ref
3134

35+
include $(TOP)/mk/files.mk
36+
3237
PARAMS_DEFINITION_FILE := $(TOP)/params.txt
3338
ALL_PARAM_IDS := $(shell cat $(PARAMS_DEFINITION_FILE) | cut -d" " -f1)
3439

@@ -50,34 +55,6 @@ CFLAGS += -I $(SPHINCS_PLUS_DIR) -I $(CUR_BUILD) -I $(cur_dir)/utils
5055

5156
LDFLAGS := -static -Wl,--gc-sections
5257

53-
SOURCES = \
54-
$(SPHINCS_PLUS_DIR)/address.c \
55-
$(SPHINCS_PLUS_DIR)/merkle.c \
56-
$(SPHINCS_PLUS_DIR)/wots.c \
57-
$(SPHINCS_PLUS_DIR)/wotsx1.c \
58-
$(SPHINCS_PLUS_DIR)/utils.c \
59-
$(SPHINCS_PLUS_DIR)/utilsx1.c \
60-
$(SPHINCS_PLUS_DIR)/fors.c \
61-
$(SPHINCS_PLUS_DIR)/sign.c \
62-
ckb-sphincsplus.c
63-
64-
HEADERS = \
65-
$(SPHINCS_PLUS_DIR)/params.h \
66-
$(SPHINCS_PLUS_DIR)/address.h \
67-
$(SPHINCS_PLUS_DIR)/merkle.h \
68-
$(SPHINCS_PLUS_DIR)/wots.h \
69-
$(SPHINCS_PLUS_DIR)/wotsx1.h \
70-
$(SPHINCS_PLUS_DIR)/utils.h \
71-
$(SPHINCS_PLUS_DIR)/utilsx1.h \
72-
$(SPHINCS_PLUS_DIR)/fors.h \
73-
$(SPHINCS_PLUS_DIR)/api.h \
74-
$(SPHINCS_PLUS_DIR)/hash.h \
75-
$(SPHINCS_PLUS_DIR)/thash.h \
76-
$(SPHINCS_PLUS_DIR)/randombytes.h \
77-
ckb-sphincsplus.h \
78-
ckb-sphincsplus-common.h \
79-
$(wildcard utils/*.h)
80-
8158
default: build
8259

8360
build: $(CUR_BUILD)/$(BINARY)
@@ -111,25 +88,23 @@ $(CUR_BUILD)/leaf-vars.h: $(CUR_BUILD)/leaf.complete
11188
--language c \
11289
--output $@
11390

114-
$(CUR_BUILD)/leaf.complete: $(SOURCES) $(HEADERS) ckb-sphincsplus-leaf-lock.c
91+
$(CUR_BUILD)/leaf.complete: $(DETECTING_SOURCES) $(HEADERS) ckb-sphincsplus-leaf-lock.c
11592
mkdir -p $(CUR_BUILD)
11693
rm -rf $(CUR_BUILD)/leaves_debug $(CUR_BUILD)/leaves_stripped
11794
mkdir -p $(CUR_BUILD)/leaves_debug $(CUR_BUILD)/leaves_stripped
11895
@set -eu; \
11996
for param_id in $(ALL_PARAM_IDS); do \
12097
param=$$(head -n $$param_id $(PARAMS_DEFINITION_FILE) | tail -1 | cut -d" " -f2); \
12198
if echo "$${param}" | grep "shake" - > /dev/null; then \
122-
others="$(SPHINCS_PLUS_DIR)/fips202.c $(SPHINCS_PLUS_DIR)/hash_shake.c"; \
123-
others="$${others} $(SPHINCS_PLUS_DIR)/thash_shake_simple.c"; \
99+
srcs="$(COMPILING_COMMON_SOURCES) $(COMPILING_SHAKE_SOURCES)"; \
124100
elif echo "$${param}" | grep "sha2" - > /dev/null; then \
125-
others="$(SPHINCS_PLUS_DIR)/sha2.c $(SPHINCS_PLUS_DIR)/hash_sha2.c"; \
126-
others="$${others} $(SPHINCS_PLUS_DIR)/thash_sha2_simple.c"; \
101+
srcs="$(COMPILING_COMMON_SOURCES) $(COMPILING_SHA2_SOURCES)"; \
127102
fi; \
128103
echo "Building sphincs+ lock with params id: $${param_id}, params: $${param}"; \
129104
set -x; \
130105
$(CLANG) $(CFLAGS) $(LDFLAGS) -DPARAMS_ID=$${param_id} -DPARAMS=$${param} \
131106
-o $(CUR_BUILD)/leaves_debug/$${param} \
132-
$(SOURCES) $${others} ckb-sphincsplus-leaf-lock.c; \
107+
$${srcs} ckb-sphincsplus-leaf-lock.c; \
133108
$(OBJCOPY) --strip-debug --strip-all \
134109
$(CUR_BUILD)/leaves_debug/$${param} \
135110
$(CUR_BUILD)/leaves_stripped/$${param}; \

contracts/c-sphincs-all-in-one-lock/ckb-sphincsplus-common.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,18 @@
2020
#endif
2121

2222
#undef ASSERT
23-
// #define ASSERT(s)
24-
#define ASSERT(s) ckb_exit(-1)
23+
#define ASSERT(s) \
24+
do { \
25+
if (!(s)) { \
26+
ckb_exit(ERROR_SPHINCSPLUS_UNEXPECTED); \
27+
} \
28+
} while (0)
2529

2630
#undef CHECK2
2731
#define CHECK2(cond, code) \
2832
do { \
2933
if (!(cond)) { \
3034
err = code; \
31-
ASSERT(0); \
3235
goto exit; \
3336
} \
3437
} while (0)
@@ -39,7 +42,6 @@
3942
int code = (_code); \
4043
if (code != 0) { \
4144
err = code; \
42-
ASSERT(0); \
4345
goto exit; \
4446
} \
4547
} while (0)

contracts/c-sphincs-all-in-one-lock/ckb-sphincsplus-leaf-lock.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
/* We only use molecule-c2's cursor here for minimal code size. */
3232
#include "witness_args_lazy_utils.h"
3333

34-
#define ITERATION_SIZE \
35-
(1 + sphincs_plus_get_pk_size() + sphincs_plus_get_sign_size())
34+
#define ITERATION_SIZE (1 + SPHINCS_PLUS_PK_SIZE + SPHINCS_PLUS_SIGN_SIZE)
3635
#define MAX_BATCH_BUFFER_SIZE (256 * 1024)
3736
#define BATCH_COUNT (MAX_BATCH_BUFFER_SIZE / ITERATION_SIZE)
3837
#define BATCH_BUFFER_SIZE (ITERATION_SIZE * BATCH_COUNT)
@@ -82,25 +81,23 @@ int handle_exec(const uint8_t *command, size_t command_length) {
8281

8382
if ((param_id & MULTISIG_SIG_MASK) != 0) {
8483
/* Validate a signature when we see one */
85-
CHECK2(cursor.size >=
86-
sphincs_plus_get_pk_size() + sphincs_plus_get_sign_size(),
84+
CHECK2(cursor.size >= SPHINCS_PLUS_PK_SIZE + SPHINCS_PLUS_SIGN_SIZE,
8785
ERROR_SPHINCSPLUS_WITNESS);
8886

89-
uint8_t pubkey[sphincs_plus_get_pk_size()];
90-
uint8_t sign[sphincs_plus_get_sign_size()];
87+
uint8_t pubkey[SPHINCS_PLUS_PK_SIZE];
88+
uint8_t sign[SPHINCS_PLUS_SIGN_SIZE];
9189

92-
CHECK(mol2_read_and_advance(&cursor, pubkey, sphincs_plus_get_pk_size()));
93-
CHECK(mol2_read_and_advance(&cursor, sign, sphincs_plus_get_sign_size()));
90+
CHECK(mol2_read_and_advance(&cursor, pubkey, SPHINCS_PLUS_PK_SIZE));
91+
CHECK(mol2_read_and_advance(&cursor, sign, SPHINCS_PLUS_SIGN_SIZE));
9492

95-
err = sphincs_plus_verify(sign, sphincs_plus_get_sign_size(), message,
96-
BLAKE2B_BLOCK_SIZE, pubkey,
97-
sphincs_plus_get_pk_size());
93+
err =
94+
sphincs_plus_verify(sign, SPHINCS_PLUS_SIGN_SIZE, message,
95+
BLAKE2B_BLOCK_SIZE, pubkey, SPHINCS_PLUS_PK_SIZE);
9896
CHECK2(err == 0, ERROR_SPHINCSPLUS_VERIFY);
9997
} else {
10098
/* Skip pubkey without a signature */
101-
CHECK2(cursor.size >= sphincs_plus_get_pk_size(),
102-
ERROR_SPHINCSPLUS_WITNESS);
103-
mol2_advance(&cursor, sphincs_plus_get_pk_size());
99+
CHECK2(cursor.size >= SPHINCS_PLUS_PK_SIZE, ERROR_SPHINCSPLUS_WITNESS);
100+
mol2_advance(&cursor, SPHINCS_PLUS_PK_SIZE);
104101
}
105102
}
106103

contracts/c-sphincs-all-in-one-lock/ckb-sphincsplus.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,33 @@ int sphincs_plus_generate_keypair(uint8_t *pk, uint8_t *sk) {
3535

3636
int sphincs_plus_sign(const uint8_t *message, const uint8_t *sk,
3737
uint8_t *out_sign) {
38-
unsigned long long out_sign_len = sphincs_plus_get_sign_size();
38+
unsigned long long out_sign_len = SPHINCS_PLUS_SIGN_SIZE;
3939
int ret = crypto_sign(out_sign, (unsigned long long *)&out_sign_len, message,
4040
SPX_MLEN, sk);
41-
if ((uint32_t)out_sign_len != sphincs_plus_get_sign_size()) {
41+
if ((uint32_t)out_sign_len != SPHINCS_PLUS_SIGN_SIZE) {
4242
return 1;
4343
}
4444
return ret;
4545
}
4646

47+
/* Defined for Rust FFI usage, script code would only require the macros */
48+
uint32_t sphincs_plus_get_pk_size() { return SPHINCS_PLUS_PK_SIZE; }
49+
uint32_t sphincs_plus_get_sk_size() { return SPHINCS_PLUS_SK_SIZE; }
50+
uint32_t sphincs_plus_get_sign_size() { return SPHINCS_PLUS_SIGN_SIZE; }
51+
4752
#endif // CKB_VM
4853

4954
int sphincs_plus_verify(const uint8_t *sign, uint32_t sign_size,
5055
const uint8_t *message, uint32_t message_size,
5156
const uint8_t *pubkey, uint32_t pubkey_size) {
52-
size_t sign_len = sphincs_plus_get_sign_size();
53-
54-
if (sign_size != sign_len || message_size != SPX_MLEN ||
55-
pubkey_size != sphincs_plus_get_pk_size()) {
57+
if (sign_size != SPHINCS_PLUS_SIGN_SIZE || message_size != SPX_MLEN ||
58+
pubkey_size != SPHINCS_PLUS_PK_SIZE) {
5659
return SphincsPlusError_Params;
5760
}
5861
unsigned char mout[SPX_BYTES + SPX_MLEN];
5962
unsigned long long mlen = 0;
6063

61-
int err = crypto_sign_open(mout, &mlen, sign, sign_len, pubkey);
64+
int err = crypto_sign_open(mout, &mlen, sign, SPHINCS_PLUS_SIGN_SIZE, pubkey);
6265
if (err != 0) {
6366
return SphincsPlusError_Verify;
6467
}

0 commit comments

Comments
 (0)