Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Diff Tracking SP1 patches #665

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pcs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ jf-utils = { git = "https://github.com/EspressoSystems/jellyfish", tag = "0.4.5"
merlin = { workspace = true }
rayon = { version = "1.5.0", optional = true }

[target.'cfg(all(target_os = "zkvm", target_vendor = "succinct"))'.dependencies]
sp1-zkvm = { git = "https://github.com/succinctlabs/sp1.git", tag = "v1.1.0" }
ark-bn254 = { workspace = true }

[dev-dependencies]
ark-bls12-381 = { workspace = true }
ark-bn254 = { workspace = true }
Expand Down
12 changes: 12 additions & 0 deletions pcs/src/univariate_kzg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ use ark_std::{
vec::Vec,
One, UniformRand, Zero,
};
use core::any::TypeId;
use jf_utils::par_utils::parallelizable_slice_iter;
#[cfg(feature = "parallel")]
use rayon::prelude::*;
use srs::{UnivariateProverParam, UnivariateUniversalParams, UnivariateVerifierParam};

pub(crate) mod srs;
#[cfg(all(target_os = "zkvm", target_vendor = "succinct"))]
mod succinct;

/// KZG Polynomial Commitment Scheme on univariate polynomial.
pub struct UnivariateKzgPCS<E> {
Expand Down Expand Up @@ -104,6 +107,15 @@ impl<E: Pairing> PolynomialCommitmentScheme for UnivariateKzgPCS<E> {
)));
}

#[cfg(all(target_os = "zkvm", target_vendor = "succinct"))]
if TypeId::of::<E>() == TypeId::of::<ark_bn254::Bn254>() {
let points: &[ark_bn254::G1Affine] =
unsafe { ark_std::mem::transmute(&prover_param.powers_of_g[..]) };
let scalars: &[ark_bn254::Fr] = unsafe { ark_std::mem::transmute(poly.coeffs()) };
let commitment =
unsafe { ark_std::mem::transmute_copy(&succinct::msm(points, scalars)) };
return Ok(Commitment(commitment));
}
let (num_leading_zeros, plain_coeffs) = skip_leading_zeros_and_convert_to_bigints(poly);

#[cfg(feature = "kzg-print-trace")]
Expand Down
67 changes: 67 additions & 0 deletions pcs/src/univariate_kzg/succinct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use ark_ec::pairing::Pairing;
use ark_ff::{BigInt, BigInteger, PrimeField, Zero};

#[derive(Clone, Copy, Debug)]
pub struct BnAffinePoint(pub BigInt<4>, pub BigInt<4>);

pub fn wrap_g1affine(p: &G1Affine) -> BnAffinePoint {
BnAffinePoint(p.x.into_bigint(), p.y.into_bigint())
}

pub fn unwrap_g1affine(p: &BnAffinePoint) -> G1Affine {
G1Affine {
x: p.0.into(),
y: p.1.into(),
infinity: false,
}
}

pub fn msm(p: &[G1Affine], s: &[ScalarField]) -> G1Affine {
let mut iter = p.iter().zip(s).filter(|(_, s)| !s.is_zero());
let mut result = {
let (p, s) = iter.next().unwrap();
let mut p = wrap_g1affine(p);
bn254_double_and_add(&mut p, s);
Comment on lines +19 to +24
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may i ask why we didn't use AffinePoint struct and the ::msm() directly from sp1-lib as suggested in EspressoSystems/zkrollup-integration#21 (comment)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw this after I had these working implementations. I don't think it's necessary to follow their APIs.

p
};
iter.for_each(|(p, s)| {
let mut p = wrap_g1affine(p);
bn254_double_and_add(&mut p, s);
bn254_add(&mut result, &p);
});
unwrap_g1affine(&result)
}

pub fn bn254_add(p: &mut BnAffinePoint, q: &BnAffinePoint) {
sp1_zkvm::syscalls::syscall_bn254_add(
p as *mut _ as *mut [u32; 16],
q as *const _ as *const [u32; 16],
);
}

pub fn bn254_double_and_add(p: &mut BnAffinePoint, s: &ScalarField) {
let mut t = *p;
let mut b = s.into_bigint().to_bits_le().into_iter();
let mut q = {
b.by_ref().take_while(|b| !b).for_each(|_| {
sp1_zkvm::syscalls::syscall_bn254_double(&mut t as *mut _ as *mut [u32; 16]);
});
t
};
b.for_each(|b| {
// double in place
sp1_zkvm::syscalls::syscall_bn254_double(&mut t as *mut _ as *mut [u32; 16]);
if b {
sp1_zkvm::syscalls::syscall_bn254_add(
&mut q as *mut _ as *mut [u32; 16],
&t as *const _ as *const [u32; 16],
);
}
});

*p = q
}

pub type E = ark_bn254::Bn254;
pub type G1Affine = <E as Pairing>::G1Affine;
pub type ScalarField = <E as Pairing>::ScalarField;
7 changes: 4 additions & 3 deletions vid/src/advz/payload_prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ pub struct SmallRangeProof<P> {
/// A proof intended for use on large payload subslices.
///
/// Metadata needed to recover a KZG commitment.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(bound = "F: CanonicalSerialize + CanonicalDeserialize")]
pub struct LargeRangeProof<F> {
#[derive(
Clone, Debug, Eq, PartialEq, CanonicalSerialize, CanonicalDeserialize, Serialize, Deserialize,
)]
pub struct LargeRangeProof<F: CanonicalSerialize + CanonicalDeserialize> {
#[serde(with = "canonical")]
prefix_elems: Vec<F>,
#[serde(with = "canonical")]
Expand Down
Loading