mirror of
https://github.com/arnaucube/ark-r1cs-std.git
synced 2026-01-10 16:01:28 +01:00
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
This commit is contained in:
@@ -48,6 +48,16 @@ impl<ConstraintF: PrimeField> CommitmentGadget<Blake2sCommitment, ConstraintF>
|
||||
}
|
||||
|
||||
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<ConstraintF: Field> 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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -89,8 +89,24 @@ impl<G: Group, W: PedersenWindow, ConstraintF: Field, GG: GroupGadget<G, Constra
|
||||
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,
|
||||
val: T,
|
||||
) -> Result<Self, SynthesisError>
|
||||
where
|
||||
T: Borrow<BoweHopwoodPedersenParameters<G>>,
|
||||
{
|
||||
let params = val.borrow().clone();
|
||||
Ok(BoweHopwoodPedersenCRHGadgetParameters {
|
||||
params,
|
||||
_group_g: PhantomData,
|
||||
_engine: PhantomData,
|
||||
_window: PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
|
||||
cs: CS,
|
||||
value_gen: F,
|
||||
) -> Result<Self, SynthesisError>
|
||||
where
|
||||
@@ -98,12 +114,7 @@ impl<G: Group, W: PedersenWindow, ConstraintF: Field, GG: GroupGadget<G, Constra
|
||||
T: Borrow<BoweHopwoodPedersenParameters<G>>,
|
||||
{
|
||||
let params = value_gen()?.borrow().clone();
|
||||
Ok(BoweHopwoodPedersenCRHGadgetParameters {
|
||||
params,
|
||||
_group_g: PhantomData,
|
||||
_engine: PhantomData,
|
||||
_window: PhantomData,
|
||||
})
|
||||
Self::alloc_constant(cs, params)
|
||||
}
|
||||
|
||||
fn alloc_input<F, T, CS: ConstraintSystem<ConstraintF>>(
|
||||
|
||||
@@ -80,8 +80,24 @@ impl<G: Group, W: PedersenWindow, ConstraintF: Field, GG: GroupGadget<G, Constra
|
||||
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,
|
||||
val: T,
|
||||
) -> Result<Self, SynthesisError>
|
||||
where
|
||||
T: Borrow<PedersenParameters<G>>,
|
||||
{
|
||||
let params = val.borrow().clone();
|
||||
Ok(PedersenCRHGadgetParameters {
|
||||
params,
|
||||
_group_g: PhantomData,
|
||||
_engine: PhantomData,
|
||||
_window: PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
fn alloc<F, T, CS: ConstraintSystem<ConstraintF>>(
|
||||
cs: CS,
|
||||
value_gen: F,
|
||||
) -> Result<Self, SynthesisError>
|
||||
where
|
||||
@@ -89,12 +105,7 @@ impl<G: Group, W: PedersenWindow, ConstraintF: Field, GG: GroupGadget<G, Constra
|
||||
T: Borrow<PedersenParameters<G>>,
|
||||
{
|
||||
let params = value_gen()?.borrow().clone();
|
||||
Ok(PedersenCRHGadgetParameters {
|
||||
params,
|
||||
_group_g: PhantomData,
|
||||
_engine: PhantomData,
|
||||
_window: PhantomData,
|
||||
})
|
||||
Self::alloc_constant(cs, params)
|
||||
}
|
||||
|
||||
fn alloc_input<F, T, CS: ConstraintSystem<ConstraintF>>(
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -450,6 +450,22 @@ impl<ConstraintF: PrimeField> ToBytesGadget<ConstraintF> 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,
|
||||
|
||||
@@ -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>,
|
||||
|
||||
Reference in New Issue
Block a user