Replace Result return type with actual value

This introduces the actual breaking API change.
This commit is contained in:
Thomas Eizinger 2018-06-03 19:08:09 +10:00
parent be7134c7f4
commit f13cdfa8a4
4 changed files with 55 additions and 59 deletions

View File

@ -99,8 +99,8 @@ mod tests {
#[test]
fn ecdh() {
let s = Secp256k1::signing_only();
let (sk1, pk1) = s.generate_keypair(&mut thread_rng()).unwrap();
let (sk2, pk2) = s.generate_keypair(&mut thread_rng()).unwrap();
let (sk1, pk1) = s.generate_keypair(&mut thread_rng());
let (sk2, pk2) = s.generate_keypair(&mut thread_rng());
let sec1 = SharedSecret::new(&s, &pk1, &sk2);
let sec2 = SharedSecret::new(&s, &pk2, &sk1);
@ -121,7 +121,7 @@ mod benches {
#[bench]
pub fn bench_ecdh(bh: &mut Bencher) {
let s = Secp256k1::signing_only();
let (sk, pk) = s.generate_keypair(&mut thread_rng()).unwrap();
let (sk, pk) = s.generate_keypair(&mut thread_rng());
let s = Secp256k1::new();
bh.iter( || {

View File

@ -146,7 +146,7 @@ impl PublicKey {
#[inline]
pub fn from_secret_key<C: Signing>(secp: &Secp256k1<C>,
sk: &SecretKey)
-> Result<PublicKey, Error> {
-> PublicKey {
let mut pk = unsafe { ffi::PublicKey::blank() };
unsafe {
// We can assume the return value because it's not possible to construct
@ -154,7 +154,7 @@ impl PublicKey {
let res = ffi::secp256k1_ec_pubkey_create(secp.ctx, &mut pk, sk.as_ptr());
debug_assert_eq!(res, 1);
}
Ok(PublicKey(pk))
PublicKey(pk)
}
/// Creates a public key directly from a slice
@ -327,7 +327,7 @@ mod test {
fn keypair_slice_round_trip() {
let s = Secp256k1::new();
let (sk1, pk1) = s.generate_keypair(&mut thread_rng()).unwrap();
let (sk1, pk1) = s.generate_keypair(&mut thread_rng());
assert_eq!(SecretKey::from_slice(&s, &sk1[..]), Ok(sk1));
assert_eq!(PublicKey::from_slice(&s, &pk1.serialize()[..]), Ok(pk1));
assert_eq!(PublicKey::from_slice(&s, &pk1.serialize_uncompressed()[..]), Ok(pk1));
@ -377,7 +377,7 @@ mod test {
}
let s = Secp256k1::new();
s.generate_keypair(&mut BadRng(0xff)).unwrap();
s.generate_keypair(&mut BadRng(0xff));
}
#[test]
@ -411,7 +411,7 @@ mod test {
}
let s = Secp256k1::new();
let (sk, _) = s.generate_keypair(&mut DumbRng(0)).unwrap();
let (sk, _) = s.generate_keypair(&mut DumbRng(0));
assert_eq!(&format!("{:?}", sk),
"SecretKey(0200000001000000040000000300000006000000050000000800000007000000)");
@ -428,7 +428,7 @@ mod test {
}
let s = Secp256k1::new();
let (_, pk1) = s.generate_keypair(&mut DumbRng(0)).unwrap();
let (_, pk1) = s.generate_keypair(&mut DumbRng(0));
assert_eq!(&pk1.serialize_uncompressed()[..],
&[4, 149, 16, 196, 140, 38, 92, 239, 179, 65, 59, 224, 230, 183, 91, 238, 240, 46, 186, 252, 175, 102, 52, 249, 98, 178, 123, 72, 50, 171, 196, 254, 236, 1, 189, 143, 242, 227, 16, 87, 247, 183, 162, 68, 237, 140, 92, 205, 151, 129, 166, 58, 111, 96, 123, 64, 180, 147, 51, 12, 209, 89, 236, 213, 206][..]);
assert_eq!(&pk1.serialize()[..],
@ -439,36 +439,36 @@ mod test {
fn test_addition() {
let s = Secp256k1::new();
let (mut sk1, mut pk1) = s.generate_keypair(&mut thread_rng()).unwrap();
let (mut sk2, mut pk2) = s.generate_keypair(&mut thread_rng()).unwrap();
let (mut sk1, mut pk1) = s.generate_keypair(&mut thread_rng());
let (mut sk2, mut pk2) = s.generate_keypair(&mut thread_rng());
assert_eq!(PublicKey::from_secret_key(&s, &sk1).unwrap(), pk1);
assert_eq!(PublicKey::from_secret_key(&s, &sk1), pk1);
assert!(sk1.add_assign(&s, &sk2).is_ok());
assert!(pk1.add_exp_assign(&s, &sk2).is_ok());
assert_eq!(PublicKey::from_secret_key(&s, &sk1).unwrap(), pk1);
assert_eq!(PublicKey::from_secret_key(&s, &sk1), pk1);
assert_eq!(PublicKey::from_secret_key(&s, &sk2).unwrap(), pk2);
assert_eq!(PublicKey::from_secret_key(&s, &sk2), pk2);
assert!(sk2.add_assign(&s, &sk1).is_ok());
assert!(pk2.add_exp_assign(&s, &sk1).is_ok());
assert_eq!(PublicKey::from_secret_key(&s, &sk2).unwrap(), pk2);
assert_eq!(PublicKey::from_secret_key(&s, &sk2), pk2);
}
#[test]
fn test_multiplication() {
let s = Secp256k1::new();
let (mut sk1, mut pk1) = s.generate_keypair(&mut thread_rng()).unwrap();
let (mut sk2, mut pk2) = s.generate_keypair(&mut thread_rng()).unwrap();
let (mut sk1, mut pk1) = s.generate_keypair(&mut thread_rng());
let (mut sk2, mut pk2) = s.generate_keypair(&mut thread_rng());
assert_eq!(PublicKey::from_secret_key(&s, &sk1).unwrap(), pk1);
assert_eq!(PublicKey::from_secret_key(&s, &sk1), pk1);
assert!(sk1.mul_assign(&s, &sk2).is_ok());
assert!(pk1.mul_assign(&s, &sk2).is_ok());
assert_eq!(PublicKey::from_secret_key(&s, &sk1).unwrap(), pk1);
assert_eq!(PublicKey::from_secret_key(&s, &sk1), pk1);
assert_eq!(PublicKey::from_secret_key(&s, &sk2).unwrap(), pk2);
assert_eq!(PublicKey::from_secret_key(&s, &sk2), pk2);
assert!(sk2.mul_assign(&s, &sk1).is_ok());
assert!(pk2.mul_assign(&s, &sk1).is_ok());
assert_eq!(PublicKey::from_secret_key(&s, &sk2).unwrap(), pk2);
assert_eq!(PublicKey::from_secret_key(&s, &sk2), pk2);
}
#[test]
@ -487,7 +487,7 @@ mod test {
let mut set = HashSet::new();
const COUNT : usize = 1024;
let count = (0..COUNT).map(|_| {
let (_, pk) = s.generate_keypair(&mut thread_rng()).unwrap();
let (_, pk) = s.generate_keypair(&mut thread_rng());
let hash = hash(&pk);
assert!(!set.contains(&hash));
set.insert(hash);

View File

@ -474,7 +474,7 @@ impl<C: Signing> Secp256k1<C> {
/// Constructs a signature for `msg` using the secret key `sk` and RFC6979 nonce
/// Requires a signing-capable context.
pub fn sign(&self, msg: &Message, sk: &key::SecretKey)
-> Result<Signature, Error> {
-> Signature {
let mut ret = unsafe { ffi::Signature::blank() };
unsafe {
@ -484,13 +484,14 @@ impl<C: Signing> Secp256k1<C> {
sk.as_ptr(), ffi::secp256k1_nonce_function_rfc6979,
ptr::null()), 1);
}
Ok(Signature::from(ret))
Signature::from(ret)
}
/// Constructs a signature for `msg` using the secret key `sk` and RFC6979 nonce
/// Requires a signing-capable context.
pub fn sign_recoverable(&self, msg: &Message, sk: &key::SecretKey)
-> Result<RecoverableSignature, Error> {
-> RecoverableSignature {
let mut ret = unsafe { ffi::RecoverableSignature::blank() };
unsafe {
@ -500,7 +501,8 @@ impl<C: Signing> Secp256k1<C> {
sk.as_ptr(), ffi::secp256k1_nonce_function_rfc6979,
ptr::null()), 1);
}
Ok(RecoverableSignature::from(ret))
RecoverableSignature::from(ret)
}
/// Generates a random keypair. Convenience function for `key::SecretKey::new`
@ -509,10 +511,10 @@ impl<C: Signing> Secp256k1<C> {
#[inline]
#[cfg(any(test, feature = "rand"))]
pub fn generate_keypair<R: Rng>(&self, rng: &mut R)
-> Result<(key::SecretKey, key::PublicKey), Error> {
-> (key::SecretKey, key::PublicKey) {
let sk = key::SecretKey::new(self, rng);
let pk = try!(key::PublicKey::from_secret_key(self, &sk));
Ok((sk, pk))
let pk = key::PublicKey::from_secret_key(self, &sk);
(sk, pk)
}
}
@ -597,19 +599,13 @@ mod tests {
let msg = Message::from_slice(&msg).unwrap();
// Try key generation
assert!(sign.generate_keypair(&mut thread_rng()).is_ok());
assert!(full.generate_keypair(&mut thread_rng()).is_ok());
let (sk, pk) = full.generate_keypair(&mut thread_rng()).unwrap();
let (sk, pk) = full.generate_keypair(&mut thread_rng());
// Try signing
assert!(sign.sign(&msg, &sk).is_ok());
assert!(full.sign(&msg, &sk).is_ok());
assert!(sign.sign_recoverable(&msg, &sk).is_ok());
assert!(full.sign_recoverable(&msg, &sk).is_ok());
assert_eq!(sign.sign(&msg, &sk), full.sign(&msg, &sk));
assert_eq!(sign.sign_recoverable(&msg, &sk), full.sign_recoverable(&msg, &sk));
let sig = full.sign(&msg, &sk).unwrap();
let sigr = full.sign_recoverable(&msg, &sk).unwrap();
let sig = full.sign(&msg, &sk);
let sigr = full.sign_recoverable(&msg, &sk);
// Try verifying
assert!(vrfy.verify(&msg, &sig, &pk).is_ok());
@ -659,7 +655,7 @@ mod tests {
let sk = SecretKey::from_slice(&s, &one).unwrap();
let msg = Message::from_slice(&one).unwrap();
let sig = s.sign_recoverable(&msg, &sk).unwrap();
let sig = s.sign_recoverable(&msg, &sk);
assert_eq!(Ok(sig), RecoverableSignature::from_compact(&s, &[
0x66, 0x73, 0xff, 0xad, 0x21, 0x47, 0x74, 0x1f,
0x04, 0x77, 0x2b, 0x6f, 0x92, 0x1f, 0x0b, 0xa6,
@ -682,8 +678,8 @@ mod tests {
thread_rng().fill_bytes(&mut msg);
let msg = Message::from_slice(&msg).unwrap();
let (sk, _) = s.generate_keypair(&mut thread_rng()).unwrap();
let sig1 = s.sign(&msg, &sk).unwrap();
let (sk, _) = s.generate_keypair(&mut thread_rng());
let sig1 = s.sign(&msg, &sk);
let der = sig1.serialize_der(&s);
let sig2 = Signature::from_der(&s, &der[..]).unwrap();
assert_eq!(sig1, sig2);
@ -728,8 +724,8 @@ mod tests {
thread_rng().fill_bytes(&mut msg);
let msg = Message::from_slice(&msg).unwrap();
let (sk, pk) = s.generate_keypair(&mut thread_rng()).unwrap();
let sig = s.sign(&msg, &sk).unwrap();
let (sk, pk) = s.generate_keypair(&mut thread_rng());
let sig = s.sign(&msg, &sk);
assert_eq!(s.verify(&msg, &sig, &pk), Ok(()));
}
}
@ -757,8 +753,8 @@ mod tests {
for key in wild_keys.iter().map(|k| SecretKey::from_slice(&s, &k[..]).unwrap()) {
for msg in wild_msgs.iter().map(|m| Message::from_slice(&m[..]).unwrap()) {
let sig = s.sign(&msg, &key).unwrap();
let pk = PublicKey::from_secret_key(&s, &key).unwrap();
let sig = s.sign(&msg, &key);
let pk = PublicKey::from_secret_key(&s, &key);
assert_eq!(s.verify(&msg, &sig, &pk), Ok(()));
}
}
@ -773,9 +769,9 @@ mod tests {
thread_rng().fill_bytes(&mut msg);
let msg = Message::from_slice(&msg).unwrap();
let (sk, pk) = s.generate_keypair(&mut thread_rng()).unwrap();
let (sk, pk) = s.generate_keypair(&mut thread_rng());
let sigr = s.sign_recoverable(&msg, &sk).unwrap();
let sigr = s.sign_recoverable(&msg, &sk);
let sig = sigr.to_standard(&s);
let mut msg = [0u8; 32];
@ -796,9 +792,9 @@ mod tests {
thread_rng().fill_bytes(&mut msg);
let msg = Message::from_slice(&msg).unwrap();
let (sk, pk) = s.generate_keypair(&mut thread_rng()).unwrap();
let (sk, pk) = s.generate_keypair(&mut thread_rng());
let sig = s.sign_recoverable(&msg, &sk).unwrap();
let sig = s.sign_recoverable(&msg, &sk);
assert_eq!(s.recover(&msg, &sig), Ok(pk));
}
@ -929,7 +925,7 @@ mod benches {
let s = Secp256k1::new();
let mut r = CounterRng(0);
bh.iter( || {
let (sk, pk) = s.generate_keypair(&mut r).unwrap();
let (sk, pk) = s.generate_keypair(&mut r);
black_box(sk);
black_box(pk);
});
@ -941,10 +937,10 @@ mod benches {
let mut msg = [0u8; 32];
thread_rng().fill_bytes(&mut msg);
let msg = Message::from_slice(&msg).unwrap();
let (sk, _) = s.generate_keypair(&mut thread_rng()).unwrap();
let (sk, _) = s.generate_keypair(&mut thread_rng());
bh.iter(|| {
let sig = s.sign(&msg, &sk).unwrap();
let sig = s.sign(&msg, &sk);
black_box(sig);
});
}
@ -955,8 +951,8 @@ mod benches {
let mut msg = [0u8; 32];
thread_rng().fill_bytes(&mut msg);
let msg = Message::from_slice(&msg).unwrap();
let (sk, pk) = s.generate_keypair(&mut thread_rng()).unwrap();
let sig = s.sign(&msg, &sk).unwrap();
let (sk, pk) = s.generate_keypair(&mut thread_rng());
let sig = s.sign(&msg, &sk);
bh.iter(|| {
let res = s.verify(&msg, &sig, &pk).unwrap();
@ -970,8 +966,8 @@ mod benches {
let mut msg = [0u8; 32];
thread_rng().fill_bytes(&mut msg);
let msg = Message::from_slice(&msg).unwrap();
let (sk, _) = s.generate_keypair(&mut thread_rng()).unwrap();
let sig = s.sign_recoverable(&msg, &sk).unwrap();
let (sk, _) = s.generate_keypair(&mut thread_rng());
let sig = s.sign_recoverable(&msg, &sk);
bh.iter(|| {
let res = s.recover(&msg, &sig).unwrap();

View File

@ -119,7 +119,7 @@ mod tests {
thread_rng().fill_bytes(&mut msg);
let msg = Message::from_slice(&msg).unwrap();
let (sk, pk) = full.generate_keypair(&mut thread_rng()).unwrap();
let (sk, pk) = full.generate_keypair(&mut thread_rng());
// Try signing
assert!(sign.sign_schnorr(&msg, &sk).is_ok());
@ -149,7 +149,7 @@ mod tests {
thread_rng().fill_bytes(&mut msg);
let msg = Message::from_slice(&msg).unwrap();
let (sk, pk) = s.generate_keypair(&mut thread_rng()).unwrap();
let (sk, pk) = s.generate_keypair(&mut thread_rng());
let sig = s.sign_schnorr(&msg, &sk).unwrap();
assert!(s.verify_schnorr(&msg, &sig, &pk).is_ok());
@ -164,7 +164,7 @@ mod tests {
thread_rng().fill_bytes(&mut msg);
let msg = Message::from_slice(&msg).unwrap();
let (sk, _) = s.generate_keypair(&mut thread_rng()).unwrap();
let (sk, _) = s.generate_keypair(&mut thread_rng());
let sig1 = s.sign_schnorr(&msg, &sk).unwrap();
let sig2 = Signature::deserialize(&sig1.serialize());