jf_prf/
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//! Trait definition for Pseudorandom function (PRF).
8#![no_std]
9
10use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
11use ark_std::{
12    borrow::Borrow,
13    fmt::Debug,
14    rand::{CryptoRng, RngCore},
15    UniformRand,
16};
17/// Trait for Pseudo-random Functions
18pub trait PRF {
19    /// Input to the PRF
20    type Input: Clone + CanonicalDeserialize;
21    /// Output of the PRF
22    type Output: Clone + Debug + PartialEq + Eq + CanonicalSerialize;
23    /// The random seed/key that index a specific function from the PRF
24    /// ensembles
25    type Seed: Clone + Debug + Default + UniformRand + CanonicalSerialize + CanonicalDeserialize;
26    /// Error type
27    type Error: ark_std::error::Error;
28
29    /// Compute PRF output with a user-provided randomly generated `seed`
30    fn evaluate<S: Borrow<Self::Seed>, I: Borrow<Self::Input>>(
31        seed: S,
32        input: I,
33    ) -> Result<Self::Output, Self::Error>;
34
35    /// same as [`Self::evaluate`] except that we generate a fresh random seed
36    /// for the evaluation
37    fn evaluate_with_rand_seed<R: RngCore + CryptoRng, T: Borrow<Self::Input>>(
38        rng: &mut R,
39        input: T,
40    ) -> Result<(Self::Seed, Self::Output), Self::Error> {
41        let seed = Self::Seed::rand(rng);
42        let output = Self::evaluate(&seed, input)?;
43        Ok((seed, output))
44    }
45}