|
| 1 | +/// Declare a crypto operations provider using the Ceno zkVM guest implementations. |
| 2 | +#[macro_export] |
| 3 | +macro_rules! ceno_crypto { |
| 4 | + ( $( $key:ident = $val:tt ),* $(,)? ) => { |
| 5 | + // default values |
| 6 | + ceno_crypto!(@parse { |
| 7 | + revm_precompile: ::revm_precompile, |
| 8 | + alloy_consensus: ::alloy_consensus, |
| 9 | + address_type: ::alloy_primitives::Address, |
| 10 | + name: CenoCrypto |
| 11 | + } $( $key = $val, )* ); |
| 12 | + }; |
| 13 | + |
| 14 | + // parse optional args |
| 15 | + (@parse { revm_precompile: $r:path, alloy_consensus: $ac:path, address_type: $addr:path, name: $n:tt } |
| 16 | + revm_precompile = $nr:path $(, $($rest:tt)*)? |
| 17 | + ) => { |
| 18 | + ceno_crypto!(@parse { revm_precompile: $nr, alloy_consensus: $ac, address_type: $addr, name: $n } $($($rest)*)? ); |
| 19 | + }; |
| 20 | + |
| 21 | + (@parse { revm_precompile: $r:path, alloy_consensus: $ac:path, address_type: $addr:path, name: $n:tt } |
| 22 | + alloy_consensus = $nac:path $(, $($rest:tt)*)? |
| 23 | + ) => { |
| 24 | + ceno_crypto!(@parse { revm_precompile: $r, alloy_consensus: $nac, address_type: $addr, name: $n } $($($rest)*)? ); |
| 25 | + }; |
| 26 | + |
| 27 | + (@parse { revm_precompile: $r:path, alloy_consensus: $ac:path, address_type: $addr:path, name: $n:tt } |
| 28 | + address_type = $na:path $(, $($rest:tt)*)? |
| 29 | + ) => { |
| 30 | + ceno_crypto!(@parse { revm_precompile: $r, alloy_consensus: $ac, address_type: $na, name: $n } $($($rest)*)? ); |
| 31 | + }; |
| 32 | + |
| 33 | + (@parse { revm_precompile: $r:path, alloy_consensus: $ac:path, address_type: $addr:path, name: $n:tt } |
| 34 | + name = $nn:ident $(, $($rest:tt)*)? |
| 35 | + ) => { |
| 36 | + ceno_crypto!(@parse { revm_precompile: $r, alloy_consensus: $ac, address_type: $addr, name: $nn } $($($rest)*)? ); |
| 37 | + }; |
| 38 | + |
| 39 | + // unknown key |
| 40 | + (@parse { $($state:tt)* } $bad:ident = $($rest:tt)*) => { |
| 41 | + compile_error!(concat!("unknown option: ", stringify!($bad))); |
| 42 | + }; |
| 43 | + |
| 44 | + |
| 45 | + // finish parsing |
| 46 | + (@parse { revm_precompile: $r:path, alloy_consensus: $ac:path, address_type: $addr:path, name: $n:ident } $(,)?) => { |
| 47 | + use $ac as __ac; |
| 48 | + use $r as __rp; |
| 49 | + |
| 50 | + /// Ceno zkVM crypto operations provider |
| 51 | + #[derive(Debug)] |
| 52 | + #[allow(dead_code)] |
| 53 | + pub struct $n; |
| 54 | + |
| 55 | + impl $n { |
| 56 | + /// Install this as the global crypto provider. |
| 57 | + /// |
| 58 | + /// # Panics |
| 59 | + /// |
| 60 | + /// Panics if a crypto provider has already been installed. |
| 61 | + #[allow(dead_code)] |
| 62 | + pub fn install() { |
| 63 | + Self::try_install().unwrap(); |
| 64 | + } |
| 65 | + |
| 66 | + /// Install this as the global crypto provider. |
| 67 | + #[allow(dead_code)] |
| 68 | + pub fn try_install() -> Result<(), $crate::CenoCryptoInstallError> { |
| 69 | + let revm_install = __rp::install_crypto(Self); |
| 70 | + let alloy_install = |
| 71 | + __ac::crypto::install_default_provider(::std::sync::Arc::new(Self)).is_ok(); |
| 72 | + if !(revm_install && alloy_install) { |
| 73 | + Err($crate::CenoCryptoInstallError) |
| 74 | + } else { |
| 75 | + Ok(()) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + #[allow(dead_code)] |
| 81 | + fn __map_err(e: $crate::CenoCryptoError) -> __rp::PrecompileError { |
| 82 | + match e { |
| 83 | + $crate::CenoCryptoError::Bn254FieldPointNotAMember => { |
| 84 | + __rp::PrecompileError::Bn254FieldPointNotAMember |
| 85 | + } |
| 86 | + $crate::CenoCryptoError::Bn254AffineGFailedToCreate => { |
| 87 | + __rp::PrecompileError::Bn254AffineGFailedToCreate |
| 88 | + } |
| 89 | + $crate::CenoCryptoError::Bn254PairLength => __rp::PrecompileError::Bn254PairLength, |
| 90 | + _ => __rp::PrecompileError::Other(e.to_string()), |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + impl __rp::Crypto for $n { |
| 95 | + #[inline] |
| 96 | + fn sha256(&self, input: &[u8]) -> [u8; 32] { |
| 97 | + $crate::sha256::sha256(input) |
| 98 | + } |
| 99 | + #[inline] |
| 100 | + fn bn254_g1_add( |
| 101 | + &self, |
| 102 | + p1: &[u8], |
| 103 | + p2: &[u8], |
| 104 | + ) -> Result<[u8; 64], __rp::PrecompileError> { |
| 105 | + $crate::bn254::bn254_g1_add(p1, p2).map_err(__map_err) |
| 106 | + } |
| 107 | + #[inline] |
| 108 | + fn bn254_g1_mul( |
| 109 | + &self, |
| 110 | + point: &[u8], |
| 111 | + scalar: &[u8], |
| 112 | + ) -> Result<[u8; 64], __rp::PrecompileError> { |
| 113 | + $crate::bn254::bn254_g1_mul(point, scalar).map_err(__map_err) |
| 114 | + } |
| 115 | + #[inline] |
| 116 | + fn bn254_pairing_check( |
| 117 | + &self, |
| 118 | + pairs: &[(&[u8], &[u8])], |
| 119 | + ) -> Result<bool, __rp::PrecompileError> { |
| 120 | + $crate::bn254::bn254_pairing_check(pairs).map_err(__map_err) |
| 121 | + } |
| 122 | + #[inline] |
| 123 | + fn secp256k1_ecrecover( |
| 124 | + &self, |
| 125 | + sig: &[u8; 64], |
| 126 | + recid: u8, |
| 127 | + msg: &[u8; 32], |
| 128 | + ) -> Result<[u8; 32], __rp::PrecompileError> { |
| 129 | + $crate::secp256k1::secp256k1_ecrecover(sig, recid, msg).map_err(__map_err) |
| 130 | + } |
| 131 | + #[inline] |
| 132 | + fn secp256r1_verify_signature( |
| 133 | + &self, |
| 134 | + msg: &[u8; 32], |
| 135 | + sig: &[u8; 64], |
| 136 | + pk: &[u8; 64], |
| 137 | + ) -> bool { |
| 138 | + $crate::secp256r1::secp256r1_verify_signature(msg, sig, pk) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + impl __ac::crypto::backend::CryptoProvider for $n { |
| 143 | + #[inline] |
| 144 | + fn recover_signer_unchecked( |
| 145 | + &self, |
| 146 | + sig: &[u8; 65], |
| 147 | + msg: &[u8; 32], |
| 148 | + ) -> Result<$addr, __ac::crypto::RecoveryError> { |
| 149 | + use $addr as Address; |
| 150 | + $crate::secp256k1::secp256k1_ecrecover( |
| 151 | + (&sig[..64]).try_into().unwrap(), |
| 152 | + sig[64], |
| 153 | + msg, |
| 154 | + ) |
| 155 | + .map(|res| Address::from_slice(&res[12..])) |
| 156 | + .map_err(__ac::crypto::RecoveryError::from_source) |
| 157 | + } |
| 158 | + } |
| 159 | + }; |
| 160 | +} |
0 commit comments