-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsecp256k1.move
279 lines (236 loc) · 9.51 KB
/
secp256k1.move
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
/// This module implements ECDSA signatures based on the prime-order secp256k1 ellptic curve (i.e., cofactor is 1).
module initia_std::secp256k1 {
use std::option::Option;
//
// Error codes
//
/// An error occurred while deserializing, for example due to wrong input size.
const E_DESERIALIZE: u64 = 1; // This code must be the same, if ever returned from the native Rust implementation.
//
// constants
//
/// The size of a secp256k1-based ECDSA public key, in bytes.
const RAW_PUBLIC_KEY_NUM_BYTES: u64 = 64;
/// The size of a secp256k1-based ECDSA compressed public key, in bytes.
const COMPRESSED_PUBLIC_KEY_SIZE: u64 = 33;
/// The size of a secp256k1-based ECDSA signature, in bytes.
const SIGNATURE_NUM_BYTES: u64 = 64;
/// The size of a hashed message for secp256k1-based ECDSA signing
const MESSAGE_SIZE: u64 = 32;
/// A 64-byte ECDSA public key.
struct ECDSARawPublicKey has copy, drop, store {
bytes: vector<u8>
}
/// A 33-byte ECDSA public key.
struct ECDSACompressedPublicKey has copy, drop, store {
bytes: vector<u8>
}
/// A 64-byte ECDSA signature.
struct ECDSASignature has copy, drop, store {
bytes: vector<u8>
}
/// Constructs an ECDSASignature struct from the given 64 bytes.
public fun ecdsa_signature_from_bytes(bytes: vector<u8>): ECDSASignature {
assert!(
std::vector::length(&bytes) == SIGNATURE_NUM_BYTES,
std::error::invalid_argument(E_DESERIALIZE)
);
ECDSASignature { bytes }
}
/// Constructs an ECDSARawPublicKey struct, given a 64-byte raw representation.
public fun ecdsa_raw_public_key_from_64_bytes(bytes: vector<u8>): ECDSARawPublicKey {
ecdsa_raw_public_key_from_bytes(bytes)
}
/// Constructs an ECDSARawPublicKey struct, given a 64-byte raw representation.
public fun ecdsa_raw_public_key_from_bytes(bytes: vector<u8>): ECDSARawPublicKey {
assert!(
std::vector::length(&bytes) == RAW_PUBLIC_KEY_NUM_BYTES,
std::error::invalid_argument(E_DESERIALIZE)
);
ECDSARawPublicKey { bytes }
}
/// Constructs an ECDSACompressedPublicKey struct, given a 33-byte raw representation.
public fun ecdsa_compressed_public_key_from_bytes(bytes: vector<u8>):
ECDSACompressedPublicKey {
assert!(
std::vector::length(&bytes) == COMPRESSED_PUBLIC_KEY_SIZE,
std::error::invalid_argument(E_DESERIALIZE)
);
ECDSACompressedPublicKey { bytes }
}
/// Serializes an ECDSARawPublicKey struct to 64-bytes.
public fun ecdsa_raw_public_key_to_bytes(pk: &ECDSARawPublicKey): vector<u8> {
pk.bytes
}
/// Serializes an ECDSARawPublicKey struct to 64-bytes.
public fun ecdsa_compressed_public_key_to_bytes(
pk: &ECDSACompressedPublicKey
): vector<u8> {
pk.bytes
}
/// Serializes an ECDSASignature struct to 64-bytes.
public fun ecdsa_signature_to_bytes(sig: &ECDSASignature): vector<u8> {
sig.bytes
}
/// Returns `true` if the signature can verify the public key on the message
public fun verify(
message: vector<u8>,
public_key: &ECDSACompressedPublicKey,
signature: &ECDSASignature
): bool {
assert!(
std::vector::length(&message) == MESSAGE_SIZE,
std::error::invalid_argument(E_DESERIALIZE)
);
return verify_internal(
message,
public_key.bytes,
signature.bytes
)
}
/// Recovers the signer's raw (64-byte) public key from a secp256k1 ECDSA `signature` given the `recovery_id` and the signed
/// `message` (32 byte digest).
///
/// Note that an invalid signature, or a signature from a different message, will result in the recovery of an
/// incorrect public key. This recovery algorithm can only be used to check validity of a signature if the signer's
/// public key (or its hash) is known beforehand.
public fun ecdsa_recover(
message: vector<u8>, recovery_id: u8, signature: &ECDSASignature
): Option<ECDSARawPublicKey> {
assert!(
std::vector::length(&message) == MESSAGE_SIZE,
std::error::invalid_argument(E_DESERIALIZE)
);
let (pk, success) =
recover_public_key_internal(
recovery_id,
message,
signature.bytes,
false
);
if (success) {
std::option::some(ecdsa_raw_public_key_from_bytes(pk))
} else {
std::option::none<ECDSARawPublicKey>()
}
}
/// Recovers the signer's raw (64-byte) public key from a secp256k1 ECDSA `signature` given the `recovery_id` and the signed
/// `message` (32 byte digest).
///
/// Note that an invalid signature, or a signature from a different message, will result in the recovery of an
/// incorrect public key. This recovery algorithm can only be used to check validity of a signature if the signer's
/// public key (or its hash) is known beforehand.
public fun ecdsa_recover_compressed(
message: vector<u8>, recovery_id: u8, signature: &ECDSASignature
): Option<ECDSACompressedPublicKey> {
assert!(
std::vector::length(&message) == MESSAGE_SIZE,
std::error::invalid_argument(E_DESERIALIZE)
);
let (pk, success) =
recover_public_key_internal(
recovery_id,
message,
signature.bytes,
true
);
if (success) {
std::option::some(ecdsa_compressed_public_key_from_bytes(pk))
} else {
std::option::none<ECDSACompressedPublicKey>()
}
}
//
// Native functions
//
/// Returns `true` if `signature` verifies on `public_key` and `message`
/// and returns `false` otherwise.
///
/// - `message`: A 32-byte hashed message.
/// - `public_key`: A compressed public key in bytes.
/// - `signature`: A 64-byte ECDSA signature.
native fun verify_internal(
message: vector<u8>,
public_key: vector<u8>,
signature: vector<u8>
): bool;
/// Returns `(public_key, true)` if `signature` verifies on `message` under the recovered `public_key`
/// and returns `([], false)` otherwise.
native fun recover_public_key_internal(
recovery_id: u8,
message: vector<u8>,
signature: vector<u8>,
compressed: bool
): (vector<u8>, bool);
#[test_only]
/// Generates an secp256k1 ECDSA key pair.
native public fun generate_keys(compressed: bool): (vector<u8>, vector<u8>);
#[test_only]
/// Generates an secp256k1 ECDSA signature for a given byte array using a given signing key.
native public fun sign(message: vector<u8>, secrete_key: vector<u8>): (u8, vector<u8>);
//
// Tests
//
#[test]
fun test_secp256k1_sign_verify() {
use std::hash;
let (sk, vk) = generate_keys(true);
let pk = ecdsa_compressed_public_key_from_bytes(vk);
let msg: vector<u8> = hash::sha2_256(b"test initia secp256k1");
let (_rid, sig_bytes) = sign(msg, sk);
let sig = ecdsa_signature_from_bytes(sig_bytes);
assert!(verify(msg, &pk, &sig), 1);
// Test with an incorrect message
let wrong_msg: vector<u8> = hash::sha2_256(b"wrong message");
assert!(!verify(wrong_msg, &pk, &sig), 2);
// Test with an incorrect signature
let invalid_sig_bytes = sig_bytes;
*std::vector::borrow_mut(&mut invalid_sig_bytes, 0) = 0xFF; // Corrupt the signature
let invalid_sig = ecdsa_signature_from_bytes(invalid_sig_bytes);
assert!(!verify(msg, &pk, &invalid_sig), 3);
}
#[test]
fun test_gen_sign_recover() {
use std::hash;
let (sk, vk) = generate_keys(false);
let pk = ecdsa_raw_public_key_from_bytes(vk);
let msg: vector<u8> = hash::sha2_256(b"test initia secp256k1");
let (rid, sig_bytes) = sign(msg, sk);
let sig = ecdsa_signature_from_bytes(sig_bytes);
let recovered_pk = ecdsa_recover(msg, rid, &sig);
assert!(std::option::is_some(&recovered_pk), 1);
assert!(
std::option::extract(&mut recovered_pk).bytes == pk.bytes,
2
);
let wrong_msg: vector<u8> = hash::sha2_256(b"test initia");
let recovered_pk = ecdsa_recover(wrong_msg, rid, &sig);
assert!(std::option::is_some(&recovered_pk), 3);
assert!(
std::option::extract(&mut recovered_pk).bytes != pk.bytes,
4
);
}
#[test]
fun test_gen_sign_recover_compressed() {
use std::hash;
let (sk, vk) = generate_keys(true);
let pk = ecdsa_compressed_public_key_from_bytes(vk);
let msg: vector<u8> = hash::sha2_256(b"test initia secp256k1");
let (rid, sig_bytes) = sign(msg, sk);
let sig = ecdsa_signature_from_bytes(sig_bytes);
let recovered_pk = ecdsa_recover_compressed(msg, rid, &sig);
assert!(std::option::is_some(&recovered_pk), 1);
assert!(
std::option::extract(&mut recovered_pk).bytes == pk.bytes,
2
);
let wrong_msg: vector<u8> = hash::sha2_256(b"test initia");
let recovered_pk = ecdsa_recover_compressed(wrong_msg, rid, &sig);
assert!(std::option::is_some(&recovered_pk), 3);
assert!(
std::option::extract(&mut recovered_pk).bytes != pk.bytes,
4
);
}
}