ark-r1cs-std/crypto-primitives/src/crh/injective_map/mod.rs
crypto-primitives/src/crh/injective_map/mod.rsRaw
1use crate::{CryptoError, Error};
2use algebra_core::bytes::ToBytes;
3use core::{fmt::Debug, hash::Hash, marker::PhantomData};
4use rand::Rng;
5
6use super::{pedersen, FixedLengthCRH};
7use algebra_core::curves::{
8 models::{ModelParameters, TEModelParameters},
9 twisted_edwards_extended::{GroupAffine as TEAffine, GroupProjective as TEProjective},
10 ProjectiveCurve,
11};
12
13#[cfg(feature = "r1cs")]
14pub mod constraints;
15
16pub trait InjectiveMap<C: ProjectiveCurve> {
17 type Output: ToBytes + Clone + Eq + Hash + Default + Debug;
18
19 fn injective_map(ge: &C::Affine) -> Result<Self::Output, CryptoError>;
20}
21
22pub struct TECompressor;
23
24impl<P: TEModelParameters> InjectiveMap<TEProjective<P>> for TECompressor {
25 type Output = <P as ModelParameters>::BaseField;
26
27 fn injective_map(ge: &TEAffine<P>) -> Result<Self::Output, CryptoError> {
28 debug_assert!(ge.is_in_correct_subgroup_assuming_on_curve());
29 Ok(ge.x)
30 }
31}
32
33pub struct PedersenCRHCompressor<C: ProjectiveCurve, I: InjectiveMap<C>, W: pedersen::Window> {
34 _group: PhantomData<C>,
35 _compressor: PhantomData<I>,
36 _crh: pedersen::CRH<C, W>,
37}
38
39impl<C: ProjectiveCurve, I: InjectiveMap<C>, W: pedersen::Window> FixedLengthCRH
40 for PedersenCRHCompressor<C, I, W>
41{
42 const INPUT_SIZE_BITS: usize = pedersen::CRH::<C, W>::INPUT_SIZE_BITS;
43 type Output = I::Output;
44 type Parameters = pedersen::Parameters<C>;
45
46 fn setup<R: Rng>(rng: &mut R) -> Result<Self::Parameters, Error> {
47 let time = start_timer!(|| format!("PedersenCRHCompressor::Setup"));
48 let params = pedersen::CRH::<C, W>::setup(rng);
49 end_timer!(time);
50 params
51 }
52
53 fn evaluate(parameters: &Self::Parameters, input: &[u8]) -> Result<Self::Output, Error> {
54 let eval_time = start_timer!(|| "PedersenCRHCompressor::Eval");
55 let result = I::injective_map(&pedersen::CRH::<C, W>::evaluate(parameters, input)?)?;
56 end_timer!(eval_time);
57 Ok(result)
58 }
59}