Skip to content

Commit

Permalink
Optimize get_witness_layout
Browse files Browse the repository at this point in the history
* avoid duplicated verifications on witnesses
* printf with format %zu for size_t
  • Loading branch information
XuJiandong committed Apr 1, 2024
1 parent 7aa854f commit b87d9f7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
28 changes: 15 additions & 13 deletions c/cobuild.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,16 @@ int new_otx_blake2b(blake2b_state *S) {
}

static inline int get_witness_layout(BytesVecType witnesses, uint32_t index,
WitnessLayoutType *witness_layout) {
WitnessLayoutType *witness_layout,
bool verify_recursively) {
bool existing = false;
mol2_cursor_t witness = witnesses.t->get(&witnesses, index, &existing);
if (!existing) {
return COBUILD_ERROR_MOL2_UNEXPECTED;
}

WitnessLayoutType witness_layout2 = make_WitnessLayout(&witness);
if (verify_WitnessLayout(&witness_layout2)) {
if (verify_WitnessLayout(&witness_layout2, verify_recursively)) {
return COBUILD_ERROR_GENERAL;
}
if (witness_layout != NULL) {
Expand Down Expand Up @@ -248,7 +249,7 @@ int ckb_fetch_sighash_message(BytesVecType witnesses, MessageType *message) {
uint32_t witness_len = witnesses.t->len(&witnesses);
for (uint32_t index = 0; index < witness_len; index++) {
WitnessLayoutType witness_layout = {0};
if (get_witness_layout(witnesses, index, &witness_layout) == 0) {
if (get_witness_layout(witnesses, index, &witness_layout, false) == 0) {
uint32_t id = witness_layout.t->item_id(&witness_layout);
if (id == WitnessLayoutSighashAll) {
// tested by:
Expand Down Expand Up @@ -280,7 +281,7 @@ static inline int ckb_fetch_otx_start(BytesVecType witnesses, bool *has_otx,
uint32_t witness_len = witnesses.t->len(&witnesses);
for (uint32_t index = 0; index < witness_len; index++) {
WitnessLayoutType witness_layout = {0};
err = get_witness_layout(witnesses, index, &witness_layout);
err = get_witness_layout(witnesses, index, &witness_layout, false);
if (err == 0) {
uint32_t id = witness_layout.t->item_id(&witness_layout);
if (id == WitnessLayoutOtxStart) {
Expand Down Expand Up @@ -398,7 +399,7 @@ int ckb_generate_smh(const Env *env, mol2_cursor_t message_cursor,
CKB_COBUILD_CHECK(err);
}
blake2b_final(&ctx, smh, BLAKE2B_BLOCK_SIZE);
printf("ckb_generate_smh total hashed %d bytes", count);
printf("ckb_generate_smh total hashed %zu bytes", count);

exit:
return err;
Expand Down Expand Up @@ -532,7 +533,7 @@ int ckb_cobuild_normal_entry(const Env *env, ScriptEntryType callback) {
CKB_SOURCE_GROUP_INPUT);
CKB_COBUILD_CHECK(err);
WitnessLayoutType witness_layout = make_WitnessLayout(&witness);
CKB_COBUILD_CHECK2(!verify_WitnessLayout(&witness_layout),
CKB_COBUILD_CHECK2(!verify_WitnessLayout(&witness_layout, false),
COBUILD_ERROR_SIGHASHALL_NOSEAL);

uint32_t id = witness_layout.t->item_id(&witness_layout);
Expand Down Expand Up @@ -691,7 +692,7 @@ int ckb_generate_otx_smh(const Env *env, mol2_cursor_t message_cursor,
err = ckb_hash_cursor(&ctx, header_dep_cursor);
count += header_dep_cursor.size;
}
printf("ckb_generate_otx_smh totally hashed %d bytes", count);
printf("ckb_generate_otx_smh totally hashed %zu bytes", count);
blake2b_final(&ctx, smh, BLAKE2B_BLOCK_SIZE);
exit:
return err;
Expand All @@ -712,9 +713,10 @@ int ckb_cobuild_entry(const Env *env, ScriptEntryType callback,
// Legacy Flow Handling
*cobuild_enabled = false;
for (uint32_t i = 0; i < witness_len; i++) {
if (get_witness_layout(witnesses, i, NULL) == 0) {
if (get_witness_layout(witnesses, i, NULL, true) == 0) {
*cobuild_enabled = true;
break;
// Do not break here. All witnesses are recursively verified at this
// point. Subsequent witnesses will not be recursively verified.
}
}
if (!*cobuild_enabled) {
Expand Down Expand Up @@ -749,7 +751,7 @@ int ckb_cobuild_entry(const Env *env, ScriptEntryType callback,
printf("Otx starts at index %d(inclusive)", index);
for (; index < witness_len; index++) {
WitnessLayoutType witness_layout = {0};
err = get_witness_layout(witnesses, index, &witness_layout);
err = get_witness_layout(witnesses, index, &witness_layout, false);
if (err != 0) {
// step 6, not WitnessLayoutOtx
break;
Expand Down Expand Up @@ -861,7 +863,7 @@ int ckb_cobuild_entry(const Env *env, ScriptEntryType callback,
// [0, i) [j, +infinity)
if (index < i || index >= j) {
WitnessLayoutType witness_layout = {0};
err = get_witness_layout(witnesses, index, &witness_layout);
err = get_witness_layout(witnesses, index, &witness_layout, false);
if (err == 0) {
// test_cobuild_otx_noexistent_otx_id
uint32_t id = witness_layout.t->item_id(&witness_layout);
Expand All @@ -883,7 +885,7 @@ int ckb_cobuild_entry(const Env *env, ScriptEntryType callback,
CKB_COBUILD_CHECK_LOOP(err);
if (memcmp(hash, env->current_script_hash, sizeof(hash)) == 0) {
printf(
"Same lock script found beyond otx, at index %d. "
"Same lock script found beyond otx, at index %zu. "
"ckb_cobuild_normal_entry called.",
index);
found = true;
Expand All @@ -898,7 +900,7 @@ int ckb_cobuild_entry(const Env *env, ScriptEntryType callback,
CKB_COBUILD_CHECK(err);
}
CKB_COBUILD_CHECK2(execution_count > 0, COBUILD_ERROR_NO_CALLBACK);
printf("execution_count = %d", execution_count);
printf("execution_count = %zu", execution_count);
exit:
return err;
}
Expand Down
42 changes: 29 additions & 13 deletions c/molecule2_verify.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ typedef enum WitnessLayoutId {
} WitnessLayoutId;

int verify_WitnessArgs(WitnessArgsType *witness);
int verify_WitnessLayout(WitnessLayoutType *witness);
int verify_WitnessLayout(WitnessLayoutType *witness, bool recursive);

#ifndef MOLECULEC_C2_DECLARATION_ONLY

Expand Down Expand Up @@ -177,28 +177,44 @@ int get_union_id(mol2_cursor_t *cur, uint32_t *union_id) {
return MOL2_OK;
}

int verify_WitnessLayout(WitnessLayoutType *witness) {
int verify_WitnessLayout(WitnessLayoutType *witness, bool recursive) {
int err = MOL2_OK;
uint32_t union_id = 0;
CHECK(get_union_id(&witness->cur, &union_id));

err = get_union_id(&witness->cur, &union_id);
CHECK(err);
switch (union_id) {
case WitnessLayoutSighashAll: {
SighashAllType sighash_all = witness->t->as_SighashAll(witness);
return verify_SighashAll(&sighash_all);
if (recursive) {
SighashAllType sighash_all = witness->t->as_SighashAll(witness);
err = verify_SighashAll(&sighash_all);
CHECK(err);
}
break;
}
case WitnessLayoutSighashAllOnly: {
SighashAllOnlyType sighash_all_only =
witness->t->as_SighashAllOnly(witness);
return verify_SighashAllOnly(&sighash_all_only);
if (recursive) {
SighashAllOnlyType sighash_all_only =
witness->t->as_SighashAllOnly(witness);
err = verify_SighashAllOnly(&sighash_all_only);
CHECK(err);
}
break;
}
case WitnessLayoutOtx: {
OtxType otx = witness->t->as_Otx(witness);
return verify_Otx(&otx);
if (recursive) {
OtxType otx = witness->t->as_Otx(witness);
err = verify_Otx(&otx);
CHECK(err);
}
break;
}
case WitnessLayoutOtxStart: {
OtxStartType otx_start = witness->t->as_OtxStart(witness);
return verify_OtxStart(&otx_start);
if (recursive) {
OtxStartType otx_start = witness->t->as_OtxStart(witness);
err = verify_OtxStart(&otx_start);
CHECK(err);
}
break;
}
default: {
return MOL2_ERR_UNKNOWN_ITEM;
Expand Down
4 changes: 2 additions & 2 deletions tests/omni_lock_rust/tests/test_omni_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,8 +879,8 @@ fn test_cobuild_sighash_all_only() {
let verify_result = verifier.verify(MAX_CYCLES);
let cycles = (&verify_result).as_ref().unwrap();
println!("cycles = {}", *cycles);
// about ~1397402
assert!(*cycles < 1410000);
// about ~1419872
assert!(*cycles < 1430000);
verify_result.expect("pass verification");
}

Expand Down

0 comments on commit b87d9f7

Please sign in to comment.