Skip to content

Commit 90ad8ba

Browse files
committed
Cobuild support
* The main implementation is in cobuild.c. * The lazy reader is implemented in cobuild.c. Other changes: * Add test cases for cobuild * Update ckb-* to 0.113.0 and rust-toolchain * Update ckb-c-stdlib * Add test vectors
1 parent 41d6fed commit 90ad8ba

35 files changed

+9523
-701
lines changed

Makefile

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ CC := $(TARGET)-gcc
33
LD := $(TARGET)-gcc
44
OBJCOPY := $(TARGET)-objcopy
55
CFLAGS := -fPIC -O3 -fno-builtin-printf -fno-builtin-memcmp -nostdinc -nostdlib -nostartfiles -fvisibility=hidden -fdata-sections -ffunction-sections -I deps/secp256k1/src -I deps/secp256k1 -I deps/ckb-c-std-lib -I deps/ckb-c-std-lib/libc -I deps/ckb-c-std-lib/molecule -I c -I build -Wall -Werror -Wno-nonnull -Wno-nonnull-compare -Wno-unused-function -g
6-
LDFLAGS := -Wl,-static -fdata-sections -ffunction-sections -Wl,--gc-sections
6+
LDFLAGS := -nostdlib -nostartfiles -fno-builtin -Wl,-static -Wl,--gc-sections
77
SECP256K1_SRC_20210801 := deps/secp256k1-20210801/src/ecmult_static_pre_context.h
88

99

1010
OMNI_LOCK_CFLAGS :=$(subst ckb-c-std-lib,ckb-c-stdlib-20210801,$(CFLAGS)) -I deps/sparse-merkle-tree/c
1111
OMNI_LOCK_CFLAGS := $(subst secp256k1,secp256k1-20210801,$(OMNI_LOCK_CFLAGS))
12+
# enable log
13+
# OMNI_LOCK_CFLAGS += -DCKB_C_STDLIB_PRINTF -DCKB_C_STDLIB_PRINTF_BUFFER_SIZE=1024
14+
1215

1316
PROTOCOL_HEADER := c/blockchain.h
1417
PROTOCOL_SCHEMA := c/blockchain.mol
@@ -54,14 +57,16 @@ ${PROTOCOL_SCHEMA}:
5457
curl -L -o $@ ${PROTOCOL_URL}
5558

5659
ALL_C_SOURCE := $(wildcard c/omni_lock.c c/omni_lock_acp.h c/omni_lock_time_lock.h \
57-
tests/omni_lock/omni_lock_sim.c tests/omni_lock/ckb_syscall_omni_lock_sim.h tests/omni_lock/omni_lock_supply.h)
60+
tests/omni_lock/omni_lock_sim.c tests/omni_lock/ckb_syscall_omni_lock_sim.h tests/omni_lock/omni_lock_supply.h\
61+
c/blake2b_decl_only.h c/cobuild.h c/cobuild.c)
5862

5963
fmt:
6064
docker run --rm -v `pwd`:/code ${CLANG_FORMAT_DOCKER} bash -c "cd code && clang-format -i -style=Google $(ALL_C_SOURCE)"
6165
git diff --exit-code $(ALL_C_SOURCE)
6266

6367
mol:
6468
make omni_lock_mol
69+
make cobuild_mol
6570

6671
c/xudt_rce_mol.h: c/xudt_rce.mol
6772
${MOLC} --language c --schema-file $< > $@
@@ -76,17 +81,27 @@ omni_lock_mol:
7681
${MOLC} --language - --schema-file c/omni_lock.mol --format json > build/omni_lock_mol2.json
7782
moleculec-c2 --input build/omni_lock_mol2.json | clang-format -style=Google > c/omni_lock_mol2.h
7883

