Skip to content

Commit 492019c

Browse files
authored
Add the non-constraint part of the Constraints PR (#71)
1 parent 290bfe9 commit 492019c

File tree

5 files changed

+79
-5
lines changed

5 files changed

+79
-5
lines changed

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ digest = "0.9"
3939
rayon = { version = "1", optional = true }
4040
derivative = { version = "2", features = [ "use_core" ] }
4141

42+
tracing = { version = "0.1", default-features = false, features = [ "attributes" ] }
43+
4244
[dev-dependencies]
4345
rand = { version = "0.7", default-features = false }
4446
ark-ed-on-bls12-381 = { git = "https://github.com/arkworks-rs/curves", default-features = false }
@@ -60,7 +62,7 @@ debug = true
6062

6163
[features]
6264
default = [ "std", "parallel" ]
63-
std = [ "ark-ff/std", "ark-ec/std", "ark-poly/std", "ark-std/std", "ark-serialize/std" ]
65+
std = [ "ark-ff/std", "ark-ec/std", "ark-nonnative-field/std", "ark-poly/std", "ark-std/std", "ark-relations/std", "ark-serialize/std" ]
6466
r1cs = [ "ark-relations", "ark-r1cs-std", "ark-nonnative-field", "hashbrown" ]
6567
print-trace = [ "bench-utils/print-trace" ]
6668
parallel = [ "std", "ark-ff/parallel", "ark-ec/parallel", "ark-poly/parallel", "ark-std/parallel", "rayon" ]

src/constraints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl<TargetField: PrimeField, BaseField: PrimeField>
7373
}
7474
}
7575

76-
#[derive(Clone)]
76+
#[derive(Clone, Debug)]
7777
/// A collection of random data used in the polynomial commitment checking.
7878
pub struct PCCheckRandomDataVar<TargetField: PrimeField, BaseField: PrimeField> {
7979
/// Opening challenges.

src/data_structures.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{Polynomial, PolynomialCommitment, Rc, String, Vec};
2-
use ark_ff::Field;
2+
use ark_ff::{Field, ToConstraintField};
33
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, SerializationError};
44
use ark_std::{
55
borrow::Borrow,
@@ -192,6 +192,14 @@ pub struct LabeledCommitment<C: PCCommitment> {
192192
degree_bound: Option<usize>,
193193
}
194194

195+
impl<F: Field, C: PCCommitment + ToConstraintField<F>> ToConstraintField<F>
196+
for LabeledCommitment<C>
197+
{
198+
fn to_field_elements(&self) -> Option<Vec<F>> {
199+
self.commitment.to_field_elements()
200+
}
201+
}
202+
195203
impl<C: PCCommitment> LabeledCommitment<C> {
196204
/// Instantiate a new polynomial_context.
197205
pub fn new(label: PolynomialLabel, commitment: C, degree_bound: Option<usize>) -> Self {

src/kzg10/data_structures.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::*;
22
use ark_ec::{AffineCurve, PairingEngine, ProjectiveCurve};
3-
use ark_ff::{PrimeField, ToBytes, Zero};
3+
use ark_ff::{PrimeField, ToBytes, ToConstraintField, Zero};
44
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, SerializationError};
55
use ark_std::{
66
borrow::Cow,
@@ -297,6 +297,23 @@ impl<E: PairingEngine> ToBytes for VerifierKey<E> {
297297
}
298298
}
299299

300+
impl<E: PairingEngine> ToConstraintField<<E::Fq as Field>::BasePrimeField> for VerifierKey<E>
301+
where
302+
E::G1Affine: ToConstraintField<<E::Fq as Field>::BasePrimeField>,
303+
E::G2Affine: ToConstraintField<<E::Fq as Field>::BasePrimeField>,
304+
{
305+
fn to_field_elements(&self) -> Option<Vec<<E::Fq as Field>::BasePrimeField>> {
306+
let mut res = Vec::new();
307+
308+
res.extend_from_slice(&self.g.to_field_elements().unwrap());
309+
res.extend_from_slice(&self.gamma_g.to_field_elements().unwrap());
310+
res.extend_from_slice(&self.h.to_field_elements().unwrap());
311+
res.extend_from_slice(&self.beta_h.to_field_elements().unwrap());
312+
313+
Some(res)
314+
}
315+
}
316+
300317
/// `PreparedVerifierKey` is the fully prepared version for checking evaluation proofs for a given commitment.
301318
/// We omit gamma here for simplicity.
302319
#[derive(Derivative)]
@@ -368,6 +385,15 @@ impl<E: PairingEngine> PCCommitment for Commitment<E> {
368385
}
369386
}
370387

388+
impl<E: PairingEngine> ToConstraintField<<E::Fq as Field>::BasePrimeField> for Commitment<E>
389+
where
390+
E::G1Affine: ToConstraintField<<E::Fq as Field>::BasePrimeField>,
391+
{
392+
fn to_field_elements(&self) -> Option<Vec<<E::Fq as Field>::BasePrimeField>> {
393+
self.0.to_field_elements()
394+
}
395+
}
396+
371397
impl<'a, E: PairingEngine> AddAssign<(E::Fr, &'a Commitment<E>)> for Commitment<E> {
372398
#[inline]
373399
fn add_assign(&mut self, (f, other): (E::Fr, &'a Commitment<E>)) {

src/marlin/marlin_pc/data_structures.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
PCVerifierKey, UVPolynomial, Vec,
44
};
55
use ark_ec::{PairingEngine, ProjectiveCurve};
6-
use ark_ff::{PrimeField, ToBytes};
6+
use ark_ff::{Field, PrimeField, ToBytes, ToConstraintField};
77
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, SerializationError};
88
use ark_std::io::{Read, Write};
99
use ark_std::ops::{Add, AddAssign};
@@ -148,6 +148,28 @@ impl<E: PairingEngine> ToBytes for VerifierKey<E> {
148148
}
149149
}
150150

