Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Update secp256k1 to btc v0.6.0 #7

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,26 +25,18 @@ all: build/omni_lock build/always_success
all-via-docker: ${PROTOCOL_HEADER}
docker run --rm -v `pwd`:/code ${BUILDER_DOCKER} bash -c "cd /code && make"


build/always_success: c/always_success.c
$(CC) $(OMNI_LOCK_CFLAGS) $(LDFLAGS) -o $@ $<
$(OBJCOPY) --only-keep-debug $@ $@.debug
$(OBJCOPY) --strip-debug --strip-all $@

build/secp256k1_data_info_20210801.h: build/dump_secp256k1_data_20210801
$<
rm -f deps/secp256k1-20210801/src/precomputed_ecmult.h

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

gcc -I deps/secp256k1-20210801/src -o $@ $<
Comment on lines -43 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these line removed? New secp256k1 lib don't require config?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

precomputed_ecmult.c already exists in the repositorie and does not need to be regenerated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is too large, about 2.3M bytes. We need a smaller one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the exported secp256k1_data_20210801 is still 1M (1048576).
The size still needs to be controlled at around 20k? (ckb-auth)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's ok to have same size as before (1M).


build/impl.o: deps/ckb-c-std-lib/libc/src/impl.c
$(CC) -c $(filter-out -DCKB_DECLARATION_ONLY, $(CFLAGS_MBEDTLS)) $(LDFLAGS_MBEDTLS) -o $@ $^
Expand Down Expand Up @@ -76,12 +67,11 @@ 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 $@


clean:
rm -rf build/secp256k1_data_info_20210801.h build/dump_secp256k1_data_20210801
rm -rf build/secp256k1_data_20210801
Expand Down
29 changes: 20 additions & 9 deletions c/dump_secp256k1_data_20210801.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@
* unused functions. For some unknown reasons, if we link in libsecp256k1.a
* directly, the final binary will include all functions rather than those used.
*/
#define HAVE_CONFIG_H 1
#include <secp256k1.c>
#include "precomputed_ecmult.c"

#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");
Expand All @@ -37,9 +36,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,
pre128_size);
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);

fprintf(fp, "static uint8_t ckb_secp256k1_data_hash[32] = {\n ");
Expand All @@ -53,5 +51,18 @@ int main(int argc, char* argv[]) {
fprintf(fp, "#endif\n");
fclose(fp);

FILE* fp_inc = fopen("build/precomputed_ecmult.h", "wb");

fprintf(fp_inc, "#ifndef SECP256K1_PRECOMPUTED_ECMULT_H\n");
fprintf(fp_inc, "#define SECP256K1_PRECOMPUTED_ECMULT_H\n");

fprintf(fp_inc, "#define WINDOW_G ECMULT_WINDOW_SIZE\n");
fprintf(fp_inc, "extern secp256k1_ge_storage *secp256k1_pre_g;\n");
fprintf(fp_inc, "extern secp256k1_ge_storage *secp256k1_pre_g_128;\n");

fprintf(fp_inc, "#endif /* SECP256K1_PRECOMPUTED_ECMULT_H */\n");

fclose(fp_inc);

return 0;
}
49 changes: 30 additions & 19 deletions c/secp256k1_helper_20210801.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@
* unused functions. For some unknown reasons, if we link in libsecp256k1.a
* directly, the final binary will include all functions rather than those used.
*/
#define HAVE_CONFIG_H 1
#define USE_EXTERNAL_DEFAULT_CALLBACKS
#include <secp256k1.c>
#include "modules/recovery/main_impl.h"

#include "precomputed_ecmult.h"

const secp256k1_ge_storage secp256k1_ecmult_gen_prec_table[COMB_BLOCKS]
[COMB_POINTS];

secp256k1_ge_storage* secp256k1_pre_g = NULL;
secp256k1_ge_storage* secp256k1_pre_g_128 = NULL;

void secp256k1_default_illegal_callback_fn(const char* str, void* data) {
(void)str;
Expand All @@ -29,12 +37,7 @@ void secp256k1_default_error_callback_fn(const char* str, void* data) {
ckb_exit(CKB_SECP256K1_HELPER_ERROR_ERROR_CALLBACK);
}

/*
* 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) {
int load_secp256k1_data(void* data) {
size_t index = 0;
int running = 1;
while (running && index < SIZE_MAX) {
Expand Down Expand Up @@ -68,20 +71,28 @@ 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_ecmult_context_init(&context->ecmult_ctx);
secp256k1_ecmult_gen_context_init(&context->ecmult_gen_ctx);
secp256k1_pre_g = (secp256k1_ge_storage*)data;
secp256k1_pre_g_128 =
(secp256k1_ge_storage*)(data + CKB_SECP256K1_DATA_PRE_SIZE);
return 0;
}

/* 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;
/*
* 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) {
if (!secp256k1_pre_g && !secp256k1_pre_g_128) {
int ret = load_secp256k1_data(data);
if (ret) return ret;
}

secp256k1_context* ctx =
secp256k1_context_preallocated_create(context, SECP256K1_CONTEXT_VERIFY);
if (!ctx) {
return CKB_SECP256K1_HELPER_ERROR_LOADING_DATA;
}
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion deps/secp256k1-20210801
2 changes: 1 addition & 1 deletion tests/omni_lock_rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const CKB_HASH_PERSONALIZATION: &[u8] = b"ckb-default-hash";
const BINARIES: &[(&str, &str)] = &[
(
"omni_lock",
"768f306681da232ceb0b94f436c5f813377179762a831c5ad8797bd4fd2d118d",
"8f3e1e01b6d8fff5c9fe12f773912902397c733c23cdc42635081cd49326db68",
),
];

Expand Down
Loading