mirror of
https://github.com/arnaucube/phantom-zone.git
synced 2026-01-11 16:41:29 +01:00
add more comments
This commit is contained in:
@@ -5,7 +5,7 @@ use crate::{
|
||||
pbs::WithShoupRepr,
|
||||
random::{NewWithSeed, RandomFillUniformInModulus},
|
||||
utils::ToShoup,
|
||||
Matrix, MatrixEntity, MatrixMut, RowEntity, RowMut, SizeInBitsWithLogModulus,
|
||||
Matrix, MatrixEntity, MatrixMut, RowEntity, RowMut,
|
||||
};
|
||||
|
||||
use super::parameters::{BoolParameters, CiphertextModulus};
|
||||
@@ -319,8 +319,8 @@ pub struct CommonReferenceSeededInteractiveMultiPartyServerKeyShare<M: Matrix, P
|
||||
/// is not the leader RGSW ciphertext is encrypted using RGSW1
|
||||
/// decomposer for RGSW0 x RGSW1
|
||||
not_self_leader_rgsws: Vec<M>,
|
||||
/// Auto key shares for auto elements [g^{-1}, g, g^2, .., g^{w}] where `w`
|
||||
/// is the window size parameter. Share corresponding to auto element g^{-1}
|
||||
/// Auto key shares for auto elements [-g, g, g^2, .., g^{w}] where `w`
|
||||
/// is the window size parameter. Share corresponding to auto element -g
|
||||
/// is stored at key `0` and share corresponding to auto element g^{k} is
|
||||
/// stored at key `k`.
|
||||
auto_keys: HashMap<usize, M>,
|
||||
@@ -393,8 +393,8 @@ pub struct SeededInteractiveMultiPartyServerKey<M: Matrix, S, P> {
|
||||
/// where `s` is ideal LWE secret key for each LWE secret dimension.
|
||||
rgsw_cts: Vec<M>,
|
||||
/// Seeded auto keys under ideal RLWE secret for RLWE automorphisms with
|
||||
/// auto elements [g^-1, g, g^2,..., g^{w}]. Auto key corresponidng to
|
||||
/// auto element g^{-1} is stored at key `0` and key corresponding to auto
|
||||
/// auto elements [-g, g, g^2,..., g^{w}]. Auto key corresponidng to
|
||||
/// auto element -g is stored at key `0` and key corresponding to auto
|
||||
/// element g^{k} is stored at key `k`
|
||||
auto_keys: HashMap<usize, M>,
|
||||
/// Seeded LWE key switching key under ideal LWE secret to switch LWE_{q,
|
||||
@@ -484,7 +484,7 @@ impl<M: Matrix, S> SeededSinglePartyServerKey<M, BoolParameters<M::MatElement>,
|
||||
pub(crate) struct ServerKeyEvaluationDomain<M, P, R, N> {
|
||||
/// RGSW ciphertext RGSW(X^{s[i]}) for each LWE index in evaluation domain
|
||||
rgsw_cts: Vec<M>,
|
||||
/// Auto keys for all auto elements [g^{-1}, g, g^2,..., g^w] in evaluation
|
||||
/// Auto keys for all auto elements [-g, g, g^2,..., g^w] in evaluation
|
||||
/// domain
|
||||
galois_keys: HashMap<usize, M>,
|
||||
/// LWE key switching key to key switch LWE_{q, s}(m) to LWE_{q, z}(m)
|
||||
@@ -801,7 +801,7 @@ pub(crate) struct NonInteractiveServerKeyEvaluationDomain<M, P, R, N> {
|
||||
/// RGSW ciphertexts RGSW(X^{s[i]}) under ideal RLWE secret key in
|
||||
/// evaluation domain
|
||||
rgsw_cts: Vec<M>,
|
||||
/// Auto keys for all auto elements [g^{-1}, g, g^2, g^w] in evaluation
|
||||
/// Auto keys for all auto elements [-g, g, g^2, g^w] in evaluation
|
||||
/// domain
|
||||
auto_keys: HashMap<usize, M>,
|
||||
/// LWE key switching key to key switch LWE_{q, s}(m) to LWE_{q, z}(m)
|
||||
@@ -1010,7 +1010,7 @@ pub struct SeededNonInteractiveMultiPartyServerKey<M: Matrix, S, P> {
|
||||
ui_to_s_ksks: Vec<M>,
|
||||
/// RGSW ciphertexts RGSW(X^{s[i]}) under ideal RLWE secret key
|
||||
rgsw_cts: Vec<M>,
|
||||
/// Auto keys for all auto elements [g^{-1}, g, g^2, g^w]
|
||||
/// Auto keys for all auto elements [-g, g, g^2, g^w]
|
||||
auto_keys: HashMap<usize, M>,
|
||||
/// LWE key switching key to key switch LWE_{q, s}(m) to LWE_{q, z}(m)
|
||||
lwe_ksk: M::R,
|
||||
@@ -1244,8 +1244,8 @@ pub struct CommonReferenceSeededNonInteractiveMultiPartyServerKeyShare<M: Matrix
|
||||
/// it is stored at index l - 1, where j is self's user_id
|
||||
ksk_zero_encs_for_others: Vec<M>,
|
||||
|
||||
/// RLWE auto key shares for auto elements [g^{-1}, g, g^2, g^{w}] where `w`
|
||||
/// is the window size. Auto key share corresponding to auto element g^{-1}
|
||||
/// RLWE auto key shares for auto elements [-g, g, g^2, g^{w}] where `w`
|
||||
/// is the window size. Auto key share corresponding to auto element -g
|
||||
/// is stored at key 0 and key share corresponding to auto element g^{k} is
|
||||
/// stored at key `k`
|
||||
auto_keys_share: HashMap<usize, M>,
|
||||
@@ -1400,7 +1400,7 @@ impl<M> WithShoupRepr for NormalAndShoup<M> {
|
||||
pub(crate) mod key_size {
|
||||
use num_traits::{FromPrimitive, PrimInt};
|
||||
|
||||
use crate::{backend::Modulus, decomposer::NumInfo};
|
||||
use crate::{backend::Modulus, decomposer::NumInfo, SizeInBitsWithLogModulus};
|
||||
|
||||
use super::*;
|
||||
|
||||
|
||||
@@ -38,10 +38,13 @@ mod common_mp_enc_dec {
|
||||
impl<E> MultiPartyDecryptor<bool, <Mat as Matrix>::R> for super::keys::ClientKey<[u8; 32], E> {
|
||||
type DecryptionShare = <Mat as Matrix>::MatElement;
|
||||
|
||||
/// Generate multi-party decryption share for LWE ciphertext `c`
|
||||
fn gen_decryption_share(&self, c: &<Mat as Matrix>::R) -> Self::DecryptionShare {
|
||||
BoolEvaluator::with_local(|e| e.multi_party_decryption_share(c, self))
|
||||
}
|
||||
|
||||
/// Aggregate mult-party decryptions shares of all parties, decrypt LWE
|
||||
/// ciphertext `c`, and return the bool plaintext
|
||||
fn aggregate_decryption_shares(
|
||||
&self,
|
||||
c: &<Mat as Matrix>::R,
|
||||
@@ -52,6 +55,8 @@ mod common_mp_enc_dec {
|
||||
}
|
||||
|
||||
impl SampleExtractor<<Mat as Matrix>::R> for Mat {
|
||||
/// Sample extract coefficient at `index` as a LWE ciphertext from RLWE
|
||||
/// ciphertext `Self`
|
||||
fn extract(&self, index: usize) -> <Mat as Matrix>::R {
|
||||
// input is RLWE ciphertext
|
||||
assert!(self.dimension().0 == 2);
|
||||
|
||||
@@ -167,9 +167,26 @@ impl Global for RuntimeServerKey {
|
||||
}
|
||||
}
|
||||
|
||||
/// Batch of bool ciphertexts stored as vector of RLWE ciphertext under user j's
|
||||
/// RLWE secret `u_j`
|
||||
///
|
||||
/// To use the bool ciphertexts in multi-party protocol first key switch the
|
||||
/// ciphertexts from u_j to ideal RLWE secret `s` with
|
||||
/// `self.key_switch(user_id)` where `user_id` is user j's id. Key switch
|
||||
/// returns `BatchedFheBools` that stored key vector of key switched RLWE
|
||||
/// ciphertext.
|
||||
pub(super) struct NonInteractiveBatchedFheBools<C> {
|
||||
data: Vec<C>,
|
||||
}
|
||||
|
||||
/// Batch of Bool cipphertexts stored as vector of RLWE ciphertexts under the
|
||||
/// ideal RLWE secret key `s` of the protocol
|
||||
///
|
||||
/// Bool ciphertext at `index` can be extracted from the coefficient at `index %
|
||||
/// N` of `index / N`th RLWE ciphertext.
|
||||
///
|
||||
/// To extract bool ciphertext at `index` as LWE ciphertext use
|
||||
/// `self.extract(index)`
|
||||
pub(super) struct BatchedFheBools<C> {
|
||||
pub(in super::super) data: Vec<C>,
|
||||
}
|
||||
@@ -191,6 +208,8 @@ mod impl_enc_dec {
|
||||
|
||||
type Mat = Vec<Vec<u64>>;
|
||||
|
||||
// Implement `extract` to extract Bool LWE ciphertext at `index` from
|
||||
// `BatchedFheBools`
|
||||
impl<C: MatrixMut<MatElement = u64>> BatchedFheBools<C>
|
||||
where
|
||||
C::R: RowEntity + RowMut,
|
||||
@@ -217,6 +236,11 @@ mod impl_enc_dec {
|
||||
where
|
||||
<M as Matrix>::R: RowMut,
|
||||
{
|
||||
/// Derive `NonInteractiveBatchedFheBools` from a vector seeded RLWE
|
||||
/// ciphertexts (Vec<RLWE>, Seed)
|
||||
///
|
||||
/// Unseed the RLWE ciphertexts and store them as vector RLWE
|
||||
/// ciphertexts in `NonInteractiveBatchedFheBools`
|
||||
fn from(value: &(Vec<M::R>, [u8; 32])) -> Self {
|
||||
BoolEvaluator::with_local(|e| {
|
||||
let parameters = e.parameters();
|
||||
@@ -252,17 +276,21 @@ mod impl_enc_dec {
|
||||
where
|
||||
K: Encryptor<[bool], (Mat, [u8; 32])>,
|
||||
{
|
||||
/// Encrypt a vector bool of arbitrary length as vector of unseeded RLWE
|
||||
/// ciphertexts in `NonInteractiveBatchedFheBools`
|
||||
fn encrypt(&self, m: &[bool]) -> NonInteractiveBatchedFheBools<Mat> {
|
||||
NonInteractiveBatchedFheBools::from(&K::encrypt(&self, m))
|
||||
}
|
||||
}
|
||||
|
||||
impl<K> Encryptor<[bool], (Mat, [u8; 32])> for K
|
||||
impl<K> Encryptor<[bool], (Vec<<Mat as Matrix>::R>, [u8; 32])> for K
|
||||
where
|
||||
K: NonInteractiveMultiPartyClientKey,
|
||||
<Mat as Matrix>::R:
|
||||
TryConvertFrom1<[K::Element], CiphertextModulus<<Mat as Matrix>::MatElement>>,
|
||||
{
|
||||
/// Encrypt a vector of bool of arbitrary length as vector of seeded
|
||||
/// RLWE ciphertexts and returns (Vec<RLWE>, Seed)
|
||||
fn encrypt(&self, m: &[bool]) -> (Mat, [u8; 32]) {
|
||||
BoolEvaluator::with_local(|e| {
|
||||
DefaultSecureRng::with_local_mut(|rng| {
|
||||
@@ -319,8 +347,13 @@ mod impl_enc_dec {
|
||||
}
|
||||
|
||||
impl KeySwitchWithId<Mat> for Mat {
|
||||
/// Key switch RLWE ciphertext `Self` from user j's RLWE secret u_j
|
||||
/// to ideal RLWE secret `s` of non-interactive multi-party protocol.
|
||||
///
|
||||
/// - user_id: user j's user_id in the protocol
|
||||
fn key_switch(&self, user_id: usize) -> Mat {
|
||||
BoolEvaluator::with_local(|e| {
|
||||
assert!(self.dimension() == (2, e.parameters().rlwe_n().0));
|
||||
let server_key = BOOL_SERVER_KEY.get().unwrap();
|
||||
let ksk = server_key.ui_to_s_ksk(user_id);
|
||||
let decomposer = e.ni_ui_to_s_ks_decomposer().as_ref().unwrap();
|
||||
@@ -342,6 +375,14 @@ mod impl_enc_dec {
|
||||
where
|
||||
C: KeySwitchWithId<C>,
|
||||
{
|
||||
/// Key switch `Self`'s vector of RLWE ciphertexts from user j's RLWE
|
||||
/// secret u_j to ideal RLWE secret `s` of non-interactive
|
||||
/// multi-party protocol.
|
||||
///
|
||||
/// Returns vector of key switched RLWE ciphertext as `BatchedFheBools`
|
||||
/// which can then be used to extract individual Bool LWE ciphertexts.
|
||||
///
|
||||
/// - user_id: user j's user_id in the protocol
|
||||
fn key_switch(&self, user_id: usize) -> BatchedFheBools<C> {
|
||||
let data = self
|
||||
.data
|
||||
@@ -355,24 +396,15 @@ mod impl_enc_dec {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use itertools::{izip, Itertools};
|
||||
use num_traits::{FromPrimitive, PrimInt, ToPrimitive, Zero};
|
||||
use rand::{thread_rng, Rng, RngCore};
|
||||
use itertools::Itertools;
|
||||
use rand::{thread_rng, RngCore};
|
||||
|
||||
use crate::{
|
||||
backend::{GetModulus, Modulus},
|
||||
bool::{
|
||||
evaluator::BooleanGates,
|
||||
keys::{
|
||||
tests::{ideal_sk_rlwe, measure_noise_lwe},
|
||||
SinglePartyClientKey,
|
||||
},
|
||||
keys::tests::{ideal_sk_rlwe, measure_noise_lwe},
|
||||
},
|
||||
lwe::decrypt_lwe,
|
||||
rgsw::decrypt_rlwe,
|
||||
utils::{tests::Stats, TryConvertFrom1},
|
||||
ArithmeticOps, Encoder, Encryptor, KeySwitchWithId, ModInit, MultiPartyDecryptor, NttInit,
|
||||
Row, VectorOps,
|
||||
Encoder, Encryptor, KeySwitchWithId, MultiPartyDecryptor,
|
||||
};
|
||||
|
||||
use super::*;
|
||||
|
||||
@@ -144,7 +144,7 @@ pub struct BoolParameters<El> {
|
||||
/// and must be supplied only for non-interactive multi-party
|
||||
non_interactive_ui_to_s_key_switch_decomposer:
|
||||
Option<(DecompostionLogBase, DecompositionCount)>,
|
||||
/// Group generator for Z^*_{2N}
|
||||
/// Group generator for Z^*_{br_q}
|
||||
g: usize,
|
||||
/// Window size parameter for LMKC++ blind rotation
|
||||
w: usize,
|
||||
@@ -353,14 +353,14 @@ impl<El> BoolParameters<El> {
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
pub(crate) struct DecompostionLogBase(pub(crate) usize);
|
||||
pub struct DecompostionLogBase(pub(crate) usize);
|
||||
impl AsRef<usize> for DecompostionLogBase {
|
||||
fn as_ref(&self) -> &usize {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
pub(crate) struct DecompositionCount(pub(crate) usize);
|
||||
pub struct DecompositionCount(pub(crate) usize);
|
||||
impl AsRef<usize> for DecompositionCount {
|
||||
fn as_ref(&self) -> &usize {
|
||||
&self.0
|
||||
|
||||
@@ -393,7 +393,7 @@ mod tests {
|
||||
.map(|(index, k)| gen_mp_keys_phase2(k, index, parties, &pk))
|
||||
.collect_vec();
|
||||
|
||||
println!("Size: {}", server_key_shares[0].size());
|
||||
// println!("Size: {}", server_key_shares[0].size());
|
||||
let seeded_server_key = aggregate_server_key_shares(&server_key_shares);
|
||||
let server_key_eval =
|
||||
ServerKeyEvaluationDomain::<_, _, DefaultSecureRng, NttBackendU64>::from(
|
||||
@@ -452,7 +452,7 @@ mod tests {
|
||||
.enumerate()
|
||||
.map(|(user_id, k)| gen_server_key_share(user_id, parties, k))
|
||||
.collect_vec();
|
||||
println!("Size: {}", server_key_shares[0].size());
|
||||
|
||||
let server_key = aggregate_server_key_shares(&server_key_shares);
|
||||
|
||||
let server_key_eval =
|
||||
@@ -469,6 +469,10 @@ mod tests {
|
||||
_,
|
||||
>(parameters, &cks, &server_key_eval);
|
||||
|
||||
println!(
|
||||
"Common reference seeded server key share key size size: {} Bits",
|
||||
server_key_shares[0].size()
|
||||
);
|
||||
println!(
|
||||
"Rgsw nsm std log2 {}",
|
||||
server_key_stats.brk_rgsw_cts.0.std_dev().abs().log2()
|
||||
|
||||
Reference in New Issue
Block a user