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
2 changes: 1 addition & 1 deletion circuits/lib/src/configs/committee/small.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ pub global N_PARTIES: u32 = 5;
/// Threshold.
pub global T: u32 = 2;
/// Number of honest parties.
pub global H: u32 = 3;
pub global H: u32 = 5;
11 changes: 6 additions & 5 deletions crates/zk-helpers/src/bin/zk_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ 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 = PkCircuitInput::generate_sample(preset)?;

let circuit = PkCircuit;
circuit.codegen(preset, &sample)?
Expand All @@ -272,7 +272,8 @@ fn main() -> Result<()> {
preset,
committee,
dkg_input_type,
);
)?;

let circuit = ShareComputationCircuit;
circuit.codegen(preset, &sample)?
}
Expand All @@ -284,13 +285,13 @@ fn main() -> Result<()> {
dkg_input_type,
sd.z,
sd.lambda,
);
)?;

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

let circuit = UserDataEncryptionCircuit;
circuit.codegen(preset, &sample)?
Expand Down Expand Up @@ -328,7 +329,7 @@ fn main() -> Result<()> {
let sample = DecryptedSharesAggregationCircuitInput::generate_sample(
preset,
CiphernodesCommitteeSize::Small.values(),
);
)?;

let circuit = DecryptedSharesAggregationCircuit;
circuit.codegen(preset, &sample)?
Expand Down
6 changes: 3 additions & 3 deletions crates/zk-helpers/src/ciphernodes_committee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ impl CiphernodesCommitteeSize {
match self {
CiphernodesCommitteeSize::Small => CiphernodesCommittee {
n: 5,
h: 3,
h: 5,
threshold: 2,
},
_ => unreachable!(),
}
// @todo add the other committee sizes
// CiphernodesCommitteeSize::Medium => CiphernodesCommittee {
// n: 5,
// h: 3,
// h: 5,
// threshold: 2,
// },
// CiphernodesCommitteeSize::Large => CiphernodesCommittee {
// n: 5,
// h: 3,
// h: 5,
// threshold: 2,
// },
}
Expand Down
2 changes: 1 addition & 1 deletion crates/zk-helpers/src/circuits/dkg/pk/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,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);
let sample = PkCircuitInput::generate_sample(BfvPreset::InsecureThreshold512).unwrap();

