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 circuits/lib/src/configs/insecure/threshold.nr
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ pk_generation (CIRCUIT 1)

pub global PK_GENERATION_BIT_EEK: u32 = 5;
pub global PK_GENERATION_BIT_SK: u32 = 1;
pub global PK_GENERATION_BIT_E_SM: u32 = 17;
pub global PK_GENERATION_BIT_E_SM: u32 = 24;
pub global PK_GENERATION_BIT_R1: u32 = 13;
pub global PK_GENERATION_BIT_R2: u32 = 35;
pub global PK_GENERATION_BIT_PK: u32 = 35;

pub global PK_GENERATION_EEK_BOUND: Field = 20;
pub global PK_GENERATION_SK_BOUND: Field = 1;
pub global PK_GENERATION_E_SM_BOUND: Field = 123072;
pub global PK_GENERATION_E_SM_BOUND: Field = 12307200;
pub global PK_GENERATION_R1_BOUNDS: [Field; L] = [5120, 5120];
pub global PK_GENERATION_R2_BOUNDS: [Field; L] = [34359701504, 34359615488];

Expand Down
6 changes: 3 additions & 3 deletions crates/events/src/enclave_event/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub enum CircuitName {
/// BFV public key proof (T0).
PkBfv,
/// TrBFV public key share proof (T1).
PkTrbfv,
PkGeneration,
Comment thread
ctrlc03 marked this conversation as resolved.
/// Encrypted shares proof (T2/T3).
EncShares,
/// Decryption share proof (T4/T5).
Expand All @@ -52,7 +52,7 @@ impl CircuitName {
pub fn as_str(&self) -> &'static str {
match self {
CircuitName::PkBfv => "pk",
CircuitName::PkTrbfv => "pk_trbfv",
CircuitName::PkGeneration => "pk_generation",
CircuitName::EncShares => "enc_shares",
CircuitName::DecShares => "dec_shares",
CircuitName::PkAgg => "pk_agg",
Expand All @@ -62,7 +62,7 @@ impl CircuitName {
pub fn group(&self) -> &'static str {
match self {
CircuitName::PkBfv => "dkg",
CircuitName::PkTrbfv => "threshold",
CircuitName::PkGeneration => "threshold",
CircuitName::EncShares => "threshold",
CircuitName::DecShares => "threshold",
CircuitName::PkAgg => "threshold",
Expand Down
7 changes: 7 additions & 0 deletions crates/zk-prover/src/circuits/dkg/mod.rs
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

mod pk;
Comment thread
ctrlc03 marked this conversation as resolved.
Comment thread
ctrlc03 marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
// without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE

use crate::circuits::utils::crt_polynomial_to_array;
use crate::error::ZkError;
use crate::traits::Provable;
use acir::FieldElement;
use e3_events::CircuitName;
use e3_fhe_params::BfvPreset;
use e3_polynomial::CrtPolynomial;
use e3_zk_helpers::circuits::dkg::pk::circuit::{PkCircuit, PkCircuitInput};
use e3_zk_helpers::circuits::dkg::pk::computation::Witness;
use e3_zk_helpers::Computation;
use fhe::bfv::PublicKey;
use noirc_abi::{input_parser::InputValue, InputMap};
use std::collections::BTreeMap;
use noirc_abi::InputMap;

impl Provable for PkCircuit {
type Params = BfvPreset;
Expand Down Expand Up @@ -50,26 +48,3 @@ impl Provable for PkCircuit {
Ok(inputs)
}
}

fn crt_polynomial_to_array(crt_poly: &CrtPolynomial) -> Result<InputValue, ZkError> {
let mut polynomials = Vec::with_capacity(crt_poly.limbs.len());

for limb in &crt_poly.limbs {
let coeffs = limb.coefficients();
let mut field_coeffs = Vec::with_capacity(coeffs.len());

for b in coeffs {
let s = b.to_string();
let field = FieldElement::try_from_str(&s).ok_or_else(|| {
ZkError::SerializationError(format!("invalid field element: {}", s))
})?;
field_coeffs.push(InputValue::Field(field));
}

let mut fields = BTreeMap::new();
fields.insert("coefficients".to_string(), InputValue::Vec(field_coeffs));
polynomials.push(InputValue::Struct(fields));
}

Ok(InputValue::Vec(polynomials))
}
4 changes: 3 additions & 1 deletion crates/zk-prover/src/circuits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
// without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.

mod pkbfv;
mod dkg;
mod threshold;
mod utils;
7 changes: 7 additions & 0 deletions crates/zk-prover/src/circuits/threshold/mod.rs
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.

mod pk_generation;
48 changes: 48 additions & 0 deletions crates/zk-prover/src/circuits/threshold/pk_generation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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::utils::{crt_polynomial_to_array, polynomial_to_input_value};
use crate::error::ZkError;
use crate::traits::Provable;
use e3_events::CircuitName;
use e3_fhe_params::BfvPreset;
use e3_zk_helpers::circuits::threshold::pk_generation::circuit::{
PkGenerationCircuit, PkGenerationCircuitInput,
};
use e3_zk_helpers::circuits::threshold::pk_generation::computation::Witness;
use e3_zk_helpers::Computation;
use noirc_abi::InputMap;
use std::collections::BTreeMap;

impl Provable for PkGenerationCircuit {
type Params = BfvPreset;
type Input = PkGenerationCircuitInput;

fn circuit(&self) -> CircuitName {
CircuitName::PkGeneration
}

fn build_witness(
&self,
preset: &Self::Params,
input: &Self::Input,
) -> Result<InputMap, ZkError> {
let witness = Witness::compute(preset.clone(), &input)
.map_err(|e| ZkError::WitnessGenerationFailed(e.to_string()))?;

let mut inputs = BTreeMap::new();
inputs.insert("a".into(), crt_polynomial_to_array(&witness.a)?);
inputs.insert("eek".into(), polynomial_to_input_value(&witness.eek)?);
inputs.insert("sk".into(), polynomial_to_input_value(&witness.sk)?);
inputs.insert("e_sm".into(), crt_polynomial_to_array(&witness.e_sm)?);
inputs.insert("r1is".into(), crt_polynomial_to_array(&witness.r1is)?);
inputs.insert("r2is".into(), crt_polynomial_to_array(&witness.r2is)?);
inputs.insert("pk0is".into(), crt_polynomial_to_array(&witness.pk0is)?);
inputs.insert("pk1is".into(), crt_polynomial_to_array(&witness.pk1is)?);

Ok(inputs)
}
}
51 changes: 51 additions & 0 deletions crates/zk-prover/src/circuits/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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 std::collections::BTreeMap;

use crate::error::ZkError;
use acir::FieldElement;
use e3_polynomial::{CrtPolynomial, Polynomial};
use noirc_abi::input_parser::InputValue;
Comment thread
coderabbitai[bot] marked this conversation as resolved.

pub fn crt_polynomial_to_array(crt_poly: &CrtPolynomial) -> Result<InputValue, ZkError> {
let mut polynomials = Vec::with_capacity(crt_poly.limbs.len());

for limb in &crt_poly.limbs {
let coeffs = limb.coefficients();
let mut field_coeffs = Vec::with_capacity(coeffs.len());

for b in coeffs {
let s = b.to_string();
let field = FieldElement::try_from_str(&s).ok_or_else(|| {
ZkError::SerializationError(format!("invalid field element: {}", s))
})?;
field_coeffs.push(InputValue::Field(field));
}

let mut fields = BTreeMap::new();
fields.insert("coefficients".to_string(), InputValue::Vec(field_coeffs));
polynomials.push(InputValue::Struct(fields));
}

Ok(InputValue::Vec(polynomials))
}

pub fn polynomial_to_input_value(poly: &Polynomial) -> Result<InputValue, ZkError> {
let coeffs = poly.coefficients();
let mut field_coeffs = Vec::with_capacity(coeffs.len());

for b in coeffs {
let s = b.to_string();
let field = FieldElement::try_from_str(&s)
.ok_or_else(|| ZkError::SerializationError(format!("invalid field element: {}", s)))?;
field_coeffs.push(InputValue::Field(field));
}

let mut fields = BTreeMap::new();
fields.insert("coefficients".to_string(), InputValue::Vec(field_coeffs));
Ok(InputValue::Struct(fields))
}
1 change: 1 addition & 0 deletions crates/zk-prover/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub trait Provable: Send + Sync {
.circuits_dir()
.join(self.circuit().dir_path())
.join(format!("{}.json", circuit_name));

let circuit = CompiledCircuit::from_file(&circuit_path)?;

let witness_gen = WitnessGenerator::new();
Expand Down
154 changes: 154 additions & 0 deletions crates/zk-prover/tests/fixtures/pk_generation.json
Comment thread
ctrlc03 marked this conversation as resolved.

Large diffs are not rendered by default.

Binary file added crates/zk-prover/tests/fixtures/pk_generation.vk
Binary file not shown.
Loading
Loading