Merge pull request #220 from rantan/add_negate_support

Add negate support
This commit is contained in:
Andrew Poelstra 2020-08-26 17:43:46 +00:00 committed by GitHub
commit 3692c94a0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 0 deletions

View File

@ -243,6 +243,14 @@ extern "C" {
//TODO secp256k1_ec_privkey_export
//TODO secp256k1_ec_privkey_import
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_ec_privkey_negate")]
pub fn secp256k1_ec_privkey_negate(cx: *const Context,
sk: *mut c_uchar) -> c_int;
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_ec_pubkey_negate")]
pub fn secp256k1_ec_pubkey_negate(cx: *const Context,
pk: *mut PublicKey) -> c_int;
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_ec_privkey_tweak_add")]
pub fn secp256k1_ec_privkey_tweak_add(cx: *const Context,
sk: *mut c_uchar,
@ -694,6 +702,20 @@ mod fuzz_dummy {
//TODO secp256k1_ec_privkey_export
//TODO secp256k1_ec_privkey_import
pub unsafe fn secp256k1_ec_privkey_negate(cx: *const Context,
sk: *mut c_uchar) -> c_int {
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
if secp256k1_ec_seckey_verify(cx, sk) != 1 { return 0; }
1
}
pub unsafe fn secp256k1_ec_pubkey_negate(cx: *const Context,
pk: *mut PublicKey) -> c_int {
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
if test_pk_validate(cx, pk) != 1 { return 0; }
1
}
/// Copies the first 16 bytes of tweak into the last 16 bytes of sk
pub unsafe fn secp256k1_ec_privkey_tweak_add(cx: *const Context,
sk: *mut c_uchar,

View File

@ -148,6 +148,20 @@ impl SecretKey {
}
}
#[inline]
/// Negates one secret key.
pub fn negate_assign(
&mut self
) {
unsafe {
let res = ffi::secp256k1_ec_privkey_negate(
ffi::secp256k1_context_no_precomp,
self.as_mut_c_ptr()
);
debug_assert_eq!(res, 1);
}
}
#[inline]
/// Adds one secret key to another, modulo the curve order. WIll
/// return an error if the resulting key would be invalid or if
@ -291,6 +305,19 @@ impl PublicKey {
ret
}
#[inline]
/// Negates the pk to the pk `self` in place
/// Will return an error if the pk would be invalid.
pub fn negate_assign<C: Verification>(
&mut self,
secp: &Secp256k1<C>
) {
unsafe {
let res = ffi::secp256k1_ec_pubkey_negate(secp.ctx, &mut self.0 as *mut _);
debug_assert_eq!(res, 1);
}
}
#[inline]
/// Adds the pk corresponding to `other` to the pk `self` in place
/// Will return an error if the resulting key would be invalid or
@ -752,6 +779,27 @@ mod test {
assert_eq!(PublicKey::from_secret_key(&s, &sk2), pk2);
}
#[test]
fn test_negation() {
let s = Secp256k1::new();
let (mut sk, mut pk) = s.generate_keypair(&mut thread_rng());
let original_sk = sk;
let original_pk = pk;
assert_eq!(PublicKey::from_secret_key(&s, &sk), pk);
sk.negate_assign();
pk.negate_assign(&s);
assert_ne!(original_sk, sk);
assert_ne!(original_pk, pk);
sk.negate_assign();
pk.negate_assign(&s);
assert_eq!(original_sk, sk);
assert_eq!(original_pk, pk);
assert_eq!(PublicKey::from_secret_key(&s, &sk), pk);
}
#[test]
fn pubkey_hash() {
use std::collections::hash_map::DefaultHasher;