-
Notifications
You must be signed in to change notification settings - Fork 37
/
basic.rs
38 lines (33 loc) · 1.32 KB
/
basic.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
//! # Basic example
//!
//! This example shows a basic usage of the `vrf-rs` crate:
//!
//! 1. Instantiate the `ECVRF` by specifying the `CipherSuite`
//! 2. Generate a VRF proof by using the `prove()` function
//! 3. (Optional) Convert the VRF proof to a hash (e.g. to be used as pseudo-random value)
//! 4. Verify a VRF proof by using `verify()` function
use vrf::openssl::{CipherSuite, ECVRF};
use vrf::VRF;
fn main() {
let mut vrf = ECVRF::from_suite(CipherSuite::SECP256K1_SHA256_TAI).unwrap();
// Inputs: Secret Key, Public Key (derived) & Message
let secret_key =
hex::decode("c9afa9d845ba75166b5c215767b1d6934e50c3db36e89b127b8a622b120f6721").unwrap();
let public_key = vrf.derive_public_key(&secret_key).unwrap();
let message: &[u8] = b"sample";
// VRF proof and hash output
let pi = vrf.prove(&secret_key, &message).unwrap();
let hash = vrf.proof_to_hash(&pi).unwrap();
println!("Generated VRF proof: {}", hex::encode(&pi));
// VRF proof verification (returns VRF hash output)
let beta = vrf.verify(&public_key, &pi, &message);
match beta {
Ok(beta) => {
println!("VRF proof is valid!\nHash output: {}", hex::encode(&beta));
assert_eq!(hash, beta);
}
Err(e) => {
println!("VRF proof is not valid: {}", e);
}
}
}