Skip to content

Commit

Permalink
feat(zk): add conformance for zk proof and crs
Browse files Browse the repository at this point in the history
  • Loading branch information
nsarlin-zama committed Nov 4, 2024
1 parent 2fb66ce commit bf57764
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
6 changes: 5 additions & 1 deletion tfhe/src/shortint/ciphertext/zk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,11 @@ impl ParameterSetConformant for ProvenCompactCiphertextList {

let mut remaining_len = *total_expected_lwe_count;

for (compact_ct_list, _proof) in proved_lists {
for (compact_ct_list, proof) in proved_lists {
if !proof.is_conformant(&()) {
return false;
}

if remaining_len == 0 {
return false;
}
Expand Down
70 changes: 70 additions & 0 deletions tfhe/src/zk.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::core_crypto::commons::math::random::BoundedDistribution;
use crate::core_crypto::prelude::*;
use crate::named::Named;
use crate::prelude::ParameterSetConformant;
use crate::shortint::parameters::CompactPublicKeyEncryptionParameters;
use rand_core::RngCore;
use std::cmp::Ordering;
use std::collections::Bound;
Expand All @@ -16,6 +18,14 @@ impl Named for CompactPkeProof {
const NAME: &'static str = "zk::CompactPkeProof";
}

impl ParameterSetConformant for CompactPkeProof {
type ParameterSet = ();

fn is_conformant(&self, _parameter_set: &Self::ParameterSet) -> bool {
self.is_usable()
}
}

pub type CompactPkePublicParams = tfhe_zk_pok::proofs::pke::PublicParams<Curve>;
pub type SerializableCompactPkePublicParams =
tfhe_zk_pok::serialization::SerializablePKEv1PublicParams;
Expand All @@ -24,6 +34,66 @@ impl Named for CompactPkePublicParams {
const NAME: &'static str = "zk::CompactPkePublicParams";
}

pub struct CompactPkePublicParamsConformanceParams {
lwe_dim: LweDimension,
max_num_message: usize,
noise_bound: u64,
ciphertext_modulus: u64,
plaintext_modulus: u64,
msbs_zero_padding_bit_count: ZkMSBZeroPaddingBitCount,
}

impl CompactPkePublicParamsConformanceParams {
pub fn new<E, P: TryInto<CompactPublicKeyEncryptionParameters, Error = E>>(
value: P,
max_num_message: usize,
) -> Result<Self, crate::Error>
where
E: Into<crate::Error>,
{
let params: CompactPublicKeyEncryptionParameters =
value.try_into().map_err(|e| e.into())?;

let mut plaintext_modulus = (params.message_modulus.0 * params.carry_modulus.0) as u64;
// Add 1 bit of modulus for the padding bit
plaintext_modulus *= 2;

let (lwe_dim, max_num_message, noise_bound, ciphertext_modulus, plaintext_modulus) =
CompactPkeCrs::prepare_crs_parameters(
params.encryption_lwe_dimension,
max_num_message,
params.encryption_noise_distribution,
params.ciphertext_modulus,
plaintext_modulus,
)?;

Ok(Self {
lwe_dim,
max_num_message,
noise_bound,
ciphertext_modulus,
plaintext_modulus,
// CRS created from shortint params have 1 MSB 0bit
msbs_zero_padding_bit_count: ZkMSBZeroPaddingBitCount(1),
})
}
}

impl ParameterSetConformant for CompactPkePublicParams {
type ParameterSet = CompactPkePublicParamsConformanceParams;

fn is_conformant(&self, parameter_set: &Self::ParameterSet) -> bool {
self.k <= self.d
&& self.d == parameter_set.lwe_dim.0
&& self.k == parameter_set.max_num_message
&& self.b == parameter_set.noise_bound
&& self.q == parameter_set.ciphertext_modulus
&& self.t == parameter_set.plaintext_modulus
&& self.msbs_zero_padding_bit_count == parameter_set.msbs_zero_padding_bit_count.0
&& self.is_usable()
}
}

// If we call `CompactPkePublicParams::compress` we end up with a
// `SerializableCompactPkePublicParams` that should also impl Named to be serializable with
// `safe_serialization`. Since the `CompactPkePublicParams` is transformed into a
Expand Down

0 comments on commit bf57764

Please sign in to comment.