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