vrf_mod/
lib.rs

1//! # Verifiable Random Function
2//!
3//! This crate defines a trait that must be implemented by VRFs.
4//!
5use openssl::{
6    rsa::Rsa,
7    pkey::{Public, Private}
8};
9
10pub mod vrf;
11
12/// A trait containing the common capabilities for VRF implementations
13///
14pub trait VRF {
15    type Error;
16
17    /// Generates proof from a private key and a message
18    ///
19    /// # Arguments:
20    ///
21    /// *    `pkey`: a private key
22    /// *    `alpha_string`: octet string message represented by a slice
23    ///
24    /// # Returns:
25    ///
26    /// *    if successful, a vector of octets representing the VRF proof
27    ///
28    fn prove(&mut self, pkey: &Rsa<Private>, alpha_string: &[u8]) -> Result<Vec<u8>, Self::Error>;
29
30    /// Generates VRF hash output from the provided proof
31    ///
32    /// # Arguments:
33    ///
34    /// *    `pi_string`: generated VRF proof
35    ///
36    /// # Returns:
37    ///
38    /// *    the VRF hash output
39    ///
40    fn proof_to_hash(&mut self, pi_string: &[u8]) -> Result<Vec<u8>, Self::Error>;
41
42    /// Verifies the provided VRF proof and computes the VRF hash output
43    ///
44    /// # Arguments:
45    ///
46    /// *    `public_key`: a public key
47    /// *    `alpha_string`: VRF hash input, an octet string
48    /// *    `pi_string`: proof to be verified, an octet string
49    /// 
50    /// # Returns:
51    ///
52    /// *    if successful, a vector of octets with the VRF hash output
53    ///
54    fn verify(&mut self, public_key: &Rsa<Public>, alpha_string: &[u8], pi_string: &[u8]) -> Result<Vec<u8>, Self::Error>;
55}
56
57pub mod ecvrf;
58
59/// A trait containing common capabilities for ECVRF implementations
60///
61pub trait ECVRF<PrivateKey, PublicKey> {
62    type Error;
63
64    /// Generates proof from a private key and a message
65    ///
66    /// # Arguments:
67    ///
68    /// *    `pkey`: a private key
69    /// *    `alpha_string`: octet string message represented by a slice
70    ///
71    /// # Returns:
72    ///
73    /// *    if successful, a vector of octets representing the VRF proof
74    ///
75    fn prove(&mut self, pkey: PrivateKey, alpha_string: &[u8]) -> Result<Vec<u8>, Self::Error>;
76
77    /// Generates VRF hash output from the provided proof
78    ///
79    /// # Arguments:
80    ///
81    /// *    `pi_string`: generated VRF proof
82    ///
83    /// # Returns:
84    ///
85    /// *    the VRF hash output
86    ///
87    fn proof_to_hash(&mut self, pi_string: &[u8]) -> Result<Vec<u8>, Self::Error>;
88
89    /// Verifies the provided VRF proof and computes the VRF hash output
90    ///
91    /// # Arguments:
92    ///
93    /// *    `public_key`: a public key
94    /// *    `alpha_string`: VRF hash input, an octet string
95    /// *    `pi_string`: proof to be verified, an octet string
96    /// 
97    /// # Returns:
98    ///
99    /// *    if successful, a vector of octets with the VRF hash output
100    ///
101    fn verify(&mut self, public_key: PublicKey, alpha_string: &[u8], pi_string: &[u8]) -> Result<Vec<u8>, Self::Error>;
102}