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::{
6 constraints::{PedersenCRHGadget, PedersenCRHGadgetParameters},
7 PedersenWindow,
8 },
9 FixedLengthCRHGadget,
10};
11
12use algebra_core::{
13 curves::{
14 models::{ModelParameters, TEModelParameters},
15 twisted_edwards_extended::{GroupAffine as TEAffine, GroupProjective as TEProjective},
16 },
17 fields::{Field, PrimeField, SquareRootField},
18 groups::Group,
19};
20use r1cs_core::{ConstraintSystem, SynthesisError};
21use r1cs_std::{
22 fields::fp::FpGadget,
23 groups::{curves::twisted_edwards::AffineGadget as TwistedEdwardsGadget, GroupGadget},
24 prelude::*,
25};
26
27pub trait InjectiveMapGadget<
28 G: Group,
29 I: InjectiveMap<G>,
30 ConstraintF: Field,
31 GG: GroupGadget<G, ConstraintF>,
32>
33{
34 type OutputGadget: EqGadget<ConstraintF>
35 + ToBytesGadget<ConstraintF>
36 + CondSelectGadget<ConstraintF>
37 + AllocGadget<I::Output, ConstraintF>
38 + Debug
39 + Clone
40 + Sized;
41
42 fn evaluate_map<CS: ConstraintSystem<ConstraintF>>(
43 cs: CS,
44 ge: &GG,
45 ) -> Result<Self::OutputGadget, SynthesisError>;
46}
47
48pub struct TECompressorGadget;
49
50impl<ConstraintF, P>
51 InjectiveMapGadget<
52 TEAffine<P>,
53 TECompressor,
54 ConstraintF,
55 TwistedEdwardsGadget<P, ConstraintF, FpGadget<ConstraintF>>,
56 > for TECompressorGadget
57where
58 ConstraintF: PrimeField + SquareRootField,
59 P: TEModelParameters + ModelParameters<BaseField = ConstraintF>,
60{
61 type OutputGadget = FpGadget<ConstraintF>;
62
63 fn evaluate_map<CS: ConstraintSystem<ConstraintF>>(
64 _cs: CS,
65 ge: &TwistedEdwardsGadget<P, ConstraintF, FpGadget<ConstraintF>>,
66 ) -> Result<Self::OutputGadget, SynthesisError> {
67 Ok(ge.x.clone())
68 }
69}
70
71impl<ConstraintF, P>
72 InjectiveMapGadget<
73 TEProjective<P>,
74 TECompressor,
75 ConstraintF,
76 TwistedEdwardsGadget<P, ConstraintF, FpGadget<ConstraintF>>,
77 > for TECompressorGadget
78where
79 ConstraintF: PrimeField + SquareRootField,
80 P: TEModelParameters + ModelParameters<BaseField = ConstraintF>,
81{
82 type OutputGadget = FpGadget<ConstraintF>;
83
84 fn evaluate_map<CS: ConstraintSystem<ConstraintF>>(
85 _cs: CS,
86 ge: &TwistedEdwardsGadget<P, ConstraintF, FpGadget<ConstraintF>>,
87 ) -> Result<Self::OutputGadget, SynthesisError> {
88 Ok(ge.x.clone())
89 }
90}
91
92pub struct PedersenCRHCompressorGadget<G, I, ConstraintF, GG, IG>
93where
94 G: Group,
95 I: InjectiveMap<G>,
96 ConstraintF: Field,
97 GG: GroupGadget<G, ConstraintF>,
98 IG: InjectiveMapGadget<G, I, ConstraintF, GG>,
99{
100 _compressor: PhantomData<I>,
101 _compressor_gadget: PhantomData<IG>,
102 _crh: PedersenCRHGadget<G, ConstraintF, GG>,
103}
104
105impl<G, I, ConstraintF, GG, IG, W> FixedLengthCRHGadget<PedersenCRHCompressor<G, I, W>, ConstraintF>
106 for PedersenCRHCompressorGadget<G, I, ConstraintF, GG, IG>
107where
108 G: Group,
109 I: InjectiveMap<G>,
110 ConstraintF: Field,
111 GG: GroupGadget<G, ConstraintF>,
112 IG: InjectiveMapGadget<G, I, ConstraintF, GG>,
113 W: PedersenWindow,
114{
115 type OutputGadget = IG::OutputGadget;
116 type ParametersGadget = PedersenCRHGadgetParameters<G, W, ConstraintF, GG>;
117
118 fn check_evaluation_gadget<CS: ConstraintSystem<ConstraintF>>(
119 mut cs: CS,
120 parameters: &Self::ParametersGadget,
121 input: &[UInt8],
122 ) -> Result<Self::OutputGadget, SynthesisError> {
123 let result = PedersenCRHGadget::<G, ConstraintF, GG>::check_evaluation_gadget(
124 cs.ns(|| "PedCRH"),
125 parameters,
126 input,
127 )?;
128 IG::evaluate_map(cs.ns(|| "InjectiveMap"), &result)
129 }
130}