Browse Source

Adds alloc_constant to AllocGadget (#189)

* adds alloc constant to AllocGadget

* fmt

* fmt

* more alloc constant impls

* fmt

* even more impls

* fixes type

* fixes alloc_constant in gm17

* uses alloc_constant from field gadget in group gadgets

* fmt

* handle most comments
master
Kobi Gurkan 4 years ago
committed by GitHub
parent
commit
5cc094be6b
22 changed files with 468 additions and 13 deletions
  1. +26
    -0
      crypto-primitives/src/commitment/blake2s/constraints.rs
  2. +30
    -5
      crypto-primitives/src/commitment/pedersen/constraints.rs
  3. +15
    -4
      crypto-primitives/src/crh/bowe_hopwood/constraints.rs
  4. +15
    -4
      crypto-primitives/src/crh/pedersen/constraints.rs
  5. +22
    -0
      crypto-primitives/src/merkle_tree/constraints.rs
  6. +63
    -0
      crypto-primitives/src/nizk/gm17/constraints.rs
  7. +59
    -0
      crypto-primitives/src/nizk/groth16/constraints.rs
  8. +16
    -0
      crypto-primitives/src/prf/blake2s/constraints.rs
  9. +30
    -0
      crypto-primitives/src/signature/schnorr/constraints.rs
  10. +22
    -0
      r1cs-std/src/alloc.rs
  11. +20
    -0
      r1cs-std/src/bits/boolean.rs
  12. +10
    -0
      r1cs-std/src/bits/uint64.rs
  13. +10
    -0
      r1cs-std/src/bits/uint8.rs
  14. +12
    -0
      r1cs-std/src/fields/fp/mod.rs
  15. +11
    -0
      r1cs-std/src/fields/fp12.rs
  16. +11
    -0
      r1cs-std/src/fields/fp2.rs
  17. +11
    -0
      r1cs-std/src/fields/fp3.rs
  18. +11
    -0
      r1cs-std/src/fields/fp4.rs
  19. +11
    -0
      r1cs-std/src/fields/fp6_2over3.rs
  20. +11
    -0
      r1cs-std/src/fields/fp6_3over2.rs
  21. +18
    -0
      r1cs-std/src/groups/curves/short_weierstrass/mod.rs
  22. +34
    -0
      r1cs-std/src/groups/curves/twisted_edwards/mod.rs

+ 26
- 0
crypto-primitives/src/commitment/blake2s/constraints.rs

@ -48,6 +48,16 @@ impl CommitmentGadget
}
impl<ConstraintF: Field> AllocGadget<(), ConstraintF> for Blake2sParametersGadget {
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
cs: CS,
val: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<()>,
{
Self::alloc(cs, || Ok(val))
}
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(_: CS, _: F) -> Result<Self, SynthesisError>
where
F: FnOnce() -> Result<T, SynthesisError>,
@ -69,6 +79,22 @@ impl AllocGadget<(), ConstraintF> for Blake2sParametersGadge
}
impl<ConstraintF: PrimeField> AllocGadget<[u8; 32], ConstraintF> for Blake2sRandomnessGadget {
#[inline]
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
val: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<[u8; 32]>,
{
let mut bytes = vec![];
for (i, b) in val.borrow().iter().enumerate() {
bytes.push(UInt8::alloc_constant(cs.ns(|| format!("value {}", i)), b)?)
}
Ok(Blake2sRandomnessGadget(bytes))
}
#[inline]
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
cs: CS,

+ 30
- 5
crypto-primitives/src/commitment/pedersen/constraints.rs

@ -98,16 +98,14 @@ where
W: PedersenWindow,
ConstraintF: PrimeField,
{
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
_cs: CS,
value_gen: F,
val: T,
) -> Result<Self, SynthesisError>
where
F: FnOnce() -> Result<T, SynthesisError>,
T: Borrow<PedersenParameters<G>>,
{
let temp = value_gen()?;
let parameters = temp.borrow().clone();
let parameters = val.borrow().clone();
Ok(PedersenCommitmentGadgetParameters {
params: parameters,
@ -117,6 +115,18 @@ where
})
}
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
cs: CS,
value_gen: F,
) -> Result<Self, SynthesisError>
where
F: FnOnce() -> Result<T, SynthesisError>,
T: Borrow<PedersenParameters<G>>,
{
let temp = value_gen()?;
Self::alloc_constant(cs, temp)
}
fn alloc_input<F, T, CS: ConstraintSystem<ConstraintF>>(
_cs: CS,
value_gen: F,
@ -142,6 +152,21 @@ where
G: Group,
ConstraintF: PrimeField,
{
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
val: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<PedersenRandomness<G>>,
{
let mut result_bytes = vec![];
for (i, byte) in to_bytes![val.borrow().0].unwrap().into_iter().enumerate() {
let cur = UInt8::alloc_constant(cs.ns(|| format!("byte {}", i)), byte)?;
result_bytes.push(cur);
}
Ok(PedersenRandomnessGadget(result_bytes))
}
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
cs: CS,
value_gen: F,

+ 15
- 4
crypto-primitives/src/crh/bowe_hopwood/constraints.rs

@ -89,15 +89,14 @@ impl
AllocGadget<BoweHopwoodPedersenParameters<G>, ConstraintF>
for BoweHopwoodPedersenCRHGadgetParameters<G, W, ConstraintF, GG>
{
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
_cs: CS,
value_gen: F,
val: T,
) -> Result<Self, SynthesisError>
where
F: FnOnce() -> Result<T, SynthesisError>,
T: Borrow<BoweHopwoodPedersenParameters<G>>,
{
let params = value_gen()?.borrow().clone();
let params = val.borrow().clone();
Ok(BoweHopwoodPedersenCRHGadgetParameters {
params,
_group_g: PhantomData,
@ -106,6 +105,18 @@ impl
})
}
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
cs: CS,
value_gen: F,
) -> Result<Self, SynthesisError>
where
F: FnOnce() -> Result<T, SynthesisError>,
T: Borrow<BoweHopwoodPedersenParameters<G>>,
{
let params = value_gen()?.borrow().clone();
Self::alloc_constant(cs, params)
}
fn alloc_input<F, T, CS: ConstraintSystem<ConstraintF>>(
_cs: CS,
value_gen: F,

+ 15
- 4
crypto-primitives/src/crh/pedersen/constraints.rs

@ -80,15 +80,14 @@ impl
AllocGadget<PedersenParameters<G>, ConstraintF>
for PedersenCRHGadgetParameters<G, W, ConstraintF, GG>
{
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
_cs: CS,
value_gen: F,
val: T,
) -> Result<Self, SynthesisError>
where
F: FnOnce() -> Result<T, SynthesisError>,
T: Borrow<PedersenParameters<G>>,
{
let params = value_gen()?.borrow().clone();
let params = val.borrow().clone();
Ok(PedersenCRHGadgetParameters {
params,
_group_g: PhantomData,
@ -97,6 +96,18 @@ impl
})
}
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
cs: CS,
value_gen: F,
) -> Result<Self, SynthesisError>
where
F: FnOnce() -> Result<T, SynthesisError>,
T: Borrow<PedersenParameters<G>>,
{
let params = value_gen()?.borrow().clone();
Self::alloc_constant(cs, params)
}
fn alloc_input<F, T, CS: ConstraintSystem<ConstraintF>>(
_cs: CS,
value_gen: F,

+ 22
- 0
crypto-primitives/src/merkle_tree/constraints.rs

@ -128,6 +128,28 @@ where
HGadget: FixedLengthCRHGadget<P::H, ConstraintF>,
ConstraintF: Field,
{
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
val: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<MerkleTreePath<P>>,
{
let mut path = Vec::new();
for (i, &(ref l, ref r)) in val.borrow().path.iter().enumerate() {
let l_hash = HGadget::OutputGadget::alloc_constant(
&mut cs.ns(|| format!("l_child_{}", i)),
l.clone(),
)?;
let r_hash = HGadget::OutputGadget::alloc_constant(
&mut cs.ns(|| format!("r_child_{}", i)),
r.clone(),
)?;
path.push((l_hash, r_hash));
}
Ok(MerkleTreePathGadget { path })
}
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
value_gen: F,

+ 63
- 0
crypto-primitives/src/nizk/gm17/constraints.rs

@ -202,6 +202,54 @@ where
ConstraintF: Field,
P: PairingGadget<PairingE, ConstraintF>,
{
#[inline]
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
val: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<VerifyingKey<PairingE>>,
{
let VerifyingKey {
h_g2,
g_alpha_g1,
h_beta_g2,
g_gamma_g1,
h_gamma_g2,
query,
} = val.borrow().clone();
let h_g2 = P::G2Gadget::alloc_constant(cs.ns(|| "h_g2"), h_g2.into_projective())?;
let g_alpha_g1 =
P::G1Gadget::alloc_constant(cs.ns(|| "g_alpha"), g_alpha_g1.into_projective())?;
let h_beta_g2 =
P::G2Gadget::alloc_constant(cs.ns(|| "h_beta"), h_beta_g2.into_projective())?;
let g_gamma_g1 =
P::G1Gadget::alloc_constant(cs.ns(|| "g_gamma_g1"), g_gamma_g1.into_projective())?;
let h_gamma_g2 =
P::G2Gadget::alloc_constant(cs.ns(|| "h_gamma_g2"), h_gamma_g2.into_projective())?;
let query = query
.into_iter()
.enumerate()
.map(|(i, query_i)| {
P::G1Gadget::alloc_constant(
cs.ns(|| format!("query_{}", i)),
query_i.into_projective(),
)
})
.collect::<Vec<_>>()
.into_iter()
.collect::<Result<_, _>>()?;
Ok(Self {
h_g2,
g_alpha_g1,
h_beta_g2,
g_gamma_g1,
h_gamma_g2,
query,
})
}
#[inline]
fn alloc<FN, T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
@ -312,6 +360,21 @@ where
ConstraintF: Field,
P: PairingGadget<PairingE, ConstraintF>,
{
#[inline]
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
val: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<Proof<PairingE>>,
{
let Proof { a, b, c } = val.borrow().clone();
let a = P::G1Gadget::alloc_constant(cs.ns(|| "a"), a.into_projective())?;
let b = P::G2Gadget::alloc_constant(cs.ns(|| "b"), b.into_projective())?;
let c = P::G1Gadget::alloc_constant(cs.ns(|| "c"), c.into_projective())?;
Ok(Self { a, b, c })
}
#[inline]
fn alloc<FN, T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,

+ 59
- 0
crypto-primitives/src/nizk/groth16/constraints.rs

@ -173,6 +173,50 @@ where
ConstraintF: Field,
P: PairingGadget<PairingE, ConstraintF>,
{
#[inline]
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
val: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<VerifyingKey<PairingE>>,
{
let VerifyingKey {
alpha_g1,
beta_g2,
gamma_g2,
delta_g2,
gamma_abc_g1,
} = val.borrow().clone();
let alpha_g1 =
P::G1Gadget::alloc_constant(cs.ns(|| "alpha_g1"), alpha_g1.into_projective())?;
let beta_g2 = P::G2Gadget::alloc_constant(cs.ns(|| "beta_g2"), beta_g2.into_projective())?;
let gamma_g2 =
P::G2Gadget::alloc_constant(cs.ns(|| "gamma_g2"), gamma_g2.into_projective())?;
let delta_g2 =
P::G2Gadget::alloc_constant(cs.ns(|| "delta_g2"), delta_g2.into_projective())?;
let gamma_abc_g1 = gamma_abc_g1
.into_iter()
.enumerate()
.map(|(i, gamma_abc_i)| {
P::G1Gadget::alloc_constant(
cs.ns(|| format!("gamma_abc_{}", i)),
gamma_abc_i.into_projective(),
)
})
.collect::<Vec<_>>()
.into_iter()
.collect::<Result<_, _>>()?;
Ok(Self {
alpha_g1,
beta_g2,
gamma_g2,
delta_g2,
gamma_abc_g1,
})
}
#[inline]
fn alloc<FN, T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
@ -276,6 +320,21 @@ where
ConstraintF: Field,
P: PairingGadget<PairingE, ConstraintF>,
{
#[inline]
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
val: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<Proof<PairingE>>,
{
let Proof { a, b, c } = val.borrow().clone();
let a = P::G1Gadget::alloc_constant(cs.ns(|| "a"), a.into_projective())?;
let b = P::G2Gadget::alloc_constant(cs.ns(|| "b"), b.into_projective())?;
let c = P::G1Gadget::alloc_constant(cs.ns(|| "c"), c.into_projective())?;
Ok(Self { a, b, c })
}
#[inline]
fn alloc<FN, T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,

+ 16
- 0
crypto-primitives/src/prf/blake2s/constraints.rs

@ -450,6 +450,22 @@ impl ToBytesGadget for Blake2sOutputGadget
}
impl<ConstraintF: PrimeField> AllocGadget<[u8; 32], ConstraintF> for Blake2sOutputGadget {
#[inline]
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
val: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<[u8; 32]>,
{
let mut bytes = vec![];
for (i, b) in val.borrow().iter().enumerate() {
bytes.push(UInt8::alloc_constant(cs.ns(|| format!("value {}", i)), b)?)
}
Ok(Blake2sOutputGadget(bytes))
}
#[inline]
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
cs: CS,

+ 30
- 0
crypto-primitives/src/signature/schnorr/constraints.rs

@ -96,6 +96,21 @@ where
GG: GroupGadget<G, ConstraintF>,
D: Digest,
{
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
cs: CS,
val: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<SchnorrSigParameters<G, D>>,
{
let generator = GG::alloc_constant(cs, val.borrow().generator)?;
Ok(Self {
generator,
_engine: PhantomData,
_group: PhantomData,
})
}
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(cs: CS, f: F) -> Result<Self, SynthesisError>
where
F: FnOnce() -> Result<T, SynthesisError>,
@ -133,6 +148,21 @@ where
ConstraintF: Field,
GG: GroupGadget<G, ConstraintF>,
{
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
cs: CS,
val: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<SchnorrPublicKey<G>>,
{
let pub_key = GG::alloc_constant(cs, val.borrow())?;
Ok(Self {
pub_key,
_engine: PhantomData,
_group: PhantomData,
})
}
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(cs: CS, f: F) -> Result<Self, SynthesisError>
where
F: FnOnce() -> Result<T, SynthesisError>,

+ 22
- 0
r1cs-std/src/alloc.rs

@ -8,6 +8,13 @@ where
Self: Sized,
V: ?Sized,
{
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
cs: CS,
t: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<V>;
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(cs: CS, f: F) -> Result<Self, SynthesisError>
where
F: FnOnce() -> Result<T, SynthesisError>,
@ -47,6 +54,21 @@ where
impl<I, ConstraintF: Field, A: AllocGadget<I, ConstraintF>> AllocGadget<[I], ConstraintF>
for Vec<A>
{
#[inline]
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
t: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<[I]>,
{
let mut vec = Vec::new();
for (i, value) in t.borrow().iter().enumerate() {
vec.push(A::alloc_constant(cs.ns(|| format!("value_{}", i)), value)?);
}
Ok(vec)
}
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
f: F,

+ 20
- 0
r1cs-std/src/bits/boolean.rs

@ -235,6 +235,16 @@ impl PartialEq for AllocatedBit {
impl Eq for AllocatedBit {}
impl<ConstraintF: Field> AllocGadget<bool, ConstraintF> for AllocatedBit {
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
_cs: CS,
_t: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<bool>,
{
unimplemented!();
}
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
value_gen: F,
@ -715,6 +725,16 @@ impl From for Boolean {
}
impl<ConstraintF: Field> AllocGadget<bool, ConstraintF> for Boolean {
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
_cs: CS,
t: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<bool>,
{
Ok(Boolean::constant(*t.borrow()))
}
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
cs: CS,
value_gen: F,

+ 10
- 0
r1cs-std/src/bits/uint64.rs

@ -277,6 +277,16 @@ impl UInt64 {
}
impl<ConstraintF: Field> AllocGadget<u64, ConstraintF> for UInt64 {
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
_cs: CS,
t: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<u64>,
{
Ok(UInt64::constant(*t.borrow()))
}
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
value_gen: F,

+ 10
- 0
r1cs-std/src/bits/uint8.rs

@ -214,6 +214,16 @@ impl ConditionalEqGadget for UInt8 {
impl<ConstraintF: Field> EqGadget<ConstraintF> for UInt8 {}
impl<ConstraintF: Field> AllocGadget<u8, ConstraintF> for UInt8 {
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
_cs: CS,
t: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<u8>,
{
Ok(UInt8::constant(*t.borrow()))
}
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
value_gen: F,

+ 12
- 0
r1cs-std/src/fields/fp/mod.rs

@ -584,6 +584,18 @@ impl Clone for FpGadget {
}
impl<F: PrimeField> AllocGadget<F, F> for FpGadget<F> {
#[inline]
fn alloc_constant<T, CS: ConstraintSystem<F>>(_cs: CS, t: T) -> Result<Self, SynthesisError>
where
T: Borrow<F>,
{
let value = t.borrow().clone();
Ok(Self {
value: Some(value),
variable: LinearCombination::from((value, CS::one())).into(),
})
}
#[inline]
fn alloc<FN, T, CS: ConstraintSystem<F>>(
mut cs: CS,

+ 11
- 0
r1cs-std/src/fields/fp12.rs

@ -834,6 +834,17 @@ where
P: Fp12Parameters,
<P::Fp6Params as Fp6Parameters>::Fp2Params: Fp2Parameters<Fp = ConstraintF>,
{
#[inline]
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
t: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<Fp12<P>>,
{
Self::zero(cs.ns(|| "zero"))?.add_constant(cs.ns(|| "add constant"), t.borrow())
}
#[inline]
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,

+ 11
- 0
r1cs-std/src/fields/fp2.rs

@ -611,6 +611,17 @@ impl, ConstraintF: PrimeField>
impl<P: Fp2Parameters<Fp = ConstraintF>, ConstraintF: PrimeField> AllocGadget<Fp2<P>, ConstraintF>
for Fp2Gadget<P, ConstraintF>
{
#[inline]
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
t: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<Fp2<P>>,
{
Self::zero(cs.ns(|| "zero"))?.add_constant(cs.ns(|| "add constant"), t.borrow())
}
#[inline]
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,

+ 11
- 0
r1cs-std/src/fields/fp3.rs

@ -867,6 +867,17 @@ impl, ConstraintF: PrimeField + SquareRootFie
impl<P: Fp3Parameters<Fp = ConstraintF>, ConstraintF: PrimeField + SquareRootField>
AllocGadget<Fp3<P>, ConstraintF> for Fp3Gadget<P, ConstraintF>
{
#[inline]
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
t: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<Fp3<P>>,
{
Self::zero(cs.ns(|| "zero"))?.add_constant(cs.ns(|| "add constant"), t.borrow())
}
#[inline]
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,

+ 11
- 0
r1cs-std/src/fields/fp4.rs

@ -673,6 +673,17 @@ where
P: Fp4Parameters,
P::Fp2Params: Fp2Parameters<Fp = ConstraintF>,
{
#[inline]
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
t: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<Fp4<P>>,
{
Self::zero(cs.ns(|| "zero"))?.add_constant(cs.ns(|| "add constant"), t.borrow())
}
#[inline]
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,

+ 11
- 0
r1cs-std/src/fields/fp6_2over3.rs

@ -664,6 +664,17 @@ where
P: Fp6Parameters,
P::Fp3Params: Fp3Parameters<Fp = ConstraintF>,
{
#[inline]
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
t: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<Fp6<P>>,
{
Self::zero(cs.ns(|| "zero"))?.add_constant(cs.ns(|| "add constant"), t.borrow())
}
#[inline]
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,

+ 11
- 0
r1cs-std/src/fields/fp6_3over2.rs

@ -964,6 +964,17 @@ where
P: Fp6Parameters,
P::Fp2Params: Fp2Parameters<Fp = ConstraintF>,
{
#[inline]
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
t: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<Fp6<P>>,
{
Self::zero(cs.ns(|| "zero"))?.add_constant(cs.ns(|| "add constant"), t.borrow())
}
#[inline]
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,

+ 18
- 0
r1cs-std/src/groups/curves/short_weierstrass/mod.rs

@ -437,6 +437,24 @@ where
ConstraintF: PrimeField,
F: FieldGadget<P::BaseField, ConstraintF>,
{
#[inline]
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
t: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<SWProjective<P>>,
{
let p = t.borrow().into_affine();
Ok(Self {
x: F::alloc_constant(cs.ns(|| "x"), &p.x)?,
y: F::alloc_constant(cs.ns(|| "y"), &p.y)?,
infinity: Boolean::constant(p.infinity),
_params: PhantomData,
_engine: PhantomData,
})
}
#[inline]
fn alloc<FN, T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,

+ 34
- 0
r1cs-std/src/groups/curves/twisted_edwards/mod.rs

@ -492,6 +492,23 @@ mod affine_impl {
F: FieldGadget<P::BaseField, ConstraintF>,
Self: GroupGadget<TEAffine<P>, ConstraintF>,
{
#[inline]
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
t: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<TEAffine<P>>,
{
let p = t.borrow();
Ok(Self {
x: F::alloc_constant(cs.ns(|| "x"), &p.x)?,
y: F::alloc_constant(cs.ns(|| "y"), &p.y)?,
_params: PhantomData,
_engine: PhantomData,
})
}
fn alloc<FN, T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
value_gen: FN,
@ -1089,6 +1106,23 @@ mod projective_impl {
F: FieldGadget<P::BaseField, ConstraintF>,
Self: GroupGadget<TEProjective<P>, ConstraintF>,
{
#[inline]
fn alloc_constant<T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
t: T,
) -> Result<Self, SynthesisError>
where
T: Borrow<TEProjective<P>>,
{
let p = t.borrow().into_affine();
Ok(Self {
x: F::alloc_constant(cs.ns(|| "x"), &p.x)?,
y: F::alloc_constant(cs.ns(|| "y"), &p.y)?,
_params: PhantomData,
_engine: PhantomData,
})
}
fn alloc<FN, T, CS: ConstraintSystem<ConstraintF>>(
mut cs: CS,
value_gen: FN,

Loading…
Cancel
Save