2014-08-12 02:26:14 +00:00
|
|
|
// Bitcoin secp256k1 bindings
|
|
|
|
// Written in 2014 by
|
|
|
|
// Dawid Ciężarkiewicz
|
|
|
|
// Andrew Poelstra
|
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
//
|
2014-08-09 20:27:08 +00:00
|
|
|
|
|
|
|
//! # Secp256k1
|
|
|
|
//! Rust bindings for Pieter Wuille's secp256k1 library, which is used for
|
|
|
|
//! fast and accurate manipulation of ECDSA signatures on the secp256k1
|
|
|
|
//! curve. Such signatures are used extensively by the Bitcoin network
|
|
|
|
//! and its derivatives.
|
|
|
|
//!
|
|
|
|
|
2014-07-07 05:41:22 +00:00
|
|
|
#![crate_type = "lib"]
|
|
|
|
#![crate_type = "rlib"]
|
|
|
|
#![crate_type = "dylib"]
|
2015-03-25 22:20:44 +00:00
|
|
|
#![crate_name = "secp256k1"]
|
2015-01-17 16:13:45 +00:00
|
|
|
|
|
|
|
// Keep this until 1.0 I guess; it's needed for `black_box` at least
|
|
|
|
#![allow(unstable)]
|
2014-07-23 23:11:18 +00:00
|
|
|
|
2014-08-09 20:27:08 +00:00
|
|
|
// Coding conventions
|
2015-01-17 16:13:45 +00:00
|
|
|
#![deny(non_upper_case_globals)]
|
2014-08-09 20:27:08 +00:00
|
|
|
#![deny(non_camel_case_types)]
|
2014-08-30 14:24:44 +00:00
|
|
|
#![deny(non_snake_case)]
|
2014-08-09 20:27:08 +00:00
|
|
|
#![deny(unused_mut)]
|
2015-01-17 16:13:45 +00:00
|
|
|
#![warn(missing_docs)]
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2015-01-17 16:13:45 +00:00
|
|
|
extern crate crypto;
|
2014-09-01 03:26:02 +00:00
|
|
|
|
2014-07-07 05:41:22 +00:00
|
|
|
extern crate libc;
|
2015-03-25 23:22:24 +00:00
|
|
|
extern crate rand;
|
2014-09-04 19:29:24 +00:00
|
|
|
extern crate serialize;
|
2014-09-01 03:26:02 +00:00
|
|
|
extern crate test;
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2015-03-25 19:10:02 +00:00
|
|
|
use std::intrinsics::copy_nonoverlapping;
|
|
|
|
use std::io;
|
2015-03-25 23:22:24 +00:00
|
|
|
use std::rand::OsRng;
|
2015-01-17 16:13:45 +00:00
|
|
|
use std::sync::{Once, ONCE_INIT};
|
2014-08-09 20:27:08 +00:00
|
|
|
use libc::c_int;
|
2015-03-25 23:22:24 +00:00
|
|
|
use rand::{Rng, SeedableRng};
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-09-12 13:28:35 +00:00
|
|
|
use crypto::fortuna::Fortuna;
|
|
|
|
|
2015-01-17 16:13:45 +00:00
|
|
|
#[macro_use]
|
2014-08-27 17:19:10 +00:00
|
|
|
mod macros;
|
2014-08-10 01:03:17 +00:00
|
|
|
pub mod constants;
|
2014-08-09 20:27:08 +00:00
|
|
|
pub mod ffi;
|
2014-08-10 01:03:17 +00:00
|
|
|
pub mod key;
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-09-05 01:09:18 +00:00
|
|
|
/// I dunno where else to put this..
|
|
|
|
fn assert_type_is_copy<T: Copy>() { }
|
|
|
|
|
2014-08-10 01:03:17 +00:00
|
|
|
/// A tag used for recovering the public key from a compact signature
|
|
|
|
pub struct RecoveryId(i32);
|
2015-01-17 16:13:45 +00:00
|
|
|
impl Copy for RecoveryId {}
|
2014-08-09 20:27:08 +00:00
|
|
|
|
2014-08-10 01:03:17 +00:00
|
|
|
/// An ECDSA signature
|
2015-01-17 16:13:45 +00:00
|
|
|
pub struct Signature(usize, [u8; constants::MAX_SIGNATURE_SIZE]);
|
|
|
|
impl Copy for Signature {}
|
2014-08-10 01:03:17 +00:00
|
|
|
|
|
|
|
impl Signature {
|
2014-09-04 16:52:25 +00:00
|
|
|
/// Converts the signature to a raw pointer suitable for use
|
|
|
|
/// with the FFI functions
|
|
|
|
#[inline]
|
|
|
|
pub fn as_ptr(&self) -> *const u8 {
|
|
|
|
let &Signature(_, ref data) = self;
|
|
|
|
data.as_slice().as_ptr()
|
|
|
|
}
|
|
|
|
|
2014-08-10 01:03:17 +00:00
|
|
|
/// Converts the signature to a mutable raw pointer suitable for use
|
|
|
|
/// with the FFI functions
|
|
|
|
#[inline]
|
|
|
|
pub fn as_mut_ptr(&mut self) -> *mut u8 {
|
2015-01-17 16:13:45 +00:00
|
|
|
let &mut Signature(_, ref mut data) = self;
|
2014-08-16 06:43:40 +00:00
|
|
|
data.as_mut_slice().as_mut_ptr()
|
2014-08-10 01:03:17 +00:00
|
|
|
}
|
2014-08-09 20:27:08 +00:00
|
|
|
|
2014-08-10 01:03:17 +00:00
|
|
|
/// Converts the signature to a byte slice suitable for verification
|
|
|
|
#[inline]
|
|
|
|
pub fn as_slice<'a>(&'a self) -> &'a [u8] {
|
2014-08-16 06:43:40 +00:00
|
|
|
let &Signature(len, ref data) = self;
|
|
|
|
data.slice_to(len)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the length of the signature
|
|
|
|
#[inline]
|
2015-01-17 16:13:45 +00:00
|
|
|
pub fn len(&self) -> usize {
|
2014-08-16 06:43:40 +00:00
|
|
|
let &Signature(len, _) = self;
|
|
|
|
len
|
2014-08-10 01:03:17 +00:00
|
|
|
}
|
2014-09-04 16:52:25 +00:00
|
|
|
|
|
|
|
/// Converts a byte slice to a signature
|
|
|
|
#[inline]
|
|
|
|
pub fn from_slice(data: &[u8]) -> Result<Signature> {
|
|
|
|
if data.len() <= constants::MAX_SIGNATURE_SIZE {
|
2015-01-17 16:13:45 +00:00
|
|
|
let mut ret = [0; constants::MAX_SIGNATURE_SIZE];
|
2014-09-04 16:52:25 +00:00
|
|
|
unsafe {
|
2015-03-25 19:10:02 +00:00
|
|
|
copy_nonoverlapping(ret.as_mut_ptr(),
|
|
|
|
data.as_ptr(),
|
|
|
|
data.len());
|
2014-09-04 16:52:25 +00:00
|
|
|
}
|
|
|
|
Ok(Signature(data.len(), ret))
|
|
|
|
} else {
|
2015-01-17 16:13:45 +00:00
|
|
|
Err(Error::InvalidSignature)
|
2014-09-04 16:52:25 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-07 05:41:22 +00:00
|
|
|
}
|
|
|
|
|
2014-08-09 20:27:08 +00:00
|
|
|
/// An ECDSA error
|
2015-03-25 18:59:54 +00:00
|
|
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
2014-07-07 05:41:22 +00:00
|
|
|
pub enum Error {
|
2014-08-10 01:03:17 +00:00
|
|
|
/// Signature failed verification
|
|
|
|
IncorrectSignature,
|
2014-08-09 20:27:08 +00:00
|
|
|
/// Bad public key
|
2014-07-07 05:41:22 +00:00
|
|
|
InvalidPublicKey,
|
2014-08-09 20:27:08 +00:00
|
|
|
/// Bad signature
|
2014-07-07 05:41:22 +00:00
|
|
|
InvalidSignature,
|
2014-08-09 20:27:08 +00:00
|
|
|
/// Bad secret key
|
2014-07-07 05:41:22 +00:00
|
|
|
InvalidSecretKey,
|
2014-08-09 20:27:08 +00:00
|
|
|
/// Bad nonce
|
2014-07-07 05:41:22 +00:00
|
|
|
InvalidNonce,
|
2014-08-28 16:16:53 +00:00
|
|
|
/// Boolean-returning function returned the wrong boolean
|
|
|
|
Unknown
|
2014-07-07 05:41:22 +00:00
|
|
|
}
|
2015-01-17 16:13:45 +00:00
|
|
|
impl Copy for Error {}
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-08-10 01:03:17 +00:00
|
|
|
/// Result type
|
2015-01-17 16:13:45 +00:00
|
|
|
pub type Result<T> = ::std::result::Result<T, Error>;
|
2014-08-10 01:03:17 +00:00
|
|
|
|
2015-01-17 16:13:45 +00:00
|
|
|
static mut Secp256k1_init: Once = ONCE_INIT;
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-09-12 13:28:35 +00:00
|
|
|
/// The secp256k1 engine, used to execute all signature operations
|
|
|
|
pub struct Secp256k1 {
|
|
|
|
rng: Fortuna
|
|
|
|
}
|
|
|
|
|
2014-08-10 03:34:16 +00:00
|
|
|
/// Does one-time initialization of the secp256k1 engine. Can be called
|
|
|
|
/// multiple times, and is called by the `Secp256k1` constructor. This
|
|
|
|
/// only needs to be called directly if you are using the library without
|
|
|
|
/// a `Secp256k1` object, e.g. batch key generation through
|
|
|
|
/// `key::PublicKey::from_secret_key`.
|
|
|
|
pub fn init() {
|
|
|
|
unsafe {
|
2015-01-17 16:13:45 +00:00
|
|
|
Secp256k1_init.call_once(|| {
|
2015-01-17 16:38:16 +00:00
|
|
|
ffi::secp256k1_start(ffi::SECP256K1_START_VERIFY |
|
|
|
|
ffi::SECP256K1_START_SIGN);
|
2014-08-10 03:34:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-12 13:28:35 +00:00
|
|
|
impl Secp256k1 {
|
|
|
|
/// Constructs a new secp256k1 engine.
|
2015-03-25 19:10:02 +00:00
|
|
|
pub fn new() -> io::Result<Secp256k1> {
|
2014-09-12 13:28:35 +00:00
|
|
|
init();
|
|
|
|
let mut osrng = try!(OsRng::new());
|
2015-01-17 16:13:45 +00:00
|
|
|
let mut seed = [0; 2048];
|
2014-09-12 13:28:35 +00:00
|
|
|
osrng.fill_bytes(seed.as_mut_slice());
|
|
|
|
Ok(Secp256k1 { rng: SeedableRng::from_seed(seed.as_slice()) })
|
|
|
|
}
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-09-12 13:28:35 +00:00
|
|
|
/// Generates a random keypair. Convenience function for `key::SecretKey::new`
|
|
|
|
/// and `key::PublicKey::from_secret_key`; call those functions directly for
|
|
|
|
/// batch key generation.
|
|
|
|
#[inline]
|
|
|
|
pub fn generate_keypair(&mut self, compressed: bool)
|
|
|
|
-> (key::SecretKey, key::PublicKey) {
|
|
|
|
let sk = key::SecretKey::new(&mut self.rng);
|
2015-01-17 16:13:45 +00:00
|
|
|
let pk = key::PublicKey::from_secret_key(&sk, compressed);
|
|
|
|
(sk, pk)
|
2014-09-12 13:28:35 +00:00
|
|
|
}
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-09-12 13:28:35 +00:00
|
|
|
/// Generates a random nonce. Convenience function for `key::Nonce::new`; call
|
|
|
|
/// that function directly for batch nonce generation
|
|
|
|
#[inline]
|
|
|
|
pub fn generate_nonce(&mut self) -> key::Nonce {
|
|
|
|
key::Nonce::new(&mut self.rng)
|
|
|
|
}
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-09-12 13:28:35 +00:00
|
|
|
/// Constructs a signature for `msg` using the secret key `sk` and nonce `nonce`
|
|
|
|
pub fn sign(&self, msg: &[u8], sk: &key::SecretKey, nonce: &key::Nonce)
|
|
|
|
-> Result<Signature> {
|
2015-01-17 16:13:45 +00:00
|
|
|
let mut sig = [0; constants::MAX_SIGNATURE_SIZE];
|
2014-09-12 13:28:35 +00:00
|
|
|
let mut len = constants::MAX_SIGNATURE_SIZE as c_int;
|
|
|
|
unsafe {
|
|
|
|
if ffi::secp256k1_ecdsa_sign(msg.as_ptr(), msg.len() as c_int,
|
|
|
|
sig.as_mut_slice().as_mut_ptr(), &mut len,
|
|
|
|
sk.as_ptr(), nonce.as_ptr()) != 1 {
|
2015-01-17 16:13:45 +00:00
|
|
|
return Err(Error::InvalidNonce);
|
2014-09-12 13:28:35 +00:00
|
|
|
}
|
|
|
|
// This assertation is probably too late :)
|
2015-01-17 16:13:45 +00:00
|
|
|
assert!(len as usize <= constants::MAX_SIGNATURE_SIZE);
|
2014-09-12 13:28:35 +00:00
|
|
|
};
|
2015-01-17 16:13:45 +00:00
|
|
|
Ok(Signature(len as usize, sig))
|
2014-09-12 13:28:35 +00:00
|
|
|
}
|
2014-09-12 03:36:15 +00:00
|
|
|
|
2014-09-12 13:28:35 +00:00
|
|
|
/// Constructs a compact signature for `msg` using the secret key `sk`
|
|
|
|
pub fn sign_compact(&self, msg: &[u8], sk: &key::SecretKey, nonce: &key::Nonce)
|
|
|
|
-> Result<(Signature, RecoveryId)> {
|
2015-01-17 16:13:45 +00:00
|
|
|
let mut sig = [0; constants::MAX_SIGNATURE_SIZE];
|
2014-09-12 13:28:35 +00:00
|
|
|
let mut recid = 0;
|
|
|
|
unsafe {
|
|
|
|
if ffi::secp256k1_ecdsa_sign_compact(msg.as_ptr(), msg.len() as c_int,
|
|
|
|
sig.as_mut_slice().as_mut_ptr(), sk.as_ptr(),
|
|
|
|
nonce.as_ptr(), &mut recid) != 1 {
|
2015-01-17 16:13:45 +00:00
|
|
|
return Err(Error::InvalidNonce);
|
2014-09-12 13:28:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok((Signature(constants::MAX_COMPACT_SIGNATURE_SIZE, sig), RecoveryId(recid)))
|
|
|
|
}
|
2014-09-12 03:36:15 +00:00
|
|
|
|
2014-09-12 13:28:35 +00:00
|
|
|
/// Determines the public key for which `sig` is a valid signature for
|
|
|
|
/// `msg`. Returns through the out-pointer `pubkey`.
|
|
|
|
pub fn recover_compact(&self, msg: &[u8], sig: &[u8],
|
|
|
|
compressed: bool, recid: RecoveryId)
|
|
|
|
-> Result<key::PublicKey> {
|
|
|
|
let mut pk = key::PublicKey::new(compressed);
|
|
|
|
let RecoveryId(recid) = recid;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let mut len = 0;
|
|
|
|
if ffi::secp256k1_ecdsa_recover_compact(msg.as_ptr(), msg.len() as c_int,
|
|
|
|
sig.as_ptr(), pk.as_mut_ptr(), &mut len,
|
|
|
|
if compressed {1} else {0},
|
|
|
|
recid) != 1 {
|
2015-01-17 16:13:45 +00:00
|
|
|
return Err(Error::InvalidSignature);
|
2014-09-12 13:28:35 +00:00
|
|
|
}
|
2015-01-17 16:13:45 +00:00
|
|
|
assert_eq!(len as usize, pk.len());
|
2014-09-12 13:28:35 +00:00
|
|
|
};
|
|
|
|
Ok(pk)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks that `sig` is a valid ECDSA signature for `msg` using the public
|
|
|
|
/// key `pubkey`. Returns `Ok(true)` on success. Note that this function cannot
|
|
|
|
/// be used for Bitcoin consensus checking since there are transactions out
|
|
|
|
/// there with zero-padded signatures that don't fit in the `Signature` type.
|
|
|
|
/// Use `verify_raw` instead.
|
|
|
|
#[inline]
|
|
|
|
pub fn verify(msg: &[u8], sig: &Signature, pk: &key::PublicKey) -> Result<()> {
|
|
|
|
Secp256k1::verify_raw(msg, sig.as_slice(), pk)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks that `sig` is a valid ECDSA signature for `msg` using the public
|
|
|
|
/// key `pubkey`. Returns `Ok(true)` on success.
|
|
|
|
#[inline]
|
|
|
|
pub fn verify_raw(msg: &[u8], sig: &[u8], pk: &key::PublicKey) -> Result<()> {
|
|
|
|
init(); // This is a static function, so we have to init
|
|
|
|
let res = unsafe {
|
|
|
|
ffi::secp256k1_ecdsa_verify(msg.as_ptr(), msg.len() as c_int,
|
|
|
|
sig.as_ptr(), sig.len() as c_int,
|
|
|
|
pk.as_ptr(), pk.len() as c_int)
|
|
|
|
};
|
|
|
|
|
|
|
|
match res {
|
|
|
|
1 => Ok(()),
|
2015-01-17 16:13:45 +00:00
|
|
|
0 => Err(Error::IncorrectSignature),
|
|
|
|
-1 => Err(Error::InvalidPublicKey),
|
|
|
|
-2 => Err(Error::InvalidSignature),
|
2014-09-12 13:28:35 +00:00
|
|
|
_ => unreachable!()
|
|
|
|
}
|
2014-07-07 05:41:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-04 23:58:57 +00:00
|
|
|
#[cfg(test)]
|
2014-09-01 03:26:02 +00:00
|
|
|
mod tests {
|
2015-01-17 16:13:45 +00:00
|
|
|
use std::iter::repeat;
|
2015-03-25 23:22:24 +00:00
|
|
|
use std::rand::thread_rng;
|
|
|
|
use rand::Rng;
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-09-01 16:13:31 +00:00
|
|
|
use test::{Bencher, black_box};
|
2014-09-01 03:26:02 +00:00
|
|
|
|
2014-09-12 13:28:35 +00:00
|
|
|
use key::{PublicKey, Nonce};
|
|
|
|
use super::{Secp256k1, Signature};
|
2015-01-17 16:13:45 +00:00
|
|
|
use super::Error::{InvalidPublicKey, IncorrectSignature, InvalidSignature};
|
2014-08-18 01:55:07 +00:00
|
|
|
|
2014-08-04 23:58:57 +00:00
|
|
|
#[test]
|
|
|
|
fn invalid_pubkey() {
|
2015-01-17 16:13:45 +00:00
|
|
|
let mut msg: Vec<u8> = repeat(0).take(32).collect();
|
|
|
|
let sig = Signature::from_slice(&[0; 72]).unwrap();
|
2014-08-10 01:03:17 +00:00
|
|
|
let pk = PublicKey::new(true);
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2015-03-25 23:22:24 +00:00
|
|
|
thread_rng().fill_bytes(msg.as_mut_slice());
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-09-12 13:28:35 +00:00
|
|
|
assert_eq!(Secp256k1::verify(msg.as_mut_slice(), &sig, &pk), Err(InvalidPublicKey));
|
2014-08-04 23:58:57 +00:00
|
|
|
}
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-08-04 23:58:57 +00:00
|
|
|
#[test]
|
|
|
|
fn valid_pubkey_uncompressed() {
|
2014-09-12 13:28:35 +00:00
|
|
|
let mut s = Secp256k1::new().unwrap();
|
|
|
|
|
|
|
|
let (_, pk) = s.generate_keypair(false);
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2015-01-17 16:13:45 +00:00
|
|
|
let mut msg: Vec<u8> = repeat(0).take(32).collect();
|
|
|
|
let sig = Signature::from_slice(&[0; 72]).unwrap();
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2015-03-25 23:22:24 +00:00
|
|
|
thread_rng().fill_bytes(msg.as_mut_slice());
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-09-12 13:28:35 +00:00
|
|
|
assert_eq!(Secp256k1::verify(msg.as_mut_slice(), &sig, &pk), Err(InvalidSignature));
|
2014-08-04 23:58:57 +00:00
|
|
|
}
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-08-04 23:58:57 +00:00
|
|
|
#[test]
|
|
|
|
fn valid_pubkey_compressed() {
|
2014-09-12 13:28:35 +00:00
|
|
|
let mut s = Secp256k1::new().unwrap();
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-09-12 13:28:35 +00:00
|
|
|
let (_, pk) = s.generate_keypair(true);
|
2015-01-17 16:13:45 +00:00
|
|
|
let mut msg: Vec<u8> = repeat(0).take(32).collect();
|
|
|
|
let sig = Signature::from_slice(&[0; 72]).unwrap();
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2015-03-25 23:22:24 +00:00
|
|
|
thread_rng().fill_bytes(msg.as_mut_slice());
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-09-12 13:28:35 +00:00
|
|
|
assert_eq!(Secp256k1::verify(msg.as_mut_slice(), &sig, &pk), Err(InvalidSignature));
|
2014-08-04 23:58:57 +00:00
|
|
|
}
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-08-04 23:58:57 +00:00
|
|
|
#[test]
|
2014-09-12 13:28:35 +00:00
|
|
|
fn sign() {
|
|
|
|
let mut s = Secp256k1::new().unwrap();
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2015-01-17 16:13:45 +00:00
|
|
|
let mut msg = [0u8; 32];
|
2015-03-25 23:22:24 +00:00
|
|
|
thread_rng().fill_bytes(&mut msg);
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-09-12 13:28:35 +00:00
|
|
|
let (sk, _) = s.generate_keypair(false);
|
|
|
|
let nonce = s.generate_nonce();
|
2014-08-10 01:03:17 +00:00
|
|
|
|
2014-09-12 13:28:35 +00:00
|
|
|
s.sign(msg.as_slice(), &sk, &nonce).unwrap();
|
2014-08-04 23:58:57 +00:00
|
|
|
}
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-08-04 23:58:57 +00:00
|
|
|
#[test]
|
|
|
|
fn sign_and_verify() {
|
2014-09-12 13:28:35 +00:00
|
|
|
let mut s = Secp256k1::new().unwrap();
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2015-01-17 16:13:45 +00:00
|
|
|
let mut msg: Vec<u8> = repeat(0).take(32).collect();
|
2015-03-25 23:22:24 +00:00
|
|
|
thread_rng().fill_bytes(msg.as_mut_slice());
|
2014-09-12 13:28:35 +00:00
|
|
|
|
|
|
|
let (sk, pk) = s.generate_keypair(false);
|
|
|
|
let nonce = s.generate_nonce();
|
|
|
|
|
|
|
|
let sig = s.sign(msg.as_slice(), &sk, &nonce).unwrap();
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-09-12 13:28:35 +00:00
|
|
|
assert_eq!(Secp256k1::verify(msg.as_slice(), &sig, &pk), Ok(()));
|
2014-08-04 23:58:57 +00:00
|
|
|
}
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-08-04 23:58:57 +00:00
|
|
|
#[test]
|
|
|
|
fn sign_and_verify_fail() {
|
2014-09-12 13:28:35 +00:00
|
|
|
let mut s = Secp256k1::new().unwrap();
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2015-01-17 16:13:45 +00:00
|
|
|
let mut msg: Vec<u8> = repeat(0).take(32).collect();
|
2015-03-25 23:22:24 +00:00
|
|
|
thread_rng().fill_bytes(msg.as_mut_slice());
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-09-12 13:28:35 +00:00
|
|
|
let (sk, pk) = s.generate_keypair(false);
|
|
|
|
let nonce = s.generate_nonce();
|
|
|
|
|
|
|
|
let sig = s.sign(msg.as_slice(), &sk, &nonce).unwrap();
|
|
|
|
|
2015-03-25 23:22:24 +00:00
|
|
|
thread_rng().fill_bytes(msg.as_mut_slice());
|
2014-09-12 13:28:35 +00:00
|
|
|
assert_eq!(Secp256k1::verify(msg.as_slice(), &sig, &pk), Err(IncorrectSignature));
|
2014-08-04 23:58:57 +00:00
|
|
|
}
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-08-04 23:58:57 +00:00
|
|
|
#[test]
|
|
|
|
fn sign_compact_with_recovery() {
|
2014-09-12 13:28:35 +00:00
|
|
|
let mut s = Secp256k1::new().unwrap();
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2015-01-17 16:13:45 +00:00
|
|
|
let mut msg = [0u8; 32];
|
2015-03-25 23:22:24 +00:00
|
|
|
thread_rng().fill_bytes(msg.as_mut_slice());
|
2014-09-12 13:28:35 +00:00
|
|
|
|
|
|
|
let (sk, pk) = s.generate_keypair(false);
|
|
|
|
let nonce = s.generate_nonce();
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-09-12 13:28:35 +00:00
|
|
|
let (sig, recid) = s.sign_compact(msg.as_slice(), &sk, &nonce).unwrap();
|
2014-07-07 05:41:22 +00:00
|
|
|
|
2014-09-12 13:28:35 +00:00
|
|
|
assert_eq!(s.recover_compact(msg.as_slice(), sig.as_slice(), false, recid), Ok(pk));
|
2014-08-04 23:58:57 +00:00
|
|
|
}
|
2014-09-01 03:26:02 +00:00
|
|
|
|
2014-09-04 19:29:24 +00:00
|
|
|
#[test]
|
|
|
|
fn deterministic_sign() {
|
2015-01-17 16:13:45 +00:00
|
|
|
let mut msg = [0u8; 32];
|
2015-03-25 23:22:24 +00:00
|
|
|
thread_rng().fill_bytes(msg.as_mut_slice());
|
2014-09-12 13:28:35 +00:00
|
|
|
|
|
|
|
let mut s = Secp256k1::new().unwrap();
|
|
|
|
let (sk, pk) = s.generate_keypair(true);
|
2015-01-17 16:13:45 +00:00
|
|
|
let nonce = Nonce::deterministic(&mut msg, &sk);
|
2014-09-04 19:29:24 +00:00
|
|
|
|
2014-09-12 13:28:35 +00:00
|
|
|
let sig = s.sign(msg.as_slice(), &sk, &nonce).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(Secp256k1::verify(msg.as_slice(), &sig, &pk), Ok(()));
|
2014-09-04 19:29:24 +00:00
|
|
|
}
|
|
|
|
|
2014-09-01 03:26:02 +00:00
|
|
|
#[bench]
|
|
|
|
pub fn generate_compressed(bh: &mut Bencher) {
|
2014-09-12 13:28:35 +00:00
|
|
|
let mut s = Secp256k1::new().unwrap();
|
2014-09-01 03:26:02 +00:00
|
|
|
bh.iter( || {
|
2014-09-12 13:28:35 +00:00
|
|
|
let (sk, pk) = s.generate_keypair(true);
|
|
|
|
black_box(sk);
|
|
|
|
black_box(pk);
|
2014-09-01 03:26:02 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
pub fn generate_uncompressed(bh: &mut Bencher) {
|
2014-09-12 13:28:35 +00:00
|
|
|
let mut s = Secp256k1::new().unwrap();
|
2014-09-01 03:26:02 +00:00
|
|
|
bh.iter( || {
|
2014-09-12 13:28:35 +00:00
|
|
|
let (sk, pk) = s.generate_keypair(false);
|
|
|
|
black_box(sk);
|
|
|
|
black_box(pk);
|
2014-09-01 03:26:02 +00:00
|
|
|
});
|
|
|
|
}
|
2014-08-04 23:58:57 +00:00
|
|
|
}
|