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

Remove dependency to common ConstraintSystem in the backend #290

Merged
merged 28 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7cd0be0
refactor: generalize ExpressionMid
ed255 Feb 7, 2024
9f3a2be
refactor: move ConstraintSystem::from(ConstraintSystemV2Backend) to b…
ed255 Feb 7, 2024
bde9732
refactor: generalize Expression type
ed255 Feb 16, 2024
831066a
feat: define ExpressionMid as an alias
ed255 Feb 27, 2024
f5b9fe9
refactor: rename ConstraintSystemV2Back to ConstraintSystemMid, GateV…
ed255 Feb 27, 2024
dfa0b88
refactor: use ConstraintSystemBack in halo2_backend
ed255 Feb 28, 2024
68c62e2
fix: warnings
ed255 Feb 28, 2024
89a949b
refactor: simplify Query type
ed255 Feb 28, 2024
eddbe95
feat(backend): rewrite pk/vk serialization
ed255 Mar 1, 2024
155a306
feat: remove feature circuit-params from halo2_backend
ed255 Mar 4, 2024
ba1fb49
wip: clean up common+backend
ed255 Mar 4, 2024
83476bc
wip: clean up frontend
ed255 Mar 5, 2024
4aa6bcf
wip: clean common plonk folder
ed255 Mar 5, 2024
2ee9ed2
refactor: move Error to frontend
ed255 Mar 6, 2024
6acc953
refactor: move Error to backend
ed255 Mar 6, 2024
0a5d1ce
refactor: clean up error types
ed255 Mar 6, 2024
c91915a
Merge branch 'main' into feature/fe-ba-expression
ed255 Mar 7, 2024
3c9090d
fix: use errors instead of temporary panics
ed255 Mar 7, 2024
c6bc658
feat: annotate columns in test
ed255 Mar 7, 2024
aad5dab
fix: remove unnecessary pub, set correct SelectorsToFixed.compressed …
ed255 Mar 7, 2024
1850fa5
fix: ponk_api unit tests
ed255 Mar 7, 2024
1603d42
fix: clippy warnings, common dependencies
ed255 Mar 7, 2024
5b4457f
Merge branch 'main' into feature/fe-ba-expression
ed255 Mar 8, 2024
7b44b19
fix: remove old TODO
ed255 Mar 12, 2024
fd78247
chore: bump VK version
ed255 Mar 13, 2024
e9f8c29
chore: remove unnecessary code
ed255 Mar 13, 2024
f3dc804
chore: remove virtual variable rules in middleware Expression
ed255 Mar 13, 2024
15fb627
chore: remove unused evaluate_lazy, deprecate directly_convert_select…
ed255 Mar 14, 2024
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ members = [
"halo2_middleware",
"halo2_backend",
"halo2_common",
]
]
resolver = "2"
1 change: 0 additions & 1 deletion halo2_backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ bits = ["halo2curves/bits"]
gadget-traces = ["backtrace"]
sanity-checks = []
batch = ["rand_core/getrandom"]
circuit-params = []
cost-estimator = ["serde", "serde_derive"]
derive_serde = ["halo2curves/derive_serde"]

Expand Down
2 changes: 1 addition & 1 deletion halo2_backend/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub(crate) use halo2_common::helpers::{SerdeFormat, SerdePrimeField};
use halo2_middleware::ff::PrimeField;
use std::io;

pub(crate) use halo2_common::helpers::{pack, unpack, CurveRead, SerdeCurveAffine};
pub(crate) use halo2_common::helpers::{CurveRead, SerdeCurveAffine};

