ark-r1cs-std/crypto-primitives/src/crh/injective_map/constraints.rs
crypto-primitives/src/crh/injective_map/constraints.rsRaw
1use core::{fmt::Debug, marker::PhantomData};
2
3use crate::crh::{
4 injective_map::{InjectiveMap, PedersenCRHCompressor, TECompressor},
5 pedersen::{constraints as ped_constraints, Window},
6 FixedLengthCRHGadget,
7};
8
9use algebra_core::{
10 curves::{
11 models::{ModelParameters, TEModelParameters},
12 twisted_edwards_extended::GroupProjective as TEProjective,
13 },
14 fields::{Field, PrimeField, SquareRootField},
15 ProjectiveCurve,
16};
17use r1cs_core::SynthesisError;
18use r1cs_std::{
19 fields::fp::FpVar,
20 groups::{curves::twisted_edwards::AffineVar as TEVar, CurveVar},
21 prelude::*,
22};
23
24type ConstraintF<C> = <<C as ProjectiveCurve>::BaseField as Field>::BasePrimeField;
25
26pub trait InjectiveMapGadget<
27 C: ProjectiveCurve,
28 I: InjectiveMap<C>,
29 GG: CurveVar<C, ConstraintF<C>>,
30> where
31 for<'a> &'a GG: GroupOpsBounds<'a, C, GG>,
32{
33 type OutputVar: EqGadget<ConstraintF<C>>
34 + ToBytesGadget<ConstraintF<C>>
35 + CondSelectGadget<ConstraintF<C>>
36 + AllocVar<I::Output, ConstraintF<C>>
37 + R1CSVar<ConstraintF<C>, Value = I::Output>
38 + Debug
39 + Clone
40 + Sized;
41
42 fn evaluate(ge: &GG) -> Result<Self::OutputVar, SynthesisError>;
43}
44
45pub struct TECompressorGadget;
46
47impl<F, P> InjectiveMapGadget<TEProjective<P>, TECompressor, TEVar<P, FpVar<F>>>
48 for TECompressorGadget
49where
50 F: PrimeField + SquareRootField,
51 P: TEModelParameters + ModelParameters<BaseField = F>,
52{
53 type OutputVar = FpVar<F>;
54
55 fn evaluate(ge: &TEVar<P, FpVar<F>>) -> Result<Self::OutputVar, SynthesisError> {
56 Ok(ge.x.clone())
57 }
58}
59
60pub struct PedersenCRHCompressorGadget<C, I, W, GG, IG>
61where
62 C: ProjectiveCurve,
63 I: InjectiveMap<C>,
64 W: Window,
65 GG: CurveVar<C, ConstraintF<C>>,
66 for<'a> &'a GG: GroupOpsBounds<'a, C, GG>,
67 IG: InjectiveMapGadget<C, I, GG>,
68{
69 #[doc(hidden)]
70 _compressor: PhantomData<I>,
71 #[doc(hidden)]
72 _compressor_gadget: PhantomData<IG>,
73 #[doc(hidden)]
74 _crh: ped_constraints::CRHGadget<C, GG, W>,
75}
76
77impl<C, I, GG, IG, W> FixedLengthCRHGadget<PedersenCRHCompressor<C, I, W>, ConstraintF<C>>
78 for PedersenCRHCompressorGadget<C, I, W, GG, IG>
79where
80 C: ProjectiveCurve,
81 I: InjectiveMap<C>,
82 GG: CurveVar<C, ConstraintF<C>>,
83 for<'a> &'a GG: GroupOpsBounds<'a, C, GG>,
84 IG: InjectiveMapGadget<C, I, GG>,
85 W: Window,
86{
87 type OutputVar = IG::OutputVar;
88 type ParametersVar = ped_constraints::CRHParametersVar<C, GG>;
89
90 fn evaluate(
91 parameters: &Self::ParametersVar,
92 input: &[UInt8<ConstraintF<C>>],
93 ) -> Result<Self::OutputVar, SynthesisError> {
94 let result = ped_constraints::CRHGadget::<C, GG, W>::evaluate(parameters, input)?;
95 IG::evaluate(&result)
96 }
97}