2015-09-18 20:22:48 +00:00
|
|
|
// Bitcoin secp256k1 bindings
|
|
|
|
// Written in 2015 by
|
|
|
|
// 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/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
//! # ECDH
|
|
|
|
//! Support for shared secret computations
|
|
|
|
//!
|
|
|
|
|
2019-02-18 12:30:39 +00:00
|
|
|
use core::{ops, ptr};
|
2015-09-18 20:22:48 +00:00
|
|
|
|
|
|
|
use key::{SecretKey, PublicKey};
|
2019-08-16 18:49:24 +00:00
|
|
|
use ffi::{self, CPtr};
|
2015-09-18 20:22:48 +00:00
|
|
|
|
|
|
|
/// A tag used for recovering the public key from a compact signature
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub struct SharedSecret(ffi::SharedSecret);
|
|
|
|
|
|
|
|
impl SharedSecret {
|
|
|
|
/// Creates a new shared secret from a pubkey and secret key
|
|
|
|
#[inline]
|
2018-11-06 22:04:02 +00:00
|
|
|
pub fn new(point: &PublicKey, scalar: &SecretKey) -> SharedSecret {
|
2015-09-18 20:22:48 +00:00
|
|
|
unsafe {
|
2019-08-08 21:21:17 +00:00
|
|
|
let mut ss = ffi::SharedSecret::new();
|
2018-11-06 21:36:43 +00:00
|
|
|
let res = ffi::secp256k1_ecdh(
|
2018-11-06 22:04:02 +00:00
|
|
|
ffi::secp256k1_context_no_precomp,
|
2018-11-06 21:36:43 +00:00
|
|
|
&mut ss,
|
2019-08-16 18:49:24 +00:00
|
|
|
point.as_c_ptr(),
|
|
|
|
scalar.as_c_ptr(),
|
2018-11-06 21:36:43 +00:00
|
|
|
ffi::secp256k1_ecdh_hash_function_default,
|
|
|
|
ptr::null_mut(),
|
|
|
|
);
|
2015-09-18 20:22:48 +00:00
|
|
|
debug_assert_eq!(res, 1);
|
|
|
|
SharedSecret(ss)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Obtains a raw pointer suitable for use with FFI functions
|
|
|
|
#[inline]
|
|
|
|
pub fn as_ptr(&self) -> *const ffi::SharedSecret {
|
|
|
|
&self.0 as *const _
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-14 14:35:02 +00:00
|
|
|
/// Creates a new shared secret from a FFI shared secret
|
|
|
|
impl From<ffi::SharedSecret> for SharedSecret {
|
|
|
|
#[inline]
|
|
|
|
fn from(ss: ffi::SharedSecret) -> SharedSecret {
|
|
|
|
SharedSecret(ss)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-18 20:22:48 +00:00
|
|
|
impl ops::Index<usize> for SharedSecret {
|
|
|
|
type Output = u8;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: usize) -> &u8 {
|
|
|
|
&self.0[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Index<ops::Range<usize>> for SharedSecret {
|
|
|
|
type Output = [u8];
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: ops::Range<usize>) -> &[u8] {
|
|
|
|
&self.0[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Index<ops::RangeFrom<usize>> for SharedSecret {
|
|
|
|
type Output = [u8];
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: ops::RangeFrom<usize>) -> &[u8] {
|
|
|
|
&self.0[index.start..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Index<ops::RangeFull> for SharedSecret {
|
|
|
|
type Output = [u8];
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, _: ops::RangeFull) -> &[u8] {
|
|
|
|
&self.0[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use rand::thread_rng;
|
|
|
|
use super::SharedSecret;
|
|
|
|
use super::super::Secp256k1;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ecdh() {
|
2018-06-03 08:47:14 +00:00
|
|
|
let s = Secp256k1::signing_only();
|
2018-06-03 09:08:09 +00:00
|
|
|
let (sk1, pk1) = s.generate_keypair(&mut thread_rng());
|
|
|
|
let (sk2, pk2) = s.generate_keypair(&mut thread_rng());
|
2015-09-18 20:22:48 +00:00
|
|
|
|
2018-11-06 22:04:02 +00:00
|
|
|
let sec1 = SharedSecret::new(&pk1, &sk2);
|
|
|
|
let sec2 = SharedSecret::new(&pk2, &sk1);
|
|
|
|
let sec_odd = SharedSecret::new(&pk1, &sk1);
|
2015-09-18 20:22:48 +00:00
|
|
|
assert_eq!(sec1, sec2);
|
|
|
|
assert!(sec_odd != sec2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(test, feature = "unstable"))]
|
|
|
|
mod benches {
|
2015-09-20 20:30:03 +00:00
|
|
|
use rand::thread_rng;
|
2015-09-18 20:22:48 +00:00
|
|
|
use test::{Bencher, black_box};
|
|
|
|
|
2015-09-20 20:18:53 +00:00
|
|
|
use super::SharedSecret;
|
2015-09-20 20:30:03 +00:00
|
|
|
use super::super::Secp256k1;
|
2015-09-18 20:22:48 +00:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
pub fn bench_ecdh(bh: &mut Bencher) {
|
2018-06-03 08:47:14 +00:00
|
|
|
let s = Secp256k1::signing_only();
|
2018-06-03 09:08:09 +00:00
|
|
|
let (sk, pk) = s.generate_keypair(&mut thread_rng());
|
2015-09-18 20:22:48 +00:00
|
|
|
|
|
|
|
let s = Secp256k1::new();
|
|
|
|
bh.iter( || {
|
2018-11-06 22:04:02 +00:00
|
|
|
let res = SharedSecret::new(&pk, &sk);
|
2015-09-18 20:22:48 +00:00
|
|
|
black_box(res);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|