/// Reads a vector of polynomials from buffer
pub(crate) fn read_polynomial_vec<R: io::Read, F: SerdePrimeField, B>(
Expand Down
1 change: 0 additions & 1 deletion halo2_backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ pub mod poly;
pub mod transcript;

// Internal re-exports
pub use halo2_common::circuit;
pub use halo2_common::multicore;
pub use halo2_common::SerdeFormat;
131 changes: 27 additions & 104 deletions halo2_backend/src/plonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ use group::ff::{Field, FromUniformBytes, PrimeField};

use crate::arithmetic::CurveAffine;
use crate::helpers::{
self, polynomial_slice_byte_length, read_polynomial_vec, write_polynomial_slice,
SerdeCurveAffine, SerdePrimeField,
polynomial_slice_byte_length, read_polynomial_vec, write_polynomial_slice, SerdeCurveAffine,
SerdePrimeField,
};
use crate::plonk::circuit::{ConstraintSystemBack, PinnedConstraintSystem};
use crate::poly::{
Coeff, EvaluationDomain, ExtendedLagrangeCoeff, LagrangeCoeff, PinnedEvaluationDomain,
Polynomial,
};
use crate::transcript::{ChallengeScalar, EncodedChallenge, Transcript};
pub(crate) use evaluation::Evaluator;
use halo2_common::plonk::{Circuit, ConstraintSystem, PinnedConstraintSystem};
use halo2_common::SerdeFormat;

use std::io;

pub(crate) use halo2_common::plonk::Error;

mod circuit;
mod error;
mod evaluation;
pub mod keygen;
mod lookup;
Expand All @@ -28,6 +28,8 @@ mod shuffle;
mod vanishing;
pub mod verifier;

pub use error::*;

/// This is a verifying key which allows for the verification of proofs for a
/// particular circuit.
#[derive(Clone, Debug)]
Expand All @@ -39,16 +41,14 @@ pub struct VerifyingKey<C: CurveAffine> {
/// Permutation verifying key
permutation: permutation::VerifyingKey<C>,
/// Constraint system
cs: ConstraintSystem<C::Scalar>,
cs: ConstraintSystemBack<C::Scalar>,
/// Cached maximum degree of `cs` (which doesn't change after construction).
cs_degree: usize,
/// The representative of this `VerifyingKey` in transcripts.
transcript_repr: C::Scalar,
/// Selectors
selectors: Vec<Vec<bool>>,
// TODO: Use setter/getter https://github.com/privacy-scaling-explorations/halo2/issues/259
/// Whether selector compression is turned on or not.
pub compress_selectors: bool,
/// Legacy field that indicates wether the circuit was compiled with compressed selectors or
/// not using the legacy API.
pub compress_selectors: Option<bool>,
}

// Current version of the VK
Expand All @@ -74,23 +74,12 @@ where
assert!(*k <= C::Scalar::S);
// k value fits in 1 byte
writer.write_all(&[*k as u8])?;
writer.write_all(&[self.compress_selectors as u8])?;
ed255 marked this conversation as resolved.
Show resolved Hide resolved
writer.write_all(&(self.fixed_commitments.len() as u32).to_le_bytes())?;
for commitment in &self.fixed_commitments {
commitment.write(writer, format)?;
}
self.permutation.write(writer, format)?;

if !self.compress_selectors {
assert!(self.selectors.is_empty());
}
// write self.selectors
for selector in &self.selectors {
// since `selector` is filled with `bool`, we pack them 8 at a time into bytes and then write
for bits in selector.chunks(8) {
writer.write_all(&[helpers::pack(bits)])?;
}
}
Ok(())
}

Expand All @@ -104,10 +93,10 @@ where
/// Checks that field elements are less than modulus, and then checks that the point is on the curve.
/// - `RawBytesUnchecked`: Reads an uncompressed curve element with coordinates in Montgomery form;
/// does not perform any checks
pub fn read<R: io::Read, ConcreteCircuit: Circuit<C::Scalar>>(
pub fn read<R: io::Read>(
reader: &mut R,
format: SerdeFormat,
#[cfg(feature = "circuit-params")] params: ConcreteCircuit::Params,
cs: ConstraintSystemBack<C::Scalar>,
) -> io::Result<Self> {
let mut version_byte = [0u8; 1];
reader.read_exact(&mut version_byte)?;
Expand All @@ -131,20 +120,7 @@ where
),
));
}
let mut compress_selectors = [0u8; 1];
reader.read_exact(&mut compress_selectors)?;
if compress_selectors[0] != 0 && compress_selectors[0] != 1 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"unexpected compress_selectors not boolean",
));
}
let compress_selectors = compress_selectors[0] == 1;
let (domain, cs, _) = keygen::create_domain::<C, ConcreteCircuit>(
k as u32,
#[cfg(feature = "circuit-params")]
params,
);
let domain = keygen::create_domain::<C>(&cs, k as u32);
let mut num_fixed_columns = [0u8; 4];
reader.read_exact(&mut num_fixed_columns)?;
let num_fixed_columns = u32::from_le_bytes(num_fixed_columns);
Expand All @@ -155,36 +131,7 @@ where

let permutation = permutation::VerifyingKey::read(reader, &cs.permutation, format)?;

let (cs, selectors) = if compress_selectors {
// read selectors
let selectors: Vec<Vec<bool>> = vec![vec![false; 1 << k]; cs.num_selectors]
.into_iter()
.map(|mut selector| {
let mut selector_bytes = vec![0u8; (selector.len() + 7) / 8];
reader.read_exact(&mut selector_bytes)?;
for (bits, byte) in selector.chunks_mut(8).zip(selector_bytes) {
helpers::unpack(byte, bits);
}
Ok(selector)
})
.collect::<io::Result<_>>()?;
let (cs, _) = cs.compress_selectors(selectors.clone());
(cs, selectors)
} else {
// we still need to replace selectors with fixed Expressions in `cs`
let fake_selectors = vec![vec![]; cs.num_selectors];
let (cs, _) = cs.directly_convert_selectors_to_fixed(fake_selectors);
(cs, vec![])
};