79-
build/omni_lock: c/omni_lock.c c/omni_lock_supply.h c/omni_lock_acp.h c/secp256k1_lock.h build/secp256k1_data_info_20210801.h $(SECP256K1_SRC_20210801) c/ckb_identity.h
80-
$(CC) $(OMNI_LOCK_CFLAGS) $(LDFLAGS) -o $@ $<
84+
build/cobuild.o: c/cobuild.c c/cobuild.h
85+
$(CC) -c $(OMNI_LOCK_CFLAGS) -o $@ $<
86+
87+
build/omni_lock.o: c/omni_lock.c c/omni_lock_supply.h c/omni_lock_acp.h c/secp256k1_lock.h build/secp256k1_data_info_20210801.h $(SECP256K1_SRC_20210801) c/ckb_identity.h
88+
$(CC) -c $(OMNI_LOCK_CFLAGS) -o $@ $<
89+
90+
build/omni_lock: build/omni_lock.o build/cobuild.o
91+
$(CC) $(LDFLAGS) -o $@ $^
8192
cp $@ $@.debug
8293
$(OBJCOPY) --strip-debug --strip-all $@
8394

95+
cobuild_mol:
96+
${MOLC} --language rust --schema-file c/basic.mol | rustfmt > tests/omni_lock_rust/src/schemas/basic.rs
97+
${MOLC} --language rust --schema-file c/top_level.mol | rustfmt > tests/omni_lock_rust/src/schemas/top_level.rs
8498

8599
clean:
86100
rm -rf build/secp256k1_data_info_20210801.h build/dump_secp256k1_data_20210801
87101
rm -rf build/secp256k1_data_20210801
88102
rm -rf build/*.debug
89103
rm -f build/omni_lock
104+
rm -f build/*.o
90105
cd deps/secp256k1-20210801 && [ -f "Makefile" ] && make clean
91106

92107
install-tools:

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ cd tests/omni_lock_rust && cargo test
1919
## Deployment
2020

2121
See [RFC](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0042-omnilock/0042-omnilock.md)
22+

c/basic.mol

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import blockchain;
2+
3+
array Hash [byte; 32];
4+
vector String <byte>; // UTF-8 encoded
5+
option Uint32Opt (Uint32);
6+
7+
table Action {
8+
script_info_hash: Byte32, // script info
9+
script_hash: Byte32, // script
10+
data: Bytes, // action data
11+
}
12+
13+
vector ActionVec <Action>;
14+
15+
table Message {
16+
actions: ActionVec,
17+
}
18+
19+
table ScriptInfo {
20+
// The dapp name and domain the script belongs to
21+
name: String,
22+
url: String,
23+
24+
// Script info.
25+
// schema: script action schema
26+
// message_type: the entry action type used in WitnessLayout
27+
script_hash: Byte32,
28+
schema: String,
29+
message_type: String,
30+
}
31+
32+
vector ScriptInfoVec <ScriptInfo>;
33+
34+
table ResolvedInputs {
35+
outputs: CellOutputVec,
36+
outputs_data: BytesVec,
37+
}
38+
39+
table BuildingPacketV1 {
40+
message: Message,
41+
payload: Transaction,
42+
resolved_inputs: ResolvedInputs,
43+
change_output: Uint32Opt,
44+
script_infos: ScriptInfoVec,
45+
lock_actions: ActionVec,
46+
}
47+
48+
union BuildingPacket {
49+
BuildingPacketV1,
50+
}
51+
52+
table SighashAll {
53+
message: Message,
54+
seal: Bytes,
55+
}
56+
57+
table SighashAllOnly {
58+
seal: Bytes,
59+
}
60+
61+
table SealPair {
62+
script_hash: Byte32,
63+
seal: Bytes,
64+
}
65+
vector SealPairVec <SealPair>;
66+
67+
table OtxStart {
68+
start_input_cell: Uint32,
69+
start_output_cell: Uint32,
70+
start_cell_deps: Uint32,
71+
start_header_deps: Uint32,
72+
}
73+
74+
table Otx {
75+
input_cells: Uint32,
76+
output_cells: Uint32,
77+
cell_deps: Uint32,
78+
header_deps: Uint32,
79+
message: Message,
80+
seals: SealPairVec,
81+
}

c/blake2b_decl_only.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
#ifndef __BLAKE2B_DECL_ONLY_H__
3+
#define __BLAKE2B_DECL_ONLY_H__
4+
5+
#include <stddef.h>
6+
#include <stdint.h>
7+
8+
#define BLAKE2_PACKED(x) x __attribute__((packed))
9+
10+
enum blake2b_constant {
11+
BLAKE2B_BLOCKBYTES = 128,
12+
BLAKE2B_OUTBYTES = 64,
13+
BLAKE2B_KEYBYTES = 64,
14+
BLAKE2B_SALTBYTES = 16,
15+
BLAKE2B_PERSONALBYTES = 16
16+
};
17+
BLAKE2_PACKED(struct blake2b_param__ {
18+
uint8_t digest_length; /* 1 */
19+
uint8_t key_length; /* 2 */
20+
uint8_t fanout; /* 3 */
21+
uint8_t depth; /* 4 */
22+
uint32_t leaf_length; /* 8 */
23+
uint32_t node_offset; /* 12 */
24+
uint32_t xof_length; /* 16 */
25+
uint8_t node_depth; /* 17 */
26+
uint8_t inner_length; /* 18 */
27+
uint8_t reserved[14]; /* 32 */
28+
uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */
29+
uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
30+
});
31+
32+
typedef struct blake2b_param__ blake2b_param;
33+
34+
typedef struct blake2b_state__ {
35+
uint64_t h[8];
36+
uint64_t t[2];
37+
uint64_t f[2];
38+
uint8_t buf[BLAKE2B_BLOCKBYTES];
39+
size_t buflen;
40+
size_t outlen;
41+
uint8_t last_node;
42+
} blake2b_state;
43+
44+
/* Streaming API */
45+
int ckb_blake2b_init(blake2b_state *S, size_t outlen);
46+
int blake2b_init(blake2b_state *S, size_t outlen);
47+
int blake2b_init_key(blake2b_state *S, size_t outlen, const void *key,
48+
size_t keylen);
49+
int blake2b_update(blake2b_state *S, const void *in, size_t inlen);
50+
int blake2b_final(blake2b_state *S, void *out, size_t outlen);
51+
/* Simple API */
52+
int blake2b(void *out, size_t outlen, const void *in, size_t inlen,
53+
const void *key, size_t keylen);
54+
55+
/* This is simply an alias for blake2b */
56+
int blake2(void *out, size_t outlen, const void *in, size_t inlen,
57+
const void *key, size_t keylen);
58+
59+
int blake2b_init_param(blake2b_state *S, const blake2b_param *P);
60+
61+
#endif

