chore: uses if_then_zero only in HPU ERC20 whitepaper (to be updated when encrypt_trivial becomes available on HPU), adds test of if_then_zero for both CPU & HPU

This commit is contained in:
pgardratzama
2026-01-06 15:23:40 +01:00
committed by Pierre Gardrat
parent 122ef489fd
commit d2a570bdd6
4 changed files with 36 additions and 2 deletions

View File

@@ -34,12 +34,12 @@ where
{
let has_enough_funds = (from_amount).ge(amount);
let amount_to_transfer = {
#[cfg(feature = "gpu")]
#[cfg(not(feature = "hpu"))]
{
let zero_amount = FheType::encrypt_trivial(0u64);
has_enough_funds.select(amount, &zero_amount)
}
#[cfg(not(feature = "gpu"))]
#[cfg(feature = "hpu")]
{
has_enough_funds.if_then_zero(amount)
}

View File

@@ -386,6 +386,12 @@ fn test_if_then_else() {
super::test_case_if_then_else(&client_key);
}
#[test]
fn test_if_then_zero() {
let client_key = setup_default_cpu();
super::test_case_if_then_zero(&client_key);
}
#[test]
fn test_flip() {
let client_key = setup_default_cpu();

View File

@@ -89,6 +89,12 @@ fn test_case_if_then_else_hpu() {
super::test_case_if_then_else(&client_key);
}
#[test]
fn test_case_if_then_zero_hpu() {
let client_key = setup_default_hpu();
super::test_case_if_then_zero(&client_key);
}
#[test]
fn test_case_flip_hpu() {
let client_key = setup_default_hpu();

View File

@@ -568,6 +568,28 @@ fn test_case_if_then_else(client_key: &ClientKey) {
);
}
fn test_case_if_then_zero(client_key: &ClientKey) {
let clear_a = 42u8;
let clear_b = 128u8;
let a = FheUint8::encrypt(clear_a, client_key);
let b = FheUint8::encrypt(clear_b, client_key);
let result = a.le(&b).if_then_zero(&a);
let decrypted_result: u8 = result.decrypt(client_key);
assert_eq!(
decrypted_result,
if clear_a <= clear_b { clear_a } else { 0 }
);
let result = a.ge(&b).if_then_zero(&a);
let decrypted_result: u8 = result.decrypt(client_key);
assert_eq!(
decrypted_result,
if clear_a >= clear_b { clear_a } else { 0 }
);
}
fn test_case_flip(client_key: &ClientKey) {
let clear_a = rand::random::<u32>();
let clear_b = rand::random::<u32>();