Skip to content
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
4 changes: 2 additions & 2 deletions crates/bfv-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use anyhow::{anyhow, Result};
use e3_fhe_params::{build_bfv_params_arc, DEFAULT_BFV_PRESET};
use e3_zk_helpers::circuits::threshold::user_data_encryption::circuit::UserDataEncryptionCircuitInput;
use e3_zk_helpers::circuits::threshold::user_data_encryption::circuit::UserDataEncryptionCircuitData;
use e3_zk_helpers::circuits::threshold::user_data_encryption::Inputs as UserDataEncryptionInputs;
use e3_zk_helpers::circuits::Computation;
use fhe::bfv::{Ciphertext, Encoding, Plaintext, PublicKey, SecretKey};
Expand Down Expand Up @@ -103,7 +103,7 @@ where

let inputs = UserDataEncryptionInputs::compute(
DEFAULT_BFV_PRESET,
&UserDataEncryptionCircuitInput {
&UserDataEncryptionCircuitData {
public_key: pk,
plaintext: plaintext,
},
Expand Down
6 changes: 3 additions & 3 deletions crates/multithread/src/multithread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use e3_trbfv::gen_esi_sss::gen_esi_sss;
use e3_trbfv::gen_pk_share_and_sk_sss::gen_pk_share_and_sk_sss;
use e3_trbfv::{TrBFVError, TrBFVRequest, TrBFVResponse};
use e3_utils::SharedRng;
use e3_zk_helpers::circuits::dkg::pk::circuit::{PkCircuit, PkCircuitInput};
use e3_zk_helpers::circuits::dkg::pk::circuit::{PkCircuit, PkCircuitData};
use e3_zk_prover::{Provable, ZkBackend, ZkProver};
use fhe::bfv::PublicKey;
use fhe_traits::DeserializeParametrized;
Expand Down Expand Up @@ -357,7 +357,7 @@ fn handle_pk_bfv_proof(
})?;

let circuit = PkCircuit;
let circuit_input = PkCircuitInput { public_key: pk_bfv };
let circuit_data = PkCircuitData { public_key: pk_bfv };
let e3_id_str = request.e3_id.to_string();
let preset_counterpart = req
.params_preset
Expand All @@ -366,7 +366,7 @@ fn handle_pk_bfv_proof(
// But here we have to pass the InsecureThreshold512 preset because the underlaying witness generator
// builds both params, but will only use the DKG one
let proof = circuit
.prove(prover, &preset_counterpart, &circuit_input, &e3_id_str)
.prove(prover, &preset_counterpart, &circuit_data, &e3_id_str)
.map_err(|e| {
ComputeRequestError::new(
ComputeRequestErrorKind::Zk(ZkEventError::ProofGenerationFailed(e.to_string())),
Expand Down
4 changes: 2 additions & 2 deletions crates/zk-helpers/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# zk-helpers

ZK circuit artifact generation for the Noir prover. Produces `configs.nr` and optionally
`Prover.toml` for the Enclave circuits. The Prover.toml contains circuit inputs for Nargo,
which executes them to produce witnesses for proof generation.
`Prover.toml` for the Enclave circuits. The Prover.toml contains circuit inputs for Nargo, which
executes them to produce witnesses for proof generation.

## zk-cli

Expand Down
36 changes: 18 additions & 18 deletions crates/zk-helpers/src/bin/zk_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ use anyhow::{anyhow, Context, Result};
use clap::{arg, command, Parser};
use e3_fhe_params::{BfvPreset, ParameterType};
use e3_zk_helpers::ciphernodes_committee::CiphernodesCommitteeSize;
use e3_zk_helpers::circuits::dkg::pk::circuit::{PkCircuit, PkCircuitInput};
use e3_zk_helpers::circuits::dkg::pk::circuit::{PkCircuit, PkCircuitData};
use e3_zk_helpers::circuits::dkg::share_computation::circuit::{
ShareComputationCircuit, ShareComputationCircuitInput,
ShareComputationCircuit, ShareComputationCircuitData,
};
use e3_zk_helpers::codegen::{write_artifacts, write_toml, CircuitCodegen};
use e3_zk_helpers::computation::DkgInputType;
use e3_zk_helpers::dkg::share_decryption::{
ShareDecryptionCircuit as DkgShareDecryptionCircuit,
ShareDecryptionCircuitInput as DkgShareDecryptionCircuitInput,
ShareDecryptionCircuitData as DkgShareDecryptionCircuitData,
};
Comment thread
cedoor marked this conversation as resolved.
use e3_zk_helpers::dkg::share_encryption::{ShareEncryptionCircuit, ShareEncryptionCircuitInput};
use e3_zk_helpers::dkg::share_encryption::{ShareEncryptionCircuit, ShareEncryptionCircuitData};
use e3_zk_helpers::registry::{Circuit, CircuitRegistry};
use e3_zk_helpers::threshold::decrypted_shares_aggregation::{
DecryptedSharesAggregationCircuit, DecryptedSharesAggregationCircuitInput,
DecryptedSharesAggregationCircuit, DecryptedSharesAggregationCircuitData,
};
use e3_zk_helpers::threshold::pk_aggregation::PkAggregationCircuit;
use e3_zk_helpers::threshold::pk_aggregation::PkAggregationCircuitInput;
use e3_zk_helpers::threshold::pk_generation::{PkGenerationCircuit, PkGenerationCircuitInput};
use e3_zk_helpers::threshold::pk_aggregation::PkAggregationCircuitData;
use e3_zk_helpers::threshold::pk_generation::{PkGenerationCircuit, PkGenerationCircuitData};
use e3_zk_helpers::threshold::share_decryption::{
ShareDecryptionCircuit as ThresholdShareDecryptionCircuit,
ShareDecryptionCircuitInput as ThresholdShareDecryptionCircuitInput,
ShareDecryptionCircuitData as ThresholdShareDecryptionCircuitData,
};
Comment thread
cedoor marked this conversation as resolved.
use e3_zk_helpers::threshold::user_data_encryption::{
UserDataEncryptionCircuit, UserDataEncryptionCircuitInput,
UserDataEncryptionCircuit, UserDataEncryptionCircuitData,
};
use std::io::Write;
use std::path::PathBuf;
Expand Down Expand Up @@ -270,13 +270,13 @@ fn main() -> Result<()> {
let committee = CiphernodesCommitteeSize::Small.values();
let artifacts = match circuit_name {
name if name == <PkCircuit as Circuit>::NAME => {
let sample = PkCircuitInput::generate_sample(preset)?;
let sample = PkCircuitData::generate_sample(preset)?;

let circuit = PkCircuit;
circuit.codegen(preset, &sample)?
}
name if name == <ShareComputationCircuit as Circuit>::NAME => {
let sample = ShareComputationCircuitInput::generate_sample(
let sample = ShareComputationCircuitData::generate_sample(
preset,
committee,
dkg_input_type,
Expand All @@ -287,7 +287,7 @@ fn main() -> Result<()> {
}
name if name == <ShareEncryptionCircuit as Circuit>::NAME => {
let sd = preset.search_defaults().unwrap();
let sample = ShareEncryptionCircuitInput::generate_sample(
let sample = ShareEncryptionCircuitData::generate_sample(
preset,
committee,
dkg_input_type,
Expand All @@ -299,19 +299,19 @@ fn main() -> Result<()> {
circuit.codegen(preset, &sample)?
}
name if name == <UserDataEncryptionCircuit as Circuit>::NAME => {
let sample = UserDataEncryptionCircuitInput::generate_sample(preset)?;
let sample = UserDataEncryptionCircuitData::generate_sample(preset)?;

let circuit = UserDataEncryptionCircuit;
circuit.codegen(preset, &sample)?
}
name if name == <PkGenerationCircuit as Circuit>::NAME => {
let sample = PkGenerationCircuitInput::generate_sample(preset, committee)?;
let sample = PkGenerationCircuitData::generate_sample(preset, committee)?;

let circuit = PkGenerationCircuit;
circuit.codegen(preset, &sample)?
}
name if name == <DkgShareDecryptionCircuit as Circuit>::NAME => {
let sample = DkgShareDecryptionCircuitInput::generate_sample(
let sample = DkgShareDecryptionCircuitData::generate_sample(
preset,
committee,
dkg_input_type,
Expand All @@ -321,20 +321,20 @@ fn main() -> Result<()> {
circuit.codegen(preset, &sample)?
}
name if name == <PkAggregationCircuit as Circuit>::NAME => {
let sample = PkAggregationCircuitInput::generate_sample(preset, committee)?;
let sample = PkAggregationCircuitData::generate_sample(preset, committee)?;

let circuit = PkAggregationCircuit;
circuit.codegen(preset, &sample)?
}
name if name == <ThresholdShareDecryptionCircuit as Circuit>::NAME => {
let sample =
ThresholdShareDecryptionCircuitInput::generate_sample(preset, committee)?;
ThresholdShareDecryptionCircuitData::generate_sample(preset, committee)?;

let circuit = ThresholdShareDecryptionCircuit;
circuit.codegen(preset, &sample)?
}
name if name == <DecryptedSharesAggregationCircuit as Circuit>::NAME => {
let sample = DecryptedSharesAggregationCircuitInput::generate_sample(
let sample = DecryptedSharesAggregationCircuitData::generate_sample(
preset,
CiphernodesCommitteeSize::Small.values(),
)?;
Expand Down
8 changes: 4 additions & 4 deletions crates/zk-helpers/src/circuits/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ pub struct Artifacts {
pub configs: CodegenConfigs,
}

/// Trait for circuits that can generate Prover.toml and configs.nr from circuit-specific input.
/// Trait for circuits that can generate Prover.toml and configs.nr from circuit-specific data.
pub trait CircuitCodegen: crate::registry::Circuit {
/// Circuit-specific BFV threshold parameters preset.
type Preset;
/// Circuit-specific codegen input (e.g. preset + public key).
type Input;
/// Circuit-specific codegen data (e.g. preset + public key).
type Data;
/// Error type for codegen failures.
type Error;

/// Produces [`Artifacts`] for this circuit from the given input.
fn codegen(&self, preset: Self::Preset, input: &Self::Input) -> Result<Artifacts, Self::Error>;
fn codegen(&self, preset: Self::Preset, data: &Self::Data) -> Result<Artifacts, Self::Error>;
}

/// Writes the Prover TOML string to `path/Prover.toml`, or `./Prover.toml` if `path` is `None`.
Expand Down
8 changes: 4 additions & 4 deletions crates/zk-helpers/src/circuits/computation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ pub enum DkgInputType {
/// Generic computation from parameters and input to a result.
pub trait Computation: Sized {
type Preset;
type Input;
type Data;
type Error;

/// Computes the result from parameters and input.
fn compute(preset: Self::Preset, input: &Self::Input) -> Result<Self, Self::Error>;
fn compute(preset: Self::Preset, data: &Self::Data) -> Result<Self, Self::Error>;

/// Converts the result to a JSON [`serde_json::Value`] for serialization.
/// Default: `serde_json::to_value(self)` when `Self: serde::Serialize`.
Expand All @@ -41,10 +41,10 @@ pub trait Computation: Sized {
/// Circuit-specific computation: parameters and input produce bounds, bits, circuit inputs, etc.
pub trait CircuitComputation: crate::registry::Circuit {
type Preset;
type Input;
type Data;
type Output;
type Error;

/// Computes circuit-specific data (bounds, bits, inputs) from parameters and input.
fn compute(preset: Self::Preset, input: &Self::Input) -> Result<Self::Output, Self::Error>;
fn compute(preset: Self::Preset, data: &Self::Data) -> Result<Self::Output, Self::Error>;
}
2 changes: 1 addition & 1 deletion crates/zk-helpers/src/circuits/dkg/pk/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ impl Circuit for PkCircuit {
const DKG_INPUT_TYPE: Option<DkgInputType> = Some(DkgInputType::SecretKey);
}

pub struct PkCircuitInput {
pub struct PkCircuitData {
pub public_key: PublicKey,
}
12 changes: 6 additions & 6 deletions crates/zk-helpers/src/circuits/dkg/pk/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! Code generation for the public-key BFV circuit: Prover.toml and configs.nr.

use crate::circuits::dkg::pk::circuit::PkCircuit;
use crate::circuits::dkg::pk::circuit::PkCircuitInput;
use crate::circuits::dkg::pk::circuit::PkCircuitData;
use crate::circuits::dkg::pk::computation::{Bits, Inputs, PkComputationOutput};
use crate::Artifacts;
use crate::Circuit;
Expand All @@ -23,11 +23,11 @@ use e3_fhe_params::BfvPreset;
/// Implementation of [`CircuitCodegen`] for [`PkCircuit`].
impl CircuitCodegen for PkCircuit {
type Preset = BfvPreset;
type Input = PkCircuitInput;
type Data = PkCircuitData;
type Error = CircuitsErrors;

fn codegen(&self, preset: Self::Preset, input: &Self::Input) -> Result<Artifacts, Self::Error> {
let PkComputationOutput { inputs, bits, .. } = PkCircuit::compute(preset, input)?;
fn codegen(&self, preset: Self::Preset, data: &Self::Data) -> Result<Artifacts, Self::Error> {
let PkComputationOutput { inputs, bits, .. } = PkCircuit::compute(preset, data)?;

let toml = generate_toml(inputs)?;
let configs = generate_configs(preset, &bits);
Expand Down Expand Up @@ -69,7 +69,7 @@ pub global {}_BIT_PK: u32 = {};
mod tests {
use super::*;
use crate::codegen::write_artifacts;
use crate::dkg::pk::PkCircuitInput;
use crate::dkg::pk::PkCircuitData;
use crate::utils::compute_modulus_bit;

use e3_fhe_params::{build_pair_for_preset, BfvPreset};
Expand All @@ -78,7 +78,7 @@ mod tests {
#[test]
fn test_toml_generation_and_structure() {
let (_, dkg_params) = build_pair_for_preset(BfvPreset::InsecureThreshold512).unwrap();
let sample = PkCircuitInput::generate_sample(BfvPreset::InsecureThreshold512).unwrap();
let sample = PkCircuitData::generate_sample(BfvPreset::InsecureThreshold512).unwrap();

let artifacts = PkCircuit
.codegen(BfvPreset::InsecureThreshold512, &sample)
Expand Down
28 changes: 14 additions & 14 deletions crates/zk-helpers/src/circuits/dkg/pk/computation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! and (for input) a public key. They implement [`Computation`] and are used by codegen.

use crate::circuits::dkg::pk::circuit::PkCircuit;
use crate::circuits::dkg::pk::circuit::PkCircuitInput;
use crate::circuits::dkg::pk::circuit::PkCircuitData;
use crate::compute_max_modulus;
use crate::crt_polynomial_to_toml_json;
use crate::get_zkp_modulus;
Expand All @@ -34,14 +34,14 @@ pub struct PkComputationOutput {
/// Implementation of [`CircuitComputation`] for [`PkCircuit`].
impl CircuitComputation for PkCircuit {
type Preset = BfvPreset;
type Input = PkCircuitInput;
type Data = PkCircuitData;
type Output = PkComputationOutput;
type Error = CircuitsErrors;

fn compute(preset: Self::Preset, input: &Self::Input) -> Result<Self::Output, Self::Error> {
fn compute(preset: Self::Preset, data: &Self::Data) -> Result<Self::Output, Self::Error> {
let bounds = Bounds::compute(preset, &())?;
let bits = Bits::compute(preset, &())?;
let inputs = Inputs::compute(preset, input)?;
let inputs = Inputs::compute(preset, data)?;

Ok(PkComputationOutput {
bounds,
Expand Down Expand Up @@ -83,10 +83,10 @@ pub struct Inputs {

impl Computation for Configs {
type Preset = BfvPreset;
type Input = ();
type Data = ();
type Error = CircuitsErrors;

fn compute(preset: Self::Preset, _: &Self::Input) -> Result<Self, CircuitsErrors> {
fn compute(preset: Self::Preset, _: &Self::Data) -> Result<Self, CircuitsErrors> {
let (_, dkg_params) =
build_pair_for_preset(preset).map_err(|e| CircuitsErrors::Sample(e.to_string()))?;

Expand All @@ -107,10 +107,10 @@ impl Computation for Configs {

impl Computation for Bits {
type Preset = BfvPreset;
type Input = ();
type Data = ();
type Error = CircuitsErrors;

fn compute(preset: Self::Preset, _: &Self::Input) -> Result<Self, Self::Error> {
fn compute(preset: Self::Preset, _: &Self::Data) -> Result<Self, Self::Error> {
let (_, dkg_params) =
build_pair_for_preset(preset).map_err(|e| CircuitsErrors::Sample(e.to_string()))?;

Expand All @@ -122,10 +122,10 @@ impl Computation for Bits {

impl Computation for Bounds {
type Preset = BfvPreset;
type Input = ();
type Data = ();
type Error = CircuitsErrors;

fn compute(preset: Self::Preset, _: &Self::Input) -> Result<Self, Self::Error> {
fn compute(preset: Self::Preset, _: &Self::Data) -> Result<Self, Self::Error> {
let (_, dkg_params) =
build_pair_for_preset(preset).map_err(|e| CircuitsErrors::Sample(e.to_string()))?;

Expand All @@ -139,16 +139,16 @@ impl Computation for Bounds {

impl Computation for Inputs {
type Preset = BfvPreset;
type Input = PkCircuitInput;
type Data = PkCircuitData;
type Error = CircuitsErrors;

fn compute(preset: Self::Preset, input: &Self::Input) -> Result<Self, Self::Error> {
fn compute(preset: Self::Preset, data: &Self::Data) -> Result<Self, Self::Error> {
let (_, dkg_params) =
build_pair_for_preset(preset).map_err(|e| CircuitsErrors::Sample(e.to_string()))?;
let moduli = dkg_params.moduli();

let mut pk0is = crate::math::fhe_poly_to_crt_centered(&input.public_key.c.c[0], moduli)?;
let mut pk1is = crate::math::fhe_poly_to_crt_centered(&input.public_key.c.c[1], moduli)?;
let mut pk0is = crate::math::fhe_poly_to_crt_centered(&data.public_key.c.c[0], moduli)?;
let mut pk1is = crate::math::fhe_poly_to_crt_centered(&data.public_key.c.c[1], moduli)?;

let zkp_modulus = &get_zkp_modulus();

Expand Down
2 changes: 1 addition & 1 deletion crates/zk-helpers/src/circuits/dkg/pk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ pub mod codegen;
pub mod computation;
pub mod sample;

pub use circuit::{PkCircuit, PkCircuitInput};
pub use circuit::{PkCircuit, PkCircuitData};
pub use codegen::{generate_configs, generate_toml};
pub use computation::{Bits, Bounds, Configs, Inputs, PkComputationOutput};
8 changes: 4 additions & 4 deletions crates/zk-helpers/src/circuits/dkg/pk/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

//! Sample data generation for the pk circuit: committee and DKG public key only.

use crate::dkg::pk::PkCircuitInput;
use crate::dkg::pk::PkCircuitData;
use crate::CircuitsErrors;
use e3_fhe_params::build_pair_for_preset;
use e3_fhe_params::BfvPreset;
use fhe::bfv::{PublicKey, SecretKey};
use rand::thread_rng;

impl PkCircuitInput {
impl PkCircuitData {
/// Generates sample data for the pk circuit.
pub fn generate_sample(preset: BfvPreset) -> Result<Self, CircuitsErrors> {
let (_, dkg_params) = build_pair_for_preset(preset).map_err(|e| {
Expand All @@ -32,12 +32,12 @@ impl PkCircuitInput {

#[cfg(test)]
mod tests {
use crate::dkg::pk::PkCircuitInput;
use crate::dkg::pk::PkCircuitData;
use e3_fhe_params::BfvPreset;

#[test]
fn test_generate_pk_sample() {
let sample = PkCircuitInput::generate_sample(BfvPreset::InsecureThreshold512).unwrap();
let sample = PkCircuitData::generate_sample(BfvPreset::InsecureThreshold512).unwrap();

assert_eq!(sample.public_key.c.c.len(), 2);
}
Expand Down
Loading
Loading