c/ckb_identity.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@
2525
#define MAX_PREIMAGE_SIZE 1024
2626
#define MESSAGE_HEX_LEN 64
2727

28-
<<<<<<< HEAD
2928
const char BTC_PREFIX[] = "CKB (Bitcoin Layer) transaction: 0x";
30-
=======
31-
const char BTC_PREFIX[] = "CKB (Bitcoin Layer-2) transaction: 0x";
32-
>>>>>>> bcd9b58 (Add missing auth id(0x02~0x05))
3329
// BTC_PREFIX_LEN = 35
3430
const size_t BTC_PREFIX_LEN = sizeof(BTC_PREFIX) - 1;
3531

@@ -87,6 +83,9 @@ typedef int (*validate_signature_t)(void *prefilled_data, const uint8_t *sig,
8783
typedef int (*convert_msg_t)(const uint8_t *msg, size_t msg_len,
8884
uint8_t *new_msg, size_t new_msg_len);
8985

86+
bool g_cobuild_enabled = false;
87+
uint8_t g_cobuild_signing_message_hash[32];
88+
9089
static void bin_to_hex(const uint8_t *source, uint8_t *dest, size_t len) {
9190
const static uint8_t HEX_TABLE[] = {'0', '1', '2', '3', '4', '5', '6', '7',
9291
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
@@ -379,6 +378,11 @@ int validate_signature_eos(void *prefilled_data, const uint8_t *sig,
379378
}
380379

381380
int generate_sighash_all(uint8_t *msg, size_t msg_len) {
381+
if (g_cobuild_enabled) {
382+
memcpy(msg, g_cobuild_signing_message_hash, BLAKE2B_BLOCK_SIZE);
383+
return 0;
384+
}
385+
382386
int ret;
383387
uint64_t len = 0;
384388
unsigned char temp[MAX_WITNESS_SIZE];

0 commit comments

Comments
 (0)