Skip to content

Commit 0ca365d

Browse files
committed
The hash type is passed by parameter replace of existing global variable
1 parent 88baa0a commit 0ca365d

40 files changed

+751
-633
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LD := $(TARGET)gcc
66
CFLAGS := -fPIC -O3 -fno-builtin-printf -fno-builtin-memcmp -nostdinc -nostartfiles -fvisibility=hidden -fdata-sections -ffunction-sections -nostdlib -Wno-nonnull-compare -DCKB_VM -DCKB_DECLARATION_ONLY
77
LDFLAGS := -fdata-sections -ffunction-sections
88

9-
CFLAGS := $(CFLAGS) -Wall -Werror -Wno-nonnull -Wno-unused-function -g
9+
CFLAGS := $(CFLAGS) -Wall -Werror -Wno-nonnull -Wno-unused-function
1010
LDFLAGS := $(LDFLAGS) -Wl,-static -Wl,--gc-sections
1111

1212
CFLAGS := $(CFLAGS) -I c -I deps/ckb-c-stdlib/libc -I deps/ckb-c-stdlib -I deps/ckb-c-stdlib/molecule
@@ -69,7 +69,7 @@ SOURCES += \
6969
HEADERS += \
7070
c/$(SOURCES_DIR)/haraka.h
7171

72-
CFLAGS := $(CFLAGS) -DCKB_C_STDLIB_PRINTF
72+
CFLAGS := $(CFLAGS) -g -DCKB_C_STDLIB_PRINTF
7373

7474
# docker pull nervos/ckb-riscv-gnu-toolchain:gnu-bionic-20191012
7575
BUILDER_DOCKER := nervos/ckb-riscv-gnu-toolchain@sha256:aae8a3f79705f67d505d1f1d5ddc694a4fd537ed1c7e9622420a470d59ba2ec3

c/ckb-sphincsplus-lock.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ int make_witness(WitnessArgsType *witness) {
226226
return 0;
227227
}
228228

