arnaucube
arnaucube/phantom-zonePublic Self-hosted repository
Use modulus as parameter to sample
Janmajaya Mall janmajayamall@protonmail.com
Sun, 19 May 2024 10:59:50 +0530
commit

eee47919ce761588d23bb679dfbee62e0d77ea96

parent

59820e9736979c51777e835536afd67558167509

3 files changed, 85 insertions(+), 15 deletions(-)

jump to
M src/backend.rs → src/backend.rs

@@ -1,4 +1,23 @@

+use std::marker::PhantomData; + use itertools::izip; +use num_traits::{WrappingAdd, WrappingMul, WrappingSub}; + +pub trait Modulus { + type Element; + fn is_native() -> bool; + /// -1 in signed representaiton + fn neg_one(&self) -> Self::Element; + /// Largest unsigned value that fits in the modulus. That is, q - 1. + fn largest_unsigned_value(&self) -> Self::Element; + /// Smallest unsigned value that fits in the modulus + /// Always assmed to be 0. + fn smallest_unsigned_value(&self) -> Self::Element; + /// Max +value in signed representation + fn signed_max(&self) -> Self::Element; + /// Min -value in signed representation + fn signed_min(&self) -> Self::Element; +} pub trait ModInit { type Element;

@@ -205,3 +224,27 @@ fn modulus(&self) -> Self::Element {

self.q } } + +pub struct WordSizeModulus<T> { + _phantom: PhantomData<T>, +} + +impl<T> ModInit for WordSizeModulus<T> { + type Element = T; + fn new<M>(q: M) -> Self { + // For now assume ModulusOpsU64 is only used for u64 + Self { + _phantom: PhantomData, + } + } +} + +// impl<T: WrappingAdd + WrappingSub + WrappingMul> ArithmeticOps for +// WordSizeModulus<T> { fn add(&self, a: &Self::Element, b: &Self::Element) +// -> Self::Element { T::wrapping_add(*a, *b) +// } + +// fn modulus(&self) -> Self::Element { + +// } +// }
M src/bool/parameters.rs → src/bool/parameters.rs

@@ -2,9 +2,9 @@ use crate::decomposer::Decomposer;

#[derive(Clone, PartialEq)] pub(super) struct BoolParameters<El> { - rlwe_q: Modulus<El>, - lwe_q: Modulus<El>, - br_q: Modulus<El>, + rlwe_q: CiphertextModulus<El>, + lwe_q: CiphertextModulus<El>, + br_q: CiphertextModulus<El>, rlwe_n: PolynomialSize, lwe_n: LweDimension, lwe_decomposer_base: DecompostionLogBase,

@@ -22,15 +22,15 @@ w: usize,

} impl<El> BoolParameters<El> { - pub(crate) fn rlwe_q(&self) -> &Modulus<El> { + pub(crate) fn rlwe_q(&self) -> &CiphertextModulus<El> { &self.rlwe_q } - pub(crate) fn lwe_q(&self) -> &Modulus<El> { + pub(crate) fn lwe_q(&self) -> &CiphertextModulus<El> { &self.lwe_q } - pub(crate) fn br_q(&self) -> &Modulus<El> { + pub(crate) fn br_q(&self) -> &CiphertextModulus<El> { &self.br_q }

@@ -164,12 +164,12 @@ pub(crate) struct LweDimension(pub(crate) usize);

#[derive(Clone, Copy, PartialEq)] pub(crate) struct PolynomialSize(pub(crate) usize); #[derive(Clone, Copy, PartialEq)] -pub(crate) struct Modulus<T>(pub(crate) T); +pub(crate) struct CiphertextModulus<T>(T); pub(super) const SP_BOOL_PARAMS: BoolParameters<u64> = BoolParameters::<u64> { - rlwe_q: Modulus(268369921u64), - lwe_q: Modulus(1 << 16), - br_q: Modulus(1 << 10), + rlwe_q: CiphertextModulus(268369921u64), + lwe_q: CiphertextModulus(1 << 16), + br_q: CiphertextModulus(1 << 10), rlwe_n: PolynomialSize(1 << 10), lwe_n: LweDimension(493), lwe_decomposer_base: DecompostionLogBase(4),

@@ -185,9 +185,9 @@ w: 1,

}; pub(super) const MP_BOOL_PARAMS: BoolParameters<u64> = BoolParameters::<u64> { - rlwe_q: Modulus(1152921504606830593), - lwe_q: Modulus(1 << 20), - br_q: Modulus(1 << 11), + rlwe_q: CiphertextModulus(1152921504606830593), + lwe_q: CiphertextModulus(1 << 20), + br_q: CiphertextModulus(1 << 11), rlwe_n: PolynomialSize(1 << 11), lwe_n: LweDimension(500), lwe_decomposer_base: DecompostionLogBase(4),
M src/random.rs → src/random.rs

@@ -1,11 +1,12 @@

use std::cell::RefCell; use itertools::izip; +use num_traits::PrimInt; use rand::{distributions::Uniform, thread_rng, CryptoRng, Rng, RngCore, SeedableRng}; use rand_chacha::ChaCha8Rng; -use rand_distr::Distribution; +use rand_distr::{uniform::SampleUniform, Distribution}; -use crate::utils::WithLocal; +use crate::{backend::Modulus, utils::WithLocal}; thread_local! { pub(crate) static DEFAULT_RNG: RefCell<DefaultSecureRng> = RefCell::new(DefaultSecureRng::new_seeded([0u8;32]));

@@ -30,6 +31,13 @@ M: ?Sized,

{ type Parameters: ?Sized; fn random_fill(&mut self, parameters: &Self::Parameters, container: &mut M); +} + +pub trait RandomUniformDist1<M, P> +where + M: ?Sized, +{ + fn random_fill(&mut self, modulus: &P, container: &mut M); } pub(crate) struct DefaultSecureRng {

@@ -78,6 +86,25 @@ type Parameters = u32;

fn random_fill(&mut self, parameters: &Self::Parameters, container: &mut [u32]) { izip!( (&mut self.rng).sample_iter(Uniform::new(0, parameters)), + container.iter_mut() + ) + .for_each(|(from, to)| { + *to = from; + }); + } +} + +impl<T, C> RandomUniformDist1<[T], C> for DefaultSecureRng +where + T: PrimInt + SampleUniform, + C: Modulus<Element = T>, +{ + fn random_fill(&mut self, modulus: &C, container: &mut [T]) { + izip!( + (&mut self.rng).sample_iter(Uniform::new_inclusive( + T::zero(), + modulus.largest_unsigned_value() + )), container.iter_mut() ) .for_each(|(from, to)| {