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

Use patched secp256k1-sys #3

Merged
merged 6 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "util/gen-fixtures/secp256k1"]
path = util/gen-fixtures/secp256k1
url = https://github.com/bitcoin-core/secp256k1.git
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/util/gen-fixtures/secp256k1
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ build-wasm-cp = cp -f target/wasm32-unknown-unknown/$(1)/tiny_secp256k1_wasm.was
build-wasm:
cargo build --target wasm32-unknown-unknown --release
$(call build-wasm-cp,release)
wasm-opt --strip-debug --strip-producers --output lib/secp256k1.wasm lib/secp256k1.wasm
node ./util/wasm-strip.js lib/secp256k1.wasm
wasm-opt -O4 --output lib/secp256k1.wasm lib/secp256k1.wasm

build-wasm-debug:
Expand All @@ -15,7 +17,7 @@ format:

lint:
cargo fmt -- --check
cargo clippy
cargo clippy --target wasm32-unknown-unknown
npx prettier -c .

test:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"license": "MIT",
"main": "lib/index.js",
"devDependencies": {
"binaryen": "^100.0.0",
"prettier": "^2.2.1",
"tap-difflet": "^0.7.2",
"tape": "^5.2.2"
Expand Down
22 changes: 0 additions & 22 deletions scripts/Makefile

This file was deleted.

3 changes: 2 additions & 1 deletion secp256k1-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ publish = false
crate-type = ["cdylib"]

[dependencies]
secp256k1-sys = "0.4.0"
# `[patch.crates-io]` is not working :(
secp256k1-sys = { version = "0.4.0", default-features = false, git = "https://github.com/fanatid/rust-secp256k1", branch = "more-features" }
51 changes: 32 additions & 19 deletions secp256k1-wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
#![no_std]
#![feature(core_intrinsics)]

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
core::intrinsics::abort()
}

#[cfg(not(target_arch = "wasm32"))]
compile_error!("Only `wasm32` target_arch is supported.");

