ark-r1cs-std/crypto-primitives/src/crh/injective_map/constraints.rs
crypto-primitives/src/crh/injective_map/constraints.rsRaw
1use std::{fmt::Debug, marker::PhantomData};
2
3use crate::crh::{
4 FixedLengthCRHGadget,
5 injective_map::{InjectiveMap, PedersenCRHCompressor, TECompressor},
6 pedersen::{
7 PedersenWindow,
8 constraints::{PedersenCRHGadget, PedersenCRHGadgetParameters},
9 }
10};
11
12use algebra::{
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<G: Group, I: InjectiveMap<G>, ConstraintF: Field, GG: GroupGadget<G, ConstraintF>> {
28 type OutputGadget: EqGadget<ConstraintF>
29 + ToBytesGadget<ConstraintF>
30 + CondSelectGadget<ConstraintF>
31 + AllocGadget<I::Output, ConstraintF>
32 + Debug
33 + Clone
34 + Sized;
35
36 fn evaluate_map<CS: ConstraintSystem<ConstraintF>>(
37 cs: CS,
38 ge: &GG,
39 ) -> Result<Self::OutputGadget, SynthesisError>;
40}
41
42pub struct TECompressorGadget;
43
44impl<ConstraintF, P> InjectiveMapGadget<TEAffine<P>, TECompressor, ConstraintF, TwistedEdwardsGadget<P, ConstraintF, FpGadget<ConstraintF>>>
45 for TECompressorGadget
46where
47 ConstraintF: PrimeField + SquareRootField,
48 P: TEModelParameters + ModelParameters<BaseField = ConstraintF>,
49{
50 type OutputGadget = FpGadget<ConstraintF>;
51
52 fn evaluate_map<CS: ConstraintSystem<ConstraintF>>(
53 _cs: CS,
54 ge: &TwistedEdwardsGadget<P, ConstraintF, FpGadget<ConstraintF>>,
55 ) -> Result<Self::OutputGadget, SynthesisError> {
56 Ok(ge.x.clone())
57 }
58}
59
60impl<ConstraintF, P>
61 InjectiveMapGadget<TEProjective<P>, TECompressor, ConstraintF, TwistedEdwardsGadget<P, ConstraintF, FpGadget<ConstraintF>>>
62 for TECompressorGadget
63where
64 ConstraintF: PrimeField + SquareRootField,
65 P: TEModelParameters + ModelParameters<BaseField = ConstraintF>,
66{
67 type OutputGadget = FpGadget<ConstraintF>;
68
69 fn evaluate_map<CS: ConstraintSystem<ConstraintF>>(
70 _cs: CS,
71 ge: &TwistedEdwardsGadget<P, ConstraintF, FpGadget<ConstraintF>>,
72 ) -> Result<Self::OutputGadget, SynthesisError> {
73 Ok(ge.x.clone())
74 }
75}
76
77pub struct PedersenCRHCompressorGadget<G, I, ConstraintF, GG, IG>
78where
79 G: Group,
80 I: InjectiveMap<G>,
81 ConstraintF: Field,
82 GG: GroupGadget<G, ConstraintF>,
83 IG: InjectiveMapGadget<G, I, ConstraintF, GG>,
84{
85 _compressor: PhantomData<I>,
86 _compressor_gadget: PhantomData<IG>,
87 _crh: PedersenCRHGadget<G, ConstraintF, GG>,
88}
89
90impl<G, I, ConstraintF, GG, IG, W> FixedLengthCRHGadget<PedersenCRHCompressor<G, I, W>, ConstraintF>
91 for PedersenCRHCompressorGadget<G, I, ConstraintF, GG, IG>
92where
93 G: Group,
94 I: InjectiveMap<G>,
95 ConstraintF: Field,
96 GG: GroupGadget<G, ConstraintF>,
97 IG: InjectiveMapGadget<G, I, ConstraintF, GG>,
98 W: PedersenWindow,
99{
100 type OutputGadget = IG::OutputGadget;
101 type ParametersGadget = PedersenCRHGadgetParameters<G, W, ConstraintF, GG>;
102
103 fn check_evaluation_gadget<CS: ConstraintSystem<ConstraintF>>(
104 mut cs: CS,
105 parameters: &Self::ParametersGadget,
106 input: &[UInt8],
107 ) -> Result<Self::OutputGadget, SynthesisError> {
108 let result = PedersenCRHGadget::<G, ConstraintF, GG>::check_evaluation_gadget(
109 cs.ns(|| "PedCRH"),
110 parameters,
111 input,
112 )?;
113 IG::evaluate_map(cs.ns(|| "InjectiveMap"), &result)
114 }
115}