let artifacts = PkCircuit
.codegen(BfvPreset::InsecureThreshold512, &sample)
Expand Down
20 changes: 7 additions & 13 deletions crates/zk-helpers/src/circuits/dkg/pk/computation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use crate::circuits::dkg::pk::circuit::PkCircuit;
use crate::circuits::dkg::pk::circuit::PkCircuitInput;
use crate::compute_max_modulus;
use crate::crt_polynomial_to_toml_json;
use crate::get_zkp_modulus;
use crate::utils::compute_modulus_bit;
Expand Down Expand Up @@ -90,12 +91,13 @@ impl Computation for Configs {
build_pair_for_preset(preset).map_err(|e| CircuitsErrors::Sample(e.to_string()))?;

let moduli = dkg_params.moduli().to_vec();
let l = moduli.len();
let bounds = Bounds::compute(preset, &())?;
let bits = Bits::compute(preset, &())?;

Ok(Configs {
n: dkg_params.degree(),
l: moduli.len(),
l,
moduli,
bits,
bounds,
Expand Down Expand Up @@ -127,19 +129,11 @@ impl Computation for Bounds {
let (_, dkg_params) =
build_pair_for_preset(preset).map_err(|e| CircuitsErrors::Sample(e.to_string()))?;

let mut pk_bound_max = BigUint::from(0u32);

for &qi in dkg_params.moduli() {
let qi_bound: BigUint = (&BigUint::from(qi) - 1u32) / 2u32;

if qi_bound > pk_bound_max {
pk_bound_max = qi_bound;
}
}
let moduli = dkg_params.moduli();
let max_mod = compute_max_modulus(moduli);
let pk_bound = (BigUint::from(max_mod) - 1u32) / 2u32;

Ok(Bounds {
pk_bound: pk_bound_max,
})
Ok(Bounds { pk_bound })
}
}

Expand Down
13 changes: 8 additions & 5 deletions crates/zk-helpers/src/circuits/dkg/pk/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@
//! Sample data generation for the pk circuit: committee and DKG public key only.

use crate::dkg::pk::PkCircuitInput;
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 {
/// Generates sample data for the pk circuit.
pub fn generate_sample(preset: BfvPreset) -> Self {
let (_, dkg_params) = build_pair_for_preset(preset).unwrap();
pub fn generate_sample(preset: BfvPreset) -> Result<Self, CircuitsErrors> {
let (_, dkg_params) = build_pair_for_preset(preset).map_err(|e| {
CircuitsErrors::Sample(format!("Failed to build pair for preset: {:?}", e))
})?;

let mut rng = thread_rng();
let dkg_secret_key = SecretKey::random(&dkg_params, &mut rng);
let dkg_public_key = PublicKey::new(&dkg_secret_key, &mut rng);

Self {
Ok(Self {
public_key: dkg_public_key,
}
})
}
}

Expand All @@ -34,7 +37,7 @@ mod tests {

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

assert_eq!(sample.public_key.c.c.len(), 2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ mod tests {
BfvPreset::InsecureThreshold512,
committee,
DkgInputType::SecretKey,
);
)
.unwrap();

let artifacts = ShareComputationCircuit
.codegen(BfvPreset::InsecureThreshold512, &sample)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ mod tests {
BfvPreset::InsecureThreshold512,
committee,
DkgInputType::SecretKey,
);
)
.unwrap();
let bounds = Bounds::compute(BfvPreset::InsecureThreshold512, &sample).unwrap();
let bits = Bits::compute(BfvPreset::InsecureThreshold512, &bounds).unwrap();
let expected_sk_bits = calculate_bit_width(BigInt::from(bounds.sk_bound.clone()));
Expand All @@ -291,7 +292,8 @@ mod tests {
BfvPreset::InsecureThreshold512,
committee,
DkgInputType::SmudgingNoise,
);
)
.unwrap();
let witness = Witness::compute(BfvPreset::InsecureThreshold512, &sample).unwrap();
let degree = witness.secret_crt.limb(0).coefficients().len();
let num_moduli = witness.secret_crt.limbs.len();
Expand All @@ -315,7 +317,8 @@ mod tests {
BfvPreset::InsecureThreshold512,
committee,
DkgInputType::SecretKey,
);
)
.unwrap();

let constants = Configs::compute(BfvPreset::InsecureThreshold512, &sample).unwrap();

Expand Down
41 changes: 29 additions & 12 deletions crates/zk-helpers/src/circuits/dkg/share_computation/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,44 @@ impl ShareComputationCircuitInput {
preset: BfvPreset,
committee: CiphernodesCommittee,
dkg_input_type: DkgInputType,
) -> Self {
let (threshold_params, _) = build_pair_for_preset(preset).unwrap();
let sd = preset.search_defaults().unwrap();
) -> Result<Self, CircuitsErrors> {
let (threshold_params, _) = build_pair_for_preset(preset).map_err(|e| {
CircuitsErrors::Sample(format!("Failed to build pair for preset: {:?}", e))
})?;
let sd = preset
.search_defaults()
.ok_or_else(|| CircuitsErrors::Sample("Preset has no search defaults".into()))?;
let mut rng = thread_rng();

let trbfv = TRBFV::new(committee.n, committee.threshold, threshold_params.clone())
.unwrap_or_else(|e| panic!("Failed to create TRBFV: {:?}", e));
.map_err(|e| CircuitsErrors::Sample(format!("Failed to create TRBFV: {:?}", e)))?;
let mut share_manager =
ShareManager::new(committee.n, committee.threshold, threshold_params.clone());

let parity_matrix =
compute_parity_matrix(threshold_params.moduli(), committee.n, committee.threshold)
.unwrap_or_else(|e| panic!("Failed to compute parity matrix: {}", e));
.map_err(|e| {
CircuitsErrors::Sample(format!("Failed to compute parity matrix: {:?}", e))
})?;

let (secret, secret_sss) = match dkg_input_type {
DkgInputType::SecretKey => {
let threshold_secret_key = SecretKey::random(&threshold_params, &mut rng);

let sk_poly = share_manager
.coeffs_to_poly_level0(threshold_secret_key.coeffs.clone().as_ref())
.unwrap();
.map_err(|e| {
CircuitsErrors::Sample(format!(
"Failed to convert secret key to poly: {:?}",
e
))
})?;

let sk_sss_u64 = share_manager
.generate_secret_shares_from_poly(sk_poly.clone(), rng)
.unwrap();
.map_err(|e| {
CircuitsErrors::Sample(format!("Failed to generate secret shares: {:?}", e))
})?;

let secret_sss: SecretShares = sk_sss_u64
.into_iter()
Expand All @@ -66,7 +79,9 @@ impl ShareComputationCircuitInput {
.collect();
let mut secret_crt =
CrtPolynomial::from_mod_q_polynomial(&sk_coeffs, threshold_params.moduli());
secret_crt.center(threshold_params.moduli()).unwrap();
secret_crt.center(threshold_params.moduli()).map_err(|e| {
CircuitsErrors::Sample(format!("Failed to center secret CRT: {:?}", e))
})?;

(secret_crt, secret_sss)
}
Expand Down Expand Up @@ -100,14 +115,14 @@ impl ShareComputationCircuitInput {
}
};

Self {
Ok(Self {
dkg_input_type,
n_parties: committee.n as u32,
threshold: committee.threshold as u32,
secret,
secret_sss,
parity_matrix,
}
})
}
}

Expand All @@ -125,7 +140,8 @@ mod tests {
BfvPreset::InsecureThreshold512,
committee.clone(),
DkgInputType::SecretKey,
);
)
.unwrap();
assert_eq!(sample.n_parties, committee.n as u32);
assert_eq!(sample.threshold, committee.threshold as u32);
assert_eq!(sample.dkg_input_type, DkgInputType::SecretKey);
Expand All @@ -140,7 +156,8 @@ mod tests {
BfvPreset::InsecureThreshold512,
committee.clone(),
DkgInputType::SmudgingNoise,
);
)
.unwrap();
assert_eq!(sample.n_parties, committee.n as u32);
assert_eq!(sample.threshold, committee.threshold as u32);
assert_eq!(sample.dkg_input_type, DkgInputType::SmudgingNoise);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use fhe_math::rq::Poly;
pub struct ShareEncryptionCircuit;

