Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rewrite] More noise stats #37

Merged
merged 25 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
26084d6
chore: restrucutre mod a bit
han0110 Aug 29, 2024
f4e4311
feat: add noise stats also for `ks_key` and `ak`
han0110 Aug 29, 2024
b6c3c0f
tmp: test with empirical std dev
han0110 Aug 29, 2024
7d429c2
chore
han0110 Aug 30, 2024
55a81af
feat: check noise stats with formula
han0110 Aug 31, 2024
656eaf0
refactor: move expected std dev calculation into `LmkcdeyNoiseAnalysi…
han0110 Sep 2, 2024
95574b2
fix: use expected noise std dev from sage script
han0110 Sep 3, 2024
0a069c3
fix: ensure prime bits up to 62
han0110 Sep 5, 2024
18b3bab
ci: rename target `wasm32-wasi` to `wasm32-wasip1` for future compati…
han0110 Sep 6, 2024
563584b
refactor: simplify `Modulus` enum
han0110 Sep 10, 2024
e1762c0
feat: allow implement boolean gate with arbitrary fan-in
han0110 Sep 9, 2024
017ee86
chore
han0110 Sep 10, 2024
eda828d
chore: make api more friendly
han0110 Sep 11, 2024
abf724a
chore: revert `FhewBoolGate`
han0110 Sep 11, 2024
747eac1
chore: remove unnecessary serde
han0110 Sep 12, 2024
09cd9a8
fix: use signed range for testing
han0110 Sep 12, 2024
effd40c
feat: recognize zero limbs
han0110 Sep 12, 2024
c009be2
feat: read env var `PZ_STATS_TARGET_SAMPLE_SIZE` and `PZ_STATS_TIMEOU…
han0110 Sep 12, 2024
05fdb9c
refactor
han0110 Sep 12, 2024
7d5775f
feat: add test `bootstrap_three_way`
han0110 Sep 12, 2024
ce562df
feat: allow env `PZ_TIME_CONSUMING_TEST_REPETITION` to control number…
han0110 Sep 12, 2024
082e21d
fix: use `NoisyNativeRing` for `bootstrap_three_way`
han0110 Sep 12, 2024
4a4850e
fix: remove unused `impl DistributionVariance`
han0110 Sep 13, 2024
9c43715
refactor: update commewnt
han0110 Sep 13, 2024
3afd1c0
chore
han0110 Sep 14, 2024
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
6 changes: 5 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
matrix:
target:
- wasm32-unknown-unknown
- wasm32-wasi
- wasm32-wasip1
steps:
- uses: actions/checkout@v4

Expand Down Expand Up @@ -50,6 +50,10 @@ jobs:

- name: Run test
run: cargo test --profile ci --workspace --all-features
env:
PZ_TIME_CONSUMING_TEST_REPETITION: 10
PZ_STATS_SAMPLE_SIZE: 1000000
PZ_STATS_TIMEOUT: 30

lint:
name: Lint
Expand Down
72 changes: 47 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rand_distr = { version = "0.4.3", default-features = false }
rustfft = "6.2.0"
serde = "1.0"
serde_bytes = "0.11"
unroll = "0.1.5"

# dev-dependencies
bincode = "1.3.3"
Expand Down
2 changes: 1 addition & 1 deletion crypto/benches/rlwe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn automorphism(c: &mut Criterion) {
RlweAutoKey::allocate_eval(ring.ring_size(), ring.eval_size(), decomposition_param, 5);
let mut ct = RlweCiphertext::allocate(ring.ring_size());
auto_key
.as_ks_key_mut()
.ks_key_mut()
.ct_iter_mut()
.for_each(|mut ct| ring.sample_uniform_into(ct.as_mut(), &mut rng));
ring.sample_uniform_into(ct.as_mut(), &mut rng);
Expand Down
51 changes: 49 additions & 2 deletions crypto/src/core/lwe/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ use crate::{
},
};
use phantom_zone_math::{
decomposer::DecompositionParam,
decomposer::{Decomposer, DecompositionParam},
distribution::Gaussian,
modulus::{Modulus, ModulusOps, Native, NonNativePowerOfTwo, Prime},
izip_eq,
modulus::{ElemFrom, Modulus, ModulusOps, Native, NonNativePowerOfTwo, Prime},
ring::{NativeRing, NonNativePowerOfTwoRing, PrimeRing},
};
use rand::{RngCore, SeedableRng};
Expand Down Expand Up @@ -145,6 +146,52 @@ impl<M: ModulusOps> Lwe<M> {
.slice_add_assign(ct_c.as_mut(), ct_b.as_ref());
ct_c
}

