Skip to content

Commit

Permalink
remove Uint
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasgeihs committed Feb 4, 2024
1 parent 7f5a800 commit fbfd7b7
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 35 deletions.
6 changes: 3 additions & 3 deletions src/ciphertext/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use rayon::{join, prelude::*};

use crate::{
ciphertext::{logic::if_then_else_zero, FheAsciiChar, Uint},
ciphertext::{logic::if_then_else_zero, FheAsciiChar},
server_key::ServerKey,
};

Expand Down Expand Up @@ -58,7 +58,7 @@ impl FheString {
.into_par_iter()
.map(|i| {
let index_add_blen = k.k.add_parallelized(index, &b_len);
k.k.scalar_gt_parallelized(&index_add_blen, i as Uint)
k.k.scalar_gt_parallelized(&index_add_blen, i as u64)
})
.collect::<Vec<_>>()
},
Expand Down Expand Up @@ -107,7 +107,7 @@ impl FheString {
// v[i] = i < index ? a[i] : (i < index + b.len ? b[i - index] : a[i - b.len])

// c0 = i < index
let c0 = k.k.scalar_gt_parallelized(index, i as Uint);
let c0 = k.k.scalar_gt_parallelized(index, i as u64);

// c1 = a[i]
let c1 = &a.0[i % a.0.len()].0;
Expand Down
4 changes: 2 additions & 2 deletions src/ciphertext/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tfhe::integer::{

use crate::server_key::ServerKey;

use super::{FheUsize, Uint};
use super::FheUsize;

// Returns `a ? b : 0`.
pub fn if_then_else_zero<T: IntegerRadixCiphertext>(k: &ServerKey, a: &BooleanBlock, b: &T) -> T {
Expand Down Expand Up @@ -60,6 +60,6 @@ pub fn all(k: &ServerKey, v: &[BooleanBlock]) -> BooleanBlock {
let sum = k.k.unchecked_sum_ciphertexts_vec_parallelized(v);
match sum {
None => k.k.create_trivial_boolean_block(true),
Some(sum) => k.k.scalar_eq_parallelized(&sum, l as Uint),
Some(sum) => k.k.scalar_eq_parallelized(&sum, l as u64),
}
}
19 changes: 8 additions & 11 deletions src/ciphertext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ impl FheAsciiChar {
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct FheString(pub(crate) Vec<FheAsciiChar>);

/// Type used for scalar operations.
type Uint = u64;

impl FheString {
/// ASCII value of the string termination character.
const TERMINATOR: u8 = 0;
Expand Down Expand Up @@ -174,7 +171,7 @@ impl FheString {
log::trace!("substr_to: at index {i}");

// a[i] = i < index ? a[i] : 0
let i_lt_index = k.k.scalar_gt_parallelized(index, i as Uint);
let i_lt_index = k.k.scalar_gt_parallelized(index, i as u64);
let ai = if_then_else_zero(k, &i_lt_index, &ai.0);
FheAsciiChar(ai)
})
Expand All @@ -190,7 +187,7 @@ impl FheString {
log::trace!("substr_from: at index {i}");

// a[i] = a[i + index]
let i_add_index = k.k.scalar_add_parallelized(index, i as Uint);
let i_add_index = k.k.scalar_add_parallelized(index, i as u64);
self.char_at(k, &i_add_index)
})
.collect();
Expand All @@ -205,7 +202,7 @@ impl FheString {
log::trace!("substr_end: at index {i}");

// a[i] = i + index < end ? a[i + index] : 0
let i_add_index = k.k.scalar_add_parallelized(start, i as Uint);
let i_add_index = k.k.scalar_add_parallelized(start, i as u64);
let i_add_index_lt_end = k.k.lt_parallelized(&i_add_index, end);
let self_i_add_index = self.char_at(k, &i_add_index);
let ai = if_then_else_zero(k, &i_add_index_lt_end, &self_i_add_index.0);
Expand All @@ -227,7 +224,7 @@ impl FheString {
log::trace!("char_at: at index {j}");

// i == j ? a[j] : 0
let i_eq_j = k.k.scalar_eq_parallelized(i, j as Uint);
let i_eq_j = k.k.scalar_eq_parallelized(i, j as u64);
if_then_else_zero(k, &i_eq_j, &aj.0)
})
.collect::<Vec<_>>();
Expand Down Expand Up @@ -280,7 +277,7 @@ pub fn element_at_bool(k: &ServerKey, v: &[BooleanBlock], i: &FheUsize) -> Boole
log::trace!("element_at: at index {j}");

// i == j ? a[j] : 0
let i_eq_j = k.k.scalar_eq_parallelized(i, j as Uint);
let i_eq_j = k.k.scalar_eq_parallelized(i, j as u64);

k.k.boolean_bitand(&i_eq_j, aj)
})
Expand Down Expand Up @@ -335,7 +332,7 @@ fn index_of_unchecked_with_options<T: Sync>(
.par_iter()
.map(|(i, x)| {
let pi = p(k, x);
let pi_mul_i = scalar_if_then_else_zero(k, &pi, *i as Uint);
let pi_mul_i = scalar_if_then_else_zero(k, &pi, *i as u64);
(i, pi, pi_mul_i)
})
.collect();
Expand Down Expand Up @@ -366,11 +363,11 @@ pub struct FheOption<T> {
}

impl FheOption<RadixCiphertext> {
pub fn decrypt(&self, k: &ClientKey) -> Option<Uint> {
pub fn decrypt(&self, k: &ClientKey) -> Option<u64> {
let is_some = k.k.decrypt_bool(&self.is_some);
match is_some {
true => {
let val = k.decrypt::<Uint>(&self.val);
let val = k.decrypt::<u64>(&self.val);
Some(val)
}
false => None,
Expand Down
5 changes: 2 additions & 3 deletions src/ciphertext/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{
ciphertext::{
element_at_bool,
logic::{if_then_else_bool, if_then_else_zero},
Uint,
},
server_key::ServerKey,
};
Expand Down Expand Up @@ -68,7 +67,7 @@ impl FheString {

// c = i + n * len_diff
let n_mul_lendiff = k.k.mul_parallelized(&n, &len_diff);
let c = k.k.scalar_add_parallelized(&n_mul_lendiff, i as Uint);
let c = k.k.scalar_add_parallelized(&n_mul_lendiff, i as u64);

let j_lt_slen = k.k.lt_parallelized(&j, &s_len);
let match_and_jltslen = k.k.boolean_bitand(&in_match, &j_lt_slen);
Expand Down Expand Up @@ -96,7 +95,7 @@ impl FheString {
let vi = k.k.if_then_else_parallelized(&in_match, &sj, &self_c);
v.push(FheAsciiChar(vi));

j = k.k.scalar_add_parallelized(&j, 1 as Uint);
j = k.k.scalar_add_parallelized(&j, 1u8);
});

// Append 0 to terminate string.
Expand Down
11 changes: 4 additions & 7 deletions src/ciphertext/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
use rayon::prelude::*;
use tfhe::integer::BooleanBlock;

use crate::{
ciphertext::{logic::all, Uint},
server_key::ServerKey,
};
use crate::{ciphertext::logic::all, server_key::ServerKey};

use super::{
index_of_unchecked,
Expand Down Expand Up @@ -101,7 +98,7 @@ impl FheString {
in_match = if_then_else_bool(k, &in_match, &in_match, mi);

// j += 1
k.k.scalar_add_assign_parallelized(&mut j, 1 as Uint);
k.k.scalar_add_assign_parallelized(&mut j, 1u8);

// in_match = in_match && j < s.len
let j_lt_slen = k.k.lt_parallelized(&j, &s_len);
Expand Down Expand Up @@ -147,7 +144,7 @@ impl FheString {
j = k.k.if_then_else_parallelized(&j_lt_slen_and_mi, &zero, &j);

// j += 1
k.k.scalar_add_assign_parallelized(&mut j, 1 as Uint);
k.k.scalar_add_assign_parallelized(&mut j, 1u8);

mi
})
Expand Down Expand Up @@ -189,7 +186,7 @@ impl FheString {
let subvec = &self.0.get(i..).unwrap_or_default();
let index = index_of_unchecked(k, subvec, p);
// Add offset.
let val = k.k.scalar_add_parallelized(&index.val, i as Uint);
let val = k.k.scalar_add_parallelized(&index.val, i as u64);
FheOption {
is_some: index.is_some,
val,
Expand Down
8 changes: 4 additions & 4 deletions src/ciphertext/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tfhe::integer::IntegerCiphertext;

use crate::{ciphertext::element_at_bool, client_key::ClientKey, server_key::ServerKey};

use super::{FheAsciiChar, FheOption, FheString, FheUsize, Uint};
use super::{FheAsciiChar, FheOption, FheString, FheUsize};

/// An element of an `FheStringSliceVector`.
#[derive(Clone)]
Expand Down Expand Up @@ -269,7 +269,7 @@ impl FheString {
let self_len = self.len(k);

let n = self.max_len() + 2; // Maximum number of entries.
let n_hidden = k.k.scalar_add_parallelized(&self_len, 2 as Uint); // Better bound based on hidden length.
let n_hidden = k.k.scalar_add_parallelized(&self_len, 2u8); // Better bound based on hidden length.
let mut next_match = self_len.clone();
let mut elems = (0..n)
.rev()
Expand All @@ -283,13 +283,13 @@ impl FheString {
let i_radix = FheUsize::new_trivial(k, i);
let i_sub_plen = k.k.sub_parallelized(&i_radix, &p_len);
let mi = element_at_bool(k, &matches, &i_sub_plen);
let i_lt_n_hidden = k.k.scalar_gt_parallelized(&n_hidden, i as Uint);
let i_lt_n_hidden = k.k.scalar_gt_parallelized(&n_hidden, i as u64);
k.k.boolean_bitand(&i_lt_n_hidden, &mi)
};

// next_match_target = i + (inclusive ? p.len : 0)
let next_match_target = if inclusive {
k.k.scalar_add_parallelized(&p_len, i as Uint)
k.k.scalar_add_parallelized(&p_len, i as u64)
} else {
FheUsize::new_trivial(k, i)
};
Expand Down
10 changes: 5 additions & 5 deletions src/ciphertext/trim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::server_key::ServerKey;

use super::{
index_of_unchecked, logic::if_then_else_zero, rindex_of_unchecked, FheAsciiChar, FheOption,
FheString, FheUsize, Uint,
FheString, FheUsize,
};

impl FheAsciiChar {
Expand All @@ -17,10 +17,10 @@ impl FheAsciiChar {
// (Vertical tab), 12 (Form feed), 13 (Carriage return), 32 (Space)

// (9 <= c <= 13) || c == 32
let c_geq_9 = k.k.scalar_ge_parallelized(&self.0, 9 as Uint);
let c_leq_13 = k.k.scalar_le_parallelized(&self.0, 13 as Uint);
let c_geq_9 = k.k.scalar_ge_parallelized(&self.0, 9u8);
let c_leq_13 = k.k.scalar_le_parallelized(&self.0, 13u8);
let c_geq_9_and_c_leq_13 = k.k.boolean_bitand(&c_geq_9, &c_leq_13);
let c_eq_32 = k.k.scalar_eq_parallelized(&self.0, 32 as Uint);
let c_eq_32 = k.k.scalar_eq_parallelized(&self.0, 32u8);
k.k.boolean_bitor(&c_geq_9_and_c_leq_13, &c_eq_32)
}
}
Expand Down Expand Up @@ -123,7 +123,7 @@ impl FheString {
.enumerate()
.map(|(i, c)| {
// a[i] = i < index ? a[i] : 0
let i_lt_index = k.k.scalar_gt_parallelized(index, i as Uint);
let i_lt_index = k.k.scalar_gt_parallelized(index, i as u64);
let ai = if_then_else_zero(k, &i_lt_index, &c.0);
FheAsciiChar(ai)
})
Expand Down

0 comments on commit fbfd7b7

Please sign in to comment.