Skip to content
This repository was archived by the owner on Feb 26, 2020. It is now read-only.

Commit 5fbb1fc

Browse files
committed
Merge branch 'ci-add-gitlab' of https://github.com/paritytech/rust-secp256k1 into ci-add-gitlab
2 parents 97b8284 + c948efe commit 5fbb1fc

File tree

7 files changed

+93
-258
lines changed

7 files changed

+93
-258
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ clippy = {version = "0.0", optional = true}
3131
rand = "0.6"
3232

3333
[dev-dependencies]
34-
hex = "0.3.1"
34+
hex-literal = "0.2"
35+
rand_core = "0.4.2"

depend/secp256k1/src/ext.c

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,21 @@
77

88
#include "src/secp256k1.c"
99

10+
11+
static int ecdh_hash_function_raw(unsigned char *output, const unsigned char *x, const unsigned char *y, void *data) {
12+
(void)y;
13+
(void)data;
14+
15+
memcpy(output, x, 32);
16+
17+
return 1;
18+
}
19+
20+
const secp256k1_ecdh_hash_function secp256k1_ecdh_hash_function_raw = ecdh_hash_function_raw;
21+
1022
int secp256k1_ecdh_raw(const secp256k1_context* ctx, unsigned char *result, const secp256k1_pubkey *point, const unsigned char *scalar)
1123
{
12-
int ret = 0;
13-
int overflow = 0;
14-
secp256k1_gej res;
15-
secp256k1_ge pt;
16-
secp256k1_scalar s;
17-
ARG_CHECK(result != NULL);
18-
ARG_CHECK(point != NULL);
19-
ARG_CHECK(scalar != NULL);
20-
21-
secp256k1_pubkey_load(ctx, &pt, point);
22-
secp256k1_scalar_set_b32(&s, scalar, &overflow);
23-
if (overflow || secp256k1_scalar_is_zero(&s))
24-
ret = 0;
25-
else
26-
{
27-
secp256k1_ecmult_const(&res, &pt, &s, 256);
28-
secp256k1_ge_set_gej(&pt, &res);
29-
secp256k1_fe_normalize(&pt.x);
30-
secp256k1_fe_normalize(&pt.y);
31-
secp256k1_fe_get_b32(result, &pt.x);
32-
ret = 1;
33-
}
34-
35-
secp256k1_scalar_clear(&s);
36-
return ret;
24+
return secp256k1_ecdh(ctx, result, point, scalar, secp256k1_ecdh_hash_function_raw, NULL);
3725
}
3826

3927
/// Returns inverse (1 / n) of secret key `seckey`