impl Circuit for ShareEncryptionCircuit {
const NAME: &'static str = "dkg-share-encryption";
const PREFIX: &'static str = "DKG_SHARE_ENCRYPTION";
const NAME: &'static str = "share-encryption";
const PREFIX: &'static str = "SHARE_ENCRYPTION";
const SUPPORTED_PARAMETER: ParameterType = ParameterType::DKG;
/// None: circuit accepts runtime-varying input type (SecretKey or SmudgingNoise).
const DKG_INPUT_TYPE: Option<DkgInputType> = None;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ mod tests {
DkgInputType::SecretKey,
sd.z,
sd.lambda,
);
)
.unwrap();
let artifacts = ShareEncryptionCircuit
.codegen(BfvPreset::InsecureThreshold512, &sample)
.unwrap();
Expand All @@ -225,7 +226,8 @@ mod tests {
DkgInputType::SecretKey,
sd.z,
sd.lambda,
);
)
.unwrap();

let artifacts = ShareEncryptionCircuit
.codegen(BfvPreset::InsecureThreshold512, &sample)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ impl Computation for Configs {
let q_mod_t = center(&BigInt::from(q_mod_t_uint), &t);
let q_mod_t_mod_p = reduce(&q_mod_t, &p);

let k0is = compute_k0is(&moduli, dkg_params.plaintext())?;
let k0is = compute_k0is(&moduli, plaintext)?;

let bounds = Bounds::compute(preset, input)?;
let bits = Bits::compute(preset, &bounds)?;

Ok(Configs {
t: dkg_params.plaintext() as usize,
t: plaintext as usize,
q_mod_t: q_mod_t_mod_p,
moduli,
k0is,
Expand Down Expand Up @@ -762,7 +762,8 @@ mod tests {
DkgInputType::SecretKey,
sd.z,
sd.lambda,
);
)
.unwrap();

let bounds = Bounds::compute(BfvPreset::InsecureThreshold512, &sample).unwrap();
let bits = Bits::compute(BfvPreset::InsecureThreshold512, &bounds).unwrap();
Expand All @@ -784,7 +785,8 @@ mod tests {
DkgInputType::SecretKey,
sd.z,
sd.lambda,
);
)
.unwrap();
let constants = Configs::compute(BfvPreset::InsecureThreshold512, &sample).unwrap();

let json = constants.to_json().unwrap();
Expand All @@ -808,7 +810,8 @@ mod tests {
DkgInputType::SecretKey,
sd.z,
sd.lambda,
);
)
.unwrap();
let witness = Witness::compute(BfvPreset::InsecureThreshold512, &sample).unwrap();

// witness.message is plaintext coefficients (reversed, as used in circuit)
Expand Down
Loading
Loading