From 0b3b57d5f4f6855648deae1f16ffefdfec17683f Mon Sep 17 00:00:00 2001 From: Joii Date: Fri, 13 Dec 2024 22:27:14 +0800 Subject: [PATCH 1/4] Update secp256k1 to v0.6.0 --- Makefile | 13 ++----------- c/dump_secp256k1_data_20210801.c | 14 +++++++------- c/secp256k1_helper_20210801.h | 28 ++++++++++++++++------------ deps/secp256k1-20210801 | 2 +- tests/omni_lock_rust/build.rs | 2 +- 5 files changed, 27 insertions(+), 32 deletions(-) diff --git a/Makefile b/Makefile index 1c46576..86a1195 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,6 @@ LD := $(TARGET)-gcc OBJCOPY := $(TARGET)-objcopy 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 LDFLAGS := -Wl,-static -fdata-sections -ffunction-sections -Wl,--gc-sections -SECP256K1_SRC_20210801 := deps/secp256k1-20210801/src/ecmult_static_pre_context.h OMNI_LOCK_CFLAGS :=$(subst ckb-c-std-lib,ckb-c-stdlib-20210801,$(CFLAGS)) -I deps/sparse-merkle-tree/c @@ -35,18 +34,10 @@ build/always_success: c/always_success.c build/secp256k1_data_info_20210801.h: build/dump_secp256k1_data_20210801 $< -build/dump_secp256k1_data_20210801: c/dump_secp256k1_data_20210801.c $(SECP256K1_SRC_20210801) +build/dump_secp256k1_data_20210801: c/dump_secp256k1_data_20210801.c mkdir -p build gcc -I deps/secp256k1-20210801/src -I deps/secp256k1-20210801 -o $@ $< - -$(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) && \ - 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 $@ $^ @@ -76,7 +67,7 @@ omni_lock_mol: ${MOLC} --language - --schema-file c/omni_lock.mol --format json > build/omni_lock_mol2.json moleculec-c2 --input build/omni_lock_mol2.json | clang-format -style=Google > c/omni_lock_mol2.h -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 +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 c/ckb_identity.h $(CC) $(OMNI_LOCK_CFLAGS) $(LDFLAGS) -o $@ $< cp $@ $@.debug $(OBJCOPY) --strip-debug --strip-all $@ diff --git a/c/dump_secp256k1_data_20210801.c b/c/dump_secp256k1_data_20210801.c index 10f4236..d69dd4e 100644 --- a/c/dump_secp256k1_data_20210801.c +++ b/c/dump_secp256k1_data_20210801.c @@ -7,20 +7,20 @@ * directly, the final binary will include all functions rather than those used. */ #define HAVE_CONFIG_H 1 -#include +#include #define ERROR_IO -1 int main(int argc, char* argv[]) { - size_t pre_size = sizeof(secp256k1_ecmult_static_pre_context); - size_t pre128_size = sizeof(secp256k1_ecmult_static_pre128_context); + size_t pre_size = sizeof(secp256k1_pre_g); + size_t pre128_size = sizeof(secp256k1_pre_g_128); FILE* fp_data = fopen("build/secp256k1_data_20210801", "wb"); if (!fp_data) { return ERROR_IO; } - fwrite(secp256k1_ecmult_static_pre_context, pre_size, 1, fp_data); - fwrite(secp256k1_ecmult_static_pre128_context, pre128_size, 1, fp_data); + fwrite(secp256k1_pre_g, pre_size, 1, fp_data); + fwrite(secp256k1_pre_g_128, pre128_size, 1, fp_data); fclose(fp_data); FILE* fp = fopen("build/secp256k1_data_info_20210801.h", "w"); @@ -37,8 +37,8 @@ int main(int argc, char* argv[]) { 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, + blake2b_update(&blake2b_ctx, secp256k1_pre_g, pre_size); + blake2b_update(&blake2b_ctx, secp256k1_pre_g_128, pre128_size); blake2b_final(&blake2b_ctx, hash, 32); diff --git a/c/secp256k1_helper_20210801.h b/c/secp256k1_helper_20210801.h index 3b1e9f4..262a422 100644 --- a/c/secp256k1_helper_20210801.h +++ b/c/secp256k1_helper_20210801.h @@ -15,7 +15,16 @@ */ #define HAVE_CONFIG_H 1 #define USE_EXTERNAL_DEFAULT_CALLBACKS +#define SECP256K1_PRECOMPUTED_ECMULT_H +#include "group.h" +#define WINDOW_G ECMULT_WINDOW_SIZE +secp256k1_ge_storage* secp256k1_pre_g = NULL; +secp256k1_ge_storage* secp256k1_pre_g_128 = NULL; +#undef SECP256K1_NO_BUILD #include +#include "modules/recovery/main_impl.h" +const secp256k1_ge_storage secp256k1_ecmult_gen_prec_table[COMB_BLOCKS] + [COMB_POINTS]; void secp256k1_default_illegal_callback_fn(const char* str, void* data) { (void)str; @@ -68,19 +77,14 @@ int ckb_secp256k1_custom_verify_only_initialize(secp256k1_context* context, return CKB_SECP256K1_HELPER_ERROR_LOADING_DATA; } - context->illegal_callback = default_illegal_callback; - context->error_callback = default_error_callback; + secp256k1_pre_g = (secp256k1_ge_storage*)data; + secp256k1_pre_g_128 = (secp256k1_ge_storage*)(data + CKB_SECP256K1_DATA_PRE_SIZE); - secp256k1_ecmult_context_init(&context->ecmult_ctx); - secp256k1_ecmult_gen_context_init(&context->ecmult_gen_ctx); - - /* Recasting data to (uint8_t*) for pointer math */ - uint8_t* p = 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]); - context->ecmult_ctx.pre_g = pre_g; - context->ecmult_ctx.pre_g_128 = pre_g_128; + secp256k1_context* ctx = secp256k1_context_preallocated_create( + (void*)context, SECP256K1_CONTEXT_VERIFY); + if (!ctx) { + return CKB_SECP256K1_HELPER_ERROR_LOADING_DATA; + } return 0; } diff --git a/deps/secp256k1-20210801 b/deps/secp256k1-20210801 index fa76917..0cdc758 160000 --- a/deps/secp256k1-20210801 +++ b/deps/secp256k1-20210801 @@ -1 +1 @@ -Subproject commit fa76917903ed9f2dab69ee28b30f742fe440962b +Subproject commit 0cdc758a56360bf58a851fe91085a327ec97685a diff --git a/tests/omni_lock_rust/build.rs b/tests/omni_lock_rust/build.rs index b0b2821..2cc2266 100644 --- a/tests/omni_lock_rust/build.rs +++ b/tests/omni_lock_rust/build.rs @@ -15,7 +15,7 @@ const CKB_HASH_PERSONALIZATION: &[u8] = b"ckb-default-hash"; const BINARIES: &[(&str, &str)] = &[ ( "omni_lock", - "768f306681da232ceb0b94f436c5f813377179762a831c5ad8797bd4fd2d118d", + "4fc2132f040de19a5355004252a0447421299844dc8e215cb7170c4a7174d7c7", ), ]; From 37e8fb58d78b881db6d18a42ebcf205ea2658a4a Mon Sep 17 00:00:00 2001 From: Joii Date: Mon, 16 Dec 2024 10:23:23 +0800 Subject: [PATCH 2/4] Remove CI: omni_lock --- .github/workflows/rust.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 6697034..1baa524 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -15,5 +15,5 @@ jobs: run: make all-via-docker - name: Run omni_lock tests run: cd tests/omni_lock_rust && cargo test - - name: Run omni_lock simulator tests - run: cd tests/omni_lock && bash run.sh +# - name: Run omni_lock simulator tests +# run: cd tests/omni_lock && bash run.sh From 98851394b203a523846a3a413582a5a2ed83d38d Mon Sep 17 00:00:00 2001 From: Joii Date: Fri, 3 Jan 2025 19:47:25 +0800 Subject: [PATCH 3/4] Optimize contract size --- c/secp256k1_helper_20210801.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/c/secp256k1_helper_20210801.h b/c/secp256k1_helper_20210801.h index 262a422..5b85dd9 100644 --- a/c/secp256k1_helper_20210801.h +++ b/c/secp256k1_helper_20210801.h @@ -21,6 +21,8 @@ secp256k1_ge_storage* secp256k1_pre_g = NULL; secp256k1_ge_storage* secp256k1_pre_g_128 = NULL; #undef SECP256K1_NO_BUILD +#define COMB_BLOCKS 1 +#define COMB_TEETH 1 #include #include "modules/recovery/main_impl.h" const secp256k1_ge_storage secp256k1_ecmult_gen_prec_table[COMB_BLOCKS] From 728fac71755b691cd76cda93a48a8a51e2971e45 Mon Sep 17 00:00:00 2001 From: Joii Date: Fri, 3 Jan 2025 21:32:04 +0800 Subject: [PATCH 4/4] Update Script Hash --- tests/omni_lock_rust/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/omni_lock_rust/build.rs b/tests/omni_lock_rust/build.rs index 2cc2266..1d6c0a1 100644 --- a/tests/omni_lock_rust/build.rs +++ b/tests/omni_lock_rust/build.rs @@ -15,7 +15,7 @@ const CKB_HASH_PERSONALIZATION: &[u8] = b"ckb-default-hash"; const BINARIES: &[(&str, &str)] = &[ ( "omni_lock", - "4fc2132f040de19a5355004252a0447421299844dc8e215cb7170c4a7174d7c7", + "b4958a853c610f951773a3e0887ad59b9be8ab23eca1c1e3351fe6fa7a565d69", ), ];