-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
126 lines (102 loc) · 3.88 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#![feature(test)]
extern crate test;
pub mod parity_libsecp256k1 {
use parity_secp256k1::{Error, PublicKey, SecretKey};
use test::Bencher;
fn ecmul() -> Result<(), Error> {
let secret_key = SecretKey::parse(&[
137, 16, 46, 159, 212, 158, 232, 178, 197, 253, 105, 137, 102, 159, 70, 217, 110, 211,
254, 82, 216, 4, 105, 171, 102, 252, 54, 190, 114, 91, 11, 69,
])?;
let mut public_key = PublicKey::parse_compressed(&[
2, 123, 236, 243, 192, 100, 34, 40, 51, 111, 129, 130, 160, 64, 129, 135, 11, 184, 68,
84, 83, 198, 234, 196, 150, 13, 208, 86, 34, 150, 10, 59, 247,
])?;
public_key.tweak_mul_assign(&secret_key)?;
assert_eq!(
public_key,
PublicKey::parse_compressed(&[
2, 151, 202, 113, 10, 9, 43, 125, 187, 101, 157, 152, 191, 94, 12, 236, 133, 229,
16, 233, 221, 52, 150, 183, 243, 61, 110, 8, 152, 132, 99, 49, 189,
])?
);
Ok(())
}
#[test]
fn test_libsecp256k1() {
ecmul().unwrap();
}
#[bench]
fn bench_libsecp256k1(b: &mut Bencher) {
b.iter(|| ecmul().unwrap());
}
}
pub mod bitcoin_core_libsecp256k1 {
use bitcoin_core_secp256k1::{ffi::types::AlignedType, Error, PublicKey, Secp256k1};
use test::Bencher;
fn ecmul() -> Result<(), Error> {
let mut buf = vec![AlignedType::zeroed(); Secp256k1::preallocate_size()];
let secp = Secp256k1::preallocated_new(&mut buf)?;
let secret_key = &[
137, 16, 46, 159, 212, 158, 232, 178, 197, 253, 105, 137, 102, 159, 70, 217, 110, 211,
254, 82, 216, 4, 105, 171, 102, 252, 54, 190, 114, 91, 11, 69,
];
let mut public_key = PublicKey::from_slice(&[
2, 123, 236, 243, 192, 100, 34, 40, 51, 111, 129, 130, 160, 64, 129, 135, 11, 184, 68,
84, 83, 198, 234, 196, 150, 13, 208, 86, 34, 150, 10, 59, 247,
])?;
public_key.mul_assign(&secp, secret_key)?;
assert_eq!(
public_key,
PublicKey::from_slice(&[
2, 151, 202, 113, 10, 9, 43, 125, 187, 101, 157, 152, 191, 94, 12, 236, 133, 229,
16, 233, 221, 52, 150, 183, 243, 61, 110, 8, 152, 132, 99, 49, 189,
])?
);
Ok(())
}
#[test]
fn test_libsecp256k1() {
ecmul().unwrap();
}
#[bench]
fn bench_libsecp256k1(b: &mut Bencher) {
b.iter(|| ecmul().unwrap());
}
}
pub mod rust_crypto_libsecp256k1 {
use rust_crypto_secp256k1::{
elliptic_curve::{ecdh::diffie_hellman, Error, PublicKey},
EncodedPoint, Secp256k1, SecretKey,
};
use std::ops::Deref;
use test::Bencher;
fn ecmul() -> Result<(), Error> {
let secret_key = SecretKey::from_bytes(&[
137, 16, 46, 159, 212, 158, 232, 178, 197, 253, 105, 137, 102, 159, 70, 217, 110, 211,
254, 82, 216, 4, 105, 171, 102, 252, 54, 190, 114, 91, 11, 69,
])?;
let encoded_point = EncodedPoint::from_bytes(&[
2, 123, 236, 243, 192, 100, 34, 40, 51, 111, 129, 130, 160, 64, 129, 135, 11, 184, 68,
84, 83, 198, 234, 196, 150, 13, 208, 86, 34, 150, 10, 59, 247,
])?;
let public_key = PublicKey::<Secp256k1>::from_sec1_bytes(encoded_point.as_ref()).unwrap();
let shared_secret = diffie_hellman(secret_key.secret_scalar(), public_key.as_affine());
assert_eq!(
shared_secret.as_bytes().deref(),
&[
151, 202, 113, 10, 9, 43, 125, 187, 101, 157, 152, 191, 94, 12, 236, 133, 229, 16,
233, 221, 52, 150, 183, 243, 61, 110, 8, 152, 132, 99, 49, 189,
]
);
Ok(())
}
#[test]
fn test_libsecp256k1() {
ecmul().unwrap();
}
#[bench]
fn bench_libsecp256k1(b: &mut Bencher) {
b.iter(|| ecmul().unwrap());
}
}