This repository has been archived by the owner on Oct 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
/
secp256k1.rs
421 lines (366 loc) · 12.8 KB
/
secp256k1.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use crate::{
pubkey_bytes::PublicKeyBytes,
serde_helpers::keypair_decode_base64,
traits::{Authenticator, EncodeDecodeBase64, KeyPair, SigningKey, ToFromBytes, VerifyingKey},
};
use base64ct::{Base64, Encoding};
use once_cell::sync::OnceCell;
use rust_secp256k1::{
constants,
ecdsa::{RecoverableSignature, RecoveryId},
Message, PublicKey, Secp256k1, SecretKey,
};
use serde::{de, Deserialize, Serialize};
use signature::{Signature, Signer, Verifier};
use std::{
fmt::{self, Debug, Display},
str::FromStr,
};
#[readonly::make]
#[derive(Debug, Clone)]
pub struct Secp256k1PublicKey {
pub pubkey: PublicKey,
pub bytes: OnceCell<[u8; constants::PUBLIC_KEY_SIZE]>,
}
pub type Secp256k1PublicKeyBytes =
PublicKeyBytes<Secp256k1PublicKey, { Secp256k1PublicKey::LENGTH }>;
#[readonly::make]
#[derive(Debug)]
pub struct Secp256k1PrivateKey {
pub privkey: SecretKey,
pub bytes: OnceCell<[u8; constants::SECRET_KEY_SIZE]>,
}
// Compact signature followed by one extra byte for recover id, used to recover public key from signature.
pub const RECOVERABLE_SIGNATURE_SIZE: usize = constants::COMPACT_SIGNATURE_SIZE + 1;
#[readonly::make]
#[derive(Debug, Clone)]
pub struct Secp256k1Signature {
pub sig: rust_secp256k1::ecdsa::RecoverableSignature,
pub bytes: OnceCell<[u8; RECOVERABLE_SIGNATURE_SIZE]>,
}
impl std::hash::Hash for Secp256k1PublicKey {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.as_ref().hash(state);
}
}
impl PartialOrd for Secp256k1PublicKey {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.as_ref().partial_cmp(other.as_ref())
}
}
impl Ord for Secp256k1PublicKey {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.as_ref().cmp(other.as_ref())
}
}
impl PartialEq for Secp256k1PublicKey {
fn eq(&self, other: &Self) -> bool {
self.pubkey == other.pubkey
}
}
impl Eq for Secp256k1PublicKey {}
impl VerifyingKey for Secp256k1PublicKey {
type PrivKey = Secp256k1PrivateKey;
type Sig = Secp256k1Signature;
const LENGTH: usize = constants::PUBLIC_KEY_SIZE;
}
impl Verifier<Secp256k1Signature> for Secp256k1PublicKey {
fn verify(&self, msg: &[u8], signature: &Secp256k1Signature) -> Result<(), signature::Error> {
// k256 defaults to keccak256 as digest to hash message for sign/verify, thus use this hash function to match in proptest.
#[cfg(test)]
let message =
Message::from_slice(<sha3::Keccak256 as sha3::digest::Digest>::digest(msg).as_slice())
.unwrap();
#[cfg(not(test))]
let message = Message::from_hashed_data::<rust_secp256k1::hashes::sha256::Hash>(msg);
// If pubkey recovered from signature matches original pubkey, verifies signature.
// To ensure non-malleability, signature.verify_ecdsa() is not used since it will verify [r, s, v] and [r, s, -v].
match signature.sig.recover(&message) {
Ok(recovered_key) if self.as_bytes() == recovered_key.serialize().as_slice() => Ok(()),
_ => Err(signature::Error::new()),
}
}
}
impl AsRef<[u8]> for Secp256k1PublicKey {
fn as_ref(&self) -> &[u8] {
self.bytes
.get_or_try_init::<_, eyre::Report>(|| {
Ok(self
.pubkey
.serialize()
.as_slice()
.try_into()
.expect("wrong length"))
})
.expect("OnceCell invariant violated")
}
}
impl ToFromBytes for Secp256k1PublicKey {
fn from_bytes(bytes: &[u8]) -> Result<Self, signature::Error> {
match PublicKey::from_slice(bytes) {
Ok(pubkey) => Ok(Secp256k1PublicKey {
pubkey,
bytes: OnceCell::new(),
}),
Err(_) => Err(signature::Error::new()),
}
}
}
impl Default for Secp256k1PublicKey {
fn default() -> Self {
Secp256k1PublicKey::from_bytes(&[0u8; constants::PUBLIC_KEY_SIZE]).unwrap()
}
}
impl Display for Secp256k1PublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", Base64::encode_string(self.as_ref()))
}
}
// There is a strong requirement for this specific impl. in Fab benchmarks
impl Serialize for Secp256k1PublicKey {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.encode_base64())
}
}
impl<'de> Deserialize<'de> for Secp256k1PublicKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
let s = <String as serde::Deserialize>::deserialize(deserializer)?;
let value = Self::decode_base64(&s).map_err(|e| de::Error::custom(e.to_string()))?;
Ok(value)
}
}
impl<'a> From<&'a Secp256k1PrivateKey> for Secp256k1PublicKey {
fn from(secret: &'a Secp256k1PrivateKey) -> Self {
let secp = Secp256k1::new();
Secp256k1PublicKey {
pubkey: secret.privkey.public_key(&secp),
bytes: OnceCell::new(),
}
}
}
impl SigningKey for Secp256k1PrivateKey {
type PubKey = Secp256k1PublicKey;
type Sig = Secp256k1Signature;
const LENGTH: usize = constants::SECRET_KEY_SIZE;
}
impl ToFromBytes for Secp256k1PrivateKey {
fn from_bytes(bytes: &[u8]) -> Result<Self, signature::Error> {
match SecretKey::from_slice(bytes) {
Ok(privkey) => Ok(Secp256k1PrivateKey {
privkey,
bytes: OnceCell::new(),
}),
Err(_) => Err(signature::Error::new()),
}
}
}
// There is a strong requirement for this specific impl. in Fab benchmarks
impl Serialize for Secp256k1PrivateKey {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.encode_base64())
}
}
// There is a strong requirement for this specific impl. in Fab benchmarks
impl<'de> Deserialize<'de> for Secp256k1PrivateKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
let value = Self::decode_base64(&s).map_err(|e| de::Error::custom(e.to_string()))?;
Ok(value)
}
}
impl AsRef<[u8]> for Secp256k1PrivateKey {
fn as_ref(&self) -> &[u8] {
self.bytes
.get_or_try_init::<_, eyre::Report>(|| Ok(self.privkey.secret_bytes()))
.expect("OnceCell invariant violated")
}
}
impl Serialize for Secp256k1Signature {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.as_ref().serialize(serializer)
}
}
impl<'de> Deserialize<'de> for Secp256k1Signature {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let data: Vec<u8> = Vec::deserialize(deserializer)?;
<Secp256k1Signature as signature::Signature>::from_bytes(&data)
.map_err(|e| de::Error::custom(e.to_string()))
}
}
impl Signature for Secp256k1Signature {
fn from_bytes(bytes: &[u8]) -> Result<Self, signature::Error> {
if bytes.len() != 65 {
return Err(signature::Error::new());
}
RecoveryId::from_i32(bytes[64] as i32)
.and_then(|rec_id| {
RecoverableSignature::from_compact(&bytes[..64], rec_id).map(|sig| {
Secp256k1Signature {
sig,
bytes: OnceCell::new(),
}
})
})
.map_err(|_| signature::Error::new())
}
}
impl Authenticator for Secp256k1Signature {
type PubKey = Secp256k1PublicKey;
type PrivKey = Secp256k1PrivateKey;
const LENGTH: usize = RECOVERABLE_SIGNATURE_SIZE;
}
impl AsRef<[u8]> for Secp256k1Signature {
fn as_ref(&self) -> &[u8] {
let mut bytes = [0u8; RECOVERABLE_SIGNATURE_SIZE];
let (recovery_id, sig) = self.sig.serialize_compact();
bytes[..64].copy_from_slice(&sig);
bytes[64] = recovery_id.to_i32() as u8;
self.bytes
.get_or_try_init::<_, eyre::Report>(|| Ok(bytes))
.expect("OnceCell invariant violated")
}
}
impl std::hash::Hash for Secp256k1Signature {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.as_ref().hash(state);
}
}
impl PartialEq for Secp256k1Signature {
fn eq(&self, other: &Self) -> bool {
self.sig == other.sig
}
}
impl Eq for Secp256k1Signature {}
impl Display for Secp256k1Signature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
write!(f, "{}", Base64::encode_string(self.as_ref()))
}
}
impl Default for Secp256k1Signature {
fn default() -> Self {
<Secp256k1Signature as Signature>::from_bytes(&[1u8; RECOVERABLE_SIGNATURE_SIZE]).unwrap()
}
}
// There is a strong requirement for this specific impl. in Fab benchmarks
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type")] // necessary so as not to deser under a != type
pub struct Secp256k1KeyPair {
pub name: Secp256k1PublicKey,
pub secret: Secp256k1PrivateKey,
}
impl EncodeDecodeBase64 for Secp256k1KeyPair {
fn decode_base64(value: &str) -> Result<Self, eyre::Report> {
keypair_decode_base64(value)
}
fn encode_base64(&self) -> String {
let mut bytes: Vec<u8> = Vec::new();
bytes.extend_from_slice(self.secret.as_ref());
bytes.extend_from_slice(self.name.as_ref());
base64ct::Base64::encode_string(&bytes[..])
}
}
impl KeyPair for Secp256k1KeyPair {
type PubKey = Secp256k1PublicKey;
type PrivKey = Secp256k1PrivateKey;
type Sig = Secp256k1Signature;
fn public(&'_ self) -> &'_ Self::PubKey {
&self.name
}
fn private(self) -> Self::PrivKey {
self.secret
}
fn generate<R: rand::CryptoRng + rand::RngCore>(rng: &mut R) -> Self {
let secp = Secp256k1::new();
let (privkey, pubkey) = secp.generate_keypair(rng);
Secp256k1KeyPair {
name: Secp256k1PublicKey {
pubkey,
bytes: OnceCell::new(),
},
secret: Secp256k1PrivateKey {
privkey,
bytes: OnceCell::new(),
},
}
}
#[cfg(feature = "copy_key")]
fn copy(&self) -> Self {
Secp256k1KeyPair {
name: self.name.clone(),
secret: Secp256k1PrivateKey::from_bytes(self.secret.as_ref()).unwrap(),
}
}
}
impl FromStr for Secp256k1KeyPair {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let kp = Self::decode_base64(s).map_err(|e| anyhow::anyhow!("{}", e.to_string()))?;
Ok(kp)
}
}
impl Signer<Secp256k1Signature> for Secp256k1KeyPair {
fn try_sign(&self, msg: &[u8]) -> Result<Secp256k1Signature, signature::Error> {
let secp = Secp256k1::signing_only();
#[cfg(test)]
let message =
Message::from_slice(<sha3::Keccak256 as sha3::digest::Digest>::digest(msg).as_slice())
.unwrap();
#[cfg(not(test))]
let message = Message::from_hashed_data::<rust_secp256k1::hashes::sha256::Hash>(msg);
// Creates a 65-bytes sigature of shape [r, s, v] where v can be 0 or 1.
// Pseudo-random deterministic nonce generation is used according to RFC6979.
Ok(Secp256k1Signature {
sig: secp.sign_ecdsa_recoverable(&message, &self.secret.privkey),
bytes: OnceCell::new(),
})
}
}
impl TryFrom<Secp256k1PublicKeyBytes> for Secp256k1PublicKey {
type Error = signature::Error;
fn try_from(bytes: Secp256k1PublicKeyBytes) -> Result<Secp256k1PublicKey, Self::Error> {
Secp256k1PublicKey::from_bytes(bytes.as_ref()).map_err(|_| Self::Error::new())
}
}
impl From<&Secp256k1PublicKey> for Secp256k1PublicKeyBytes {
fn from(pk: &Secp256k1PublicKey) -> Self {
Secp256k1PublicKeyBytes::from_bytes(pk.as_ref()).unwrap()
}
}
impl From<Secp256k1PrivateKey> for Secp256k1KeyPair {
fn from(secret: Secp256k1PrivateKey) -> Self {
let name = Secp256k1PublicKey::from(&secret);
Secp256k1KeyPair { name, secret }
}
}
impl Secp256k1Signature {
/// Recover public key from signature
pub fn recover(&self, hashed_msg: &[u8]) -> Result<Secp256k1PublicKey, signature::Error> {
match rust_secp256k1::Message::from_slice(hashed_msg) {
Ok(message) => match self.sig.recover(&message) {
Ok(pubkey) => Secp256k1PublicKey::from_bytes(pubkey.serialize().as_slice()),
Err(_) => Err(signature::Error::new()),
},
Err(_) => Err(signature::Error::new()),
}
}
}