-
Notifications
You must be signed in to change notification settings - Fork 40
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
35999fe
Add secp256k1-zkp commit 1c830b4c9ac30aa32e246ea3ab8ef9ef99acf664 and…
jonasnick 281fc71
Add as_mut_ptr for Schnorr Signatures
jonasnick 6117826
Allow vendoring a specific C-secp-zkp commit
jonasnick 62cbb23
Add serde feature and serde for Schnorr Signatures
jonasnick 46efd67
Impl Send and Sync for ScratchSpace
jonasnick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
**/*.rs.bk |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[workspace] | ||
members = [ | ||
"secp256k1-zkp-dev", | ||
"secp256k1-zkp-sys", | ||
"secp256k1-zkp" | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
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) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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") becauseSecretKey
isn't defined in this crate. This looks ergonomic but a downside is that copy & pasted code starts diverging from rust-secp.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.