Ok(Self::from_parts(
domain,
fixed_commitments,
permutation,
cs,
selectors,
compress_selectors,
))
Ok(Self::from_parts(domain, fixed_commitments, permutation, cs))
}

/// Writes a verifying key to a vector of bytes using [`Self::write`].
Expand All @@ -195,17 +142,12 @@ where
}

/// Reads a verification key from a slice of bytes using [`Self::read`].
pub fn from_bytes<ConcreteCircuit: Circuit<C::Scalar>>(
pub fn from_bytes(
mut bytes: &[u8],
format: SerdeFormat,
#[cfg(feature = "circuit-params")] params: ConcreteCircuit::Params,
cs: ConstraintSystemBack<C::Scalar>,
) -> io::Result<Self> {
Self::read::<_, ConcreteCircuit>(
&mut bytes,
format,
#[cfg(feature = "circuit-params")]
params,
)
Self::read(&mut bytes, format, cs)
}
}

Expand All @@ -216,21 +158,13 @@ impl<C: CurveAffine> VerifyingKey<C> {
{
10 + (self.fixed_commitments.len() * C::byte_length(format))
+ self.permutation.bytes_length(format)
+ self.selectors.len()
* (self
.selectors
.get(0)
.map(|selector| (selector.len() + 7) / 8)
.unwrap_or(0))
}

fn from_parts(
domain: EvaluationDomain<C::Scalar>,
fixed_commitments: Vec<C>,
permutation: permutation::VerifyingKey<C>,
cs: ConstraintSystem<C::Scalar>,
selectors: Vec<Vec<bool>>,
compress_selectors: bool,
cs: ConstraintSystemBack<C::Scalar>,
) -> Self
where
C::ScalarExt: FromUniformBytes<64>,
Expand All @@ -246,8 +180,7 @@ impl<C: CurveAffine> VerifyingKey<C> {
cs_degree,
// Temporary, this is not pinned.
transcript_repr: C::Scalar::ZERO,
selectors,
compress_selectors,
compress_selectors: None,
};

let mut hasher = Blake2bParams::new()
Expand Down Expand Up @@ -300,7 +233,7 @@ impl<C: CurveAffine> VerifyingKey<C> {
}

/// Returns `ConstraintSystem`
pub fn cs(&self) -> &ConstraintSystem<C::Scalar> {
pub fn cs(&self) -> &ConstraintSystemBack<C::Scalar> {
&self.cs
}

Expand Down Expand Up @@ -400,17 +333,12 @@ where
/// Checks that field elements are less than modulus, and then checks that the point is on the curve.
/// - `RawBytesUnchecked`: Reads an uncompressed curve element with coordinates in Montgomery form;
/// does not perform any checks
pub fn read<R: io::Read, ConcreteCircuit: Circuit<C::Scalar>>(
pub fn read<R: io::Read>(
reader: &mut R,
format: SerdeFormat,
#[cfg(feature = "circuit-params")] params: ConcreteCircuit::Params,
cs: ConstraintSystemBack<C::Scalar>,
) -> io::Result<Self> {
let vk = VerifyingKey::<C>::read::<R, ConcreteCircuit>(
reader,
format,
#[cfg(feature = "circuit-params")]
params,
)?;
let vk = VerifyingKey::<C>::read::<R>(reader, format, cs)?;
let l0 = Polynomial::read(reader, format)?;
let l_last = Polynomial::read(reader, format)?;
let l_active_row = Polynomial::read(reader, format)?;
Expand Down Expand Up @@ -440,17 +368,12 @@ where
}

/// Reads a proving key from a slice of bytes using [`Self::read`].
pub fn from_bytes<ConcreteCircuit: Circuit<C::Scalar>>(
pub fn from_bytes(
mut bytes: &[u8],
format: SerdeFormat,
#[cfg(feature = "circuit-params")] params: ConcreteCircuit::Params,
cs: ConstraintSystemBack<C::Scalar>,
) -> io::Result<Self> {
Self::read::<_, ConcreteCircuit>(
&mut bytes,
format,
#[cfg(feature = "circuit-params")]
params,
)
Self::read(&mut bytes, format, cs)
}
}

Expand Down
Loading
Loading