Skip to content

Commit

Permalink
Minimize secp256k1 precomputed table (#30)
Browse files Browse the repository at this point in the history
* Minimize secp256k1 precomputed table
* Choose ecmult-window = 6 for better performance
* Remove not used code and files
  • Loading branch information
XuJiandong authored Feb 6, 2024
1 parent f7988ed commit bfc04c0
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 432 deletions.
19 changes: 9 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,9 @@ build/dump_secp256k1_data_20210801: c/dump_secp256k1_data_20210801.c $(SECP256K1
$(SECP256K1_SRC_20210801):
cd deps/secp256k1-20210801 && \
./autogen.sh && \
CC=$(CC) LD=$(LD) ./configure --with-bignum=no --enable-ecmult-static-precomputation --enable-endomorphism --enable-module-recovery --host=$(TARGET) && \
CC=$(CC) LD=$(LD) ./configure --enable-ecmult-static-precomputation --with-ecmult-window=6 --enable-module-recovery --host=$(TARGET) && \
make src/ecmult_static_pre_context.h src/ecmult_static_context.h


build/impl.o: deps/ckb-c-std-lib/libc/src/impl.c
$(CC) -c $(filter-out -DCKB_DECLARATION_ONLY, $(CFLAGS_MBEDTLS)) $(LDFLAGS_MBEDTLS) -o $@ $^

${PROTOCOL_SCHEMA}:
curl -L -o $@ ${PROTOCOL_URL}

Expand Down Expand Up @@ -84,7 +80,7 @@ omni_lock_mol:
build/cobuild.o: c/cobuild.c c/cobuild.h
$(CC) -c $(OMNI_LOCK_CFLAGS) -o $@ $<

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
build/omni_lock.o: c/omni_lock.c c/omni_lock_supply.h c/omni_lock_acp.h build/secp256k1_data_info_20210801.h $(SECP256K1_SRC_20210801) c/ckb_identity.h
$(CC) -c $(OMNI_LOCK_CFLAGS) -o $@ $<

build/omni_lock: build/omni_lock.o build/cobuild.o
Expand All @@ -98,14 +94,17 @@ cobuild_mol:
${MOLC} --language - --schema-file c/basic.mol --format json > build/cobuild_basic_mol2.json
moleculec-c2 --input build/cobuild_basic_mol2.json | clang-format -style=Google > c/cobuild_basic_mol2.h

clean:
clean: clean2
rm -rf build/secp256k1_data_info_20210801.h build/dump_secp256k1_data_20210801
rm -rf build/secp256k1_data_20210801
rm -f build/secp256k1_data_20210801
cd deps/secp256k1-20210801 && [ -f "Makefile" ] && make clean

clean2:
rm -rf build/*.debug
rm -f build/omni_lock
rm -f build/*.o
cd deps/secp256k1-20210801 && [ -f "Makefile" ] && make clean

rm -f build/always_success
install-tools:
if [ ! -x "$$(command -v "${MOLC}")" ] \
|| [ "$$(${MOLC} --version | awk '{ print $$2 }' | tr -d ' ')" != "${MOLC_VERSION}" ]; then \
Expand Down
9 changes: 3 additions & 6 deletions c/ckb_identity.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ static int _ckb_recover_secp256k1_pubkey(const uint8_t *sig, size_t sig_len,

/* Load signature */
secp256k1_context context;
uint8_t secp_data[CKB_SECP256K1_DATA_SIZE];
ret = ckb_secp256k1_custom_verify_only_initialize(&context, secp_data);
ret = ckb_secp256k1_custom_verify_only_initialize(&context);
if (ret != 0) {
return ret;
}
Expand Down Expand Up @@ -285,8 +284,7 @@ static int _recover_secp256k1_pubkey_btc(const uint8_t *sig, size_t sig_len,
}

secp256k1_context context;
uint8_t secp_data[CKB_SECP256K1_DATA_SIZE];
ret = ckb_secp256k1_custom_verify_only_initialize(&context, secp_data);
ret = ckb_secp256k1_custom_verify_only_initialize(&context);
if (ret != 0) {
return ret;
}
Expand Down Expand Up @@ -823,8 +821,7 @@ int verify_multisig(const uint8_t *lock_bytes, size_t lock_bytes_len,
// contract, you don't have to wait for the foundation to ship a new
// cryptographic algorithm. You can just build and ship your own.
secp256k1_context context;
uint8_t secp_data[CKB_SECP256K1_DATA_SIZE];
ret = ckb_secp256k1_custom_verify_only_initialize(&context, secp_data);
ret = ckb_secp256k1_custom_verify_only_initialize(&context);
if (ret != 0) return ret;

// We will perform *threshold* number of signature verifications here.
Expand Down
57 changes: 0 additions & 57 deletions c/dump_secp256k1_data.c

This file was deleted.

25 changes: 13 additions & 12 deletions c/dump_secp256k1_data_20210801.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,23 @@ int main(int argc, char* argv[]) {
fprintf(fp, "#define CKB_SECP256K1_DATA_PRE_SIZE %ld\n", pre_size);
fprintf(fp, "#define CKB_SECP256K1_DATA_PRE128_SIZE %ld\n", pre128_size);

blake2b_state blake2b_ctx;
uint8_t hash[32];
blake2b_init(&blake2b_ctx, 32);
blake2b_update(&blake2b_ctx, secp256k1_ecmult_static_pre_context, pre_size);
blake2b_update(&blake2b_ctx, secp256k1_ecmult_static_pre128_context,
pre128_size);
blake2b_final(&blake2b_ctx, hash, 32);

fprintf(fp, "static uint8_t ckb_secp256k1_data_hash[32] = {\n ");
for (int i = 0; i < 32; i++) {
fprintf(fp, "%u", hash[i]);
if (i != 31) {
fprintf(fp, "static uint8_t ckb_secp256k1_data[] = {\n ");
unsigned char* p = (unsigned char*)secp256k1_ecmult_static_pre_context;
for (int i = 0; i < pre_size; i++) {
fprintf(fp, "0x%02x", p[i]);
fprintf(fp, ", ");
}
fprintf(fp, "\n");
p = (unsigned char*)secp256k1_ecmult_static_pre128_context;
for (int i = 0; i < pre128_size; i++) {
fprintf(fp, "0x%02x", p[i]);
if (i != (pre128_size -1)) {
fprintf(fp, ", ");
}
}

fprintf(fp, "\n};\n");

fprintf(fp, "#endif\n");
fclose(fp);

Expand Down
88 changes: 0 additions & 88 deletions c/secp256k1_helper.h

This file was deleted.

37 changes: 2 additions & 35 deletions c/secp256k1_helper_20210801.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,7 @@ void secp256k1_default_error_callback_fn(const char* str, void* data) {
* data should at least be CKB_SECP256K1_DATA_SIZE big
* so as to hold all loaded data.
*/
int ckb_secp256k1_custom_verify_only_initialize(secp256k1_context* context,
void* data) {
size_t index = 0;
int running = 1;
while (running && index < SIZE_MAX) {
uint64_t len = 32;
uint8_t hash[32];

int ret = ckb_load_cell_by_field(hash, &len, 0, index, CKB_SOURCE_CELL_DEP,
CKB_CELL_FIELD_DATA_HASH);
switch (ret) {
case CKB_ITEM_MISSING:
break;
case CKB_SUCCESS:
if (memcmp(ckb_secp256k1_data_hash, hash, 32) == 0) {
/* Found a match, load data here */
len = CKB_SECP256K1_DATA_SIZE;
ret = ckb_load_cell_data(data, &len, 0, index, CKB_SOURCE_CELL_DEP);
if (ret != CKB_SUCCESS || len != CKB_SECP256K1_DATA_SIZE) {
return CKB_SECP256K1_HELPER_ERROR_LOADING_DATA;
}
running = 0;
}
break;
default:
return CKB_SECP256K1_HELPER_ERROR_LOADING_DATA;
}
if (running) {
index++;
}
}
if (index == SIZE_MAX) {
return CKB_SECP256K1_HELPER_ERROR_LOADING_DATA;
}
int ckb_secp256k1_custom_verify_only_initialize(secp256k1_context* context) {

context->illegal_callback = default_illegal_callback;
context->error_callback = default_error_callback;
Expand All @@ -75,7 +42,7 @@ int ckb_secp256k1_custom_verify_only_initialize(secp256k1_context* context,
secp256k1_ecmult_gen_context_init(&context->ecmult_gen_ctx);

/* Recasting data to (uint8_t*) for pointer math */
uint8_t* p = data;
uint8_t* p = ckb_secp256k1_data;
secp256k1_ge_storage(*pre_g)[] = (secp256k1_ge_storage(*)[])p;
secp256k1_ge_storage(*pre_g_128)[] =
(secp256k1_ge_storage(*)[])(&p[CKB_SECP256K1_DATA_PRE_SIZE]);
Expand Down
Loading

0 comments on commit bfc04c0

Please sign in to comment.