Skip to content

Commit d3f9fae

Browse files
committed
[ecash-secp256k1] Add ecash-secp256k1-sys
Summary: Import the `secp256k1-sys` crate from [[ https://github.com/rust-bitcoin/rust-secp256k1 | rust-secp256k1 ]] repository, and apply the following modifications: - Removed the vendored secp256k1 library imported from bitcoin-core - Use our secp256k1 library of the monorepo - Add ABC's `secp256k1_schnorr_sign` and `secp256k1_schnorr_verify`, which are the Schnorr signatures used by ABC. These are distinct from the x-only pubkey BIP340 signatures from BTC, but we keep them to simplify backports, and to (in the future) allow eCash apps to also talk some BTC. - Port the GitHub CI from the repository to a test.sh. Those depend on https://github.com/rust-bitcoin/rust-bitcoin-maintainer-tools, so the relevant parts have been ported and cleaned up. - Adapt the Rust workspace structure to our workspace. - Reformat everything with our formatting rules. Test Plan: `./modules/ecash-secp256k1/contrib/test.sh` Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Differential Revision: https://reviews.bitcoinabc.org/D16953
1 parent 7539ac9 commit d3f9fae

File tree

15 files changed

+2613
-0
lines changed

15 files changed

+2613
-0
lines changed

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ members = [
1818
"chronik/chronik-proto",
1919
"chronik/chronik-util",
2020
"modules/ecash-lib-wasm",
21+
"modules/ecash-secp256k1/ecash-secp256k1-sys",
2122
]
2223

2324
[workspace.package]

contrib/teamcity/build-configurations.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,14 @@ builds:
552552
- - bench-secp256k1
553553
timeout: 1200
554554

555+
build-ecash-secp256k1:
556+
runOnDiffRegex:
557+
- src/secp256k1/
558+
- modules/ecash-secp256k1/
559+
script: |
560+
"${TOPLEVEL}/modules/ecash-secp256k1/contrib/test.sh"
561+
timeout: 1200
562+
555563
build-tsan:
556564
Werror: true
557565
clang: true
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env bash
2+
3+
export LC_ALL=C.UTF-8
4+
5+
set -euox pipefail
6+
7+
# Setup
8+
: "${TOPLEVEL:=$(git rev-parse --show-toplevel)}"
9+
10+
# Use the current `Cargo.lock` file without updating it.
11+
CARGO="cargo --locked"
12+
13+
# Make all cargo invocations verbose.
14+
export CARGO_TERM_VERBOSE=true
15+
16+
main() {
17+
ecash_secp256k1_sys
18+
}
19+
20+
ecash_secp256k1_sys() {
21+
FEATURES_WITH_STD="lowmemory recovery"
22+
FEATURES_WITHOUT_STD="lowmemory recovery alloc"
23+
24+
# Navigate to ecash-secp256k1-sys
25+
pushd "${TOPLEVEL}/modules/ecash-secp256k1/ecash-secp256k1-sys"
26+
27+
run_tests
28+
}
29+
30+
run_tests() {
31+
# Defaults / sanity checks
32+
$CARGO build
33+
$CARGO clippy
34+
$CARGO test
35+
36+
# All features disabled
37+
$CARGO build --no-default-features
38+
$CARGO clippy --no-default-features
39+
$CARGO test --no-default-features
40+
41+
# Test feature combinations
42+
loop_features "std" "${FEATURES_WITH_STD}"
43+
loop_features "" "${FEATURES_WITHOUT_STD}"
44+
45+
# Extra tests
46+
RUSTFLAGS='--cfg=secp256k1_fuzz' RUSTDOCFLAGS='--cfg=secp256k1_fuzz' $CARGO test --locked
47+
RUSTFLAGS='--cfg=secp256k1_fuzz' RUSTDOCFLAGS='--cfg=secp256k1_fuzz' $CARGO test --locked --features="$FEATURES_WITH_STD"
48+
49+
build_docs
50+
build_wasm
51+
52+
popd
53+
}
54+
55+
# Build with each feature as well as all combinations of two features.
56+
#
57+
# Usage: loop_features "std" "this-feature that-feature other"
58+
loop_features() {
59+
local use="${1:-}" # Allow empty string.
60+
local features="$2" # But require features.
61+
62+
# All the provided features including $use
63+
$CARGO build --no-default-features --features="$use $features"
64+
$CARGO test --no-default-features --features="$use $features"
65+
$CARGO clippy --no-default-features --features="$use $features"
66+
67+
read -r -a array <<< "$features"
68+
local len="${#array[@]}"
69+
70+
if (( len > 1 )); then
71+
for ((i = 0 ; i < len ; i++ ));
72+
do
73+
$CARGO build --no-default-features --features="$use ${array[i]}"
74+
$CARGO test --no-default-features --features="$use ${array[i]}"
75+
$CARGO clippy --no-default-features --features="$use ${array[i]}"
76+
77+
if (( i < len - 1 )); then
78+
for ((j = i + 1 ; j < len ; j++ ));
79+
do
80+
$CARGO build --no-default-features --features="$use ${array[i]} ${array[j]}"
81+
$CARGO test --no-default-features --features="$use ${array[i]} ${array[j]}"
82+
$CARGO clippy --no-default-features --features="$use ${array[i]} ${array[j]}"
83+
done
84+
fi
85+
done
86+
fi
87+
}
88+
89+
# Build the docs with a stable toolchain, in unison with the function
90+
# above this checks that we feature guarded docs imports correctly.
91+
build_docs() {
92+
RUSTDOCFLAGS="-D warnings" $CARGO doc --all-features
93+
}
94+
95+
build_wasm() {
96+
CFLAGS="-Wno-pointer-sign -Wno-implicit-function-declaration" \
97+
RUSTFLAGS="-C strip=debuginfo" \
98+
$CARGO build \
99+
--profile=release-wasm \
100+
--target=wasm32-unknown-unknown
101+
}
102+
103+
# Main script
104+
main "$@"
105+
exit 0
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[package]
2+
name = "ecash-secp256k1-sys"
3+
version = "0.10.0"
4+
authors = [
5+
"Dawid Ciężarkiewicz <[email protected]>",
6+
"Andrew Poelstra <[email protected]>",
7+
"Steven Roose <[email protected]>",
8+
"Tobias Ruck <[email protected]>",
9+
]
10+
license = "CC0-1.0"
11+
homepage = "https://e.cash"
12+
repository = "https://github.com/Bitcoin-ABC/bitcoin-abc/"
13+
description = "FFI for the Bitcoin ABC version of Pieter Wuille's `libsecp256k1` library."
14+
readme = "README.md"
15+
build = "build.rs"
16+
edition = "2021"
17+
rust-version.workspace = true
18+
19+
[package.metadata.docs.rs]
20+
all-features = true
21+
rustdoc-args = ["--cfg", "docsrs"]
22+
23+
[build-dependencies]
24+
cc = "1.0.28"
25+
26+
[dev-dependencies]
27+
libc = "0.2"
28+
29+
[features]
30+
default = ["std"]
31+
recovery = []
32+
lowmemory = []
33+
std = ["alloc"]
34+
alloc = []
35+
36+
[lints.rust.unexpected_cfgs]
37+
level = "deny"
38+
check-cfg = ['cfg(bench)', 'cfg(secp256k1_fuzz)']
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
Creative Commons Legal Code
2+
3+
CC0 1.0 Universal
4+
5+
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
6+
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
7+
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
8+
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
9+
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
10+
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
11+
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
12+
HEREUNDER.
13+
14+
Statement of Purpose
15+
16+
The laws of most jurisdictions throughout the world automatically confer
17+
exclusive Copyright and Related Rights (defined below) upon the creator
18+
and subsequent owner(s) (each and all, an "owner") of an original work of
19+
authorship and/or a database (each, a "Work").
20+
21+
Certain owners wish to permanently relinquish those rights to a Work for
22+
the purpose of contributing to a commons of creative, cultural and
23+
scientific works ("Commons") that the public can reliably and without fear
24+
of later claims of infringement build upon, modify, incorporate in other
25+
works, reuse and redistribute as freely as possible in any form whatsoever
26+
and for any purposes, including without limitation commercial purposes.
27+
These owners may contribute to the Commons to promote the ideal of a free
28+
culture and the further production of creative, cultural and scientific
29+
works, or to gain reputation or greater distribution for their Work in
30+
part through the use and efforts of others.
31+
32+
For these and/or other purposes and motivations, and without any
33+
expectation of additional consideration or compensation, the person
34+
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
35+
is an owner of Copyright and Related Rights in the Work, voluntarily
36+
elects to apply CC0 to the Work and publicly distribute the Work under its
37+
terms, with knowledge of his or her Copyright and Related Rights in the
38+
Work and the meaning and intended legal effect of CC0 on those rights.
39+
40+
1. Copyright and Related Rights. A Work made available under CC0 may be
41+
protected by copyright and related or neighboring rights ("Copyright and
42+
Related Rights"). Copyright and Related Rights include, but are not
43+
limited to, the following:
44+
45+
i. the right to reproduce, adapt, distribute, perform, display,
46+
communicate, and translate a Work;
47+
ii. moral rights retained by the original author(s) and/or performer(s);
48+
iii. publicity and privacy rights pertaining to a person's image or
49+
likeness depicted in a Work;
50+
iv. rights protecting against unfair competition in regards to a Work,
51+
subject to the limitations in paragraph 4(a), below;
52+
v. rights protecting the extraction, dissemination, use and reuse of data
53+
in a Work;
54+
vi. database rights (such as those arising under Directive 96/9/EC of the
55+
European Parliament and of the Council of 11 March 1996 on the legal
56+
protection of databases, and under any national implementation
57+
thereof, including any amended or successor version of such
58+
directive); and
59+
vii. other similar, equivalent or corresponding rights throughout the
60+
world based on applicable law or treaty, and any national
61+
implementations thereof.
62+
63+
2. Waiver. To the greatest extent permitted by, but not in contravention
64+
of, applicable law, Affirmer hereby overtly, fully, permanently,
65+
irrevocably and unconditionally waives, abandons, and surrenders all of
66+
Affirmer's Copyright and Related Rights and associated claims and causes
67+
of action, whether now known or unknown (including existing as well as
68+
future claims and causes of action), in the Work (i) in all territories
69+
worldwide, (ii) for the maximum duration provided by applicable law or
70+
treaty (including future time extensions), (iii) in any current or future
71+
medium and for any number of copies, and (iv) for any purpose whatsoever,
72+
including without limitation commercial, advertising or promotional
73+
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
74+
member of the public at large and to the detriment of Affirmer's heirs and
75+
successors, fully intending that such Waiver shall not be subject to
76+
revocation, rescission, cancellation, termination, or any other legal or
77+
equitable action to disrupt the quiet enjoyment of the Work by the public
78+
as contemplated by Affirmer's express Statement of Purpose.
79+
80+
3. Public License Fallback. Should any part of the Waiver for any reason
81+
be judged legally invalid or ineffective under applicable law, then the
82+
Waiver shall be preserved to the maximum extent permitted taking into
83+
account Affirmer's express Statement of Purpose. In addition, to the
84+
extent the Waiver is so judged Affirmer hereby grants to each affected
85+
person a royalty-free, non transferable, non sublicensable, non exclusive,
86+
irrevocable and unconditional license to exercise Affirmer's Copyright and
87+
Related Rights in the Work (i) in all territories worldwide, (ii) for the
88+
maximum duration provided by applicable law or treaty (including future
89+
time extensions), (iii) in any current or future medium and for any number
90+
of copies, and (iv) for any purpose whatsoever, including without
91+
limitation commercial, advertising or promotional purposes (the
92+
"License"). The License shall be deemed effective as of the date CC0 was
93+
applied by Affirmer to the Work. Should any part of the License for any
94+
reason be judged legally invalid or ineffective under applicable law, such
95+
partial invalidity or ineffectiveness shall not invalidate the remainder
96+
of the License, and in such case Affirmer hereby affirms that he or she
97+
will not (i) exercise any of his or her remaining Copyright and Related
98+
Rights in the Work or (ii) assert any associated claims and causes of
99+
action with respect to the Work, in either case contrary to Affirmer's
100+
express Statement of Purpose.
101+
102+
4. Limitations and Disclaimers.
103+
104+
a. No trademark or patent rights held by Affirmer are waived, abandoned,
105+
surrendered, licensed or otherwise affected by this document.
106+
b. Affirmer offers the Work as-is and makes no representations or
107+
warranties of any kind concerning the Work, express, implied,
108+
statutory or otherwise, including without limitation warranties of
109+
title, merchantability, fitness for a particular purpose, non
110+
infringement, or the absence of latent or other defects, accuracy, or
111+
the present or absence of errors, whether or not discoverable, all to
112+
the greatest extent permissible under applicable law.
113+
c. Affirmer disclaims responsibility for clearing rights of other persons
114+
that may apply to the Work or any use thereof, including without
115+
limitation any person's Copyright and Related Rights in the Work.
116+
Further, Affirmer disclaims responsibility for obtaining any necessary
117+
consents, permissions or other rights required for any use of the
118+
Work.
119+
d. Affirmer understands and acknowledges that Creative Commons is not a
120+
party to this document and has no duty or obligation with respect to
121+
this CC0 or use of the Work.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
3+
//! # Build script
4+
5+
// Coding conventions
6+
#![deny(non_upper_case_globals)]
7+
#![deny(non_camel_case_types)]
8+
#![deny(non_snake_case)]
9+
#![deny(unused_mut)]
10+
#![warn(missing_docs)]
11+
12+
use std::env;
13+
14+
// Macro pointing to our secp256k1 library
15+
macro_rules! secp256k1path {
16+
($path:literal) => {
17+
concat!("../../../src/secp256k1", $path)
18+
};
19+
}
20+
21+
fn main() {
22+
// Configure building secp256k1
23+
let mut base_config = cc::Build::new();
24+
base_config
25+
.include(secp256k1path!("/"))
26+
.include(secp256k1path!("/include"))
27+
.include(secp256k1path!("/src"))
28+
// some ecmult stuff is defined but not used upstream
29+
.flag_if_supported("-Wno-unused-function")
30+
// patching out printf causes this warning
31+
.flag_if_supported("-Wno-unused-parameter")
32+
.define("SECP256K1_API", Some(""))
33+
.define("ENABLE_MODULE_ECDH", Some("1"))
34+
.define("ENABLE_MODULE_SCHNORRSIG", Some("1"))
35+
.define("ENABLE_MODULE_SCHNORR", Some("1"))
36+
.define("ENABLE_MODULE_EXTRAKEYS", Some("1"))
37+
// upstream sometimes introduces calls to printf, which we cannot
38+
// compile with WASM due to its lack of libc. printf is never
39+
// necessary and we can just #define it away.
40+
.define("printf(...)", Some(""));
41+
42+
if cfg!(feature = "lowmemory") {
43+
// A low-enough value to consume negligible memory
44+
base_config.define("ECMULT_WINDOW_SIZE", Some("4"));
45+
base_config.define("ECMULT_GEN_PREC_BITS", Some("2"));
46+
} else {
47+
base_config.define("ECMULT_GEN_PREC_BITS", Some("4"));
48+
// This is the default in the configure file (`auto`)
49+
base_config.define("ECMULT_WINDOW_SIZE", Some("15"));
50+
}
51+
base_config.define("USE_EXTERNAL_DEFAULT_CALLBACKS", Some("1"));
52+
#[cfg(feature = "recovery")]
53+
base_config.define("ENABLE_MODULE_RECOVERY", Some("1"));
54+
55+
// WASM headers and size/align defines.
56+
if env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "wasm32" {
57+
base_config.include("wasm/wasm-sysroot").file("wasm/wasm.c");
58+
}
59+
60+
// secp256k1
61+
base_config
62+
.file(secp256k1path!("/contrib/lax_der_parsing.c"))
63+
.file(secp256k1path!("/src/secp256k1.c"));
64+
65+
if base_config.try_compile("libsecp256k1.a").is_err() {
66+
// Some embedded platforms may not have, eg, string.h available, so if
67+
// the build fails simply try again with the wasm sysroot (but
68+
// without the wasm type sizes) in the hopes that it works.
69+
base_config.include("wasm/wasm-sysroot");
70+
base_config.compile("libsecp256k1.a");
71+
}
72+
}

0 commit comments

Comments
 (0)