Skip to content

Commit

Permalink
Draft support for otx
Browse files Browse the repository at this point in the history
* update gcc toolchain
* remove wrong test cases without any witness
  • Loading branch information
XuJiandong committed Jan 31, 2024
1 parent 6195643 commit b8a68fc
Show file tree
Hide file tree
Showing 10 changed files with 427 additions and 263 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SECP256K1_SRC_20210801 := deps/secp256k1-20210801/src/ecmult_static_pre_context.
OMNI_LOCK_CFLAGS :=$(subst ckb-c-std-lib,ckb-c-stdlib-20210801,$(CFLAGS)) -I deps/sparse-merkle-tree/c
OMNI_LOCK_CFLAGS := $(subst secp256k1,secp256k1-20210801,$(OMNI_LOCK_CFLAGS))
# enable log
# OMNI_LOCK_CFLAGS += -DCKB_C_STDLIB_PRINTF -DCKB_C_STDLIB_PRINTF_BUFFER_SIZE=1024
OMNI_LOCK_CFLAGS += -DCKB_C_STDLIB_PRINTF -DCKB_C_STDLIB_PRINTF_BUFFER_SIZE=1024


PROTOCOL_HEADER := c/blockchain.h
Expand Down
59 changes: 17 additions & 42 deletions c/ckb_identity.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ typedef int (*validate_signature_t)(void *prefilled_data, const uint8_t *sig,
typedef int (*convert_msg_t)(const uint8_t *msg, size_t msg_len,
uint8_t *new_msg, size_t new_msg_len);

bool g_cobuild_enabled = false;
uint8_t g_cobuild_signing_message_hash[32];

static void bin_to_hex(const uint8_t *source, uint8_t *dest, size_t len) {
const static uint8_t HEX_TABLE[] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
Expand Down Expand Up @@ -378,11 +375,6 @@ int validate_signature_eos(void *prefilled_data, const uint8_t *sig,
}

int generate_sighash_all(uint8_t *msg, size_t msg_len) {
if (g_cobuild_enabled) {
memcpy(msg, g_cobuild_signing_message_hash, BLAKE2B_BLOCK_SIZE);
return 0;
}

int ret;
uint64_t len = 0;
unsigned char temp[MAX_WITNESS_SIZE];
Expand Down Expand Up @@ -530,15 +522,10 @@ static int convert_eth_message_displaying(const uint8_t *msg, size_t msg_len,
}

int verify_sighash_all(uint8_t *pubkey_hash, uint8_t *sig, uint32_t sig_len,
validate_signature_t func, convert_msg_t convert) {
validate_signature_t func, convert_msg_t convert, const uint8_t* signing_message_hash) {
int ret = 0;
uint8_t old_msg[BLAKE2B_BLOCK_SIZE];
uint8_t new_msg[BLAKE2B_BLOCK_SIZE];
ret = generate_sighash_all(old_msg, sizeof(old_msg));
if (ret != 0) {
return ret;
}
ret = convert(old_msg, sizeof(old_msg), new_msg, sizeof(new_msg));
ret = convert(signing_message_hash, BLAKE2B_BLOCK_SIZE, new_msg, sizeof(new_msg));
if (ret != 0) return ret;

uint8_t output_pubkey_hash[BLAKE160_SIZE];
Expand Down Expand Up @@ -679,7 +666,7 @@ bool is_lock_script_hash_present(uint8_t *lock_script_hash) {

int verify_via_dl(CkbIdentityType *id, uint8_t *sig, uint32_t sig_len,
uint8_t *preimage, uint32_t preimage_len,
CkbSwappableSignatureInstance *inst) {
CkbSwappableSignatureInstance *inst, const uint8_t* signing_message_hash) {
int err = 0;
uint8_t hash[BLAKE2B_BLOCK_SIZE];

Expand All @@ -702,11 +689,11 @@ int verify_via_dl(CkbIdentityType *id, uint8_t *sig, uint32_t sig_len,
if (err != 0) return err;

return verify_sighash_all(pubkey_hash, sig, sig_len, inst->verify_func,
_ckb_convert_copy);
_ckb_convert_copy, signing_message_hash);
}

int verify_via_exec(CkbIdentityType *id, uint8_t *sig, uint32_t sig_len,
uint8_t *preimage, uint32_t preimage_len) {
uint8_t *preimage, uint32_t preimage_len, const uint8_t* signing_message_hash) {
int err = 0;
uint8_t hash[BLAKE2B_BLOCK_SIZE];

Expand All @@ -718,9 +705,6 @@ int verify_via_exec(CkbIdentityType *id, uint8_t *sig, uint32_t sig_len,
if (preimage_len != (32 + 1 + 1 + 8 + 20)) {
return ERROR_INVALID_PREIMAGE;
}

int ret = 0;

// check preimage hash
blake2b_state ctx;
blake2b_init(&ctx, BLAKE2B_BLOCK_SIZE);
Expand All @@ -730,12 +714,6 @@ int verify_via_exec(CkbIdentityType *id, uint8_t *sig, uint32_t sig_len,
return ERROR_INVALID_PREIMAGE;
}

// get message
uint8_t msg[BLAKE2B_BLOCK_SIZE];
ret = generate_sighash_all(msg, sizeof(msg));
if (ret != 0) {
return ret;
}

uint8_t *code_hash = preimage;
uint8_t hash_type = *(preimage + 32);
Expand All @@ -756,7 +734,7 @@ int verify_via_exec(CkbIdentityType *id, uint8_t *sig, uint32_t sig_len,
if (err != 0) return err;
err = ckb_exec_append(&bin_args, pubkey_hash, 20);
if (err != 0) return err;
err = ckb_exec_append(&bin_args, msg, sizeof(msg));
err = ckb_exec_append(&bin_args, (uint8_t*)signing_message_hash, BLAKE2B_BLOCK_SIZE);
if (err != 0) return err;
err = ckb_exec_append(&bin_args, sig, sig_len);
if (err != 0) return err;
Expand Down Expand Up @@ -920,54 +898,51 @@ static uint8_t *g_identity_code_buffer = NULL;
static uint32_t g_identity_code_size = 0;

int ckb_verify_identity(CkbIdentityType *id, uint8_t *sig, uint32_t sig_size,
uint8_t *preimage, uint32_t preimage_size) {
uint8_t *preimage, uint32_t preimage_size, const uint8_t* signing_message_hash) {
if (id->flags == IdentityFlagsCkb) {
if (sig == NULL || sig_size != SECP256K1_SIGNATURE_SIZE) {
return ERROR_IDENTITY_WRONG_ARGS;
}
return verify_sighash_all(id->id, sig, sig_size,
validate_signature_secp256k1, _ckb_convert_copy);
validate_signature_secp256k1, _ckb_convert_copy, signing_message_hash);
} else if (id->flags == IdentityFlagsEthereum) {
if (sig == NULL || sig_size != SECP256K1_SIGNATURE_SIZE) {
return ERROR_IDENTITY_WRONG_ARGS;
}
return verify_sighash_all(id->id, sig, sig_size, validate_signature_eth,
convert_eth_message);
convert_eth_message, signing_message_hash);
} else if (id->flags == IdentityFlagsEthereumDisplaying) {
if (sig == NULL || sig_size != SECP256K1_SIGNATURE_SIZE) {
return ERROR_IDENTITY_WRONG_ARGS;
}
return verify_sighash_all(id->id, sig, sig_size, validate_signature_eth,
convert_eth_message_displaying);
convert_eth_message_displaying, signing_message_hash);
} else if (id->flags == IdentityFlagsEos) {
if (sig == NULL || sig_size != SECP256K1_SIGNATURE_SIZE) {
return ERROR_IDENTITY_WRONG_ARGS;
}
return verify_sighash_all(id->id, sig, sig_size, validate_signature_eos,
convert_copy);
convert_copy, signing_message_hash);
} else if (id->flags == IdentityFlagsTron) {
if (sig == NULL || sig_size != SECP256K1_SIGNATURE_SIZE) {
return ERROR_IDENTITY_WRONG_ARGS;
}
return verify_sighash_all(id->id, sig, sig_size, validate_signature_eth,
convert_tron_message);
convert_tron_message, signing_message_hash);
} else if (id->flags == IdentityFlagsBitcoin) {
if (sig == NULL || sig_size != SECP256K1_SIGNATURE_SIZE) {
return ERROR_IDENTITY_WRONG_ARGS;
}
return verify_sighash_all(id->id, sig, sig_size, validate_signature_btc,
convert_btc_message);
convert_btc_message, signing_message_hash);
} else if (id->flags == IdentityFlagsDogecoin) {
if (sig == NULL || sig_size != SECP256K1_SIGNATURE_SIZE) {
return ERROR_IDENTITY_WRONG_ARGS;
}
return verify_sighash_all(id->id, sig, sig_size, validate_signature_btc,
convert_doge_message);
convert_doge_message, signing_message_hash);
} else if (id->flags == IdentityCkbMultisig) {
uint8_t msg[BLAKE2B_BLOCK_SIZE];
int ret = generate_sighash_all(msg, sizeof(msg));
if (ret != 0) return ret;
return verify_multisig(sig, sig_size, msg, id->id);
return verify_multisig(sig, sig_size, signing_message_hash, id->id);
} else if (id->flags == IdentityFlagsOwnerLock) {
if (is_lock_script_hash_present(id->id)) {
return 0;
Expand All @@ -983,9 +958,9 @@ int ckb_verify_identity(CkbIdentityType *id, uint8_t *sig, uint32_t sig_size,
.prefilled_buffer_size = 0,
.verify_func = NULL};
return verify_via_dl(id, sig, sig_size, preimage, preimage_size,
&swappable_inst);
&swappable_inst, signing_message_hash);
} else if (id->flags == IdentityFlagsExec) {
return verify_via_exec(id, sig, sig_size, preimage, preimage_size);
return verify_via_exec(id, sig, sig_size, preimage, preimage_size, signing_message_hash);
}
return CKB_INVALID_DATA;
}
Expand Down
Loading

0 comments on commit b8a68fc

Please sign in to comment.