Use this file to discover all available pages before exploring further.
FHE programs cannot use traditional if-else statements with encrypted conditions. Instead, Privora provides encrypted comparison and selection operations.
FHE uses oblivious selection: compute both branches and select the result based on an encrypted condition.
use privora_sdk_program::prelude::*;// Compare encrypted valueslet condition: EncryptedBool = price.ge(&threshold)?;// Select based on encrypted conditionlet result = condition.select(&high_value, &low_value)?;
Both high_value and low_value are computed; the select operation picks one based on the encrypted condition without revealing which.
// Ensure value is within boundsfn clamp( value: &Encrypted<u8>, min: &Encrypted<u8>, max: &Encrypted<u8>,) -> Result<Encrypted<u8>, ProgramError> { // If value < min, use min let below_min = value.lt(min)?; let clamped_low = below_min.select(min, value)?; // If clamped_low > max, use max let above_max = clamped_low.gt(max)?; let result = above_max.select(max, &clamped_low)?; Ok(result)}
// Subtract only if we have enough balancefn safe_subtract( balance: &Encrypted<u64>, amount: &Encrypted<u64>,) -> Result<Encrypted<u64>, ProgramError> { let has_enough = balance.ge(amount)?; let new_balance = balance.sub(amount)?; // If not enough, return original balance (no change) let result = has_enough.select(&new_balance, balance)?; Ok(result)}
// Perform comparisonlet is_valid: EncryptedBool = price.ge(&minimum)?;// Store to get referencelet is_valid_ref: EncryptedBoolRef = is_valid.store()?;// Save in accountaccount.is_valid_ref = is_valid_ref;// Later: Load and uselet is_valid: EncryptedBool = account.is_valid_ref.load()?;let value = is_valid.select(&yes_value, &no_value)?;
// Bad: Multiple redundant comparisonslet a_ge_b = a.ge(&b)?;let a_lt_b = a.lt(&b)?; // Redundant!// Good: One comparison, derive the restlet a_ge_b = a.ge(&b)?;// a_lt_b is just the negation - use select if needed
Use min/max instead of compare + select
// Badlet is_smaller = a.lt(&b)?;let min = is_smaller.select(&a, &b)?;// Goodlet min = a.min(&b)?;
Batch arithmetic before comparisons
// Good: All arithmetic firstlet total_a = price_a.mul(&qty_a)?;let total_b = price_b.mul(&qty_b)?;let a_larger = total_a.gt(&total_b)?; // Single comparison// Avoid: Comparisons interleaved
// In normal code, this might skip expensive_op() if condition is falseif condition { expensive_op();}// In FHE, expensive_op() is ALWAYS computedlet result = condition.select(&expensive_result, &default)?;