feat: Add IfThenZero impl for Cpu

This commit is contained in:
Baptiste Roux
2025-12-23 11:13:27 +01:00
committed by Pierre Gardrat
parent 569abd9a3b
commit e645ee3397
2 changed files with 14 additions and 6 deletions

View File

@@ -28,13 +28,11 @@ pub fn transfer_whitepaper<FheType>(
amount: &FheType,
) -> (FheType, FheType)
where
FheType: Add<Output = FheType> + for<'a> FheOrd<&'a FheType> + FheTrivialEncrypt<u64>,
FheType: Add<Output = FheType> + for<'a> FheOrd<&'a FheType>,
FheBool: IfThenZero<FheType>,
for<'a> &'a FheType: Add<Output = FheType> + Sub<Output = FheType>,
{
let has_enough_funds = (from_amount).ge(amount);
//let zero_amount = FheType::encrypt_trivial(0u64);
//let amount_to_transfer = has_enough_funds.select(amount, &zero_amount);
let amount_to_transfer = has_enough_funds.if_then_zero(amount);
let new_to_amount = to_amount + &amount_to_transfer;

View File

@@ -564,12 +564,22 @@ where
/// - if `self` is false, the output will be an encryption of 0
fn if_then_zero(&self, ct_then: &FheUint<Id>) -> FheUint<Id> {
global_state::with_internal_keys(|sks| match sks {
InternalServerKey::Cpu(_) => {
panic!("CPU does not support if_then_zero at this point")
InternalServerKey::Cpu(cpu_sks) => {
let ct_condition = self;
let mut ct_out = ct_then.ciphertext.on_cpu().clone();
cpu_sks.pbs_key().zero_out_if_condition_is_false(
&mut ct_out,
&ct_condition.ciphertext.on_cpu().0,
);
FheUint::new(
ct_out,
cpu_sks.tag.clone(),
ReRandomizationMetadata::default(),
)
}
#[cfg(feature = "gpu")]
InternalServerKey::Cuda(_) => {
panic!("Cuda does not support if_then_zero at this point")
panic!("Cuda does not support if_then_zero")
}
#[cfg(feature = "hpu")]
InternalServerKey::Hpu(device) => {