From 1650a0b07fbbfeebab71f5650ccb62ccd20de0ff Mon Sep 17 00:00:00 2001 From: duguorong009 <80258679+duguorong009@users.noreply.github.com> Date: Wed, 28 Feb 2024 10:52:08 +0800 Subject: [PATCH] refactor: move some types from common to backend (post-split)(plonk-0) (#287) * refactor: move "Challenge*" from "halo2_common" to "halo2_backend" * refactor: move the "transcript" from "halo2_common" to "halo2_backend" * chore: correct the imports --- halo2_backend/src/lib.rs | 2 +- halo2_backend/src/plonk.rs | 22 ++++++++++++++++++- halo2_backend/src/plonk/lookup/prover.rs | 5 ++--- halo2_backend/src/plonk/lookup/verifier.rs | 6 ++--- halo2_backend/src/plonk/permutation/prover.rs | 4 ++-- .../src/plonk/permutation/verifier.rs | 4 ++-- halo2_backend/src/plonk/prover.rs | 11 +++++----- halo2_backend/src/plonk/shuffle/prover.rs | 3 ++- halo2_backend/src/plonk/shuffle/verifier.rs | 4 ++-- halo2_backend/src/plonk/vanishing/prover.rs | 3 ++- halo2_backend/src/plonk/vanishing/verifier.rs | 4 ++-- halo2_backend/src/plonk/verifier.rs | 4 +--- .../src/transcript.rs | 0 halo2_common/src/lib.rs | 1 - halo2_common/src/plonk.rs | 21 ------------------ halo2_proofs/src/lib.rs | 2 +- halo2_proofs/src/plonk/prover.rs | 2 +- halo2_proofs/tests/frontend_backend_split.rs | 16 ++++++++------ 18 files changed, 56 insertions(+), 58 deletions(-) rename {halo2_common => halo2_backend}/src/transcript.rs (100%) diff --git a/halo2_backend/src/lib.rs b/halo2_backend/src/lib.rs index 6a60c32919..7dbf971d03 100644 --- a/halo2_backend/src/lib.rs +++ b/halo2_backend/src/lib.rs @@ -2,9 +2,9 @@ pub mod arithmetic; mod helpers; pub mod plonk; pub mod poly; +pub mod transcript; // Internal re-exports pub use halo2_common::circuit; pub use halo2_common::multicore; -pub use halo2_common::transcript; pub use halo2_common::SerdeFormat; diff --git a/halo2_backend/src/plonk.rs b/halo2_backend/src/plonk.rs index a88c8763a3..27f2d2f9ed 100644 --- a/halo2_backend/src/plonk.rs +++ b/halo2_backend/src/plonk.rs @@ -10,9 +10,9 @@ use crate::poly::{ Coeff, EvaluationDomain, ExtendedLagrangeCoeff, LagrangeCoeff, PinnedEvaluationDomain, Polynomial, }; +use crate::transcript::{ChallengeScalar, EncodedChallenge, Transcript}; use evaluation::Evaluator; use halo2_common::plonk::{Circuit, ConstraintSystem, PinnedConstraintSystem}; -use halo2_common::transcript::{EncodedChallenge, Transcript}; use halo2_common::SerdeFormat; use std::io; @@ -455,3 +455,23 @@ impl VerifyingKey { &self.domain } } + +#[derive(Clone, Copy, Debug)] +pub struct Theta; +pub type ChallengeTheta = ChallengeScalar; + +#[derive(Clone, Copy, Debug)] +pub struct Beta; +pub type ChallengeBeta = ChallengeScalar; + +#[derive(Clone, Copy, Debug)] +pub struct Gamma; +pub type ChallengeGamma = ChallengeScalar; + +#[derive(Clone, Copy, Debug)] +pub struct Y; +pub type ChallengeY = ChallengeScalar; + +#[derive(Clone, Copy, Debug)] +pub struct X; +pub type ChallengeX = ChallengeScalar; diff --git a/halo2_backend/src/plonk/lookup/prover.rs b/halo2_backend/src/plonk/lookup/prover.rs index f95054608f..00967ccea1 100644 --- a/halo2_backend/src/plonk/lookup/prover.rs +++ b/halo2_backend/src/plonk/lookup/prover.rs @@ -3,6 +3,7 @@ use super::Argument; use crate::plonk::evaluation::evaluate; use crate::{ arithmetic::{eval_polynomial, parallelize, CurveAffine}, + plonk::{ChallengeBeta, ChallengeGamma, ChallengeTheta, ChallengeX}, poly::{ commitment::{Blind, Params}, Coeff, EvaluationDomain, LagrangeCoeff, Polynomial, ProverQuery, @@ -13,9 +14,7 @@ use group::{ ff::{BatchInvert, Field}, Curve, }; -use halo2_common::plonk::{ - ChallengeBeta, ChallengeGamma, ChallengeTheta, ChallengeX, Error, Expression, -}; +use halo2_common::plonk::{Error, Expression}; use halo2_middleware::ff::WithSmallOrderMulGroup; use halo2_middleware::poly::Rotation; use rand_core::RngCore; diff --git a/halo2_backend/src/plonk/lookup/verifier.rs b/halo2_backend/src/plonk/lookup/verifier.rs index dda0104200..1688afdd19 100644 --- a/halo2_backend/src/plonk/lookup/verifier.rs +++ b/halo2_backend/src/plonk/lookup/verifier.rs @@ -3,13 +3,11 @@ use std::iter; use super::Argument; use crate::{ arithmetic::CurveAffine, - plonk::VerifyingKey, + plonk::{ChallengeBeta, ChallengeGamma, ChallengeTheta, ChallengeX, VerifyingKey}, poly::{commitment::MSM, VerifierQuery}, transcript::{EncodedChallenge, TranscriptRead}, }; -use halo2_common::plonk::{ - ChallengeBeta, ChallengeGamma, ChallengeTheta, ChallengeX, Error, Expression, -}; +use halo2_common::plonk::{Error, Expression}; use halo2_middleware::ff::Field; use halo2_middleware::poly::Rotation; diff --git a/halo2_backend/src/plonk/permutation/prover.rs b/halo2_backend/src/plonk/permutation/prover.rs index b4ccc1e35a..fda5a21156 100644 --- a/halo2_backend/src/plonk/permutation/prover.rs +++ b/halo2_backend/src/plonk/permutation/prover.rs @@ -9,14 +9,14 @@ use std::iter::{self, ExactSizeIterator}; use super::Argument; use crate::{ arithmetic::{eval_polynomial, parallelize, CurveAffine}, - plonk::{self, permutation::ProvingKey}, + plonk::{self, permutation::ProvingKey, ChallengeBeta, ChallengeGamma, ChallengeX}, poly::{ commitment::{Blind, Params}, Coeff, ExtendedLagrangeCoeff, LagrangeCoeff, Polynomial, ProverQuery, }, transcript::{EncodedChallenge, TranscriptWrite}, }; -use halo2_common::plonk::{ChallengeBeta, ChallengeGamma, ChallengeX, Error}; +use halo2_common::plonk::Error; use halo2_middleware::circuit::Any; use halo2_middleware::poly::Rotation; diff --git a/halo2_backend/src/plonk/permutation/verifier.rs b/halo2_backend/src/plonk/permutation/verifier.rs index 21646f7b02..5ad874ec68 100644 --- a/halo2_backend/src/plonk/permutation/verifier.rs +++ b/halo2_backend/src/plonk/permutation/verifier.rs @@ -4,11 +4,11 @@ use std::iter; use super::{Argument, VerifyingKey}; use crate::{ arithmetic::CurveAffine, - plonk::{self}, + plonk::{self, ChallengeBeta, ChallengeGamma, ChallengeX}, poly::{commitment::MSM, VerifierQuery}, transcript::{EncodedChallenge, TranscriptRead}, }; -use halo2_common::plonk::{ChallengeBeta, ChallengeGamma, ChallengeX, Error}; +use halo2_common::plonk::Error; use halo2_middleware::circuit::Any; use halo2_middleware::poly::Rotation; diff --git a/halo2_backend/src/plonk/prover.rs b/halo2_backend/src/plonk/prover.rs index 82ad9cdc47..8d5c848b27 100644 --- a/halo2_backend/src/plonk/prover.rs +++ b/halo2_backend/src/plonk/prover.rs @@ -8,17 +8,18 @@ use crate::arithmetic::{eval_polynomial, CurveAffine}; use crate::plonk::lookup::prover::lookup_commit_permuted; use crate::plonk::permutation::prover::permutation_commit; use crate::plonk::shuffle::prover::shuffle_commit_product; -use crate::plonk::{lookup, permutation, shuffle, vanishing, ProvingKey}; +use crate::plonk::{ + lookup, permutation, shuffle, vanishing, ChallengeBeta, ChallengeGamma, ChallengeTheta, + ChallengeX, ChallengeY, ProvingKey, +}; use crate::poly::{ commitment::{Blind, CommitmentScheme, Params, Prover}, Basis, Coeff, LagrangeCoeff, Polynomial, ProverQuery, }; +use crate::transcript::{EncodedChallenge, TranscriptWrite}; use group::prime::PrimeCurveAffine; -use halo2_common::plonk::{ - circuit::sealed, ChallengeBeta, ChallengeGamma, ChallengeTheta, ChallengeX, ChallengeY, Error, -}; -use halo2_common::transcript::{EncodedChallenge, TranscriptWrite}; +use halo2_common::plonk::{circuit::sealed, Error}; /// Collection of instance data used during proving for a single circuit proof. #[derive(Debug)] diff --git a/halo2_backend/src/plonk/shuffle/prover.rs b/halo2_backend/src/plonk/shuffle/prover.rs index 31dc9714bb..4e32367153 100644 --- a/halo2_backend/src/plonk/shuffle/prover.rs +++ b/halo2_backend/src/plonk/shuffle/prover.rs @@ -3,6 +3,7 @@ use super::Argument; use crate::plonk::evaluation::evaluate; use crate::{ arithmetic::{eval_polynomial, parallelize, CurveAffine}, + plonk::{ChallengeGamma, ChallengeTheta, ChallengeX}, poly::{ commitment::{Blind, Params}, Coeff, EvaluationDomain, LagrangeCoeff, Polynomial, ProverQuery, @@ -10,7 +11,7 @@ use crate::{ transcript::{EncodedChallenge, TranscriptWrite}, }; use group::{ff::BatchInvert, Curve}; -use halo2_common::plonk::{ChallengeGamma, ChallengeTheta, ChallengeX, Error, Expression}; +use halo2_common::plonk::{Error, Expression}; use halo2_middleware::ff::WithSmallOrderMulGroup; use halo2_middleware::poly::Rotation; use rand_core::RngCore; diff --git a/halo2_backend/src/plonk/shuffle/verifier.rs b/halo2_backend/src/plonk/shuffle/verifier.rs index 96b374e751..20b8ec0532 100644 --- a/halo2_backend/src/plonk/shuffle/verifier.rs +++ b/halo2_backend/src/plonk/shuffle/verifier.rs @@ -3,11 +3,11 @@ use std::iter; use super::Argument; use crate::{ arithmetic::CurveAffine, - plonk::VerifyingKey, + plonk::{ChallengeGamma, ChallengeTheta, ChallengeX, VerifyingKey}, poly::{commitment::MSM, VerifierQuery}, transcript::{EncodedChallenge, TranscriptRead}, }; -use halo2_common::plonk::{ChallengeGamma, ChallengeTheta, ChallengeX, Error, Expression}; +use halo2_common::plonk::{Error, Expression}; use halo2_middleware::ff::Field; use halo2_middleware::poly::Rotation; diff --git a/halo2_backend/src/plonk/vanishing/prover.rs b/halo2_backend/src/plonk/vanishing/prover.rs index 942441aefa..0300f22c4e 100644 --- a/halo2_backend/src/plonk/vanishing/prover.rs +++ b/halo2_backend/src/plonk/vanishing/prover.rs @@ -1,7 +1,7 @@ use std::{collections::HashMap, iter}; use group::Curve; -use halo2_common::plonk::{ChallengeX, Error}; +use halo2_common::plonk::Error; use halo2_middleware::ff::Field; use rand_chacha::ChaCha20Rng; use rand_core::{RngCore, SeedableRng}; @@ -10,6 +10,7 @@ use super::Argument; use crate::{ arithmetic::{eval_polynomial, parallelize, CurveAffine}, multicore::current_num_threads, + plonk::ChallengeX, poly::{ commitment::{Blind, ParamsProver}, Coeff, EvaluationDomain, ExtendedLagrangeCoeff, Polynomial, ProverQuery, diff --git a/halo2_backend/src/plonk/vanishing/verifier.rs b/halo2_backend/src/plonk/vanishing/verifier.rs index bdf4a45bc5..2e7394f7ca 100644 --- a/halo2_backend/src/plonk/vanishing/verifier.rs +++ b/halo2_backend/src/plonk/vanishing/verifier.rs @@ -1,11 +1,11 @@ use std::iter; -use halo2_common::plonk::{ChallengeX, ChallengeY, Error}; +use halo2_common::plonk::Error; use halo2_middleware::ff::Field; use crate::{ arithmetic::CurveAffine, - plonk::VerifyingKey, + plonk::{ChallengeX, ChallengeY, VerifyingKey}, poly::{ commitment::{Params, MSM}, VerifierQuery, diff --git a/halo2_backend/src/plonk/verifier.rs b/halo2_backend/src/plonk/verifier.rs index c8e81f43e2..02ebbf5650 100644 --- a/halo2_backend/src/plonk/verifier.rs +++ b/halo2_backend/src/plonk/verifier.rs @@ -1,7 +1,4 @@ use group::Curve; -use halo2_common::plonk::{ - ChallengeBeta, ChallengeGamma, ChallengeTheta, ChallengeX, ChallengeY, Error, -}; use halo2_middleware::ff::{Field, FromUniformBytes, WithSmallOrderMulGroup}; use std::iter; @@ -10,6 +7,7 @@ use crate::arithmetic::compute_inner_product; use crate::plonk::lookup::verifier::lookup_read_permuted_commitments; use crate::plonk::permutation::verifier::permutation_read_product_commitments; use crate::plonk::shuffle::verifier::shuffle_read_product_commitment; +use crate::plonk::{ChallengeBeta, ChallengeGamma, ChallengeTheta, ChallengeX, ChallengeY, Error}; use crate::poly::commitment::{CommitmentScheme, Verifier}; use crate::poly::VerificationStrategy; use crate::poly::{ diff --git a/halo2_common/src/transcript.rs b/halo2_backend/src/transcript.rs similarity index 100% rename from halo2_common/src/transcript.rs rename to halo2_backend/src/transcript.rs diff --git a/halo2_common/src/lib.rs b/halo2_common/src/lib.rs index 6e0a149bef..26cef30838 100644 --- a/halo2_common/src/lib.rs +++ b/halo2_common/src/lib.rs @@ -10,7 +10,6 @@ pub mod circuit; pub use halo2curves; pub mod multicore; pub mod plonk; -pub mod transcript; pub mod helpers; pub use helpers::SerdeFormat; diff --git a/halo2_common/src/plonk.rs b/halo2_common/src/plonk.rs index 01f9177203..ecabbfea3a 100644 --- a/halo2_common/src/plonk.rs +++ b/halo2_common/src/plonk.rs @@ -5,7 +5,6 @@ //! [halo]: https://eprint.iacr.org/2019/1021 //! [plonk]: https://eprint.iacr.org/2019/953 -use crate::transcript::ChallengeScalar; use halo2_middleware::circuit::{Advice, Fixed, Instance}; use halo2_middleware::ff::Field; use halo2_middleware::poly::Rotation; @@ -443,26 +442,6 @@ impl Queries { } } -#[derive(Clone, Copy, Debug)] -pub struct Theta; -pub type ChallengeTheta = ChallengeScalar; - -#[derive(Clone, Copy, Debug)] -pub struct Beta; -pub type ChallengeBeta = ChallengeScalar; - -#[derive(Clone, Copy, Debug)] -pub struct Gamma; -pub type ChallengeGamma = ChallengeScalar; - -#[derive(Clone, Copy, Debug)] -pub struct Y; -pub type ChallengeY = ChallengeScalar; - -#[derive(Clone, Copy, Debug)] -pub struct X; -pub type ChallengeX = ChallengeScalar; - #[cfg(test)] mod tests { use halo2curves::pasta::Fp; diff --git a/halo2_proofs/src/lib.rs b/halo2_proofs/src/lib.rs index 8a8b8bbb13..72e41d7ccc 100644 --- a/halo2_proofs/src/lib.rs +++ b/halo2_proofs/src/lib.rs @@ -46,7 +46,7 @@ pub mod poly { /// This module contains utilities and traits for dealing with Fiat-Shamir /// transcripts. pub mod transcript { - pub use halo2_common::transcript::{ + pub use halo2_backend::transcript::{ Blake2bRead, Blake2bWrite, Challenge255, EncodedChallenge, TranscriptReadBuffer, TranscriptWriterBuffer, }; diff --git a/halo2_proofs/src/plonk/prover.rs b/halo2_proofs/src/plonk/prover.rs index 4a22afc18c..31cc450760 100644 --- a/halo2_proofs/src/plonk/prover.rs +++ b/halo2_proofs/src/plonk/prover.rs @@ -1,7 +1,7 @@ use crate::poly::commitment::{CommitmentScheme, Params, Prover}; use halo2_backend::plonk::{prover::ProverV2, ProvingKey}; +use halo2_backend::transcript::{EncodedChallenge, TranscriptWrite}; use halo2_common::plonk::{circuit::Circuit, Error}; -use halo2_common::transcript::{EncodedChallenge, TranscriptWrite}; use halo2_frontend::circuit::{compile_circuit, WitnessCalculator}; use halo2_middleware::ff::{FromUniformBytes, WithSmallOrderMulGroup}; use rand_core::RngCore; diff --git a/halo2_proofs/tests/frontend_backend_split.rs b/halo2_proofs/tests/frontend_backend_split.rs index 4b7621b9ea..f67e2201ca 100644 --- a/halo2_proofs/tests/frontend_backend_split.rs +++ b/halo2_proofs/tests/frontend_backend_split.rs @@ -5,10 +5,15 @@ #[global_allocator] static ALLOC: dhat::Alloc = dhat::Alloc; -use halo2_backend::plonk::{ - keygen::{keygen_pk_v2, keygen_vk_v2}, - prover::ProverV2Single, - verifier::{verify_proof, verify_proof_single}, +use halo2_backend::{ + plonk::{ + keygen::{keygen_pk_v2, keygen_vk_v2}, + prover::ProverV2Single, + verifier::{verify_proof, verify_proof_single}, + }, + transcript::{ + Blake2bRead, Blake2bWrite, Challenge255, TranscriptReadBuffer, TranscriptWriterBuffer, + }, }; use halo2_common::{ circuit::{AssignedCell, Layouter, Region, SimpleFloorPlanner, Value}, @@ -16,9 +21,6 @@ use halo2_common::{ circuit::{Challenge, Column}, Circuit, ConstraintSystem, Error, Expression, FirstPhase, SecondPhase, Selector, }, - transcript::{ - Blake2bRead, Blake2bWrite, Challenge255, TranscriptReadBuffer, TranscriptWriterBuffer, - }, }; use halo2_frontend::{ circuit::{compile_circuit, WitnessCalculator},