arnaucube
arnaucube/ark-r1cs-stdPublic Self-hosted repository
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{
 29    type OutputGadget: EqGadget<ConstraintF>
 30        + ToBytesGadget<ConstraintF>
 31        + CondSelectGadget<ConstraintF>
 32        + AllocGadget<I::Output, ConstraintF>
 33        + Debug
 34        + Clone
 35        + Sized;
 36
 37    fn evaluate_map<CS: ConstraintSystem<ConstraintF>>(
 38        cs: CS,
 39        ge: &GG,
 40    ) -> Result<Self::OutputGadget, SynthesisError>;
 41}
 42
 43pub struct TECompressorGadget;
 44
 45impl<ConstraintF, P> InjectiveMapGadget<TEAffine<P>, TECompressor, ConstraintF, TwistedEdwardsGadget<P, ConstraintF, FpGadget<ConstraintF>>>
 46    for TECompressorGadget
 47where
 48    ConstraintF: PrimeField + SquareRootField,
 49    P: TEModelParameters + ModelParameters<BaseField = ConstraintF>,
 50{
 51    type OutputGadget = FpGadget<ConstraintF>;
 52
 53    fn evaluate_map<CS: ConstraintSystem<ConstraintF>>(
 54        _cs: CS,
 55        ge: &TwistedEdwardsGadget<P, ConstraintF, FpGadget<ConstraintF>>,
 56    ) -> Result<Self::OutputGadget, SynthesisError> {
 57        Ok(ge.x.clone())
 58    }
 59}
 60
 61impl<ConstraintF, P>
 62    InjectiveMapGadget<TEProjective<P>, TECompressor, ConstraintF, TwistedEdwardsGadget<P, ConstraintF, FpGadget<ConstraintF>>>
 63    for TECompressorGadget
 64where
 65    ConstraintF: PrimeField + SquareRootField,
 66    P: TEModelParameters + ModelParameters<BaseField = ConstraintF>,
 67{
 68    type OutputGadget = FpGadget<ConstraintF>;
 69
 70    fn evaluate_map<CS: ConstraintSystem<ConstraintF>>(
 71        _cs: CS,
 72        ge: &TwistedEdwardsGadget<P, ConstraintF, FpGadget<ConstraintF>>,
 73    ) -> Result<Self::OutputGadget, SynthesisError> {
 74        Ok(ge.x.clone())
 75    }
 76}
 77
 78pub struct PedersenCRHCompressorGadget<
 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}