Skip to main content
The test environment provides assertion methods for verifying encrypted computation results.

Integer Assertions

Assert that an encrypted value equals an expected plaintext:
// Assert u8 value
env.assert_encrypted_eq_u8(&hash, 100);

// Assert u32 value
env.assert_encrypted_eq_u32(&hash, 100000);

// Assert u64 value
env.assert_encrypted_eq_u64(&hash, 10000000000);
These methods:
  1. Fetch the ciphertext from the data store
  2. Decrypt using the test environment’s client key
  3. Compare against the expected value
  4. Panic with a descriptive message on mismatch

Boolean Assertions

Assert encrypted boolean values:
// Assert encrypted boolean is true
env.assert_encrypted_true(&bool_hash);

// Assert encrypted boolean is false
env.assert_encrypted_false(&bool_hash);

Custom Assertions

For more complex assertions, fetch and decrypt manually:
// Fetch and decrypt
let actual: u8 = env.fetch_and_decrypt_u8(&hash);

// Custom assertion
assert!(actual >= 50, "Value should be at least 50, got {}", actual);
assert!(actual <= 100, "Value should be at most 100, got {}", actual);

Assertion Failure Messages

On failure, assertions provide clear messages:
thread 'test_addition' panicked at 'Encrypted u8 mismatch: expected 80, got 70'

Testing Comparisons

Test that FHE comparisons produce correct results:
#[test]
fn test_comparison() {
    let mut env = FheTestEnv::new();

    // Create test values
    let a_hash = env.encrypt_and_submit_u8(100);
    let b_hash = env.encrypt_and_submit_u8(50);

    // ... run comparison instruction ...

    // Verify comparison result (a >= b should be true)
    env.assert_encrypted_true(&result_bool_hash);
}

Testing Min/Max

#[test]
fn test_min_max() {
    let mut env = FheTestEnv::new();

    let a_hash = env.encrypt_and_submit_u8(100);
    let b_hash = env.encrypt_and_submit_u8(50);

    // ... run min instruction ...

    // min(100, 50) = 50
    env.assert_encrypted_eq_u8(&min_result_hash, 50);

    // ... run max instruction ...

    // max(100, 50) = 100
    env.assert_encrypted_eq_u8(&max_result_hash, 100);
}

Complete Test Example

#[test]
fn test_order_matching() {
    let mut env = FheTestEnv::new();
    env.deploy_program(PROGRAM_ID, include_bytes!("../program.so"));

    let payer = env.create_funded_keypair();

    // Setup: buy_price=100, sell_price=95
    // Expected: can_match=true, fill_price=95
    let buy_price_hash = env.encrypt_and_submit_u8(100);
    let sell_price_hash = env.encrypt_and_submit_u8(95);
    let buy_qty_hash = env.encrypt_and_submit_u8(50);
    let sell_qty_hash = env.encrypt_and_submit_u8(30);

    // ... build and send transaction ...

    env.sync_data_store_to_syscalls();
    // ... send transaction ...
    env.sync_data_store_from_syscalls();

    // Read result from account
    let result_account = env.svm.get_account(&result_pubkey).unwrap();
    let fill_price_hash: [u8; 32] = result_account.data[0..32].try_into().unwrap();
    let fill_qty_hash: [u8; 32] = result_account.data[32..64].try_into().unwrap();

    // Verify: fill_price = sell_price = 95
    env.assert_encrypted_eq_u8(&fill_price_hash, 95);

    // Verify: fill_qty = min(50, 30) = 30
    env.assert_encrypted_eq_u8(&fill_qty_hash, 30);
}