jf_vrf/
lib.rs

1// Copyright (c) 2022 Espresso Systems (espressosys.com)
2// This file is part of the Jellyfish library.
3
4// You should have received a copy of the MIT License
5// along with the Jellyfish library. If not, see <https://mit-license.org/>.
6
7//! Verifiable random functions (VRF).
8
9#![cfg_attr(not(feature = "std"), no_std)]
10// Temporarily allow warning for nightly compilation with [`displaydoc`].
11#![allow(warnings)]
12#![deny(missing_docs)]
13#[cfg(test)]
14extern crate std;
15
16#[cfg(any(not(feature = "std"), target_has_atomic = "ptr"))]
17#[doc(hidden)]
18extern crate alloc;
19
20use ark_std::rand::{CryptoRng, RngCore};
21use core::fmt::Debug;
22use serde::{Deserialize, Serialize};
23use zeroize::Zeroize;
24
25pub mod blsvrf;
26
27/// A trait for VRF proof, evaluation and verification.
28pub trait Vrf {
29    /// Public parameters
30    type PublicParameter;
31
32    /// VRF public key.
33    type PublicKey: Debug
34        + Clone
35        + Send
36        + Sync
37        + for<'a> Deserialize<'a>
38        + Serialize
39        + PartialEq
40        + Eq;
41
42    /// VRF secret key.
43    type SecretKey: Debug + Clone + Send + Sync + Zeroize + PartialEq + Eq;
44
45    /// VRF signature.
46    type Proof: Debug + Clone + Send + Sync + for<'a> Deserialize<'a> + Serialize + PartialEq + Eq;
47
48    /// The input of VRF proof.
49    type Input: Debug + Clone + Send + Sync + for<'a> Deserialize<'a> + Serialize + PartialEq + Eq;
50
51    /// The output of VRF evaluation.
52    type Output: Debug + Clone + Send + Sync + for<'a> Deserialize<'a> + Serialize + PartialEq + Eq;
53
54    /// Error type
55    type Error: ark_std::error::Error;
56
57    /// generate public parameters from RNG.
58    /// If the RNG is not presented, use the default group generator.
59    // FIXME: the API looks a bit strange when the default generator is used.
60    // For example:
61    //   `S::param_gen::<StdRng>(None)`
62    // where `StdRng` is redundant.
63    fn param_gen<R: CryptoRng + RngCore>(
64        &self,
65        prng: Option<&mut R>,
66    ) -> Result<Self::PublicParameter, Self::Error>;
67
68    /// Creates a pair of VRF public and private keys.
69    fn key_gen<R: CryptoRng + RngCore>(
70        &self,
71        pp: &Self::PublicParameter,
72        prng: &mut R,
73    ) -> Result<(Self::SecretKey, Self::PublicKey), Self::Error>;
74
75    /// Creates the VRF proof associated with a VRF secret key.
76    fn prove<R: CryptoRng + RngCore>(
77        &self,
78        pp: &Self::PublicParameter,
79        secret_key: &Self::SecretKey,
80        input: &Self::Input,
81        prng: &mut R,
82    ) -> Result<Self::Proof, Self::Error>;
83
84    /// Computes the VRF output associated with a VRF proof.
85    fn proof_to_hash(
86        &mut self,
87        pp: &Self::PublicParameter,
88        proof: &Self::Proof,
89    ) -> Result<Self::Output, Self::Error>;
90
91    /// Computes the VRF output given a public input and a VRF secret key.
92    fn evaluate<R: CryptoRng + RngCore>(
93        &mut self,
94        pp: &Self::PublicParameter,
95        secret_key: &Self::SecretKey,
96        input: &Self::Input,
97        prng: &mut R,
98    ) -> Result<Self::Output, Self::Error> {
99        let proof = self.prove(pp, secret_key, input, prng)?;
100        self.proof_to_hash(pp, &proof)
101    }
102
103    /// Verifies a VRF proof.
104    #[must_use = "Output must be used"]
105    fn verify(
106        &mut self,
107        pp: &Self::PublicParameter,
108        proof: &Self::Proof,
109        public_key: &Self::PublicKey,
110        input: &Self::Input,
111    ) -> Result<(bool, Option<Self::Output>), Self::Error>;
112}