Browse Source

Add ToConstraintField impls for some primitives

master
Pratyush Mishra 5 years ago
parent
commit
cb2ff3b412
7 changed files with 40 additions and 11 deletions
  1. +4
    -2
      crypto-primitives/src/commitment/injective_map/constraints.rs
  2. +11
    -1
      crypto-primitives/src/commitment/pedersen/mod.rs
  3. +4
    -4
      crypto-primitives/src/crh/injective_map/constraints.rs
  4. +1
    -2
      crypto-primitives/src/crh/pedersen/constraints.rs
  5. +9
    -0
      crypto-primitives/src/crh/pedersen/mod.rs
  6. +1
    -2
      crypto-primitives/src/mht/constraints.rs
  7. +10
    -0
      crypto-primitives/src/signature/schnorr/mod.rs

+ 4
- 2
crypto-primitives/src/commitment/injective_map/constraints.rs

@ -16,13 +16,15 @@ use r1cs_std::{groups::GroupGadget, uint8::UInt8};
use std::marker::PhantomData; use std::marker::PhantomData;
pub struct PedersenCommitmentCompressorGadget<
pub struct PedersenCommitmentCompressorGadget<G, I, ConstraintF, GG, IG>
where
G: Group, G: Group,
I: InjectiveMap<G>, I: InjectiveMap<G>,
ConstraintF: Field, ConstraintF: Field,
GG: GroupGadget<G, ConstraintF>, GG: GroupGadget<G, ConstraintF>,
IG: InjectiveMapGadget<G, I, ConstraintF, GG>, IG: InjectiveMapGadget<G, I, ConstraintF, GG>,
> {
{
_compressor: PhantomData<I>, _compressor: PhantomData<I>,
_compressor_gadget: PhantomData<IG>, _compressor_gadget: PhantomData<IG>,
_crh: PedersenCommitmentGadget<G, ConstraintF, GG>, _crh: PedersenCommitmentGadget<G, ConstraintF, GG>,

+ 11
- 1
crypto-primitives/src/commitment/pedersen/mod.rs

@ -1,10 +1,12 @@
use crate::Error; use crate::Error;
use algebra::UniformRand; use algebra::UniformRand;
use algebra::{Field, ToConstraintField};
use algebra::{bytes::ToBytes, groups::Group, BitIterator, FpParameters, PrimeField};
use rand::Rng; use rand::Rng;
use std::marker::PhantomData; use std::marker::PhantomData;
use super::CommitmentScheme; use super::CommitmentScheme;
use algebra::{bytes::ToBytes, groups::Group, BitIterator, FpParameters, PrimeField};
use std::io::{Result as IoResult, Write}; use std::io::{Result as IoResult, Write};
pub use crate::crh::pedersen::PedersenWindow; pub use crate::crh::pedersen::PedersenWindow;
@ -121,3 +123,11 @@ impl CommitmentScheme for PedersenCommitment
Ok(result) Ok(result)
} }
} }
impl<ConstraintF: Field, G: Group + ToConstraintField<ConstraintF>> ToConstraintField<ConstraintF> for PedersenParameters<G> {
#[inline]
fn to_field_elements(&self) -> Result<Vec<ConstraintF>, Error> {
Ok(Vec::new())
}
}

+ 4
- 4
crypto-primitives/src/crh/injective_map/constraints.rs

