Browse Source

fit T64 into the 'Ring' trait, this is to use it in tfhe instead of using Tn<1> which is more tedious

main
arnaucube 1 week ago
parent
commit
4f89caef1e
4 changed files with 69 additions and 15 deletions
  1. +2
    -1
      arith/src/ring.rs
  2. +10
    -2
      arith/src/ring_torus.rs
  3. +42
    -11
      arith/src/torus.rs
  4. +15
    -1
      arith/src/tuple_ring.rs

+ 2
- 1
arith/src/ring.rs

@ -1,7 +1,7 @@
use rand::{distributions::Distribution, Rng};
use std::fmt::Debug;
use std::iter::Sum;
use std::ops::{Add, AddAssign, Mul, Sub, SubAssign};
use std::ops::{Add, AddAssign, Mul, Neg, Sub, SubAssign};
/// Represents a ring element. Currently implemented by ring_nq.rs#Rq and
/// ring_torus.rs#Tn. Is not a 'pure algebraic ring', but more a custom trait
@ -17,6 +17,7 @@ pub trait Ring:
+ Mul<Output = Self> // internal product
+ Mul<u64, Output = Self> // scalar mul, external product
+ Mul<Self::C, Output = Self>
+ Neg<Output = Self>
+ PartialEq
+ Debug
+ Clone

+ 10
- 2
arith/src/ring_torus.rs

@ -10,7 +10,7 @@
use rand::{distributions::Distribution, Rng};
use std::array;
use std::iter::Sum;
use std::ops::{Add, AddAssign, Mul, Sub, SubAssign};
use std::ops::{Add, AddAssign, Mul, Neg, Sub, SubAssign};
use crate::{ring::Ring, torus::T64, Rq, Zq};
@ -34,7 +34,7 @@ impl Ring for Tn {
}
fn rand(mut rng: impl Rng, dist: impl Distribution<f64>) -> Self {
Self(array::from_fn(|_| T64::rand_f64(&mut rng, &dist)))
Self(array::from_fn(|_| T64::rand(&mut rng, &dist)))
}
fn from_vec(coeffs: Vec<Self::C>) -> Self {
@ -152,6 +152,14 @@ impl SubAssign for Tn {
}
}
impl<const N: usize> Neg for Tn<N> {
type Output = Self;
fn neg(self) -> Self::Output {
Tn(array::from_fn(|i| -self.0[i]))
}
}
impl<const N: usize> PartialEq for Tn<N> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0

+ 42
- 11
arith/src/torus.rs

@ -4,38 +4,66 @@ use std::{
ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
};
use crate::ring::Ring;
/// Let 𝕋 = ℝ/ℤ, where 𝕋 is a ℤ-module, with homogeneous external product.
/// Let 𝕋q
/// T64 is 𝕋q with q=2^Ω, with Ω=64. We identify 𝕋q=(1/q)ℤ/ℤ ≈ ℤq.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct T64(pub u64);
impl T64 {
pub fn zero() -> Self {
Self(0u64)
// implement the `Ring` trait for T64, so that it can be used where we would use
// `Tn<1>`.
impl Ring for T64 {
type C = T64;
const Q: u64 = u64::MAX; // WIP
const N: usize = 1;
fn coeffs(&self) -> Vec<T64> {
vec![self.clone()]
}
pub fn rand(mut rng: impl Rng, dist: impl Distribution<u64>) -> Self {
let r: u64 = dist.sample(&mut rng);
Self(r)
fn zero() -> Self {
Self(0u64)
}
pub fn rand_f64(mut rng: impl Rng, dist: impl Distribution<f64>) -> Self {
fn rand(mut rng: impl Rng, dist: impl Distribution<f64>) -> Self {
let r: f64 = dist.sample(&mut rng);
Self(r.round() as u64)
}
fn from_vec(coeffs: Vec<Self::C>) -> Self {
assert_eq!(coeffs.len(), 1);
coeffs[0]
}
// TODO rm beta & l from inputs, make it always beta=2,l=64.
/// Note: only beta=2 and l=64 is supported.
pub fn decompose(&self, beta: u32, l: u32) -> Vec<Self> {
fn decompose(&self, beta: u32, l: u32) -> Vec<Self> {
assert_eq!(beta, 2u32); // only beta=2 supported
assert_eq!(l, 64u32); // only l=64 supported
// assert_eq!(l, 64u32); // only l=64 supported
(0..64)
// (0..64)
(0..l)
.rev()
.map(|i| T64(((self.0 >> i) & 1) as u64))
.collect()
}
pub fn mod_switch<const Q2: u64>(&self) -> T64 {
fn remodule<const P: u64>(&self) -> T64 {
todo!()
}
fn mod_switch<const Q2: u64>(&self) -> T64 {
todo!()
}
fn mul_div_round(&self, num: u64, den: u64) -> Self {
T64(((num as f64 * self.0 as f64) / den as f64).round() as u64)
}
}
impl T64 {
pub fn rand_u64(mut rng: impl Rng, dist: impl Distribution<u64>) -> Self {
let r: u64 = dist.sample(&mut rng);
Self(r)
}
}
impl Add<T64> for T64 {
@ -99,6 +127,9 @@ impl Mul for T64 {
type Output = Self;
fn mul(self, s: u64) -> Self {
if self.0 == 0 {
return Self(0);
}
Self(self.0.wrapping_mul(s))
}
}

+ 15
- 1
arith/src/tuple_ring.rs

@ -8,7 +8,7 @@ use rand_distr::{Normal, Uniform};
use std::iter::Sum;
use std::{
array,
ops::{Add, Mul, Sub},
ops::{Add, Mul, Neg, Sub},
};
use crate::Ring;
@ -37,6 +37,12 @@ impl TR {
}
}
impl<R: Ring, const K: usize> TR<R, K> {
pub fn iter(&self) -> std::slice::Iter<R> {
self.0.iter()
}
}
impl<R: Ring, const K: usize> Add<TR<R, K>> for TR<R, K> {
type Output = Self;
fn add(self, other: Self) -> Self {
@ -55,6 +61,14 @@ impl Sub> for TR {
}
}
impl<R: Ring, const K: usize> Neg for TR<R, K> {
type Output = Self;
fn neg(self) -> Self::Output {
Self(self.0.iter().map(|&e_i| -e_i).collect())
}
}
/// for (TR,TR), the Mul operation is defined as the dot product:
/// for A, B \in R^k, result = Σ A_i * B_i \in R
impl<R: Ring, const K: usize> Mul<TR<R, K>> for TR<R, K> {

Loading…
Cancel
Save