Operations
Inco Lightning provides arithmetic, comparison, and bitwise operations on encrypted values.
Arithmetic Operations
All arithmetic operations return an Euint128.
| Function | Description | Signature |
|---|
e_add | Addition | (Euint128, Euint128) -> Euint128 |
e_sub | Subtraction | (Euint128, Euint128) -> Euint128 |
e_mul | Multiplication | (Euint128, Euint128) -> Euint128 |
e_rem | Remainder | (Euint128, Euint128) -> Euint128 |
Example: Encrypted Addition
pub fn add_balances(
ctx: Context<AddBalances>,
amount_a: Euint128,
amount_b: Euint128,
) -> Result<()> {
let cpi_ctx = CpiContext::new(
ctx.accounts.inco_lightning_program.to_account_info(),
Operation {
signer: ctx.accounts.authority.to_account_info(),
},
);
// Add two encrypted values
let result: Euint128 = e_add(cpi_ctx, amount_a, amount_b, 0)?;
ctx.accounts.account.total = result;
// IMPORTANT: After storing the result handle, grant decryption access
// to the appropriate address (e.g., owner) using the `allow` function.
// See the Access Control guide for details.
Ok(())
}
Comparison Operations
Comparison operations return an Ebool.
| Function | Description | Signature |
|---|
e_ge | Greater than or equal | (Euint128, Euint128) -> Ebool |
e_gt | Greater than | (Euint128, Euint128) -> Ebool |
e_le | Less than or equal | (Euint128, Euint128) -> Ebool |
e_lt | Less than | (Euint128, Euint128) -> Ebool |
e_eq | Equal | (Euint128, Euint128) -> Ebool |
Bitwise Operations
| Function | Description | Signature |
|---|
e_and | Bitwise AND | (Euint128, Euint128) -> Euint128 |
e_or | Bitwise OR | (Euint128, Euint128) -> Euint128 |
e_not | Bitwise NOT | (Euint128) -> Euint128 |
e_shl | Shift left | (Euint128, Euint128) -> Euint128 |
e_shr | Shift right | (Euint128, Euint128) -> Euint128 |
CPI Context Pattern
All operations require a CPI context with the Inco Lightning program:
let cpi_ctx = CpiContext::new(
ctx.accounts.inco_lightning_program.to_account_info(),
Operation {
signer: ctx.accounts.authority.to_account_info(),
},
);
let result = e_add(cpi_ctx, a, b, 0)?;
// IMPORTANT: Define allowance rules after each operation that produces
// a handle you want to decrypt later. See Access Control guide.
The last parameter in operation calls identifies whether the left-hand side operand is ciphertext (0) or plaintext (1). Always pass 0 when working with encrypted handles.