From f227504ea75ad687cb25b87a353118135d2d0075 Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Thu, 30 Sep 2021 12:13:02 -0700 Subject: [PATCH] Add sol_zk_token_elgamal syscall declarations --- zk-token-sdk/src/zk_token_elgamal/ops.rs | 175 +++++++++++++++++------ 1 file changed, 134 insertions(+), 41 deletions(-) diff --git a/zk-token-sdk/src/zk_token_elgamal/ops.rs b/zk-token-sdk/src/zk_token_elgamal/ops.rs index d05d07814..56b3effd4 100644 --- a/zk-token-sdk/src/zk_token_elgamal/ops.rs +++ b/zk-token-sdk/src/zk_token_elgamal/ops.rs @@ -13,69 +13,69 @@ mod target_arch { // returns `Some(x0*ct0 + x1*ct1)` or `None` if the input was invalid fn add_ciphertexts( scalar_0: Scalar, - ct_0: pod::ElGamalCiphertext, + ct_0: &pod::ElGamalCiphertext, scalar_1: Scalar, - ct_1: pod::ElGamalCiphertext, + ct_1: &pod::ElGamalCiphertext, ) -> Option { - let ct_0: ElGamalCiphertext = ct_0.try_into().ok()?; - let ct_1: ElGamalCiphertext = ct_1.try_into().ok()?; + let ct_0: ElGamalCiphertext = (*ct_0).try_into().ok()?; + let ct_1: ElGamalCiphertext = (*ct_1).try_into().ok()?; let ct_sum = ct_0 * scalar_0 + ct_1 * scalar_1; Some(pod::ElGamalCiphertext::from(ct_sum)) } pub(crate) fn combine_lo_hi( - ct_lo: pod::ElGamalCiphertext, - ct_hi: pod::ElGamalCiphertext, + ct_lo: &pod::ElGamalCiphertext, + ct_hi: &pod::ElGamalCiphertext, ) -> Option { add_ciphertexts(Scalar::one(), ct_lo, Scalar::from(TWO_32), ct_hi) } pub fn add( - ct_0: pod::ElGamalCiphertext, - ct_1: pod::ElGamalCiphertext, + ct_0: &pod::ElGamalCiphertext, + ct_1: &pod::ElGamalCiphertext, ) -> Option { add_ciphertexts(Scalar::one(), ct_0, Scalar::one(), ct_1) } pub fn add_with_lo_hi( - ct_0: pod::ElGamalCiphertext, - ct_1_lo: pod::ElGamalCiphertext, - ct_1_hi: pod::ElGamalCiphertext, + ct_0: &pod::ElGamalCiphertext, + ct_1_lo: &pod::ElGamalCiphertext, + ct_1_hi: &pod::ElGamalCiphertext, ) -> Option { let ct_1 = combine_lo_hi(ct_1_lo, ct_1_hi)?; - add_ciphertexts(Scalar::one(), ct_0, Scalar::one(), ct_1) + add_ciphertexts(Scalar::one(), ct_0, Scalar::one(), &ct_1) } pub fn subtract( - ct_0: pod::ElGamalCiphertext, - ct_1: pod::ElGamalCiphertext, + ct_0: &pod::ElGamalCiphertext, + ct_1: &pod::ElGamalCiphertext, ) -> Option { add_ciphertexts(Scalar::one(), ct_0, -Scalar::one(), ct_1) } pub fn subtract_with_lo_hi( - ct_0: pod::ElGamalCiphertext, - ct_1_lo: pod::ElGamalCiphertext, - ct_1_hi: pod::ElGamalCiphertext, + ct_0: &pod::ElGamalCiphertext, + ct_1_lo: &pod::ElGamalCiphertext, + ct_1_hi: &pod::ElGamalCiphertext, ) -> Option { let ct_1 = combine_lo_hi(ct_1_lo, ct_1_hi)?; - add_ciphertexts(Scalar::one(), ct_0, -Scalar::one(), ct_1) + add_ciphertexts(Scalar::one(), ct_0, -Scalar::one(), &ct_1) } - pub fn add_to(ct: pod::ElGamalCiphertext, amount: u64) -> Option { + pub fn add_to(ct: &pod::ElGamalCiphertext, amount: u64) -> Option { let mut amount_as_ct = [0_u8; 64]; amount_as_ct[..32].copy_from_slice(RISTRETTO_BASEPOINT_COMPRESSED.as_bytes()); add_ciphertexts( Scalar::one(), ct, Scalar::from(amount), - pod::ElGamalCiphertext(amount_as_ct), + &pod::ElGamalCiphertext(amount_as_ct), ) } pub fn subtract_from( - ct: pod::ElGamalCiphertext, + ct: &pod::ElGamalCiphertext, amount: u64, ) -> Option { let mut amount_as_ct = [0_u8; 64]; @@ -84,7 +84,7 @@ mod target_arch { Scalar::one(), ct, -Scalar::from(amount), - pod::ElGamalCiphertext(amount_as_ct), + &pod::ElGamalCiphertext(amount_as_ct), ) } } @@ -92,50 +92,143 @@ mod target_arch { #[cfg(target_arch = "bpf")] #[allow(unused_variables)] mod target_arch { - use crate::zk_token_elgamal::pod; + use {super::*, crate::zk_token_elgamal::pod, bytemuck::Zeroable}; + + fn op( + op: u64, + ct_0: &pod::ElGamalCiphertext, + ct_1: &pod::ElGamalCiphertext, + ) -> Option { + let mut ct_result = pod::ElGamalCiphertext::zeroed(); + let result = unsafe { + sol_zk_token_elgamal_op( + op, + &ct_0.0 as *const u8, + &ct_1.0 as *const u8, + &mut ct_result.0 as *mut u8, + ) + }; + + if result == 0 { + Some(ct_result) + } else { + None + } + } + + fn op_with_lo_hi( + op: u64, + ct_0: &pod::ElGamalCiphertext, + ct_1_lo: &pod::ElGamalCiphertext, + ct_1_hi: &pod::ElGamalCiphertext, + ) -> Option { + let mut ct_result = pod::ElGamalCiphertext::zeroed(); + let result = unsafe { + sol_zk_token_elgamal_op_with_lo_hi( + op, + &ct_0.0 as *const u8, + &ct_1_lo.0 as *const u8, + &ct_1_hi.0 as *const u8, + &mut ct_result.0 as *mut u8, + ) + }; + + if result == 0 { + Some(ct_result) + } else { + None + } + } + + fn op_with_scalar( + op: u64, + ct: &pod::ElGamalCiphertext, + scalar: u64, + ) -> Option { + let mut ct_result = pod::ElGamalCiphertext::zeroed(); + let result = unsafe { + sol_zk_token_elgamal_op_with_scalar( + op, + &ct.0 as *const u8, + scalar, + &mut ct_result.0 as *mut u8, + ) + }; + + if result == 0 { + Some(ct_result) + } else { + None + } + } pub fn add( - ct_0: pod::ElGamalCiphertext, - ct_1: pod::ElGamalCiphertext, + ct_0: &pod::ElGamalCiphertext, + ct_1: &pod::ElGamalCiphertext, ) -> Option { - None + op(OP_ADD, ct_0, ct_1) } pub fn add_with_lo_hi( - ct_0: pod::ElGamalCiphertext, - ct_1_lo: pod::ElGamalCiphertext, - ct_1_hi: pod::ElGamalCiphertext, + ct_0: &pod::ElGamalCiphertext, + ct_1_lo: &pod::ElGamalCiphertext, + ct_1_hi: &pod::ElGamalCiphertext, ) -> Option { - None + op_with_lo_hi(OP_ADD, ct_0, ct_1_lo, ct_1_hi) } pub fn subtract( - ct_0: pod::ElGamalCiphertext, - ct_1: pod::ElGamalCiphertext, + ct_0: &pod::ElGamalCiphertext, + ct_1: &pod::ElGamalCiphertext, ) -> Option { - None + op(OP_SUB, ct_0, ct_1) } pub fn subtract_with_lo_hi( - ct_0: pod::ElGamalCiphertext, - ct_1_lo: pod::ElGamalCiphertext, - ct_1_hi: pod::ElGamalCiphertext, + ct_0: &pod::ElGamalCiphertext, + ct_1_lo: &pod::ElGamalCiphertext, + ct_1_hi: &pod::ElGamalCiphertext, ) -> Option { - None + op_with_lo_hi(OP_SUB, ct_0, ct_1_lo, ct_1_hi) } - pub fn add_to(ct: pod::ElGamalCiphertext, amount: u64) -> Option { - None + pub fn add_to(ct: &pod::ElGamalCiphertext, amount: u64) -> Option { + op_with_scalar(OP_ADD, ct, amount) } pub fn subtract_from( - ct: pod::ElGamalCiphertext, + ct: &pod::ElGamalCiphertext, amount: u64, ) -> Option { - None + op_with_scalar(OP_SUB, ct, amount) } } +pub const OP_ADD: u64 = 0; +pub const OP_SUB: u64 = 1; + +extern "C" { + pub fn sol_zk_token_elgamal_op( + op: u64, + ct_0: *const u8, + ct_1: *const u8, + ct_result: *mut u8, + ) -> u64; + pub fn sol_zk_token_elgamal_op_with_lo_hi( + op: u64, + ct_0: *const u8, + ct_1_lo: *const u8, + ct_1_hi: *const u8, + ct_result: *mut u8, + ) -> u64; + pub fn sol_zk_token_elgamal_op_with_scalar( + op: u64, + ct: *const u8, + scalar: u64, + ct_result: *mut u8, + ) -> u64; +} + #[cfg(test)] mod tests { use {