Skip to content

Commit 2f8adae

Browse files
authored
Merge pull request #1 from ElementsProject/schnorrsig
Add secp256k1-zkp and schnorrsig modules
2 parents 68e228e + 46efd67 commit 2f8adae

File tree

152 files changed

+34455
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

152 files changed

+34455
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
**/*.rs.bk

Cargo.lock

Lines changed: 233 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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[workspace]
2+
members = [
3+
"secp256k1-zkp-dev",
4+
"secp256k1-zkp-sys",
5+
"secp256k1-zkp"
6+
]

contrib/test.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
if [ -z "$1" ]; then
4+
echo This test script runs the workspace tests and the -sys tests with the
5+
echo serde feature enabled. This is necessary because you can not test
6+
echo features on the workspace level. The script also checks if the files are
7+
echo rustfmt\'d.
8+
echo
9+
echo "ERROR: \$1 parameter must be the workspace directory"
10+
exit 1
11+
fi
12+
DIR=$1
13+
14+
shopt -s globstar
15+
16+
(
17+
cd "$DIR"
18+
set -e
19+
cargo test
20+
(
21+
cd secp256k1-zkp-sys
22+
cargo test --features serde
23+
)
24+
rustfmt --check -- **/*.rs
25+
)
26+
27+
if [ $? -ne 0 ]; then
28+
echo ERROR: $0 failed
29+
exit 1
30+
fi
31+

contrib/vendor-libsecp-zkp.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
set -e
3+
4+
5+
if [ -z "$1" ]; then
6+
echo "\$1 parameter must be the rust-secp256k1-zkp-sys depend directory"
7+
echo "\$2 parameter (optional) can be the revision to check out"
8+
exit 1
9+
fi
10+
11+
PARENT_DIR=$1
12+
REV=$2
13+
DIR=secp256k1-zkp
14+
15+
while true; do
16+
read -r -p "$PARENT_DIR/$DIR will be deleted [yn]: " yn
17+
case $yn in
18+
[Yy]* ) break;;
19+
[Nn]* ) exit;;
20+
* ) echo "Please answer yes or no.";;
21+
esac
22+
done
23+
24+
cd "$PARENT_DIR"
25+
rm -rf "$DIR"
26+
git clone [email protected]:ElementsProject/secp256k1-zkp.git
27+
cd "$DIR"
28+
if [ -n "$REV" ]; then
29+
git checkout "$REV"
30+
fi
31+
HEAD=$(git rev-parse HEAD)
32+
cd ..
33+
echo "\# This file was automatically created by $0" > ./secp256k1-zkp-HEAD-revision.txt
34+
echo "$HEAD" >> ./secp256k1-zkp-HEAD-revision.txt
35+
36+
find "$DIR" -not -path '*/\.*' -type f -print0 | xargs -0 sed -i '/^#include/! s/secp256k1_/secp256k1_zkp_/g'
37+
# TODO: can be removed once 496c5b43b lands in secp-zkp
38+
find "$DIR" -not -path '*/\.*' -type f -print0 | xargs -0 sed -i 's/^const int CURVE_B/static const int CURVE_B/g'

secp256k1-zkp-dev/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "secp256k1-zkp-dev"
3+
version = "0.1.0"
4+
authors = ["Jonas Nick <[email protected]>"]
5+
6+
license = "CC0-1.0"
7+
homepage = "https://github.com/ElementsProject/rust-secp256k1-zkp/"
8+
repository = "https://github.com/ElementsProject/rust-secp256k1-zkp/"
9+
description = "dev-dependencies for secp256k1-zkp-sys and secp256k1-zkp."
10+
keywords = [ "crypto", "ECDSA", "Schnorr", "secp256k1", "libsecp256k1", "secp256k1-zkp", "libsecp256k1-zkp", "bitcoin" ]
11+
12+
[dependencies]
13+
rand = "0.6"
14+
15+
[dependencies.secp256k1]
16+
version = "0.13"

secp256k1-zkp-dev/src/lib.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// secp256k1-zkp bindings
2+
// Written in 2019 by
3+
// Jonas Nick
4+
//
5+
// To the extent possible under law, the author(s) have dedicated all
6+
// copyright and related and neighboring rights to this software to
7+
// the public domain worldwide. This software is distributed without
8+
// any warranty.
9+
//
10+
// You should have received a copy of the CC0 Public Domain Dedication
11+
// along with this software.
12+
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
13+
//
14+
15+
/// The functions in this module are copied from secp256k1 because they can only be used when
16+
/// compiled with the `rand` feature. But the other libraries need them as a dev-dependency for
17+
/// `cargo test` also when `rand` is not enabled. Currently with cargo we can't have a `rand`
18+
/// dev-dependency and a non-`rand` dependency at the same time (see
19+
/// https://github.com/rust-lang/cargo/issues/1796).
20+
pub extern crate rand;
21+
pub extern crate secp256k1;
22+
23+
use rand::Rng;
24+
use secp256k1::{PublicKey, Secp256k1, SecretKey, Signing};
25+
26+
fn random_32_bytes<R: Rng>(rng: &mut R) -> [u8; 32] {
27+
let mut ret = [0u8; 32];
28+
rng.fill_bytes(&mut ret);
29+
ret
30+
}
31+
32+
trait NewSecretKey {
33+
fn new<R: Rng>(rng: &mut R) -> SecretKey;
34+
}
35+
36+
impl NewSecretKey for SecretKey {
37+
/// Creates a new random secret key.
38+
#[inline]
39+
fn new<R: Rng>(rng: &mut R) -> SecretKey {
40+
loop {
41+
if let Ok(key) = SecretKey::from_slice(&random_32_bytes(rng)) {
42+
return key;
43+
}
44+
}
45+
}
46+
}
47+
48+
pub trait GenerateKeypair {
49+
/// Generates a random keypair.
50+
fn generate_keypair<R: Rng>(&self, rng: &mut R) -> (SecretKey, PublicKey);
51+
}
52+
53+
impl<C: Signing> GenerateKeypair for Secp256k1<C> {
54+
#[inline]
55+
fn generate_keypair<R: Rng>(&self, rng: &mut R) -> (SecretKey, PublicKey) {
56+
let sk = SecretKey::new(rng);
57+
let pk = PublicKey::from_secret_key(self, &sk);
58+
(sk, pk)
59+
}
60+
}

0 commit comments

Comments
 (0)