From 9884158d993ca2b8a505ea6bde0f13d8833fc97d Mon Sep 17 00:00:00 2001 From: Vindaar Date: Tue, 2 Jul 2024 13:02:29 +0200 Subject: [PATCH] [rust] update EVM precompiles API to return values --- .../src/lib.rs | 242 ++++++++---------- 1 file changed, 113 insertions(+), 129 deletions(-) diff --git a/constantine-rust/constantine-ethereum-evm-precompiles/src/lib.rs b/constantine-rust/constantine-ethereum-evm-precompiles/src/lib.rs index b3e8fb62..e8bbb3da 100644 --- a/constantine-rust/constantine-ethereum-evm-precompiles/src/lib.rs +++ b/constantine-rust/constantine-ethereum-evm-precompiles/src/lib.rs @@ -18,38 +18,44 @@ use ::core::mem::MaybeUninit; #[inline] pub fn evm_sha256(message: &[u8]) -> Result<[u8; 32], ctt_evm_status> { let mut result: MaybeUninit<[u8; 32]> = MaybeUninit::uninit(); - unsafe { - let status = ctt_eth_evm_sha256( - result.as_mut_ptr() as *mut byte, - 32, - message.as_ptr() as *const byte, - message.len() as isize, - ); + unsafe { + let status = ctt_eth_evm_sha256( + result.as_mut_ptr() as *mut byte, + 32, + message.as_ptr() as *const byte, + message.len() as usize, + ); match status { ctt_evm_status::cttEVM_Success => Ok(result.assume_init()), - _ => Err(status) + _ => Err(status), } } } - -/// TODO: We want an extra proc in C to return the size of the return buffer needed. -/// so we can preallocate before calling evm_modexp itself. #[inline] -pub fn evm_modexp( - result: &mut [u8], - inputs: &[u8] -) -> Result<(), ctt_evm_status> { +pub fn evm_modexp(inputs: &[u8]) -> Result, ctt_evm_status> { + // Call Nim function to determine correct size to allocate for `result` unsafe { - let status = ctt_eth_evm_modexp( + let mut size = 0u64; + let status = ctt_modexp_result_size( + &mut size, + inputs.as_ptr() as *const byte, + inputs.len() as usize, + ); + if status != ctt_evm_status::cttEVM_Success { + return Err(status); + } + + let mut result = vec![0u8; size as usize]; + let status = ctt_eth_evm_modexp( result.as_mut_ptr() as *mut byte, - result.len() as isize, + result.len() as usize, inputs.as_ptr() as *const byte, - inputs.len() as isize, - ); + inputs.len() as usize, + ); match status { - ctt_evm_status::cttEVM_Success => Ok(()), - _ => Err(status) + ctt_evm_status::cttEVM_Success => Ok(result), + _ => Err(status), } } } @@ -59,15 +65,15 @@ pub fn evm_bn254_g1add(inputs: &[u8]) -> Result<[u8; 64], ctt_evm_status> { let mut result: MaybeUninit<[u8; 64]> = MaybeUninit::uninit(); unsafe { let status = ctt_eth_evm_bn254_g1add( - result.as_mut_ptr() as *mut byte, - 64, - inputs.as_ptr() as *const byte, - inputs.len() as isize, + result.as_mut_ptr() as *mut byte, + 64, + inputs.as_ptr() as *const byte, + inputs.len() as usize, ); match status { ctt_evm_status::cttEVM_Success => Ok(result.assume_init()), - _ => Err(status) - } + _ => Err(status), + } } } @@ -75,15 +81,15 @@ pub fn evm_bn254_g1add(inputs: &[u8]) -> Result<[u8; 64], ctt_evm_status> { pub fn evm_bn254_g1mul(inputs: &[u8]) -> Result<[u8; 64], ctt_evm_status> { let mut result: MaybeUninit<[u8; 64]> = MaybeUninit::uninit(); unsafe { - let status = ctt_eth_evm_bn254_g1mul( + let status = ctt_eth_evm_bn254_g1mul( result.as_mut_ptr() as *mut byte, 64, inputs.as_ptr() as *const byte, - inputs.len() as isize, - ); + inputs.len() as usize, + ); match status { ctt_evm_status::cttEVM_Success => Ok(result.assume_init()), - _ => Err(status) + _ => Err(status), } } } @@ -92,189 +98,167 @@ pub fn evm_bn254_g1mul(inputs: &[u8]) -> Result<[u8; 64], ctt_evm_status> { pub fn evm_bn254_ec_pairing_check(inputs: &[u8]) -> Result<[u8; 32], ctt_evm_status> { let mut result: MaybeUninit<[u8; 32]> = MaybeUninit::uninit(); unsafe { - let status = ctt_eth_evm_bn254_ecpairingcheck( + let status = ctt_eth_evm_bn254_ecpairingcheck( result.as_mut_ptr() as *mut byte, 32, inputs.as_ptr() as *const byte, - inputs.len() as isize, - ); + inputs.len() as usize, + ); match status { ctt_evm_status::cttEVM_Success => Ok(result.assume_init()), - _ => Err(status) + _ => Err(status), } } } -// -// TODO: takeover from here -// - #[inline] -pub fn evm_bls12381_g1add( - result: &mut [u8], - inputs: &[u8] -) -> Result<(), ctt_evm_status> { +pub fn evm_bls12381_g1add(inputs: &[u8]) -> Result<[u8; 128], ctt_evm_status> { + let mut result: MaybeUninit<[u8; 128]> = MaybeUninit::uninit(); unsafe { - let status = ctt_eth_evm_bls12381_g1add( + let status = ctt_eth_evm_bls12381_g1add( result.as_mut_ptr() as *mut byte, - result.len() as isize, + 128, inputs.as_ptr() as *const byte, - inputs.len() as isize, - ); + inputs.len() as usize, + ); match status { - ctt_evm_status::cttEVM_Success => Ok(()), - _ => Err(status) + ctt_evm_status::cttEVM_Success => Ok(result.assume_init()), + _ => Err(status), } } } #[inline] -pub fn evm_bls12381_g1mul( - result: &mut [u8], - inputs: &[u8] -) -> Result<(), ctt_evm_status> { +pub fn evm_bls12381_g1mul(inputs: &[u8]) -> Result<[u8; 128], ctt_evm_status> { + let mut result: MaybeUninit<[u8; 128]> = MaybeUninit::uninit(); unsafe { - let status = ctt_eth_evm_bls12381_g1mul( + let status = ctt_eth_evm_bls12381_g1mul( result.as_mut_ptr() as *mut byte, - result.len() as isize, + 128, inputs.as_ptr() as *const byte, - inputs.len() as isize, - ); + inputs.len() as usize, + ); match status { - ctt_evm_status::cttEVM_Success => Ok(()), - _ => Err(status) + ctt_evm_status::cttEVM_Success => Ok(result.assume_init()), + _ => Err(status), } } } #[inline] -pub fn evm_bls12381_g1msm( - result: &mut [u8], - inputs: &[u8] -) -> Result<(), ctt_evm_status> { +pub fn evm_bls12381_g1msm(inputs: &[u8]) -> Result<[u8; 128], ctt_evm_status> { + let mut result: MaybeUninit<[u8; 128]> = MaybeUninit::uninit(); unsafe { - let status = ctt_eth_evm_bls12381_g1msm( + let status = ctt_eth_evm_bls12381_g1msm( result.as_mut_ptr() as *mut byte, - result.len() as isize, + 128, inputs.as_ptr() as *const byte, - inputs.len() as isize, - ); + inputs.len() as usize, + ); match status { - ctt_evm_status::cttEVM_Success => Ok(()), - _ => Err(status) + ctt_evm_status::cttEVM_Success => Ok(result.assume_init()), + _ => Err(status), } } } #[inline] -pub fn evm_bls12381_g2add( - result: &mut [u8], - inputs: &[u8] -) -> Result<(), ctt_evm_status> { +pub fn evm_bls12381_g2add(inputs: &[u8]) -> Result<[u8; 256], ctt_evm_status> { + let mut result: MaybeUninit<[u8; 256]> = MaybeUninit::uninit(); unsafe { - let status = ctt_eth_evm_bls12381_g2add( + let status = ctt_eth_evm_bls12381_g2add( result.as_mut_ptr() as *mut byte, - result.len() as isize, + 256, inputs.as_ptr() as *const byte, - inputs.len() as isize, - ); + inputs.len() as usize, + ); match status { - ctt_evm_status::cttEVM_Success => Ok(()), - _ => Err(status) + ctt_evm_status::cttEVM_Success => Ok(result.assume_init()), + _ => Err(status), } } } #[inline] -pub fn evm_bls12381_g2mul( - result: &mut [u8], - inputs: &[u8] -) -> Result<(), ctt_evm_status> { +pub fn evm_bls12381_g2mul(inputs: &[u8]) -> Result<[u8; 256], ctt_evm_status> { + let mut result: MaybeUninit<[u8; 256]> = MaybeUninit::uninit(); unsafe { - let status = ctt_eth_evm_bls12381_g2mul( + let status = ctt_eth_evm_bls12381_g2mul( result.as_mut_ptr() as *mut byte, - result.len() as isize, + 256, inputs.as_ptr() as *const byte, - inputs.len() as isize, - ); + inputs.len() as usize, + ); match status { - ctt_evm_status::cttEVM_Success => Ok(()), - _ => Err(status) + ctt_evm_status::cttEVM_Success => Ok(result.assume_init()), + _ => Err(status), } } } #[inline] -pub fn evm_bls12381_g2msm( - result: &mut [u8], - inputs: &[u8] -) -> Result<(), ctt_evm_status> { +pub fn evm_bls12381_g2msm(inputs: &[u8]) -> Result<[u8; 256], ctt_evm_status> { + let mut result: MaybeUninit<[u8; 256]> = MaybeUninit::uninit(); unsafe { - let status = ctt_eth_evm_bls12381_g2msm( + let status = ctt_eth_evm_bls12381_g2msm( result.as_mut_ptr() as *mut byte, - result.len() as isize, + 256, inputs.as_ptr() as *const byte, - inputs.len() as isize, - ); + inputs.len() as usize, + ); match status { - ctt_evm_status::cttEVM_Success => Ok(()), - _ => Err(status) + ctt_evm_status::cttEVM_Success => Ok(result.assume_init()), + _ => Err(status), } } } #[inline] -pub fn evm_bls12381_pairing_check( - result: &mut [u8], - inputs: &[u8] -) -> Result<(), ctt_evm_status> { +pub fn evm_bls12381_pairing_check(inputs: &[u8]) -> Result<[u8; 32], ctt_evm_status> { + let mut result: MaybeUninit<[u8; 32]> = MaybeUninit::uninit(); unsafe { - let status = ctt_eth_evm_bls12381_pairingcheck( + let status = ctt_eth_evm_bls12381_pairingcheck( result.as_mut_ptr() as *mut byte, - result.len() as isize, + 32, inputs.as_ptr() as *const byte, - inputs.len() as isize, - ); + inputs.len() as usize, + ); match status { - ctt_evm_status::cttEVM_Success => Ok(()), - _ => Err(status) + ctt_evm_status::cttEVM_Success => Ok(result.assume_init()), + _ => Err(status), } } } #[inline] -pub fn evm_bls12381_map_fp_to_g1( - result: &mut [u8], - inputs: &[u8] -) -> Result<(), ctt_evm_status> { +pub fn evm_bls12381_map_fp_to_g1(inputs: &[u8]) -> Result<[u8; 128], ctt_evm_status> { + let mut result: MaybeUninit<[u8; 128]> = MaybeUninit::uninit(); unsafe { - let status = ctt_eth_evm_bls12381_map_fp_to_g1( + let status = ctt_eth_evm_bls12381_map_fp_to_g1( result.as_mut_ptr() as *mut byte, - result.len() as isize, + 128, inputs.as_ptr() as *const byte, - inputs.len() as isize, - ); + inputs.len() as usize, + ); match status { - ctt_evm_status::cttEVM_Success => Ok(()), - _ => Err(status) + ctt_evm_status::cttEVM_Success => Ok(result.assume_init()), + _ => Err(status), } } } -pub fn evm_bls12381_map_fp2_to_g2( - result: &mut [u8], - inputs: &[u8] -) -> Result<(), ctt_evm_status> { +pub fn evm_bls12381_map_fp2_to_g2(inputs: &[u8]) -> Result<[u8; 256], ctt_evm_status> { + let mut result: MaybeUninit<[u8; 256]> = MaybeUninit::uninit(); unsafe { - let status = ctt_eth_evm_bls12381_map_fp2_to_g2( + let status = ctt_eth_evm_bls12381_map_fp2_to_g2( result.as_mut_ptr() as *mut byte, - result.len() as isize, + 128, inputs.as_ptr() as *const byte, - inputs.len() as isize, - ); + inputs.len() as usize, + ); match status { - ctt_evm_status::cttEVM_Success => Ok(()), - _ => Err(status) + ctt_evm_status::cttEVM_Success => Ok(result.assume_init()), + _ => Err(status), } } }