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

decouple config creation from VerifyingKey::read #206

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 1 addition & 7 deletions halo2_proofs/examples/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,7 @@ fn main() {
let f = File::open("serialization-test.pk").unwrap();
let mut reader = BufReader::new(f);
#[allow(clippy::unit_arg)]
let pk = ProvingKey::<G1Affine>::read::<_, StandardPlonk>(
&mut reader,
SerdeFormat::RawBytes,
#[cfg(feature = "circuit-params")]
circuit.params(),
)
.unwrap();
let pk = ProvingKey::<G1Affine>::read::<_>(&mut reader, SerdeFormat::RawBytes).unwrap();

std::fs::remove_file("serialization-test.pk").unwrap();

Expand Down
51 changes: 8 additions & 43 deletions halo2_proofs/src/plonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,11 @@ 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>>(
reader: &mut R,
format: SerdeFormat,
#[cfg(feature = "circuit-params")] params: ConcreteCircuit::Params,
) -> io::Result<Self> {
pub fn read<R: io::Read>(reader: &mut R, format: SerdeFormat) -> io::Result<Self> {
let mut k = [0u8; 4];
reader.read_exact(&mut k)?;
let k = u32::from_be_bytes(k);
let (domain, cs, _) = keygen::create_domain::<C, ConcreteCircuit>(
k,
#[cfg(feature = "circuit-params")]
params,
);
let (domain, cs) = keygen::create_domain::<C>(k);
let mut num_fixed_columns = [0u8; 4];
reader.read_exact(&mut num_fixed_columns)?;
let num_fixed_columns = u32::from_be_bytes(num_fixed_columns);
Expand Down Expand Up @@ -153,17 +145,8 @@ where
}

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

Expand Down Expand Up @@ -349,17 +332,8 @@ 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>>(
reader: &mut R,
format: SerdeFormat,
#[cfg(feature = "circuit-params")] params: ConcreteCircuit::Params,
) -> io::Result<Self> {
let vk = VerifyingKey::<C>::read::<R, ConcreteCircuit>(
reader,
format,
#[cfg(feature = "circuit-params")]
params,
)?;
pub fn read<R: io::Read>(reader: &mut R, format: SerdeFormat) -> io::Result<Self> {
let vk = VerifyingKey::<C>::read::<R>(reader, format)?;
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 @@ -389,17 +363,8 @@ where
}

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

Expand Down
39 changes: 22 additions & 17 deletions halo2_proofs/src/plonk/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,33 @@ use crate::{
},
};

pub(crate) fn create_domain<C, ConcreteCircuit>(
k: u32,
#[cfg(feature = "circuit-params")] params: ConcreteCircuit::Params,
) -> (
EvaluationDomain<C::Scalar>,
ConstraintSystem<C::Scalar>,
ConcreteCircuit::Config,
)
pub(crate) fn create_domain<C>(k: u32) -> (EvaluationDomain<C::Scalar>, ConstraintSystem<C::Scalar>)
where
C: CurveAffine,
ConcreteCircuit: Circuit<C::Scalar>,
{
let mut cs = ConstraintSystem::default();
#[cfg(feature = "circuit-params")]
let config = ConcreteCircuit::configure_with_params(&mut cs, params);
#[cfg(not(feature = "circuit-params"))]
let config = ConcreteCircuit::configure(&mut cs);
let cs = ConstraintSystem::default();

let degree = cs.degree();

let domain = EvaluationDomain::new(degree as u32, k);

(domain, cs, config)
(domain, cs)
}

pub(crate) fn create_config<C, ConcreteCircuit>(
cs: &mut ConstraintSystem<C::Scalar>,
#[cfg(feature = "circuit-params")] params: ConcreteCircuit::Params,
) -> ConcreteCircuit::Config
where
C: CurveAffine,
ConcreteCircuit: Circuit<C::Scalar>,
{
#[cfg(feature = "circuit-params")]
let config = ConcreteCircuit::configure_with_params(cs, params);
#[cfg(not(feature = "circuit-params"))]
let config = ConcreteCircuit::configure(cs);

config
}

/// Assembly to be used in circuit synthesis.
Expand Down Expand Up @@ -213,8 +217,9 @@ where
ConcreteCircuit: Circuit<C::Scalar>,
C::Scalar: FromUniformBytes<64>,
{
let (domain, cs, config) = create_domain::<C, ConcreteCircuit>(
params.k(),
let (domain, mut cs) = create_domain::<C>(params.k());
let config = create_config::<C, ConcreteCircuit>(
&mut cs,
#[cfg(feature = "circuit-params")]
circuit.params(),
);
Expand Down
Loading