use secp256k1_sys::{
secp256k1_context_no_precomp, secp256k1_context_preallocated_create,
secp256k1_context_preallocated_size, secp256k1_context_randomize, secp256k1_ec_pubkey_combine,
Expand Down Expand Up @@ -75,30 +86,32 @@ fn initialize_context_seed() {
unsafe {
for offset in (0..8).map(|v| v * 4) {
let value = generate_int32();
let bytes: [u8; 4] = std::mem::transmute(value);
let bytes: [u8; 4] = core::mem::transmute(value);
CONTEXT_SEED[offset..offset + 4].copy_from_slice(&bytes);
}
}
}

fn get_context() -> *const Context {
static mut CONTEXT: *const Context = std::ptr::null();
static ONCE: std::sync::Once = std::sync::Once::new();
ONCE.call_once(|| unsafe {
let size =
secp256k1_context_preallocated_size(SECP256K1_START_SIGN | SECP256K1_START_VERIFY);
assert_eq!(size, CONTEXT_BUFFER.len());
let ctx = secp256k1_context_preallocated_create(
CONTEXT_BUFFER.as_ptr() as *mut c_void,
SECP256K1_START_SIGN | SECP256K1_START_VERIFY,
);
initialize_context_seed();
let retcode = secp256k1_context_randomize(ctx, CONTEXT_SEED.as_ptr());
CONTEXT_SEED.fill(0);
assert_eq!(retcode, 1);
CONTEXT = ctx
});
unsafe { CONTEXT }
static mut CONTEXT: *const Context = core::ptr::null();
unsafe {
if CONTEXT_SEED[0] == 0 {
let size =
secp256k1_context_preallocated_size(SECP256K1_START_SIGN | SECP256K1_START_VERIFY);
assert_eq!(size, CONTEXT_BUFFER.len());
let ctx = secp256k1_context_preallocated_create(
CONTEXT_BUFFER.as_ptr() as *mut c_void,
SECP256K1_START_SIGN | SECP256K1_START_VERIFY,
);
initialize_context_seed();
let retcode = secp256k1_context_randomize(ctx, CONTEXT_SEED.as_ptr());
CONTEXT_SEED[0] = 1;
CONTEXT_SEED[1..].fill(0);
assert_eq!(retcode, 1);
CONTEXT = ctx
}
CONTEXT
}
}

unsafe fn pubkey_parse(input: *const u8, inputlen: usize) -> InvalidInputResult<PublicKey> {
Expand Down Expand Up @@ -262,7 +275,7 @@ pub extern "C" fn sign(extra_data: i32) {
unsafe {
let mut sig = Signature::new();
let noncedata = if extra_data == 0 {
std::ptr::null()
core::ptr::null()
} else {
EXTRA_DATA_INPUT.as_ptr()
} as *const c_void;
Expand Down
File renamed without changes.
29 changes: 29 additions & 0 deletions util/gen-fixtures/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
all: json-privates json-points json-ecdsa

.PHONY: secp256k1
secp256k1:
cd secp256k1 && ./autogen.sh && ./configure && make

privates: secp256k1
privates: privates.cpp shared.hpp
g++ $< -Lsecp256k1/.libs/ -lgmp -l:libsecp256k1.a -o $@

points: secp256k1
points: points.cpp shared.hpp
g++ $< -Lsecp256k1/.libs/ -lgmp -l:libsecp256k1.a -o $@

ecdsa: secp256k1
ecdsa: ecdsa.cpp shared.hpp
g++ $< -Lsecp256k1/.libs/ -lgmp -lcrypto -lssl -l:libsecp256k1.a -o $@

clean:
rm privates points ecdsa && cd secp256k1 && make clean

json-points: points
./points | jq . > ../../tests/fixtures/points.json

json-privates: privates
./privates | jq . > ../../tests/fixtures/privates.json

json-ecdsa: ecdsa
./ecdsa | jq . > ../../tests/fixtures/ecdsa.json
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions util/gen-fixtures/secp256k1
Submodule secp256k1 added at 4c3ba8
2 changes: 1 addition & 1 deletion scripts/shared.hpp → util/gen-fixtures/shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <sstream>
#include <vector>

#include "../native/secp256k1/include/secp256k1.h"
#include "secp256k1/include/secp256k1.h"
#include "hexxer.hpp"
#include "json.hpp"

Expand Down
73 changes: 73 additions & 0 deletions util/wasm-strip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const fs = require("fs");
const binaryen = require("binaryen");

const NOT_USED_FUNCTIONS = [
"rustsecp256k1_v0_4_0_default_error_callback_fn",
"rustsecp256k1_v0_4_0_default_illegal_callback_fn",
"rustsecp256k1_v0_4_0_context_preallocated_clone_size",
"rustsecp256k1_v0_4_0_context_preallocated_clone",
"rustsecp256k1_v0_4_0_context_preallocated_destroy",
"rustsecp256k1_v0_4_0_context_set_illegal_callback",
"rustsecp256k1_v0_4_0_context_set_error_callback",
"rustsecp256k1_v0_4_0_ecdsa_signature_parse_der",
"rustsecp256k1_v0_4_0_ecdsa_signature_serialize_der",
"rustsecp256k1_v0_4_0_ec_seckey_verify",
"rustsecp256k1_v0_4_0_ec_privkey_negate",
"rustsecp256k1_v0_4_0_ec_pubkey_negate",
"rustsecp256k1_v0_4_0_ec_privkey_tweak_add",
"rustsecp256k1_v0_4_0_ec_seckey_tweak_mul",
"rustsecp256k1_v0_4_0_ec_privkey_tweak_mul",
];
const NOT_USED_GLOBALS = ["rustsecp256k1_v0_4_0_nonce_function_default"];

const NOT_EXPORTED_FUNCTIONS = [
"rustsecp256k1_v0_4_0_context_preallocated_size",
"rustsecp256k1_v0_4_0_context_preallocated_create",
"rustsecp256k1_v0_4_0_context_randomize",
"rustsecp256k1_v0_4_0_context_no_precomp",
"rustsecp256k1_v0_4_0_ec_pubkey_parse",
"rustsecp256k1_v0_4_0_ec_pubkey_combine",
"rustsecp256k1_v0_4_0_ec_pubkey_serialize",
"rustsecp256k1_v0_4_0_ec_pubkey_tweak_add",
"rustsecp256k1_v0_4_0_ec_pubkey_create",
"rustsecp256k1_v0_4_0_ec_pubkey_tweak_mul",
"rustsecp256k1_v0_4_0_ec_seckey_tweak_add",
"rustsecp256k1_v0_4_0_ec_seckey_negate",
"rustsecp256k1_v0_4_0_nonce_function_rfc6979",
"rustsecp256k1_v0_4_0_ecdsa_sign",
"rustsecp256k1_v0_4_0_ecdsa_signature_serialize_compact",
"rustsecp256k1_v0_4_0_ecdsa_signature_parse_compact",
"rustsecp256k1_v0_4_0_ecdsa_signature_normalize",
"rustsecp256k1_v0_4_0_ecdsa_verify",
];

function strip(input) {
const module = binaryen.readBinary(input);

for (const name of NOT_USED_FUNCTIONS) {
module.removeFunction(name);
module.removeExport(name);
}
for (const name of NOT_USED_GLOBALS) {
module.removeGlobal(name);
module.removeExport(name);
}

for (const name of NOT_EXPORTED_FUNCTIONS) {
module.removeExport(name);
}

return module.emitBinary();
}

function main(location) {
const input = fs.readFileSync(location);
const output = strip(input);

const change = input.length - output.length;
console.log(`Size: ${input.length} -> ${output.length} (save ${change}+)`);

fs.writeFileSync(location, output);
}

main(process.argv[2]);