-
Notifications
You must be signed in to change notification settings - Fork 22
refactor: add pk_generation circuit #1268
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c908f45
refactor: add pk generation circuit
cedoor 7c23390
refactor: add pk1is to pk generation circuit
cedoor b4b8570
chore: add missing license header
cedoor 7979f1e
fix: fix import error
cedoor 5f996c8
fix: set correct number of ciphertexts
cedoor 5d71152
fix: fix import error
cedoor cee8930
refactor: remove unused config file
cedoor 38ffbde
fix(zk-helpers): restore limb order before CRT in pk_generation compu…
cedoor 793ee23
refactor: update secure threshold pk generation configs
cedoor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
crates/zk-helpers/src/circuits/threshold/pk_generation/circuit.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
| // | ||
| // This file is provided WITHOUT ANY WARRANTY; | ||
| // without even the implied warranty of MERCHANTABILITY | ||
| // or FITNESS FOR A PARTICULAR PURPOSE. | ||
|
|
||
| use crate::computation::DkgInputType; | ||
| use crate::registry::Circuit; | ||
| use crate::CiphernodesCommittee; | ||
| use e3_fhe_params::ParameterType; | ||
| use e3_polynomial::CrtPolynomial; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct PkGenerationCircuit; | ||
|
|
||
| impl Circuit for PkGenerationCircuit { | ||
| const NAME: &'static str = "pk-generation"; | ||
| const PREFIX: &'static str = "PK_GENERATION"; | ||
| const SUPPORTED_PARAMETER: ParameterType = ParameterType::THRESHOLD; | ||
| const DKG_INPUT_TYPE: Option<DkgInputType> = None; | ||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct PkGenerationCircuitInput { | ||
| pub committee: CiphernodesCommittee, | ||
| pub pk_share: CrtPolynomial, | ||
| pub a: CrtPolynomial, | ||
| pub eek: CrtPolynomial, | ||
| pub e_sm: CrtPolynomial, | ||
| pub sk: CrtPolynomial, | ||
| } |
195 changes: 195 additions & 0 deletions
195
crates/zk-helpers/src/circuits/threshold/pk_generation/codegen.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
| // | ||
| // This file is provided WITHOUT ANY WARRANTY; | ||
| // without even the implied warranty of MERCHANTABILITY | ||
| // or FITNESS FOR A PARTICULAR PURPOSE. | ||
|
|
||
| //! Code generation for the public-key BFV circuit: Prover.toml and configs.nr. | ||
|
|
||
| use e3_fhe_params::BfvPreset; | ||
|
|
||
| use crate::circuits::computation::Computation; | ||
| use crate::threshold::pk_generation::circuit::PkGenerationCircuit; | ||
| use crate::threshold::pk_generation::computation::{Configs, Witness}; | ||
| use crate::threshold::pk_generation::PkGenerationCircuitInput; | ||
| use crate::utils::join_display; | ||
| use crate::CircuitCodegen; | ||
| use crate::CircuitsErrors; | ||
| use crate::{Artifacts, CodegenToml}; | ||
| use crate::{Circuit, CodegenConfigs}; | ||
|
|
||
| /// Implementation of [`CircuitCodegen`] for [`PkGenerationCircuit`]. | ||
| impl CircuitCodegen for PkGenerationCircuit { | ||
| type Preset = BfvPreset; | ||
| type Input = PkGenerationCircuitInput; | ||
| type Error = CircuitsErrors; | ||
|
|
||
| fn codegen(&self, preset: Self::Preset, input: &Self::Input) -> Result<Artifacts, Self::Error> { | ||
| let witness = Witness::compute(preset, input)?; | ||
| let configs = Configs::compute(preset, &input.committee)?; | ||
|
|
||
| let toml = generate_toml(witness)?; | ||
| let configs = generate_configs(preset, &configs); | ||
|
|
||
| Ok(Artifacts { toml, configs }) | ||
| } | ||
| } | ||
|
|
||
| pub fn generate_toml(witness: Witness) -> Result<CodegenToml, CircuitsErrors> { | ||
| let json = witness | ||
| .to_json() | ||
| .map_err(|e| CircuitsErrors::SerdeJson(e))?; | ||
|
|
||
| Ok(toml::to_string(&json)?) | ||
| } | ||
|
|
||
| pub fn generate_configs(_preset: BfvPreset, configs: &Configs) -> CodegenConfigs { | ||
| let prefix = <PkGenerationCircuit as Circuit>::PREFIX; | ||
|
|
||
| let qis_str = join_display(&configs.moduli, ", "); | ||
|
|
||
| let r1_bounds_str = join_display(&configs.bounds.r1_bounds, ", "); | ||
| let r2_bounds_str = join_display(&configs.bounds.r2_bounds, ", "); | ||
|
|
||
| format!( | ||
| r#"use crate::core::threshold::pk_generation::Configs as PkGenerationConfigs; | ||
|
|
||
| // Global configs for Threshold Public Key Generation circuit | ||
| pub global N: u32 = {}; | ||
| pub global L: u32 = {}; | ||
| pub global QIS: [Field; L] = [{}]; | ||
|
|
||
| /************************************ | ||
| ------------------------------------- | ||
| pk_generation (CIRCUIT 1 - PUBLIC KEY THRESHOLD BFV) | ||
| ------------------------------------- | ||
| ************************************/ | ||
|
|
||
| pub global {}_BIT_EEK: u32 = {}; | ||
| pub global {}_BIT_SK: u32 = {}; | ||
| pub global {}_BIT_E_SM: u32 = {}; | ||
| pub global {}_BIT_R1: u32 = {}; | ||
| pub global {}_BIT_R2: u32 = {}; | ||
| pub global {}_BIT_PK: u32 = {}; | ||
|
|
||
| pub global {}_EEK_BOUND: Field = {}; | ||
| pub global {}_SK_BOUND: Field = {}; | ||
| pub global {}_E_SM_BOUND: Field = {}; | ||
| pub global {}_R1_BOUNDS: [Field; L] = [{}]; | ||
| pub global {}_R2_BOUNDS: [Field; L] = [{}]; | ||
|
|
||
| pub global {}_CONFIGS: PkGenerationConfigs<N, L> = PkGenerationConfigs::new( | ||
| QIS, | ||
| {}_EEK_BOUND, | ||
| {}_SK_BOUND, | ||
| {}_E_SM_BOUND, | ||
| {}_R1_BOUNDS, | ||
| {}_R2_BOUNDS, | ||
| ); | ||
| "#, | ||
| configs.n, | ||
| configs.l, | ||
| qis_str, | ||
| prefix, | ||
| configs.bits.eek_bit, | ||
| prefix, | ||
| configs.bits.sk_bit, | ||
| prefix, | ||
| configs.bits.e_sm_bit, | ||
| prefix, | ||
| configs.bits.r1_bit, | ||
| prefix, | ||
| configs.bits.r2_bit, | ||
| prefix, | ||
| configs.bits.pk_bit, | ||
| prefix, | ||
| configs.bounds.eek_bound, | ||
| prefix, | ||
| configs.bounds.sk_bound, | ||
| prefix, | ||
| configs.bounds.e_sm_bound, | ||
| prefix, | ||
| r1_bounds_str, | ||
| prefix, | ||
| r2_bounds_str, | ||
| prefix, | ||
| prefix, | ||
| prefix, | ||
| prefix, | ||
| prefix, | ||
| prefix, | ||
| ) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| use crate::codegen::write_artifacts; | ||
| use crate::threshold::pk_generation::computation::{Bits, Bounds}; | ||
| use crate::threshold::pk_generation::PkGenerationCircuitInput; | ||
| use crate::CiphernodesCommitteeSize; | ||
|
|
||
| use e3_fhe_params::DEFAULT_BFV_PRESET; | ||
| use tempfile::TempDir; | ||
|
|
||
| #[test] | ||
| fn test_toml_generation_and_structure() { | ||
| let committee = CiphernodesCommitteeSize::Small.values(); | ||
| let sample = | ||
| PkGenerationCircuitInput::generate_sample(DEFAULT_BFV_PRESET, committee).unwrap(); | ||
| let artifacts = PkGenerationCircuit | ||
| .codegen(DEFAULT_BFV_PRESET, &sample) | ||
| .unwrap(); | ||
|
|
||
| let parsed: toml::Value = artifacts.toml.parse().unwrap(); | ||
| let pk0is = parsed | ||
| .get("pk0is") | ||
| .and_then(|value| value.as_array()) | ||
| .unwrap(); | ||
| let pk1is = parsed | ||
| .get("pk1is") | ||
| .and_then(|value| value.as_array()) | ||
| .unwrap(); | ||
| assert!(!pk0is.is_empty()); | ||
| assert!(!pk1is.is_empty()); | ||
|
|
||
| let temp_dir = TempDir::new().unwrap(); | ||
| write_artifacts( | ||
| Some(&artifacts.toml), | ||
| &artifacts.configs, | ||
| Some(temp_dir.path()), | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| let output_path = temp_dir.path().join("Prover.toml"); | ||
| assert!(output_path.exists()); | ||
|
|
||
| let content = std::fs::read_to_string(&output_path).unwrap(); | ||
| assert!(content.contains("pk0is")); | ||
| assert!(content.contains("pk1is")); | ||
|
|
||
| assert!(artifacts.toml.contains("[[pk0is]]")); | ||
| assert!(artifacts.toml.contains("[[pk1is]]")); | ||
|
|
||
| let configs_path = temp_dir.path().join("configs.nr"); | ||
| assert!(configs_path.exists()); | ||
|
|
||
| let configs_content = std::fs::read_to_string(&configs_path).unwrap(); | ||
| let bounds = Bounds::compute(DEFAULT_BFV_PRESET, &sample.committee).unwrap(); | ||
| let bits = Bits::compute(DEFAULT_BFV_PRESET, &bounds).unwrap(); | ||
|
|
||
| assert!(configs_content | ||
| .contains(format!("N: u32 = {}", DEFAULT_BFV_PRESET.metadata().degree).as_str())); | ||
| assert!(configs_content | ||
| .contains(format!("L: u32 = {}", DEFAULT_BFV_PRESET.metadata().num_moduli).as_str())); | ||
| assert!(configs_content.contains( | ||
| format!( | ||
| "{}_BIT_PK: u32 = {}", | ||
| <PkGenerationCircuit as Circuit>::PREFIX, | ||
| bits.pk_bit | ||
| ) | ||
| .as_str() | ||
| )); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.