src/ecdh.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ impl SharedSecret {
3333
pub fn new(secp: &Secp256k1, point: &PublicKey, scalar: &SecretKey) -> SharedSecret {
3434
unsafe {
3535
let mut ss = ffi::SharedSecret::blank();
36-
let res = ffi::secp256k1_ecdh(secp.ctx, &mut ss, point.as_ptr(), scalar.as_ptr());
36+
let res = ffi::secp256k1_ecdh(secp.ctx,
37+
&mut ss,
38+
point.as_ptr(),
39+
scalar.as_ptr(),
40+
ffi::secp256k1_ecdh_hash_function_default,
41+
std::ptr::null_mut());
3742
debug_assert_eq!(res, 1);
3843
SharedSecret(ss)
3944
}
@@ -120,6 +125,19 @@ mod tests {
120125
assert_eq!(sec1, sec2);
121126
assert!(sec_odd != sec2);
122127
}
128+
129+
#[test]
130+
fn ecdh_raw() {
131+
let s = Secp256k1::with_caps(::ContextFlag::SignOnly);
132+
let (sk1, pk1) = s.generate_keypair(&mut thread_rng()).unwrap();
133+
let (sk2, pk2) = s.generate_keypair(&mut thread_rng()).unwrap();
134+
135+
let sec1 = SharedSecret::new_raw(&s, &pk1, &sk2);
136+
let sec2 = SharedSecret::new_raw(&s, &pk2, &sk1);
137+
let sec_odd = SharedSecret::new_raw(&s, &pk1, &sk1);
138+
assert_eq!(sec1, sec2);
139+
assert!(sec_odd != sec2);
140+
}
123141
}
124142

125143
#[cfg(all(test, feature = "unstable"))]

src/ffi.rs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ pub type NonceFn = unsafe extern "C" fn(nonce32: *mut c_uchar,
4545
attempt: c_uint,
4646
data: *const c_void);
4747

48+
/// A pointer to a function that applies a hash function to a point
49+
///
50+
/// Returns: 1 if a point was successfully hashed. 0 will cause ecdh to fail
51+
///
52+
/// Out: output: pointer to an array to be filled by the function
53+
/// In: x: pointer to a 32-byte x coordinate
54+
/// y: pointer to a 32-byte y coordinate
55+
/// data: Arbitrary data pointer that is passed through
56+
///
57+
pub type EcdhHashFn = unsafe extern "C" fn(output: *mut c_uchar,
58+
x: *const c_uchar,
59+
y: *const c_uchar,
60+
data: *mut c_void)
61+
-> c_int;
62+
63+
4864

4965
/// A Secp256k1 context, containing various precomputed values and such
5066
/// needed to do elliptic curve computations. If you create one of these
@@ -116,6 +132,10 @@ extern "C" {
116132

117133
pub static secp256k1_nonce_function_default: NonceFn;
118134

135+
pub static secp256k1_ecdh_hash_function_sha256: EcdhHashFn;
136+
137+
pub static secp256k1_ecdh_hash_function_default: EcdhHashFn;
138+
119139
// Contexts
120140
pub fn secp256k1_context_create(flags: c_uint) -> *mut Context;
121141

@@ -202,27 +222,6 @@ extern "C" {
202222
msg32: *const c_uchar)
203223
-> c_int;
204224

205-
// Schnorr
206-
pub fn secp256k1_schnorr_sign(cx: *const Context,
207-
sig64: *mut c_uchar,
208-
msg32: *const c_uchar,
209-
sk: *const c_uchar,
210-
noncefn: NonceFn,
211-
noncedata: *const c_void)
212-
-> c_int;
213-
214-
pub fn secp256k1_schnorr_verify(cx: *const Context,
215-
sig64: *const c_uchar,
216-
msg32: *const c_uchar,
217-
pk: *const PublicKey)
218-
-> c_int;
219-
220-
pub fn secp256k1_schnorr_recover(cx: *const Context,
221-
pk: *mut PublicKey,
222-
sig64: *const c_uchar,
223-
msg32: *const c_uchar)
224-
-> c_int;
225-
226225
// EC
227226
pub fn secp256k1_ec_seckey_verify(cx: *const Context,
228227
sk: *const c_uchar) -> c_int;
@@ -262,14 +261,16 @@ extern "C" {
262261
pub fn secp256k1_ecdh(cx: *const Context,
263262
out: *mut SharedSecret,
264263
point: *const PublicKey,
265-
scalar: *const c_uchar)
264+
scalar: *const c_uchar,
265+
hash_fn: EcdhHashFn,
266+
data: *mut c_void)
266267
-> c_int;
267268

268269
pub fn secp256k1_ecdh_raw(cx: *const Context,
269-
out: *mut SharedSecret,
270-
point: *const PublicKey,
271-
scalar: *const c_uchar)
272-
-> c_int;
270+
out: *mut SharedSecret,
271+
point: *const PublicKey,
272+
scalar: *const c_uchar)
273+
-> c_int;
273274

274275
pub fn secp256k1_ec_privkey_inverse(cx: *const Context,
275276
out: *mut c_uchar,

src/key.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ mod test {
285285
use super::{PublicKey, SecretKey};
286286
use super::super::constants;
287287

288-
use rand::{Rng, thread_rng};
288+
use rand::{RngCore, thread_rng};
289289

290290
#[test]
291291
fn skey_from_slice() {
@@ -378,8 +378,9 @@ mod test {
378378
fn test_out_of_range() {
379379

380380
struct BadRng(u8);
381-
impl Rng for BadRng {
381+
impl RngCore for BadRng {
382382
fn next_u32(&mut self) -> u32 { unimplemented!() }
383+
fn next_u64(&mut self) -> u64 { unimplemented!() }
383384
// This will set a secret key to a little over the
384385
// group order, then decrement with repeated calls
385386
// until it returns a valid key
@@ -394,6 +395,9 @@ mod test {
394395
data[31] = self.0;
395396
self.0 -= 1;
396397
}
398+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
399+
Ok(self.fill_bytes(dest))
400+
}
397401
}
398402

399403
let s = Secp256k1::new();
@@ -420,16 +424,28 @@ mod test {
420424
Err(InvalidPublicKey));
421425
}
422426

423-
#[test]
424-
fn test_debug_output() {
425-
struct DumbRng(u32);
426-
impl Rng for DumbRng {
427-
fn next_u32(&mut self) -> u32 {
428-
self.0 = self.0.wrapping_add(1);
429-
self.0
430-
}
427+
struct DumbRng(u32);
428+
impl RngCore for DumbRng {
429+
fn next_u32(&mut self) -> u32 {
430+
self.0 = self.0.wrapping_add(1);
431+
self.0
432+
}
433+
fn next_u64(&mut self) -> u64 {
434+
let hi = self.next_u32();
435+
let lo = self.next_u32();
436+
((hi as u64) << 32 | lo as u64)
437+
}
438+
fn fill_bytes(&mut self, data: &mut [u8]) {
439+
rand_core::impls::fill_bytes_via_next(self, data);
440+
}
441+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
442+
Ok(self.fill_bytes(dest))
431443
}
444+
}
445+
432446

447+
#[test]
448+
fn test_debug_output() {
433449
let s = Secp256k1::new();
434450
let (sk, _) = s.generate_keypair(&mut DumbRng(0)).unwrap();
435451

@@ -439,14 +455,6 @@ mod test {
439455

440456
#[test]
441457
fn test_pubkey_serialize() {
442-
struct DumbRng(u32);
443-
impl Rng for DumbRng {
444-
fn next_u32(&mut self) -> u32 {
445-
self.0 = self.0.wrapping_add(1);
446-
self.0
447-
}
448-
}
449-
450458
let s = Secp256k1::new();
451459
let (_, pk1) = s.generate_keypair(&mut DumbRng(0)).unwrap();
452460
assert_eq!(&pk1.serialize_vec(&s, false)[..],

src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
extern crate arrayvec;
4343

4444
pub extern crate rand;
45+
#[cfg(test)]
46+
extern crate rand_core;
47+
#[cfg(test)]
48+
#[macro_use]
49+
extern crate hex_literal;
4550

4651
use std::{error, fmt, ops, ptr};
4752
use rand::Rng;
@@ -52,7 +57,6 @@ pub mod constants;
5257
pub mod ecdh;
5358
pub mod ffi;
5459
pub mod key;
55-
pub mod schnorr;
5660

5761
/// A tag used for recovering the public key from a compact signature
5862
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
@@ -536,17 +540,14 @@ impl Secp256k1 {
536540

537541
#[cfg(test)]
538542
mod tests {
539-
extern crate hex;
540-
use rand::{Rng, thread_rng};
543+
use rand::{RngCore, thread_rng};
541544

542545
use key::{SecretKey, PublicKey};
543546
use super::constants;
544547
use super::{Secp256k1, Signature, RecoverableSignature, Message, RecoveryId, ContextFlag};
545548
use super::Error::{InvalidMessage, InvalidPublicKey, IncorrectSignature, InvalidSignature,
546549
IncapableContext};
547550

548-
macro_rules! hex (($hex:expr) => (hex::decode($hex).unwrap()));
549-
550551
#[test]
551552
fn capabilities() {
552553
let none = Secp256k1::with_caps(ContextFlag::None);

0 commit comments

Comments
 (0)