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

Add deep_clone to BaseCircuitBuilder #131

Merged
merged 2 commits into from
Sep 1, 2023
Merged
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
40 changes: 30 additions & 10 deletions halo2-base/src/gates/circuit/builder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::{Arc, Mutex};

use getset::{Getters, MutGetters, Setters};
use itertools::Itertools;

Expand All @@ -17,7 +19,8 @@ use crate::{
},
utils::ScalarField,
virtual_region::{
copy_constraints::SharedCopyConstraintManager, lookups::LookupAnyManager,
copy_constraints::{CopyConstraintManager, SharedCopyConstraintManager},
lookups::LookupAnyManager,
manager::VirtualRegionManager,
},
AssignedValue, Context,
Expand Down Expand Up @@ -95,6 +98,32 @@ impl<F: ScalarField> BaseCircuitBuilder<F> {
Self::new(true).use_params(config_params).use_break_points(break_points)
}

/// Sets the copy manager to the given one in all shared references.
pub fn set_copy_manager(&mut self, copy_manager: SharedCopyConstraintManager<F>) {
for lm in &mut self.lookup_manager {
lm.copy_manager = copy_manager.clone();
}
self.core.set_copy_manager(copy_manager);
}

/// Returns `self` with a given copy manager
pub fn use_copy_manager(mut self, copy_manager: SharedCopyConstraintManager<F>) -> Self {
self.set_copy_manager(copy_manager);
self
}

/// Deep clone of `self`, where the underlying object of shared references in [SharedCopyConstraintManager] and [LookupAnyManager] are cloned.
pub fn deep_clone(&self) -> Self {
let cm: CopyConstraintManager<F> = self.core.copy_manager.lock().unwrap().clone();
let cm_ref = Arc::new(Mutex::new(cm));
let mut clone = self.clone().use_copy_manager(cm_ref);
for lm in &mut clone.lookup_manager {
let ctl_clone = lm.cells_to_lookup.lock().unwrap().clone();
lm.cells_to_lookup = Arc::new(Mutex::new(ctl_clone));
}
clone
}

/// The log_2 size of the lookup table, if using.
pub fn lookup_bits(&self) -> Option<usize> {
self.config_params.lookup_bits
Expand Down Expand Up @@ -166,15 +195,6 @@ impl<F: ScalarField> BaseCircuitBuilder<F> {
self
}

/// Returns `self` with a gven copy manager
pub fn use_copy_manager(mut self, copy_manager: SharedCopyConstraintManager<F>) -> Self {
for lm in &mut self.lookup_manager {
lm.copy_manager = copy_manager.clone();
}
self.core = self.core.use_copy_manager(copy_manager);
self
}

/// Returns if the circuit is only used for witness generation.
pub fn witness_gen_only(&self) -> bool {
self.core.witness_gen_only()
Expand Down
8 changes: 8 additions & 0 deletions halo2-base/src/gates/circuit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ impl<F: ScalarField> BaseConfig<F> {
MaybeRangeConfig::WithRange(config) => &config.q_lookup,
}
}

/// Updates the number of usable rows in the circuit. Used if you mutate [ConstraintSystem] after `BaseConfig::configure` is called.
pub fn set_usable_rows(&mut self, usable_rows: usize) {
match &mut self.base {
MaybeRangeConfig::WithoutRange(config) => config.max_rows = usable_rows,
MaybeRangeConfig::WithRange(config) => config.gate.max_rows = usable_rows,
}
}
}

impl<F: ScalarField> Circuit<F> for BaseCircuitBuilder<F> {
Expand Down
11 changes: 8 additions & 3 deletions halo2-base/src/gates/flex_gate/threads/multi_phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ impl<F: ScalarField> MultiPhaseCoreManager<F> {
Self::new(stage.witness_gen_only()).unknown(stage == CircuitBuilderStage::Keygen)
}

/// Returns `self` with a given copy manager
pub fn use_copy_manager(mut self, copy_manager: SharedCopyConstraintManager<F>) -> Self {
/// Mutates `self` to use the given copy manager in all phases and all threads.
pub fn set_copy_manager(&mut self, copy_manager: SharedCopyConstraintManager<F>) {
for pm in &mut self.phase_manager {
pm.copy_manager = copy_manager.clone();
pm.set_copy_manager(copy_manager.clone());
}
self.copy_manager = copy_manager;
}

/// Returns `self` with a given copy manager
pub fn use_copy_manager(mut self, copy_manager: SharedCopyConstraintManager<F>) -> Self {
self.set_copy_manager(copy_manager);
self
}

Expand Down
14 changes: 14 additions & 0 deletions halo2-base/src/gates/flex_gate/threads/single_phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ impl<F: ScalarField> SinglePhaseCoreManager<F> {
Self { use_unknown, ..self }
}

/// Mutates `self` to use the given copy manager everywhere, including in all threads.
pub fn set_copy_manager(&mut self, copy_manager: SharedCopyConstraintManager<F>) {
self.copy_manager = copy_manager.clone();
for ctx in &mut self.threads {
ctx.copy_manager = copy_manager.clone();
}
}

/// Returns `self` with a given copy manager
pub fn use_copy_manager(mut self, copy_manager: SharedCopyConstraintManager<F>) -> Self {
self.set_copy_manager(copy_manager);
self
}

/// Returns a mutable reference to the [Context] of a gate thread. Spawns a new thread for the given phase, if none exists.
pub fn main(&mut self) -> &mut Context<F> {
if self.threads.is_empty() {
Expand Down
Loading