Homomorphic ciphertext operations
Focus: a concrete balance update that subtracts a fee, adds a reward, and scales the encrypted amount.
ciphertext_ops.slx
entry init() {
let balance = Ciphertext::zero();
balance.add(100);
balance.sub(25);
balance.add(12);
balance.mul(2);
println(balance)
require(
balance.commitment() == Scalar::from_u64(174).mul_base(),
"Encrypted value should be equal to 174"
)
return 0
}
entry combine(left: Ciphertext, right: Ciphertext) -> Ciphertext {
left.add_ct(right);
return left
}How it works
Ciphertext::generate(recipient, amount)creates an encrypted amount for the recipient address, whileCiphertext::zero()creates an encrypted zero value to build up from.encrypted.add(value)adds a plaintext amount to the ciphertext in place.encrypted.sub(value)subtracts a plaintext amount from the ciphertext in place.encrypted.mul(value)scales the ciphertext by a plaintext multiplier.- The resulting ciphertext is equal to
((100 - 25 + 12) * 2)and can be applied to others ciphertexts.
Last updated on