@ -24,8 +24,7 @@ use r1cs_std::{
prelude::*, prelude::*,
}; };
pub trait InjectiveMapGadget<G: Group, I: InjectiveMap<G>, ConstraintF: Field, GG: GroupGadget<G, ConstraintF>>
{
pub trait InjectiveMapGadget<G: Group, I: InjectiveMap<G>, ConstraintF: Field, GG: GroupGadget<G, ConstraintF>> {
type OutputGadget: EqGadget<ConstraintF> type OutputGadget: EqGadget<ConstraintF>
+ ToBytesGadget<ConstraintF> + ToBytesGadget<ConstraintF>
+ CondSelectGadget<ConstraintF> + CondSelectGadget<ConstraintF>
@ -75,13 +74,14 @@ where
} }
} }
pub struct PedersenCRHCompressorGadget<
pub struct PedersenCRHCompressorGadget<G, I, ConstraintF, GG, IG>
where
G: Group, G: Group,
I: InjectiveMap<G>, I: InjectiveMap<G>,
ConstraintF: Field, ConstraintF: Field,
GG: GroupGadget<G, ConstraintF>, GG: GroupGadget<G, ConstraintF>,
IG: InjectiveMapGadget<G, I, ConstraintF, GG>, IG: InjectiveMapGadget<G, I, ConstraintF, GG>,
> {
{
_compressor: PhantomData<I>, _compressor: PhantomData<I>,
_compressor_gadget: PhantomData<IG>, _compressor_gadget: PhantomData<IG>,
_crh: PedersenCRHGadget<G, ConstraintF, GG>, _crh: PedersenCRHGadget<G, ConstraintF, GG>,

+ 1
- 2
crypto-primitives/src/crh/pedersen/constraints.rs

@ -1,10 +1,9 @@
use algebra::Field;
use crate::crh::{ use crate::crh::{
FixedLengthCRHGadget, FixedLengthCRHGadget,
pedersen::{PedersenCRH, PedersenParameters, PedersenWindow}, pedersen::{PedersenCRH, PedersenParameters, PedersenWindow},
}; };
use algebra::groups::Group;
use algebra::{Field, Group};
use r1cs_core::{ConstraintSystem, SynthesisError}; use r1cs_core::{ConstraintSystem, SynthesisError};
use r1cs_std::prelude::*; use r1cs_std::prelude::*;

+ 9
- 0
crypto-primitives/src/crh/pedersen/mod.rs

@ -7,6 +7,7 @@ use std::{
}; };
use crate::crh::FixedLengthCRH; use crate::crh::FixedLengthCRH;
use algebra::{Field, ToConstraintField};
use algebra::groups::Group; use algebra::groups::Group;
@ -139,3 +140,11 @@ impl Debug for PedersenParameters {
write!(f, "}}\n") write!(f, "}}\n")
} }
} }
impl<ConstraintF: Field, G: Group + ToConstraintField<ConstraintF>> ToConstraintField<ConstraintF> for PedersenParameters<G> {
#[inline]
fn to_field_elements(&self) -> Result<Vec<ConstraintF>, Error> {
Ok(Vec::new())
}
}

+ 1
- 2
crypto-primitives/src/mht/constraints.rs

@ -28,13 +28,12 @@ where
_f: PhantomData<ConstraintF>, _f: PhantomData<ConstraintF>,
} }
impl<P, CRHGadget, ConstraintF> MerklePathVerifierGadget<P, CRHGadget, ConstraintF>
impl<P, CRHGadget, ConstraintF> MerklePathVerifierGadget<P, CRHGadget, ConstraintF>
where where
P: MHTParameters, P: MHTParameters,
ConstraintF: Field, ConstraintF: Field,
CRHGadget: FixedLengthCRHGadget<P::H, ConstraintF>, CRHGadget: FixedLengthCRHGadget<P::H, ConstraintF>,
{ {
pub fn check_membership<CS: ConstraintSystem<ConstraintF>>( pub fn check_membership<CS: ConstraintSystem<ConstraintF>>(
cs: CS, cs: CS,
parameters: &CRHGadget::ParametersGadget, parameters: &CRHGadget::ParametersGadget,

+ 10
- 0
crypto-primitives/src/signature/schnorr/mod.rs

@ -1,5 +1,6 @@
use crate::SignatureScheme; use crate::SignatureScheme;
use algebra::{ use algebra::{
ToConstraintField,
bytes::ToBytes, bytes::ToBytes,
fields::{Field, PrimeField}, fields::{Field, PrimeField},
groups::Group, groups::Group,
@ -221,3 +222,12 @@ pub fn bytes_to_bits(bytes: &[u8]) -> Vec {
} }
bits bits
} }
impl<ConstraintF: Field, G: Group + ToConstraintField<ConstraintF>, D: Digest> ToConstraintField<ConstraintF>
for SchnorrSigParameters<G, D>
{
#[inline]
fn to_field_elements(&self) -> Result<Vec<ConstraintF>, Error> {
self.generator.to_field_elements()
}
}

Loading…
Cancel
Save