Skip to content

Add secp256k1-zkp and schnorrsig modules #1

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

Merged
merged 5 commits into from
May 28, 2019
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
**/*.rs.bk
233 changes: 233 additions & 0 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[workspace]
members = [
"secp256k1-zkp-dev",
"secp256k1-zkp-sys",
"secp256k1-zkp"
]
31 changes: 31 additions & 0 deletions contrib/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

if [ -z "$1" ]; then
echo This test script runs the workspace tests and the -sys tests with the
echo serde feature enabled. This is necessary because you can not test
echo features on the workspace level. The script also checks if the files are
echo rustfmt\'d.
echo
echo "ERROR: \$1 parameter must be the workspace directory"
exit 1
fi
DIR=$1

shopt -s globstar

(
cd "$DIR"
set -e
cargo test
(
cd secp256k1-zkp-sys
cargo test --features serde
)
rustfmt --check -- **/*.rs
)

if [ $? -ne 0 ]; then
echo ERROR: $0 failed
exit 1
fi

38 changes: 38 additions & 0 deletions contrib/vendor-libsecp-zkp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
set -e


if [ -z "$1" ]; then
echo "\$1 parameter must be the rust-secp256k1-zkp-sys depend directory"
echo "\$2 parameter (optional) can be the revision to check out"
exit 1
fi

PARENT_DIR=$1
REV=$2
DIR=secp256k1-zkp

while true; do
read -r -p "$PARENT_DIR/$DIR will be deleted [yn]: " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done

cd "$PARENT_DIR"
rm -rf "$DIR"
git clone [email protected]:ElementsProject/secp256k1-zkp.git
cd "$DIR"
if [ -n "$REV" ]; then
git checkout "$REV"
fi
HEAD=$(git rev-parse HEAD)
cd ..
echo "\# This file was automatically created by $0" > ./secp256k1-zkp-HEAD-revision.txt
echo "$HEAD" >> ./secp256k1-zkp-HEAD-revision.txt

find "$DIR" -not -path '*/\.*' -type f -print0 | xargs -0 sed -i '/^#include/! s/secp256k1_/secp256k1_zkp_/g'
# TODO: can be removed once 496c5b43b lands in secp-zkp
find "$DIR" -not -path '*/\.*' -type f -print0 | xargs -0 sed -i 's/^const int CURVE_B/static const int CURVE_B/g'
16 changes: 16 additions & 0 deletions secp256k1-zkp-dev/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "secp256k1-zkp-dev"
version = "0.1.0"
authors = ["Jonas Nick <[email protected]>"]

license = "CC0-1.0"
homepage = "https://github.com/ElementsProject/rust-secp256k1-zkp/"
repository = "https://github.com/ElementsProject/rust-secp256k1-zkp/"
description = "dev-dependencies for secp256k1-zkp-sys and secp256k1-zkp."
keywords = [ "crypto", "ECDSA", "Schnorr", "secp256k1", "libsecp256k1", "secp256k1-zkp", "libsecp256k1-zkp", "bitcoin" ]

[dependencies]
rand = "0.6"

[dependencies.secp256k1]
version = "0.13"
60 changes: 60 additions & 0 deletions secp256k1-zkp-dev/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// secp256k1-zkp bindings
// Written in 2019 by
// Jonas Nick
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//

/// The functions in this module are copied from secp256k1 because they can only be used when
/// compiled with the `rand` feature. But the other libraries need them as a dev-dependency for
/// `cargo test` also when `rand` is not enabled. Currently with cargo we can't have a `rand`
/// dev-dependency and a non-`rand` dependency at the same time (see
/// https://github.com/rust-lang/cargo/issues/1796).
pub extern crate rand;
pub extern crate secp256k1;

use rand::Rng;
use secp256k1::{PublicKey, Secp256k1, SecretKey, Signing};

fn random_32_bytes<R: Rng>(rng: &mut R) -> [u8; 32] {
let mut ret = [0u8; 32];
rng.fill_bytes(&mut ret);
ret
}

trait NewSecretKey {
fn new<R: Rng>(rng: &mut R) -> SecretKey;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you want impl Distribution<SecretKey> for Standard rather than this trait.

See the second example in https://docs.rs/rand/0.6.5/rand/distributions/index.html

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm getting E0117 ("impl doesn't use types inside crate") because SecretKey isn't defined in this crate. This looks ergonomic but a downside is that copy & pasted code starts diverging from rust-secp.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you PR to rust-secp? Or then do we run into the problem with dev-dependencies features?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, yes we'd run into that problem.

}

impl NewSecretKey for SecretKey {
/// Creates a new random secret key.
#[inline]
fn new<R: Rng>(rng: &mut R) -> SecretKey {
loop {
if let Ok(key) = SecretKey::from_slice(&random_32_bytes(rng)) {
return key;
}
}
}
}

pub trait GenerateKeypair {
/// Generates a random keypair.
fn generate_keypair<R: Rng>(&self, rng: &mut R) -> (SecretKey, PublicKey);
}

impl<C: Signing> GenerateKeypair for Secp256k1<C> {
#[inline]
fn generate_keypair<R: Rng>(&self, rng: &mut R) -> (SecretKey, PublicKey) {
let sk = SecretKey::new(rng);
let pk = PublicKey::from_secret_key(self, &sk);
(sk, pk)
}
}
Loading