This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
lib.rs
551 lines (481 loc) · 20.7 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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// for enum variants
#![allow(unused_variables)]
#![allow(non_snake_case)]
extern crate schnorrkel;
// Copyright 2019 Soramitsu via https://github.com/Warchant/sr25519-crust
// Copyright 2019 Paritytech via https://github.com/paritytech/schnorrkel-js/
// Copyright 2019 @polkadot/wasm-schnorrkel authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
// Originally developed (as a fork) in https://github.com/polkadot-js/schnorrkel-js/
// which was adopted from the initial https://github.com/paritytech/schnorrkel-js/
// forked at commit eff430ddc3090f56317c80654208b8298ef7ab3f
use std::os::raw::c_ulong;
use std::ptr;
use std::slice;
use schnorrkel::{
context::signing_context,
derive::{CHAIN_CODE_LENGTH, ChainCode, Derivation}, ExpansionMode, Keypair, MiniSecretKey, PublicKey,
SecretKey, Signature, SignatureError, vrf::{VRFOutput, VRFProof}};
use std::fmt::{Formatter, Error};
// cbindgen has an issue with macros, so define it outside,
// otherwise it would've been possible to avoid duplication of macro variant list
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum Sr25519SignatureResult {
Ok,
EquationFalse,
PointDecompressionError,
ScalarFormatError,
BytesLengthError,
NotMarkedSchnorrkel,
MuSigAbsent,
MuSigInconsistent,
}
/// converts from schnorrkel::SignatureError
/// to Sr25519SignatureResult (which is exported to C header)
fn convert_error(err: &SignatureError) -> Sr25519SignatureResult {
match err {
SignatureError::EquationFalse => Sr25519SignatureResult::EquationFalse,
SignatureError::PointDecompressionError => Sr25519SignatureResult::PointDecompressionError,
SignatureError::ScalarFormatError => Sr25519SignatureResult::ScalarFormatError,
SignatureError::BytesLengthError { name: _, description: _, length: _ }
=> Sr25519SignatureResult::BytesLengthError,
SignatureError::MuSigAbsent { musig_stage: _ } => Sr25519SignatureResult::MuSigAbsent,
SignatureError::MuSigInconsistent { musig_stage: _, duplicate: _ }
=> Sr25519SignatureResult::MuSigInconsistent,
SignatureError::NotMarkedSchnorrkel => Sr25519SignatureResult::NotMarkedSchnorrkel
}
}
// We must make sure that this is the same as declared in the substrate source code.
const SIGNING_CTX: &'static [u8] = b"substrate";
pub const BABE_VRF_PREFIX: &'static [u8] = b"substrate-babe-vrf";
/// ChainCode construction helper
fn create_cc(data: &[u8]) -> ChainCode {
let mut cc = [0u8; CHAIN_CODE_LENGTH];
cc.copy_from_slice(&data);
ChainCode(cc)
}
/// Keypair helper function.
fn create_from_seed(seed: &[u8]) -> Keypair {
match MiniSecretKey::from_bytes(seed) {
Ok(mini) => return mini.expand_to_keypair(ExpansionMode::Ed25519),
Err(_) => panic!("Provided seed is invalid."),
}
}
/// Keypair helper function.
fn create_from_pair(pair: &[u8]) -> Keypair {
match Keypair::from_bytes(pair) {
Ok(pair) => return pair,
Err(_) => panic!(format!("Provided pair is invalid: {:?}", pair)),
}
}
/// PublicKey helper
fn create_public(public: &[u8]) -> PublicKey {
match PublicKey::from_bytes(public) {
Ok(public) => return public,
Err(_) => panic!("Provided public key is invalid."),
}
}
/// SecretKey helper
fn create_secret(secret: &[u8]) -> SecretKey {
match SecretKey::from_bytes(secret) {
Ok(secret) => return secret,
Err(_) => panic!("Provided private key is invalid."),
}
}
/// Size of input SEED for derivation, bytes
pub const SR25519_SEED_SIZE: c_ulong = 32;
/// Size of CHAINCODE, bytes
pub const SR25519_CHAINCODE_SIZE: c_ulong = 32;
/// Size of SR25519 PUBLIC KEY, bytes
pub const SR25519_PUBLIC_SIZE: c_ulong = 32;
/// Size of SR25519 PRIVATE (SECRET) KEY, which consists of [32 bytes key | 32 bytes nonce]
pub const SR25519_SECRET_SIZE: c_ulong = 64;
/// Size of SR25519 SIGNATURE, bytes
pub const SR25519_SIGNATURE_SIZE: c_ulong = 64;
/// Size of SR25519 KEYPAIR. [32 bytes key | 32 bytes nonce | 32 bytes public]
pub const SR25519_KEYPAIR_SIZE: c_ulong = 96;
/// Size of VRF output, bytes
pub const SR25519_VRF_OUTPUT_SIZE: c_ulong = 32;
/// Size of VRF proof, bytes
pub const SR25519_VRF_PROOF_SIZE: c_ulong = 64;
/// Size of VRF raw output, bytes
pub const SR25519_VRF_RAW_OUTPUT_SIZE: c_ulong = 16;
/// Size of VRF limit, bytes
pub const SR25519_VRF_THRESHOLD_SIZE: c_ulong = 16;
/// Perform a derivation on a secret
///
/// * keypair_out: pre-allocated output buffer of SR25519_KEYPAIR_SIZE bytes
/// * pair_ptr: existing keypair - input buffer of SR25519_KEYPAIR_SIZE bytes
/// * cc_ptr: chaincode - input buffer of SR25519_CHAINCODE_SIZE bytes
///
#[allow(unused_attributes)]
#[no_mangle]
pub unsafe extern "C" fn sr25519_derive_keypair_hard(
keypair_out: *mut u8,
pair_ptr: *const u8,
cc_ptr: *const u8,
) {
let pair = slice::from_raw_parts(pair_ptr, SR25519_KEYPAIR_SIZE as usize);
let cc = slice::from_raw_parts(cc_ptr, SR25519_CHAINCODE_SIZE as usize);
let kp = create_from_pair(pair)
.secret
.hard_derive_mini_secret_key(Some(create_cc(cc)), &[])
.0
.expand_to_keypair(ExpansionMode::Ed25519);
ptr::copy(kp.to_bytes().as_ptr(), keypair_out, SR25519_KEYPAIR_SIZE as usize);
}
/// Perform a derivation on a secret
///
/// * keypair_out: pre-allocated output buffer of SR25519_KEYPAIR_SIZE bytes
/// * pair_ptr: existing keypair - input buffer of SR25519_KEYPAIR_SIZE bytes
/// * cc_ptr: chaincode - input buffer of SR25519_CHAINCODE_SIZE bytes
///
#[allow(unused_attributes)]
#[no_mangle]
pub unsafe extern "C" fn sr25519_derive_keypair_soft(
keypair_out: *mut u8,
pair_ptr: *const u8,
cc_ptr: *const u8,
) {
let pair = slice::from_raw_parts(pair_ptr, SR25519_KEYPAIR_SIZE as usize);
let cc = slice::from_raw_parts(cc_ptr, SR25519_CHAINCODE_SIZE as usize);
let kp = create_from_pair(pair)
.derived_key_simple(create_cc(cc), &[])
.0;
ptr::copy(kp.to_bytes().as_ptr(), keypair_out, SR25519_KEYPAIR_SIZE as usize);
}
/// Perform a derivation on a publicKey
///
/// * pubkey_out: pre-allocated output buffer of SR25519_PUBLIC_SIZE bytes
/// * public_ptr: public key - input buffer of SR25519_PUBLIC_SIZE bytes
/// * cc_ptr: chaincode - input buffer of SR25519_CHAINCODE_SIZE bytes
///
#[allow(unused_attributes)]
#[no_mangle]
pub unsafe extern "C" fn sr25519_derive_public_soft(
pubkey_out: *mut u8,
public_ptr: *const u8,
cc_ptr: *const u8,
) {
let public = slice::from_raw_parts(public_ptr, SR25519_PUBLIC_SIZE as usize);
let cc = slice::from_raw_parts(cc_ptr, SR25519_CHAINCODE_SIZE as usize);
let p = create_public(public)
.derived_key_simple(create_cc(cc), &[])
.0;
ptr::copy(p.to_bytes().as_ptr(), pubkey_out, SR25519_PUBLIC_SIZE as usize);
}
/// Generate a key pair.
///
/// * keypair_out: keypair [32b key | 32b nonce | 32b public], pre-allocated output buffer of SR25519_KEYPAIR_SIZE bytes
/// * seed: generation seed - input buffer of SR25519_SEED_SIZE bytes
///
#[allow(unused_attributes)]
#[no_mangle]
pub unsafe extern "C" fn sr25519_keypair_from_seed(keypair_out: *mut u8, seed_ptr: *const u8) {
let seed = slice::from_raw_parts(seed_ptr, SR25519_SEED_SIZE as usize);
let kp = create_from_seed(seed);
ptr::copy(kp.to_bytes().as_ptr(), keypair_out, SR25519_KEYPAIR_SIZE as usize);
}
/// Sign a message
///
/// The combination of both public and private key must be provided.
/// This is effectively equivalent to a keypair.
///
/// * signature_out: output buffer of ED25519_SIGNATURE_SIZE bytes
/// * public_ptr: public key - input buffer of SR25519_PUBLIC_SIZE bytes
/// * secret_ptr: private key (secret) - input buffer of SR25519_SECRET_SIZE bytes
/// * message_ptr: Arbitrary message; input buffer of size message_length
/// * message_length: Length of a message
///
#[allow(unused_attributes)]
#[no_mangle]
pub unsafe extern "C" fn sr25519_sign(
signature_out: *mut u8,
public_ptr: *const u8,
secret_ptr: *const u8,
message_ptr: *const u8,
message_length: c_ulong,
) {
let public = slice::from_raw_parts(public_ptr, SR25519_PUBLIC_SIZE as usize);
let secret = slice::from_raw_parts(secret_ptr, SR25519_SECRET_SIZE as usize);
let message = slice::from_raw_parts(message_ptr, message_length as usize);
let sig = create_secret(secret).sign_simple(SIGNING_CTX, message, &create_public(public));
ptr::copy(
sig.to_bytes().as_ptr(),
signature_out,
SR25519_SIGNATURE_SIZE as usize,
);
}
/// Verify a message and its corresponding against a public key;
///
/// * signature_ptr: verify this signature
/// * message_ptr: Arbitrary message; input buffer of message_length bytes
/// * message_length: Message size
/// * public_ptr: verify with this public key; input buffer of SR25519_PUBLIC_SIZE bytes
///
/// * returned true if signature is valid, false otherwise
#[allow(unused_attributes)]
#[no_mangle]
pub unsafe extern "C" fn sr25519_verify(
signature_ptr: *const u8,
message_ptr: *const u8,
message_length: c_ulong,
public_ptr: *const u8,
) -> bool {
let public = slice::from_raw_parts(public_ptr, SR25519_PUBLIC_SIZE as usize);
let signature = slice::from_raw_parts(signature_ptr, SR25519_SIGNATURE_SIZE as usize);
let message = slice::from_raw_parts(message_ptr, message_length as usize);
let signature = match Signature::from_bytes(signature) {
Ok(signature) => signature,
Err(_) => return false,
};
create_public(public).verify_simple(SIGNING_CTX, message, &signature).is_ok()
}
#[repr(C)]
pub struct VrfResult {
pub result: Sr25519SignatureResult,
pub is_less: bool,
}
impl VrfResult {
fn create_err(err: &SignatureError) -> VrfResult {
VrfResult { is_less: false, result: convert_error(&err) }
}
fn create_val(is_less: bool) -> VrfResult {
VrfResult { is_less, result: Sr25519SignatureResult::Ok }
}
}
impl std::fmt::Debug for VrfResult {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
f.write_str("VrfResult { ")?;
f.write_str(self.is_less.to_string().as_str())?;
f.write_str(", ")?;
write!(f, "{:?}", self)?;
f.write_str(" }")?;
Result::Ok(())
}
}
/// Sign the provided message using a Verifiable Random Function and
/// if the result is less than \param limit provide the proof
/// @param out_and_proof_ptr pointer to output array, where the VRF out and proof will be written
/// @param keypair_ptr byte representation of the keypair that will be used during signing
/// @param message_ptr byte array to be signed
/// @param limit_ptr byte array, must be 16 bytes long
///
#[allow(unused_attributes)]
#[no_mangle]
pub unsafe extern "C" fn sr25519_vrf_sign_if_less(
out_and_proof_ptr: *mut u8,
keypair_ptr: *const u8,
message_ptr: *const u8,
message_length: c_ulong,
limit_ptr: *const u8,
) -> VrfResult {
let keypair_bytes = slice::from_raw_parts(keypair_ptr, SR25519_KEYPAIR_SIZE as usize);
let keypair = create_from_pair(keypair_bytes);
let message = slice::from_raw_parts(message_ptr, message_length as usize);
let limit = slice::from_raw_parts(limit_ptr, SR25519_VRF_THRESHOLD_SIZE as usize);
let mut limit_arr: [u8; SR25519_VRF_THRESHOLD_SIZE as usize] = Default::default();
limit_arr.copy_from_slice(&limit[0..SR25519_VRF_THRESHOLD_SIZE as usize]);
let (io, proof, _) =
keypair.vrf_sign(
signing_context(SIGNING_CTX).bytes(message));
let limit_int = u128::from_le_bytes(limit_arr);
let raw_out_bytes = io.make_bytes::<[u8; SR25519_VRF_RAW_OUTPUT_SIZE as usize]>(BABE_VRF_PREFIX);
let check = u128::from_le_bytes(raw_out_bytes) < limit_int;
ptr::copy(io.to_output().as_bytes().as_ptr(), out_and_proof_ptr, SR25519_VRF_OUTPUT_SIZE as usize);
ptr::copy(proof.to_bytes().as_ptr(), out_and_proof_ptr.add(SR25519_VRF_OUTPUT_SIZE as usize), SR25519_VRF_PROOF_SIZE as usize);
if check {
VrfResult::create_val(true)
} else {
VrfResult::create_val(false)
}
}
/// Verify a signature produced by a VRF with its original input and the corresponding proof and
/// check if the result of the function is less than the threshold.
/// @note If errors, is_less field of the returned structure is not meant to contain a valid value
/// @param public_key_ptr byte representation of the public key that was used to sign the message
/// @param message_ptr the orignal signed message
/// @param output_ptr the signature
/// @param proof_ptr the proof of the signature
/// @param threshold_ptr the threshold to be compared against
#[allow(unused_attributes)]
#[no_mangle]
pub unsafe extern "C" fn sr25519_vrf_verify(
public_key_ptr: *const u8,
message_ptr: *const u8,
message_length: c_ulong,
output_ptr: *const u8,
proof_ptr: *const u8,
threshold_ptr: *const u8,
) -> VrfResult {
let public_key = create_public(slice::from_raw_parts(public_key_ptr, SR25519_PUBLIC_SIZE as usize));
let message = slice::from_raw_parts(message_ptr, message_length as usize);
let ctx = signing_context(SIGNING_CTX).bytes(message);
let given_out = match VRFOutput::from_bytes(
slice::from_raw_parts(output_ptr, SR25519_VRF_OUTPUT_SIZE as usize)) {
Ok(val) => val,
Err(err) => return VrfResult::create_err(&err)
};
let given_proof = match VRFProof::from_bytes(
slice::from_raw_parts(proof_ptr, SR25519_VRF_PROOF_SIZE as usize)) {
Ok(val) => val,
Err(err) => return VrfResult::create_err(&err)
};
let (in_out, proof) =
match public_key.vrf_verify(ctx.clone(), &given_out, &given_proof) {
Ok(val) => val,
Err(err) => return VrfResult::create_err(&err)
};
let raw_output = in_out.make_bytes::<[u8; SR25519_VRF_RAW_OUTPUT_SIZE as usize]>(BABE_VRF_PREFIX);
let threshold = slice::from_raw_parts(threshold_ptr, SR25519_VRF_THRESHOLD_SIZE as usize);
let mut threshold_arr: [u8; SR25519_VRF_THRESHOLD_SIZE as usize] = Default::default();
threshold_arr.copy_from_slice(&threshold[0..SR25519_VRF_THRESHOLD_SIZE as usize]);
let threshold_int = u128::from_le_bytes(threshold_arr);
let check = u128::from_le_bytes(raw_output) < threshold_int;
let decomp_proof = match
proof.shorten_vrf(&public_key, ctx.clone(), &in_out.to_output()) {
Ok(val) => val,
Err(e) => return VrfResult::create_err(&e)
};
if in_out.to_output() == given_out &&
decomp_proof == given_proof {
VrfResult::create_val(check)
} else {
VrfResult::create_err(&SignatureError::EquationFalse)
}
}
#[cfg(test)]
pub mod tests {
extern crate rand;
extern crate schnorrkel;
use super::*;
use hex_literal::hex;
use schnorrkel::{KEYPAIR_LENGTH, SECRET_KEY_LENGTH, SIGNATURE_LENGTH};
fn generate_random_seed() -> Vec<u8> {
(0..32).map(|_| rand::random::<u8>()).collect()
}
#[test]
fn can_create_keypair() {
let seed = generate_random_seed();
let mut keypair = [0u8; SR25519_KEYPAIR_SIZE as usize];
unsafe { sr25519_keypair_from_seed(keypair.as_mut_ptr(), seed.as_ptr()) };
assert_eq!(keypair.len(), KEYPAIR_LENGTH);
}
#[test]
fn creates_pair_from_known() {
let seed = hex!("fac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e");
let expected = hex!("46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a");
let mut keypair = [0u8; SR25519_KEYPAIR_SIZE as usize];
unsafe { sr25519_keypair_from_seed(keypair.as_mut_ptr(), seed.as_ptr()) };
let public = &keypair[SECRET_KEY_LENGTH..KEYPAIR_LENGTH];
assert_eq!(public, expected);
}
#[test]
fn can_sign_message() {
let seed = generate_random_seed();
let mut keypair = [0u8; SR25519_KEYPAIR_SIZE as usize];
unsafe { sr25519_keypair_from_seed(keypair.as_mut_ptr(), seed.as_ptr()) };
let private = &keypair[0..SECRET_KEY_LENGTH];
let public = &keypair[SECRET_KEY_LENGTH..KEYPAIR_LENGTH];
let message = b"this is a message";
let mut signature = [0u8; SR25519_SIGNATURE_SIZE as usize];
unsafe {
sr25519_sign(
signature.as_mut_ptr(),
public.as_ptr(),
private.as_ptr(),
message.as_ptr(),
message.len() as c_ulong,
)
};
assert_eq!(signature.len(), SIGNATURE_LENGTH);
}
#[test]
fn can_verify_message() {
let seed = generate_random_seed();
let mut keypair = [0u8; SR25519_KEYPAIR_SIZE as usize];
unsafe { sr25519_keypair_from_seed(keypair.as_mut_ptr(), seed.as_ptr()) };
let private = &keypair[0..SECRET_KEY_LENGTH];
let public = &keypair[SECRET_KEY_LENGTH..KEYPAIR_LENGTH];
let message = b"this is a message";
let mut signature = [0u8; SR25519_SIGNATURE_SIZE as usize];
unsafe {
sr25519_sign(
signature.as_mut_ptr(),
public.as_ptr(),
private.as_ptr(),
message.as_ptr(),
message.len() as c_ulong,
)
};
let is_valid = unsafe {
sr25519_verify(
signature.as_ptr(),
message.as_ptr(),
message.len() as c_ulong,
public.as_ptr(),
)
};
assert!(is_valid);
}
#[test]
fn soft_derives_pair() {
let cc = hex!("0c666f6f00000000000000000000000000000000000000000000000000000000"); // foo
let seed = hex!("fac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e");
let expected = hex!("40b9675df90efa6069ff623b0fdfcf706cd47ca7452a5056c7ad58194d23440a");
let mut keypair = [0u8; SR25519_KEYPAIR_SIZE as usize];
let mut derived = [0u8; SR25519_KEYPAIR_SIZE as usize];
unsafe { sr25519_keypair_from_seed(keypair.as_mut_ptr(), seed.as_ptr()) };
unsafe { sr25519_derive_keypair_soft(derived.as_mut_ptr(), keypair.as_ptr(), cc.as_ptr()) };
let public = &derived[SECRET_KEY_LENGTH..KEYPAIR_LENGTH];
assert_eq!(public, expected);
}
#[test]
fn soft_derives_public() {
let cc = hex!("0c666f6f00000000000000000000000000000000000000000000000000000000"); // foo
let public = hex!("46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a");
let expected = hex!("40b9675df90efa6069ff623b0fdfcf706cd47ca7452a5056c7ad58194d23440a");
let mut derived = [0u8; SR25519_PUBLIC_SIZE as usize];
unsafe { sr25519_derive_public_soft(derived.as_mut_ptr(), public.as_ptr(), cc.as_ptr()) };
assert_eq!(derived, expected);
}
#[test]
fn hard_derives_pair() {
let cc = hex!("14416c6963650000000000000000000000000000000000000000000000000000"); // Alice
let seed = hex!("fac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e");
let expected = hex!("d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d");
let mut keypair = [0u8; SR25519_KEYPAIR_SIZE as usize];
unsafe { sr25519_keypair_from_seed(keypair.as_mut_ptr(), seed.as_ptr()) };
let mut derived = [0u8; SR25519_KEYPAIR_SIZE as usize];
unsafe { sr25519_derive_keypair_hard(derived.as_mut_ptr(), keypair.as_ptr(), cc.as_ptr()) };
let public = &derived[SECRET_KEY_LENGTH..KEYPAIR_LENGTH];
assert_eq!(public, expected);
}
#[test]
fn vrf_verify() {
let seed = generate_random_seed();
let mut keypair_bytes = [0u8; SR25519_KEYPAIR_SIZE as usize];
unsafe { sr25519_keypair_from_seed(keypair_bytes.as_mut_ptr(), seed.as_ptr()) };
let private = &keypair_bytes[0..SECRET_KEY_LENGTH];
let public = &keypair_bytes[SECRET_KEY_LENGTH..KEYPAIR_LENGTH];
let message = b"Hello, world!";
let keypair = Keypair::from_bytes(&keypair_bytes).expect("Keypair creation error");
let ctx = signing_context(SIGNING_CTX).bytes(message);
let (io, proof, _) = keypair.vrf_sign(ctx.clone());
let (io_, proof_) = keypair.public.vrf_verify(ctx.clone(), &io.to_output(), &proof).expect("Verification error");
assert_eq!(io_, io);
let decomp_proof = proof_.shorten_vrf(
&keypair.public, ctx.clone(), &io.to_output()).expect("Shorten VRF");
assert_eq!(proof, decomp_proof);
unsafe {
let threshold_bytes = [0u8; SR25519_VRF_THRESHOLD_SIZE as usize];
let res = sr25519_vrf_verify(public.as_ptr(),
message.as_ptr(), message.len() as c_ulong,
io.as_output_bytes().as_ptr(),
proof.to_bytes().as_ptr(), threshold_bytes.as_ptr());
assert_eq!(res.result, Sr25519SignatureResult::Ok);
}
}
}