pub fn scalar_fma<'a, T>(
&self,
cts: impl IntoIterator<Item = &'a LweCiphertextOwned<M::Elem>>,
scalars: impl IntoIterator<Item = T>,
) -> LweCiphertextOwned<M::Elem>
where
M: ElemFrom<T>,
{
let modulus = self.modulus();
let mut ct_lc = LweCiphertext::allocate(self.dimension());
izip_eq!(cts, scalars).for_each(|(ct, scalar)| {
modulus.slice_scalar_fma(ct_lc.as_mut(), ct.as_ref(), &modulus.elem_from(scalar))
});
ct_lc
}

pub fn noise(
&self,
sk: &LweSecretKeyOwned<i32>,
pt: &LwePlaintext<M::Elem>,
ct: &LweCiphertextOwned<M::Elem>,
) -> i64 {
let pt_noisy = self.decrypt(sk, ct);
self.modulus.to_i64(self.modulus.sub(&pt_noisy.0, &pt.0))
}

pub fn ks_key_noise(
&self,
sk_from: &LweSecretKeyOwned<i32>,
sk_to: &LweSecretKeyOwned<i32>,
ks_key: &LweKeySwitchKeyOwned<M::Elem>,
) -> Vec<Vec<i64>> {
let modulus = self.modulus();
let decomposer = M::Decomposer::new(modulus.modulus(), ks_key.decomposition_param());
izip_eq!(ks_key.cts_iter(), sk_from.as_ref())
.map(|(ks_key_i, sk_from_i)| {
izip_eq!(ks_key_i.iter(), decomposer.gadget_iter())
.map(|(ks_key_i_j, beta_j)| {
let pt = LwePlaintext(modulus.mul_elem_from(&beta_j, &-sk_from_i));
self.noise(sk_to, &pt, &ks_key_i_j.cloned())
})
.collect()
})
.collect()
}
}

pub fn test_param(ciphertext_modulus: impl Into<Modulus>) -> LweParam {
Expand Down
16 changes: 8 additions & 8 deletions crypto/src/core/rlwe/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn pk_gen<'a, 'b, R, T>(
{
sk_encrypt_zero(
ring,
pk.into().as_ct_mut(),
pk.into().ct_mut(),
sk,
noise_distribution,
scratch,
Expand Down Expand Up @@ -296,8 +296,8 @@ pub fn prepare_auto_key<'a, 'b, R: RingOps>(
) {
prepare_ks_key(
ring,
auto_key_prep.into().as_ks_key_mut(),
auto_key.into().as_ks_key(),
auto_key_prep.into().ks_key_mut(),
auto_key.into().ks_key(),
scratch,
);
}
Expand Down Expand Up @@ -410,7 +410,7 @@ pub fn seeded_pk_gen<'a, 'b, R, T>(
{
let mut t = RlwePublicKey::scratch(ring.ring_size(), ring.ring_size(), &mut scratch);
pk_gen(ring, &mut t, sk, noise_distribution, scratch, rng);
pk.into().as_ct_mut().b_mut().copy_from_slice(t.b());
pk.into().ct_mut().b_mut().copy_from_slice(t.b());
}

fn seeded_ks_key_gen_inner<R, T>(
Expand Down Expand Up @@ -448,7 +448,7 @@ pub fn seeded_auto_key_gen<'a, 'b, R, T>(
{
let (mut auto_key_seeded, sk) = (auto_key_seeded.into(), sk.into());
let auto_map = AutomorphismMap::new(ring.ring_size(), auto_key_seeded.k());
let ks_key = auto_key_seeded.as_ks_key_mut();
let ks_key = auto_key_seeded.ks_key_mut();
let sk_auto = scratch.copy_iter(auto_map.apply(sk.as_ref(), |&v| -v));
seeded_ks_key_gen_inner(ring, ks_key, sk_auto, sk, noise_distribution, scratch, rng);
}
Expand All @@ -470,7 +470,7 @@ pub fn unseed_pk<'a, 'b, R: RingOps>(
pk_seeded: impl Into<SeededRlwePublicKeyView<'b, R::Elem>>,
rng: &mut LweRng<(), impl RngCore>,
) {
unseed_ct(ring, pk.into().as_ct_mut(), pk_seeded.into().as_ct(), rng);
unseed_ct(ring, pk.into().ct_mut(), pk_seeded.into().ct(), rng);
}

pub fn unseed_ks_key<'a, 'b, R: RingOps>(
Expand All @@ -491,8 +491,8 @@ pub fn unseed_auto_key<'a, 'b, R: RingOps>(
) {
unseed_ks_key(
ring,
auto_key.into().as_ks_key_mut(),
auto_key_seeded.into().as_ks_key(),
auto_key.into().ks_key_mut(),
auto_key_seeded.into().ks_key(),
rng,
)
}
Loading