diff --git a/src/ciphertext/insert.rs b/src/ciphertext/insert.rs index 95ad982..276f6fe 100644 --- a/src/ciphertext/insert.rs +++ b/src/ciphertext/insert.rs @@ -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, }; @@ -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::>() }, @@ -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; diff --git a/src/ciphertext/logic.rs b/src/ciphertext/logic.rs index 933a9b4..130ca8b 100644 --- a/src/ciphertext/logic.rs +++ b/src/ciphertext/logic.rs @@ -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(k: &ServerKey, a: &BooleanBlock, b: &T) -> T { @@ -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), } } diff --git a/src/ciphertext/mod.rs b/src/ciphertext/mod.rs index 1f28fb3..af8b298 100644 --- a/src/ciphertext/mod.rs +++ b/src/ciphertext/mod.rs @@ -42,9 +42,6 @@ impl FheAsciiChar { #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] pub struct FheString(pub(crate) Vec); -/// Type used for scalar operations. -type Uint = u64; - impl FheString { /// ASCII value of the string termination character. const TERMINATOR: u8 = 0; @@ -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) }) @@ -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(); @@ -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); @@ -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::>(); @@ -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) }) @@ -335,7 +332,7 @@ fn index_of_unchecked_with_options( .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(); @@ -366,11 +363,11 @@ pub struct FheOption { } impl FheOption { - pub fn decrypt(&self, k: &ClientKey) -> Option { + pub fn decrypt(&self, k: &ClientKey) -> Option { let is_some = k.k.decrypt_bool(&self.is_some); match is_some { true => { - let val = k.decrypt::(&self.val); + let val = k.decrypt::(&self.val); Some(val) } false => None, diff --git a/src/ciphertext/replace.rs b/src/ciphertext/replace.rs index 476fd35..28aebe8 100644 --- a/src/ciphertext/replace.rs +++ b/src/ciphertext/replace.rs @@ -6,7 +6,6 @@ use crate::{ ciphertext::{ element_at_bool, logic::{if_then_else_bool, if_then_else_zero}, - Uint, }, server_key::ServerKey, }; @@ -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); @@ -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. diff --git a/src/ciphertext/search.rs b/src/ciphertext/search.rs index 85aabab..e97c367 100644 --- a/src/ciphertext/search.rs +++ b/src/ciphertext/search.rs @@ -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, @@ -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); @@ -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 }) @@ -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, diff --git a/src/ciphertext/split.rs b/src/ciphertext/split.rs index 3cfd386..58dd862 100644 --- a/src/ciphertext/split.rs +++ b/src/ciphertext/split.rs @@ -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)] @@ -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() @@ -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) }; diff --git a/src/ciphertext/trim.rs b/src/ciphertext/trim.rs index a2e8c88..23cee1d 100644 --- a/src/ciphertext/trim.rs +++ b/src/ciphertext/trim.rs @@ -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 { @@ -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) } } @@ -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) })