-
Notifications
You must be signed in to change notification settings - Fork 22
feat: add pvss and pvss-cli rust crates with C0 [skip-line-limit] #1217
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
14 commits
Select commit
Hold shift + click to select a range
c3c4d69
bootstrap circuit registry
0xjei cc90827
add licenses
0xjei 54cfb32
add computation for c0
0xjei 0ffcf90
add shared traits for computation
0xjei ed2e819
complete computation and traits for c0
0xjei 4a695a2
initiate codegen for c0
0xjei cbb1fad
complete c0 and connect to registry
0xjei e7e9654
simplify registry
0xjei adc6d27
add first version of pvss-cli
0xjei b71f57c
Merge branch 'main' into feat/codegen-compute
0xjei 2ee1044
fix test
0xjei 2b3c657
remove par_bridge in favour of par_iter
0xjei 97c10fe
modularize, update configs for c0, max reuse of writers
0xjei 79e343f
Merge branch 'main' into feat/codegen-compute
0xjei 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| [package] | ||
| name = "e3-pvss-cli" | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| license.workspace = true | ||
| description = "PVSS CLI for artifact generation" | ||
| repository = "https://github.com/gnosisguild/enclave/crates/pvss-cli" | ||
|
0xjei marked this conversation as resolved.
|
||
|
|
||
| [dependencies] | ||
| e3-pvss = { workspace = true } | ||
| e3-fhe-params = { workspace = true } | ||
| clap = { workspace = true } | ||
| anyhow = { workspace = true } | ||
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,120 @@ | ||
| // 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 anyhow::{anyhow, Context, Result}; | ||
| use clap::Parser; | ||
| use e3_fhe_params::{BfvParamSet, BfvPreset}; | ||
| use e3_pvss::circuits::pk_bfv::circuit::{PkBfvCircuit, PkBfvCodegenInput}; | ||
| use e3_pvss::circuits::pk_bfv::codegen::write_artifacts; | ||
| use e3_pvss::registry::CircuitRegistry; | ||
| use e3_pvss::sample; | ||
| use e3_pvss::traits::Circuit; | ||
| use e3_pvss::traits::CircuitCodegen; | ||
| use std::path::PathBuf; | ||
| use std::sync::Arc; | ||
|
|
||
| /// Minimal PVSS CLI for generating circuit artifacts. | ||
| #[derive(Debug, Parser)] | ||
| #[command(name = "pvss-cli")] | ||
| struct Cli { | ||
| /// List all available circuits and exit. | ||
| #[arg(long)] | ||
| list_circuits: bool, | ||
| /// Circuit name to generate artifacts for (e.g. pk-bfv). | ||
| #[arg(long, required_unless_present = "list_circuits")] | ||
| circuit: Option<String>, | ||
| /// BFV preset name (must match circuit's parameter type). | ||
| #[arg(long, required_unless_present = "list_circuits")] | ||
| preset: Option<String>, | ||
| /// Output directory for generated artifacts. | ||
| #[arg(long, default_value = "output")] | ||
| output: PathBuf, | ||
| } | ||
|
|
||
| /// Parse a preset name into a BFV preset. | ||
| fn parse_preset(name: &str) -> Result<BfvPreset> { | ||
| BfvPreset::from_name(name).map_err(|_| { | ||
| let available = BfvPreset::list().join(", "); | ||
| anyhow!("unknown preset: {name}. Available: {available}") | ||
| }) | ||
| } | ||
|
|
||
| fn main() -> Result<()> { | ||
| let args = Cli::parse(); | ||
|
|
||
| // Register all circuits in the registry (metadata only). | ||
| let mut registry = CircuitRegistry::new(); | ||
| registry.register(Arc::new(PkBfvCircuit)); | ||
|
|
||
| // Handle list circuits flag. | ||
| if args.list_circuits { | ||
| let circuits = registry.list_circuits(); | ||
| println!("Available circuits:"); | ||
| for circuit_name in circuits { | ||
| if let Ok(circuit_meta) = registry.get(&circuit_name) { | ||
| println!( | ||
| " {} - params_type: {:?}, n_recursive_proofs: {}, pub_inputs: {}", | ||
| circuit_name, | ||
| circuit_meta.supported_parameter(), | ||
| circuit_meta.n_recursive_proofs(), | ||
| circuit_meta.n_public_inputs() | ||
| ); | ||
| } | ||
| } | ||
| return Ok(()); | ||
| } | ||
|
|
||
| // Unwrap required arguments (clap ensures they're present when list_circuits is false). | ||
| let circuit = args.circuit.unwrap(); | ||
| let preset = parse_preset(&args.preset.unwrap())?; | ||
|
|
||
| std::fs::create_dir_all(&args.output) | ||
| .with_context(|| format!("failed to create output dir {}", args.output.display()))?; | ||
|
|
||
| // Validate circuit exists in registry. | ||
| let circuit_meta = registry.get(&circuit).map_err(|_| { | ||
| let available = registry.list_circuits().join(", "); | ||
| anyhow!("unknown circuit: {}. Available: {}", circuit, available) | ||
| })?; | ||
|
|
||
| // Validate preset parameter type matches circuit's supported parameter type. | ||
| let preset_param_type = preset.metadata().parameter_type; | ||
| let circuit_param_type = circuit_meta.supported_parameter(); | ||
| if preset_param_type != circuit_param_type { | ||
| return Err(anyhow!( | ||
| "preset has parameter type {:?}, but circuit {} requires {:?}", | ||
| preset_param_type, | ||
| circuit, | ||
| circuit_param_type | ||
| )); | ||
| } | ||
|
|
||
| // Generate artifacts based on circuit name from registry. | ||
| let params = BfvParamSet::from(preset).build_arc(); | ||
| let sample = sample::generate_sample(¶ms); | ||
| let circuit_name = circuit_meta.name(); | ||
| let artifacts = match circuit_name { | ||
| name if name == <PkBfvCircuit as Circuit>::NAME => { | ||
| let circuit = PkBfvCircuit; | ||
| circuit.codegen(PkBfvCodegenInput { | ||
| preset, | ||
| public_key: sample.public_key, | ||
| })? | ||
| } | ||
| name => return Err(anyhow!("circuit {} not yet implemented", name)), | ||
| }; | ||
|
|
||
| write_artifacts( | ||
| &artifacts.toml, | ||
| &artifacts.template, | ||
| &artifacts.configs, | ||
| &artifacts.wrapper, | ||
| Some(args.output.as_path()), | ||
| )?; | ||
|
|
||
| println!("Artifacts written to {}", args.output.display()); | ||
| Ok(()) | ||
| } |
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,25 @@ | ||
| [package] | ||
| name = "e3-pvss" | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| license.workspace = true | ||
| description = "PVSS core codegen and computation crate" | ||
| repository = "https://github.com/gnosisguild/enclave/crates/pvss" | ||
|
0xjei marked this conversation as resolved.
0xjei marked this conversation as resolved.
|
||
|
|
||
| [dependencies] | ||
| e3-polynomial = { workspace = true } | ||
| fhe = { workspace = true } | ||
| fhe-math = { workspace = true } | ||
| e3-fhe-params = { workspace = true } | ||
| e3-zk-helpers = { workspace = true } | ||
| num-bigint = { workspace = true } | ||
| num-traits = { workspace = true } | ||
| rand = { workspace = true } | ||
| rayon = { workspace = true } | ||
| itertools = "0.14.0" | ||
| serde = { workspace = true } | ||
| serde_json = { workspace = true } | ||
| toml = "0.8.23" | ||
| anyhow = { workspace = true } | ||
| tempfile = { workspace = true } | ||
| thiserror = { workspace = true } | ||
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,7 @@ | ||
| // 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. | ||
|
|
||
| pub mod pk_bfv; |
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,70 @@ | ||
| // 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::circuits::pk_bfv::codegen; | ||
| use crate::circuits::pk_bfv::computation::{Bits, Bounds, Witness}; | ||
| use crate::errors::CodegenError; | ||
| use crate::traits::{Circuit, CircuitCodegen, CircuitComputation, Computation}; | ||
| use crate::types::{Artifacts, DkgInputType}; | ||
| use e3_fhe_params::{BfvPreset, ParameterType}; | ||
| use fhe::bfv::{BfvParameters, PublicKey}; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct PkBfvCircuit; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct PkBfvComputationOutput { | ||
| pub bounds: Bounds, | ||
| pub bits: Bits, | ||
| pub witness: Witness, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct PkBfvCodegenInput { | ||
| pub preset: BfvPreset, | ||
| pub public_key: PublicKey, | ||
| } | ||
|
|
||
| impl Circuit for PkBfvCircuit { | ||
| const NAME: &'static str = "pk-bfv"; | ||
| const PREFIX: &'static str = "PK_BFV"; | ||
| const SUPPORTED_PARAMETER: ParameterType = ParameterType::DKG; | ||
| const DKG_INPUT_TYPE: Option<DkgInputType> = None; | ||
| const N_PROOFS: usize = 1; | ||
| const N_PUBLIC_INPUTS: usize = 1; | ||
| } | ||
|
|
||
| impl CircuitCodegen for PkBfvCircuit { | ||
| type Input = PkBfvCodegenInput; | ||
| type Error = CodegenError; | ||
|
|
||
| fn codegen(&self, input: Self::Input) -> Result<Artifacts, Self::Error> { | ||
| codegen::codegen(input.preset, input.public_key) | ||
| } | ||
| } | ||
|
|
||
| impl CircuitComputation for PkBfvCircuit { | ||
| type Params = BfvParameters; | ||
| type Input = PublicKey; | ||
| type Output = PkBfvComputationOutput; | ||
| type Error = CodegenError; | ||
|
|
||
| fn compute( | ||
| &self, | ||
| params: &Self::Params, | ||
| input: &Self::Input, | ||
| ) -> Result<Self::Output, Self::Error> { | ||
| let bounds = Bounds::compute(params, &())?; | ||
| let bits = Bits::compute(params, &bounds)?; | ||
| let witness = Witness::compute(params, input)?; | ||
|
|
||
| Ok(PkBfvComputationOutput { | ||
| bounds, | ||
| bits, | ||
| witness, | ||
| }) | ||
| } | ||
| } |
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.