151+
impl<E: PairingEngine> ToConstraintField<<E::Fq as Field>::BasePrimeField> for VerifierKey<E>
152+
where
153+
E::G1Affine: ToConstraintField<<E::Fq as Field>::BasePrimeField>,
154+
E::G2Affine: ToConstraintField<<E::Fq as Field>::BasePrimeField>,
155+
{
156+
fn to_field_elements(&self) -> Option<Vec<<E::Fq as Field>::BasePrimeField>> {
157+
let mut res = Vec::new();
158+
res.extend_from_slice(&self.vk.to_field_elements().unwrap());
159+
160+
if let Some(degree_bounds_and_shift_powers) = &self.degree_bounds_and_shift_powers {
161+
for (d, shift_power) in degree_bounds_and_shift_powers.iter() {
162+
let d_elem: <E::Fq as Field>::BasePrimeField = (*d as u64).into();
163+
164+
res.push(d_elem);
165+
res.extend_from_slice(&shift_power.to_field_elements().unwrap());
166+
}
167+
}
168+
169+
Some(res)
170+
}
171+
}
172+
151173
/// `PreparedVerifierKey` is used to check evaluation proofs for a given commitment.
152174
#[derive(Derivative)]
153175
#[derivative(Clone(bound = ""), Debug(bound = ""))]
@@ -240,6 +262,22 @@ impl<E: PairingEngine> ToBytes for Commitment<E> {
240262
}
241263
}
242264

265+
impl<E: PairingEngine> ToConstraintField<<E::Fq as Field>::BasePrimeField> for Commitment<E>
266+
where
267+
E::G1Affine: ToConstraintField<<E::Fq as Field>::BasePrimeField>,
268+
{
269+
fn to_field_elements(&self) -> Option<Vec<<E::Fq as Field>::BasePrimeField>> {
270+
let mut res = Vec::new();
271+
res.extend_from_slice(&self.comm.to_field_elements().unwrap());
272+
273+
if let Some(shifted_comm) = &self.shifted_comm {
274+
res.extend_from_slice(&shifted_comm.to_field_elements().unwrap());
275+
}
276+
277+
Some(res)
278+
}
279+
}
280+
243281
impl<E: PairingEngine> PCCommitment for Commitment<E> {
244282
#[inline]
245283
fn empty() -> Self {

0 commit comments

Comments
 (0)