229-
int get_sign(uint8_t *sign) {
229+
int get_sign(uint8_t *sign, crypto_context *cctx) {
230230
int err = CKB_SUCCESS;
231-
size_t sign_size = sphincs_plus_get_sign_size();
231+
size_t sign_size = sphincs_plus_get_sign_size(cctx);
232232
WitnessArgsType witness_args;
233233

234234
uint8_t witness_data_source[MAX_WITNESS_SIZE] = {0};
@@ -252,7 +252,7 @@ int get_sign(uint8_t *sign) {
252252
// | hash type 4 bytes | public key |
253253
//
254254
// note: different hash types have different public key lengths
255-
int get_public_key(uint8_t *pub_key) {
255+
int get_public_key(uint8_t *pub_key, crypto_context *cctx) {
256256
int err = CKB_SUCCESS;
257257

258258
uint8_t script[SCRIPT_SIZE];
@@ -269,9 +269,10 @@ int get_public_key(uint8_t *pub_key) {
269269
CHECK2((args_bytes_seg.size > sizeof(uint32_t)), ERROR_SPHINCSPLUS_ARGS);
270270
uint32_t hash_type = 0;
271271
memcpy(&hash_type, args_bytes_seg.ptr, sizeof(uint32_t));
272-
CHECK2(!sphincs_plus_init(hash_type), ERROR_SPHINCSPLUS_HASH_TYPE);
272+
CHECK2(!sphincs_plus_init_context(hash_type, cctx),
273+
ERROR_SPHINCSPLUS_HASH_TYPE);
273274

274-
size_t pubkey_size = sphincs_plus_get_pk_size();
275+
size_t pubkey_size = sphincs_plus_get_pk_size(cctx);
275276
CHECK2((args_bytes_seg.size == sizeof(uint32_t)) + pubkey_size,
276277
ERROR_SPHINCSPLUS_ARGS);
277278
memcpy(pub_key, args_bytes_seg.ptr + sizeof(uint32_t), pubkey_size);
@@ -285,19 +286,20 @@ int main() {
285286

286287
// signature data size depends on args data(hash type)
287288
uint8_t pubkey[SPHINCSPLUS_PUBKEY_MAX_SIZE];
288-
err = get_public_key(pubkey);
289+
crypto_context cctx = {0};
290+
err = get_public_key(pubkey, &cctx);
289291
if (err) {
290292
return err;
291293
}
292294

293295
uint8_t message[BLAKE2B_BLOCK_SIZE];
294-
uint8_t sign[sphincs_plus_get_sign_size()];
296+
uint8_t sign[sphincs_plus_get_sign_size(&cctx)];
295297
CHECK(generate_sighash_all(message, BLAKE2B_BLOCK_SIZE));
296298

297-
CHECK(get_sign(sign));
298-
err = sphincs_plus_verify(sign, sphincs_plus_get_sign_size(), message,
299-
BLAKE2B_BLOCK_SIZE, pubkey,
300-
sphincs_plus_get_pk_size());
299+
CHECK(get_sign(sign, &cctx));
300+
err = sphincs_plus_verify(&cctx, sign, sphincs_plus_get_sign_size(&cctx),
301+
message, BLAKE2B_BLOCK_SIZE, pubkey,
302+
sphincs_plus_get_pk_size(&cctx));
301303
CHECK2(err == 0, ERROR_SPHINCSPLUS_VERIFY);
302304

303305
exit:

c/ckb-sphincsplus.c

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,45 +20,73 @@ enum SphincsPlusError {
2020
SphincsPlusError_Verify,
2121
SphincsPlusError_Verify_MsgLen,
2222
SphincsPlusError_Verify_MsgCmp,
23-
2423
};
2524

26-
uint32_t sphincs_plus_get_pk_size() { return SPX_PK_BYTES; }
27-
uint32_t sphincs_plus_get_sk_size() { return SPX_SK_BYTES; }
28-
uint32_t sphincs_plus_get_sign_size() { return SPX_BYTES + SPX_MLEN; }
25+
#ifndef CKB_VM
26+
27+
#include <stdlib.h>
28+
29+
crypto_context *sphincs_plus_new_context(crypto_type type) {
30+
crypto_context *cctx = (crypto_context *)malloc(sizeof(crypto_context));
31+
int err = crypto_init_context(type, cctx);
32+
ASSERT(err == 0);
33+
return cctx;
34+
}
35+
36+
void sphincs_plus_del_context(crypto_context *cctx) { free(cctx); }
37+
38+
#endif // CKB_VM
39+
40+
int sphincs_plus_init_context(crypto_type type, crypto_context *cctx) {
41+
memset(cctx, 0, sizeof(crypto_context));
42+
return crypto_init_context(type, cctx);
43+
}
44+
45+
uint32_t sphincs_plus_get_pk_size(crypto_context *cctx) {
46+
return cctx->spx_pk_bytes;
47+
}
48+
49+
uint32_t sphincs_plus_get_sk_size(crypto_context *cctx) {
50+
return cctx->spx_sk_bytes;
51+
}
2952

30-
int sphincs_plus_init(crypto_type type) { return crypto_init_context(type); }
53+
uint32_t sphincs_plus_get_sign_size(crypto_context *cctx) {
54+
return cctx->spx_bytes + SPX_MLEN;
55+
}
3156

3257
#ifndef CKB_VM
3358

34-
int sphincs_plus_generate_keypair(uint8_t *pk, uint8_t *sk) {
35-
return crypto_sign_keypair(pk, sk);
59+
int sphincs_plus_generate_keypair(crypto_context *cctx, uint8_t *pk,
60+
uint8_t *sk) {
61+
return crypto_sign_keypair(cctx, pk, sk);
3662
}
3763

38-
int sphincs_plus_sign(uint8_t *message, uint8_t *sk, uint8_t *out_sign) {
39-
unsigned long long out_sign_len = sphincs_plus_get_sign_size();
40-
int ret = crypto_sign(out_sign, (unsigned long long *)&out_sign_len, message,
41-
SPX_MLEN, sk);
42-
if ((uint32_t)out_sign_len != sphincs_plus_get_sign_size()) {
64+
int sphincs_plus_sign(crypto_context *cctx, uint8_t *message, uint8_t *sk,
65+
uint8_t *out_sign) {
66+
unsigned long long out_sign_len = sphincs_plus_get_sign_size(cctx);
67+
int ret = crypto_sign(cctx, out_sign, (unsigned long long *)&out_sign_len,
68+
message, SPX_MLEN, sk);
69+
if ((uint32_t)out_sign_len != sphincs_plus_get_sign_size(cctx)) {
4370
return 1;
4471
}
4572
return ret;
4673
}
4774

4875
#endif // CKB_VM
4976

50-
int sphincs_plus_verify(uint8_t *sign, uint32_t sign_size, uint8_t *message,
51-
uint32_t message_size, uint8_t *pubkey,
52-
uint32_t pubkey_size) {
53-
if (sign_size != sphincs_plus_get_sign_size() || message_size != SPX_MLEN ||
54-
pubkey_size != sphincs_plus_get_pk_size()) {
77+
int sphincs_plus_verify(crypto_context *cctx, uint8_t *sign, uint32_t sign_size,
78+
uint8_t *message, uint32_t message_size,
79+
uint8_t *pubkey, uint32_t pubkey_size) {
80+
size_t sign_len = sphincs_plus_get_sign_size(cctx);
81+
82+
if (sign_size != sign_len || message_size != SPX_MLEN ||
83+
pubkey_size != sphincs_plus_get_pk_size(cctx)) {
5584
return SphincsPlusError_Params;
5685
}
5786
unsigned char mout[SPX_BYTES + SPX_MLEN];
5887
unsigned long long mlen = 0;
5988

60-
size_t sign_len = sphincs_plus_get_sign_size();
61-
int err = crypto_sign_open(mout, &mlen, sign, sign_len, pubkey);
89+
int err = crypto_sign_open(cctx, mout, &mlen, sign, sign_len, pubkey);
6290
if (err != 0) {
6391
return SphincsPlusError_Verify;
6492
}

c/ckb-sphincsplus.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,26 @@
55

66
#define SPX_MLEN 32
77

8-
uint32_t sphincs_plus_get_pk_size();
9-
uint32_t sphincs_plus_get_sk_size();
10-
uint32_t sphincs_plus_get_sign_size();
8+
#ifndef CKB_VM
9+
10+
crypto_context *sphincs_plus_new_context(crypto_type type);
11+
void sphincs_plus_del_context(crypto_context *cctx);
12+
13+
#endif // CKB_VM
14+
15+
uint32_t sphincs_plus_get_pk_size(crypto_context *cctx);
16+
uint32_t sphincs_plus_get_sk_size(crypto_context *cctx);
17+
uint32_t sphincs_plus_get_sign_size(crypto_context *cctx);
1118

12-
int sphincs_plus_init(crypto_type type);
19+
int sphincs_plus_init_context(crypto_type type, crypto_context *cctx);
1320

1421
#ifndef CKB_VM
15-
int sphincs_plus_generate_keypair(uint8_t *pk, uint8_t *sk);
16-
int sphincs_plus_sign(uint8_t *message, uint8_t *sk, uint8_t *out_sign);
22+
int sphincs_plus_generate_keypair(crypto_context *cctx, uint8_t *pk,
23+
uint8_t *sk);
24+
int sphincs_plus_sign(crypto_context *cctx, uint8_t *message, uint8_t *sk,
25+
uint8_t *out_sign);
1726
#endif // !CKB_VM
1827

19-
int sphincs_plus_verify(uint8_t *sign, uint32_t sign_size, uint8_t *message,
20-
uint32_t message_size, uint8_t *pubkey,
21-
uint32_t pubkey_size);
28+
int sphincs_plus_verify(crypto_context *cctx, uint8_t *sign, uint32_t sign_size,
29+
uint8_t *message, uint32_t message_size,
30+
uint8_t *pubkey, uint32_t pubkey_size);

c/ref/address.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
/*
1010
* Specify which level of Merkle tree (the "layer") we're working on
1111
*/
12-
void set_layer_addr(uint32_t addr[8], uint32_t layer) {
12+
void set_layer_addr(crypto_context *cctx, uint32_t addr[8], uint32_t layer) {
1313
((unsigned char *)addr)[SPX_OFFSET_LAYER] = (unsigned char)layer;
1414
}
1515

1616
/*
1717
* Specify which Merkle tree within the level (the "tree address") we're working
1818
* on
1919
*/
20-
void set_tree_addr(uint32_t addr[8], uint64_t tree) {
20+
void set_tree_addr(crypto_context *cctx, uint32_t addr[8], uint64_t tree) {
2121
ASSERT((SPX_TREE_HEIGHT * (SPX_D - 1)) <= 64);
2222
ull_to_bytes(&((unsigned char *)addr)[SPX_OFFSET_TREE], 8, tree);
2323
}
@@ -28,15 +28,16 @@ void set_tree_addr(uint32_t addr[8], uint64_t tree) {
2828
* hashes don't accidentally get the same address structure. The type will be
2929
* one of the SPX_ADDR_TYPE constants
3030
*/
31-
void set_type(uint32_t addr[8], uint32_t type) {
31+
void set_type(crypto_context *cctx, uint32_t addr[8], uint32_t type) {
3232
((unsigned char *)addr)[SPX_OFFSET_TYPE] = (unsigned char)type;
3333
}
3434

3535
/*
3636
* Copy the layer and tree fields of the address structure. This is used
3737
* when we're doing multiple types of hashes within the same Merkle tree
3838
*/
39-
void copy_subtree_addr(uint32_t out[8], const uint32_t in[8]) {
39+
void copy_subtree_addr(crypto_context *cctx, uint32_t out[8],
40+
const uint32_t in[8]) {
4041
memcpy(out, in, SPX_OFFSET_TREE + 8);
4142
}
4243

@@ -46,7 +47,8 @@ void copy_subtree_addr(uint32_t out[8], const uint32_t in[8]) {
4647
* Specify which Merkle leaf we're working on; that is, which OTS keypair
4748
* we're talking about.
4849
*/
49-
void set_keypair_addr(uint32_t addr[8], uint32_t keypair) {
50+
void set_keypair_addr(crypto_context *cctx, uint32_t addr[8],
51+
uint32_t keypair) {
5052
if (SPX_FULL_HEIGHT / SPX_D > 8) {
5153
/* We have > 256 OTS at the bottom of the Merkle tree; to specify */
5254
/* which one, we'd need to express it in two bytes */
@@ -60,7 +62,8 @@ void set_keypair_addr(uint32_t addr[8], uint32_t keypair) {
6062
* Copy the layer, tree and keypair fields of the address structure. This is
6163
* used when we're doing multiple things within the same OTS keypair
6264
*/
63-
void copy_keypair_addr(uint32_t out[8], const uint32_t in[8]) {
65+
void copy_keypair_addr(crypto_context *cctx, uint32_t out[8],
66+
const uint32_t in[8]) {
6467
memcpy(out, in, SPX_OFFSET_TREE + 8);
6568
if (SPX_FULL_HEIGHT / SPX_D > 8) {
6669
((unsigned char *)out)[SPX_OFFSET_KP_ADDR2] =
@@ -74,15 +77,15 @@ void copy_keypair_addr(uint32_t out[8], const uint32_t in[8]) {
7477
* Specify which Merkle chain within the OTS we're working with
7578
* (the chain address)
7679
*/
77-
void set_chain_addr(uint32_t addr[8], uint32_t chain) {
80+
void set_chain_addr(crypto_context *cctx, uint32_t addr[8], uint32_t chain) {
7881
((unsigned char *)addr)[SPX_OFFSET_CHAIN_ADDR] = (unsigned char)chain;
7982
}
8083

8184
/*
8285
* Specify where in the Merkle chain we are
8386
* (the hash address)
8487
*/
85-
void set_hash_addr(uint32_t addr[8], uint32_t hash) {
88+
void set_hash_addr(crypto_context *cctx, uint32_t addr[8], uint32_t hash) {
8689
((unsigned char *)addr)[SPX_OFFSET_HASH_ADDR] = (unsigned char)hash;
8790
}
8891

@@ -92,14 +95,16 @@ void set_hash_addr(uint32_t addr[8], uint32_t hash) {
9295
* Specify the height of the node in the Merkle/FORS tree we are in
9396
* (the tree height)
9497
*/
95-
void set_tree_height(uint32_t addr[8], uint32_t tree_height) {
98+
void set_tree_height(crypto_context *cctx, uint32_t addr[8],
99+
uint32_t tree_height) {
96100
((unsigned char *)addr)[SPX_OFFSET_TREE_HGT] = (unsigned char)tree_height;
97101
}
98102

99103
/*
100104
* Specify the distance from the left edge of the node in the Merkle/FORS tree
101105
* (the tree index)
102106
*/
103-
void set_tree_index(uint32_t addr[8], uint32_t tree_index) {
107+
void set_tree_index(crypto_context *cctx, uint32_t addr[8],
108+
uint32_t tree_index) {
104109
u32_to_bytes(&((unsigned char *)addr)[SPX_OFFSET_TREE_INDEX], tree_index);
105110
}

c/ref/address.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,42 @@
1515
#define SPX_ADDR_TYPE_FORSPRF 6
1616

1717
#define set_layer_addr SPX_NAMESPACE(set_layer_addr)
18-
void set_layer_addr(uint32_t addr[8], uint32_t layer);
18+
void set_layer_addr(crypto_context *cctx, uint32_t addr[8], uint32_t layer);
1919

2020
#define set_tree_addr SPX_NAMESPACE(set_tree_addr)
21-
void set_tree_addr(uint32_t addr[8], uint64_t tree);
21+
void set_tree_addr(crypto_context *cctx, uint32_t addr[8], uint64_t tree);
2222

2323
#define set_type SPX_NAMESPACE(set_type)
24-
void set_type(uint32_t addr[8], uint32_t type);
24+
void set_type(crypto_context *cctx, uint32_t addr[8], uint32_t type);
2525

2626
/* Copies the layer and tree part of one address into the other */
2727
#define copy_subtree_addr SPX_NAMESPACE(copy_subtree_addr)
28-
void copy_subtree_addr(uint32_t out[8], const uint32_t in[8]);
28+
void copy_subtree_addr(crypto_context *cctx, uint32_t out[8],
29+
const uint32_t in[8]);
2930

3031
/* These functions are used for WOTS and FORS addresses. */
3132

3233
#define set_keypair_addr SPX_NAMESPACE(set_keypair_addr)
33-
void set_keypair_addr(uint32_t addr[8], uint32_t keypair);
34+
void set_keypair_addr(crypto_context *cctx, uint32_t addr[8], uint32_t keypair);
3435

3536
#define set_chain_addr SPX_NAMESPACE(set_chain_addr)
36-
void set_chain_addr(uint32_t addr[8], uint32_t chain);
37+
void set_chain_addr(crypto_context *cctx, uint32_t addr[8], uint32_t chain);
3738

3839
#define set_hash_addr SPX_NAMESPACE(set_hash_addr)
39-
void set_hash_addr(uint32_t addr[8], uint32_t hash);
40+
void set_hash_addr(crypto_context *cctx, uint32_t addr[8], uint32_t hash);
4041

4142
#define copy_keypair_addr SPX_NAMESPACE(copy_keypair_addr)
42-
void copy_keypair_addr(uint32_t out[8], const uint32_t in[8]);
43+
void copy_keypair_addr(crypto_context *cctx, uint32_t out[8],
44+
const uint32_t in[8]);
4345

4446
/* These functions are used for all hash tree addresses (including FORS). */
4547

4648
#define set_tree_height SPX_NAMESPACE(set_tree_height)
47-
void set_tree_height(uint32_t addr[8], uint32_t tree_height);
49+
void set_tree_height(crypto_context *cctx, uint32_t addr[8],
50+
uint32_t tree_height);
4851

4952
#define set_tree_index SPX_NAMESPACE(set_tree_index)
50-
void set_tree_index(uint32_t addr[8], uint32_t tree_index);
53+
void set_tree_index(crypto_context *cctx, uint32_t addr[8],
54+
uint32_t tree_index);
5155

5256
#endif

0 commit comments

Comments
 (0)