1#[cfg(not(feature = "std"))]
2use alloc::{
3 string::{String, ToString},
4 vec,
5 vec::Vec,
6};
7
8use ethereum_types::Address;
9use sha2::Digest as _;
10
11#[derive(Debug, thiserror::Error)]
13pub enum CryptoError {
14 #[error("invalid signature")]
15 InvalidSignature,
16 #[error("invalid recovery id")]
17 InvalidRecoveryId,
18 #[error("recovery failed")]
19 RecoveryFailed,
20 #[error("invalid point: {0}")]
21 InvalidPoint(&'static str),
22 #[error("invalid input: {0}")]
23 InvalidInput(&'static str),
24 #[error("verification failed")]
25 VerificationFailed,
26 #[error("unsupported: {0}")]
27 Unsupported(&'static str),
28 #[error("{0}")]
29 Other(String),
30}
31
32#[cfg(not(feature = "blst"))]
37const BLS_UNSUPPORTED: &str =
38 "bls12_381 requires the `blst` feature (host/L1) or a zkVM provider override";
39
40pub trait Crypto: Send + Sync + core::fmt::Debug {
71 #[cfg(feature = "secp256k1")]
77 fn secp256k1_ecrecover(
78 &self,
79 sig: &[u8; 64],
80 recid: u8,
81 msg: &[u8; 32],
82 ) -> Result<[u8; 32], CryptoError> {
83 let recovery_id = secp256k1::ecdsa::RecoveryId::try_from(recid as i32)
84 .map_err(|_| CryptoError::InvalidRecoveryId)?;
85
86 let recoverable_sig =
87 secp256k1::ecdsa::RecoverableSignature::from_compact(sig, recovery_id)
88 .map_err(|_| CryptoError::InvalidSignature)?;
89
90 let message = secp256k1::Message::from_digest(*msg);
91
92 let public_key = recoverable_sig
93 .recover(&message)
94 .map_err(|_| CryptoError::RecoveryFailed)?;
95
96 let hash = crate::keccak::keccak_hash(&public_key.serialize_uncompressed()[1..]);
97 Ok(hash)
98 }
99
100 #[cfg(not(feature = "secp256k1"))]
101 fn secp256k1_ecrecover(
102 &self,
103 sig: &[u8; 64],
104 recid: u8,
105 msg: &[u8; 32],
106 ) -> Result<[u8; 32], CryptoError> {
107 use k256::{
108 AffinePoint, ProjectivePoint, Scalar,
109 elliptic_curve::{
110 PrimeField,
111 group::prime::PrimeCurveAffine,
112 ops::{Invert, LinearCombination, Reduce},
113 point::DecompressPoint,
114 sec1::ToEncodedPoint,
115 },
116 };
117
118 let r_bytes = k256::FieldBytes::from_slice(&sig[..32]);
120 let s_bytes = k256::FieldBytes::from_slice(&sig[32..]);
121 let r: Option<Scalar> = Scalar::from_repr(*r_bytes).into();
122 let s: Option<Scalar> = Scalar::from_repr(*s_bytes).into();
123
124 let (Some(r), Some(s)) = (r, s) else {
125 return Err(CryptoError::InvalidSignature);
126 };
127
128 if r.is_zero().into() || s.is_zero().into() {
129 return Err(CryptoError::InvalidSignature);
130 }
131
132 let y_is_odd = (recid & 1) != 0;
137 let r_point: Option<AffinePoint> =
138 AffinePoint::decompress(r_bytes, u8::from(y_is_odd).into()).into();
139 let Some(r_point) = r_point else {
140 return Err(CryptoError::RecoveryFailed);
141 };
142
143 let r_proj = ProjectivePoint::from(r_point);
145 let z = <Scalar as Reduce<k256::U256>>::reduce_bytes(k256::FieldBytes::from_slice(msg));
146 let r_inv: Option<Scalar> = r.invert_vartime().into();
147 let Some(r_inv) = r_inv else {
148 return Err(CryptoError::RecoveryFailed);
149 };
150 let u1 = -(r_inv * z);
151 let u2 = r_inv * s;
152 let pk = ProjectivePoint::lincomb(&ProjectivePoint::GENERATOR, &u1, &r_proj, &u2);
153
154 let pk_affine = pk.to_affine();
155 if bool::from(pk_affine.is_identity()) {
156 return Err(CryptoError::RecoveryFailed);
157 }
158 let uncompressed = pk_affine.to_encoded_point(false);
159 let hash = crate::keccak::keccak_hash(&uncompressed.as_bytes()[1..]);
160 Ok(hash)
161 }
162
163 fn recover_signer(&self, sig: &[u8; 65], msg: &[u8; 32]) -> Result<Address, CryptoError> {
166 const SECP256K1_N_HALF: [u8; 32] =
168 hex_literal::hex!("7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0");
169 if sig[32..64] > SECP256K1_N_HALF[..] {
170 return Err(CryptoError::InvalidSignature);
171 }
172
173 let hash = self.secp256k1_ecrecover(
174 sig[..64]
175 .try_into()
176 .map_err(|_| CryptoError::InvalidSignature)?,
177 sig[64],
178 msg,
179 )?;
180 Ok(Address::from_slice(&hash[12..]))
181 }
182
183 fn keccak256(&self, input: &[u8]) -> [u8; 32] {
187 crate::keccak::keccak_hash(input)
188 }
189
190 fn sha256(&self, input: &[u8]) -> [u8; 32] {
192 sha2::Sha256::digest(input).into()
193 }
194
195 fn ripemd160(&self, input: &[u8]) -> [u8; 32] {
197 let mut hasher = ripemd::Ripemd160::new();
198 hasher.update(input);
199 let result = hasher.finalize();
200
201 let mut output = [0u8; 32];
202 output[12..].copy_from_slice(&result);
203 output
204 }
205
206 fn bn254_g1_add(&self, p1: &[u8], p2: &[u8]) -> Result<[u8; 64], CryptoError> {
212 use ark_bn254::Fq;
213 use ark_ec::CurveGroup;
214 use ark_ff::{BigInteger, PrimeField as _, Zero};
215
216 let parse_point = |bytes: &[u8]| -> Result<ark_bn254::G1Affine, CryptoError> {
217 if bytes.len() < 64 {
218 return Err(CryptoError::InvalidInput("G1 point must be 64 bytes"));
219 }
220 let x = Fq::from_be_bytes_mod_order(&bytes[..32]);
221 let y = Fq::from_be_bytes_mod_order(&bytes[32..64]);
222
223 if x.is_zero() && y.is_zero() {
224 return Ok(ark_bn254::G1Affine::identity());
225 }
226
227 let point = ark_bn254::G1Affine::new_unchecked(x, y);
228 if !point.is_on_curve() {
229 return Err(CryptoError::InvalidPoint("G1 point not on curve"));
230 }
231 Ok(point)
232 };
233
234 let pt1 = parse_point(p1)?;
235 let pt2 = parse_point(p2)?;
236
237 #[allow(clippy::arithmetic_side_effects)]
238 let sum = (pt1 + pt2).into_affine();
239
240 let mut out = [0u8; 64];
241 out[..32].copy_from_slice(&sum.x.into_bigint().to_bytes_be());
242 out[32..].copy_from_slice(&sum.y.into_bigint().to_bytes_be());
243 Ok(out)
244 }
245
246 fn bn254_g1_mul(&self, point: &[u8], scalar: &[u8]) -> Result<[u8; 64], CryptoError> {
250 use ark_bn254::{Fq, Fr as FrArk};
251 use ark_ec::CurveGroup;
252 use ark_ff::{BigInteger, PrimeField as _, Zero};
253 use core::ops::Mul as _;
254
255 if point.len() < 64 || scalar.len() < 32 {
256 return Err(CryptoError::InvalidInput("invalid input length"));
257 }
258
259 let x = Fq::from_be_bytes_mod_order(&point[..32]);
260 let y = Fq::from_be_bytes_mod_order(&point[32..64]);
261
262 if x.is_zero() && y.is_zero() {
263 return Ok([0u8; 64]);
264 }
265
266 let pt = ark_bn254::G1Affine::new_unchecked(x, y);
267 if !pt.is_on_curve() {
268 return Err(CryptoError::InvalidPoint("G1 point not on curve"));
269 }
270
271 let s = FrArk::from_be_bytes_mod_order(scalar);
272 if s.is_zero() {
273 return Ok([0u8; 64]);
274 }
275
276 let result = pt.mul(s).into_affine();
277
278 let mut out = [0u8; 64];
279 out[..32].copy_from_slice(&result.x.into_bigint().to_bytes_be());
280 out[32..].copy_from_slice(&result.y.into_bigint().to_bytes_be());
281 Ok(out)
282 }
283
284 fn bn254_pairing_check(&self, pairs: &[(&[u8], &[u8])]) -> Result<bool, CryptoError> {
288 use ark_bn254::{Bn254, Fq, G1Affine, G2Affine};
289 use ark_ec::pairing::Pairing;
290 use ark_ff::{One, PrimeField as _, QuadExtField, Zero};
291
292 let mut g1_points = Vec::with_capacity(pairs.len());
293 let mut g2_points = Vec::with_capacity(pairs.len());
294
295 for (g1_bytes, g2_bytes) in pairs {
296 if g1_bytes.len() < 64 {
297 return Err(CryptoError::InvalidInput("G1 must be 64 bytes"));
298 }
299 let g1x = Fq::from_be_bytes_mod_order(&g1_bytes[..32]);
300 let g1y = Fq::from_be_bytes_mod_order(&g1_bytes[32..64]);
301
302 let g1 = if g1x.is_zero() && g1y.is_zero() {
303 G1Affine::identity()
304 } else {
305 let p = G1Affine::new_unchecked(g1x, g1y);
306 if !p.is_on_curve() || !p.is_in_correct_subgroup_assuming_on_curve() {
307 return Err(CryptoError::InvalidPoint("G1 not on BN254 curve"));
308 }
309 p
310 };
311 g1_points.push(g1);
312
313 if g2_bytes.len() < 128 {
314 return Err(CryptoError::InvalidInput("G2 must be 128 bytes"));
315 }
316
317 let g2_x_im = Fq::from_be_bytes_mod_order(&g2_bytes[..32]);
318 let g2_x_re = Fq::from_be_bytes_mod_order(&g2_bytes[32..64]);
319 let g2_y_im = Fq::from_be_bytes_mod_order(&g2_bytes[64..96]);
320 let g2_y_re = Fq::from_be_bytes_mod_order(&g2_bytes[96..128]);
321
322 let g2 =
323 if g2_x_im.is_zero() && g2_x_re.is_zero() && g2_y_im.is_zero() && g2_y_re.is_zero()
324 {
325 G2Affine::identity()
326 } else {
327 let p = G2Affine::new_unchecked(
328 QuadExtField::new(g2_x_re, g2_x_im),
329 QuadExtField::new(g2_y_re, g2_y_im),
330 );
331 if !p.is_on_curve() || !p.is_in_correct_subgroup_assuming_on_curve() {
332 return Err(CryptoError::InvalidPoint("G2 not on BN254 curve"));
333 }
334 p
335 };
336 g2_points.push(g2);
337 }
338
339 Ok(Bn254::multi_pairing(g1_points, g2_points).0 == QuadExtField::one())
340 }
341
342 #[cfg(feature = "std")]
347 fn modexp(&self, base: &[u8], exp: &[u8], modulus: &[u8]) -> Result<Vec<u8>, CryptoError> {
348 use malachite::base::num::arithmetic::traits::ModPow as _;
349 use malachite::base::num::basic::traits::Zero as _;
350 use malachite::{Natural, base::num::conversion::traits::*};
351
352 let base_nat = Natural::from_power_of_2_digits_desc(8u64, base.iter().cloned())
353 .ok_or(CryptoError::InvalidInput("base"))?;
354 let exp_nat = Natural::from_power_of_2_digits_desc(8u64, exp.iter().cloned())
355 .ok_or(CryptoError::InvalidInput("exponent"))?;
356 let mod_nat = Natural::from_power_of_2_digits_desc(8u64, modulus.iter().cloned())
357 .ok_or(CryptoError::InvalidInput("modulus"))?;
358
359 let result = if mod_nat == Natural::ZERO {
360 Natural::ZERO
361 } else if exp_nat == Natural::ZERO {
362 Natural::from(1_u8) % &mod_nat
363 } else {
364 let base_mod = base_nat % &mod_nat;
365 base_mod.mod_pow(&exp_nat, &mod_nat)
366 };
367
368 let res_bytes: Vec<u8> = result.to_power_of_2_digits_desc(8);
369 pad_modexp_output(res_bytes, modulus.len())
370 }
371
372 #[cfg(not(feature = "std"))]
373 fn modexp(&self, base: &[u8], exp: &[u8], modulus: &[u8]) -> Result<Vec<u8>, CryptoError> {
374 use num_bigint::BigUint;
375
376 let base_nat = BigUint::from_bytes_be(base);
377 let exp_nat = BigUint::from_bytes_be(exp);
378 let mod_nat = BigUint::from_bytes_be(modulus);
379
380 let result = if mod_nat == BigUint::ZERO {
381 BigUint::ZERO
382 } else if exp_nat == BigUint::ZERO {
383 BigUint::from(1_u8) % &mod_nat
384 } else {
385 base_nat.modpow(&exp_nat, &mod_nat)
386 };
387
388 let res_bytes = result.to_bytes_be();
389 pad_modexp_output(res_bytes, modulus.len())
390 }
391
392 fn mulmod256(&self, a: &[u8; 32], b: &[u8; 32], m: &[u8; 32]) -> [u8; 32] {
396 let a = ethereum_types::U256::from_big_endian(a);
397 let b = ethereum_types::U256::from_big_endian(b);
398 let m = ethereum_types::U256::from_big_endian(m);
399
400 let result = if m.is_zero() {
401 ethereum_types::U256::zero()
402 } else {
403 let product = a.full_mul(b);
404 let m512 = ethereum_types::U512::from(m);
405 if product < m512 {
406 product.try_into().unwrap_or(ethereum_types::U256::zero())
408 } else {
409 let (_, rem) = product.div_mod(m512);
410 rem.try_into().unwrap_or(ethereum_types::U256::zero())
411 }
412 };
413
414 result.to_big_endian()
415 }
416
417 fn blake2_compress(&self, rounds: u32, h: &mut [u64; 8], m: [u64; 16], t: [u64; 2], f: bool) {
421 #[allow(clippy::as_conversions)]
422 crate::blake2f::blake2b_f(rounds as usize, h, &m, &t, f);
423 }
424
425 fn secp256r1_verify(&self, msg: &[u8; 32], sig: &[u8; 64], pk: &[u8; 64]) -> bool {
429 use p256::{
430 EncodedPoint,
431 ecdsa::{Signature as P256Signature, signature::hazmat::PrehashVerifier},
432 elliptic_curve::bigint::U256 as P256Uint,
433 };
434
435 let r = P256Uint::from_be_slice(&sig[..32]);
436 let s = P256Uint::from_be_slice(&sig[32..]);
437
438 const P256_N: P256Uint = P256Uint::from_be_hex(
439 "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
440 );
441
442 if r == P256Uint::ZERO || r >= P256_N || s == P256Uint::ZERO || s >= P256_N {
443 return false;
444 }
445
446 let x_bytes: &[u8; 32] = match pk[..32].try_into() {
447 Ok(b) => b,
448 Err(_) => return false,
449 };
450 let y_bytes: &[u8; 32] = match pk[32..].try_into() {
451 Ok(b) => b,
452 Err(_) => return false,
453 };
454
455 let Ok(verifier) = p256::ecdsa::VerifyingKey::from_encoded_point(
456 &EncodedPoint::from_affine_coordinates(x_bytes.into(), y_bytes.into(), false),
457 ) else {
458 return false;
459 };
460
461 let r_arr: [u8; 32] = sig[..32].try_into().unwrap_or([0u8; 32]);
462 let s_arr: [u8; 32] = sig[32..].try_into().unwrap_or([0u8; 32]);
463
464 let Ok(signature) = P256Signature::from_scalars(r_arr, s_arr) else {
465 return false;
466 };
467
468 verifier.verify_prehash(msg, &signature).is_ok()
469 }
470
471 #[cfg(feature = "c-kzg")]
475 fn verify_kzg_proof(
476 &self,
477 z: &[u8; 32],
478 y: &[u8; 32],
479 commitment: &[u8; 48],
480 proof: &[u8; 48],
481 ) -> Result<(), CryptoError> {
482 let c_kzg_settings = c_kzg::ethereum_kzg_settings(crate::kzg::KZG_PRECOMPUTE);
483 c_kzg_settings
484 .verify_kzg_proof(
485 &(*commitment).into(),
486 &(*z).into(),
487 &(*y).into(),
488 &(*proof).into(),
489 )
490 .map_err(|e| CryptoError::Other(e.to_string()))
491 .and_then(|valid| {
492 if valid {
493 Ok(())
494 } else {
495 Err(CryptoError::VerificationFailed)
496 }
497 })
498 }
499
500 #[cfg(not(feature = "c-kzg"))]
501 fn verify_kzg_proof(
502 &self,
503 z: &[u8; 32],
504 y: &[u8; 32],
505 commitment: &[u8; 48],
506 proof: &[u8; 48],
507 ) -> Result<(), CryptoError> {
508 crate::kzg::verify_kzg_proof(*commitment, *z, *y, *proof)
509 .map_err(|e| CryptoError::Other(e.to_string()))
510 .and_then(|valid| {
511 if valid {
512 Ok(())
513 } else {
514 Err(CryptoError::VerificationFailed)
515 }
516 })
517 }
518
519 #[cfg(feature = "c-kzg")]
521 fn verify_blob_kzg_proof(
522 &self,
523 blob: &[u8],
524 commitment: &[u8; 48],
525 proof: &[u8; 48],
526 ) -> Result<bool, CryptoError> {
527 use crate::kzg::BYTES_PER_BLOB;
528
529 let blob_arr: [u8; BYTES_PER_BLOB] = blob
530 .try_into()
531 .map_err(|_| CryptoError::InvalidInput("blob must be 131072 bytes"))?;
532
533 let c_kzg_settings = c_kzg::ethereum_kzg_settings(crate::kzg::KZG_PRECOMPUTE);
534 c_kzg_settings
535 .verify_blob_kzg_proof(&blob_arr.into(), &(*commitment).into(), &(*proof).into())
536 .map_err(|e| CryptoError::Other(e.to_string()))
537 }
538
539 #[cfg(not(feature = "c-kzg"))]
540 fn verify_blob_kzg_proof(
541 &self,
542 blob: &[u8],
543 commitment: &[u8; 48],
544 proof: &[u8; 48],
545 ) -> Result<bool, CryptoError> {
546 use crate::kzg::BYTES_PER_BLOB;
547
548 let blob_arr: [u8; BYTES_PER_BLOB] = blob
549 .try_into()
550 .map_err(|_| CryptoError::InvalidInput("blob must be 131072 bytes"))?;
551
552 crate::kzg::verify_blob_kzg_proof(blob_arr, *commitment, *proof)
553 .map_err(|e| CryptoError::Other(e.to_string()))
554 }
555
556 fn bls12_381_g1_add(
568 &self,
569 a: ([u8; 48], [u8; 48]),
570 b: ([u8; 48], [u8; 48]),
571 ) -> Result<[u8; 96], CryptoError> {
572 #[cfg(feature = "blst")]
573 {
574 crate::bls_blst::g1_add(a, b)
575 }
576 #[cfg(not(feature = "blst"))]
577 {
578 let _ = (a, b);
579 Err(CryptoError::Unsupported(BLS_UNSUPPORTED))
580 }
581 }
582
583 #[allow(clippy::type_complexity)]
585 fn bls12_381_g1_msm(
586 &self,
587 pairs: &[(([u8; 48], [u8; 48]), [u8; 32])],
588 ) -> Result<[u8; 96], CryptoError> {
589 #[cfg(feature = "blst")]
590 {
591 crate::bls_blst::g1_msm(pairs)
592 }
593 #[cfg(not(feature = "blst"))]
594 {
595 let _ = pairs;
596 Err(CryptoError::Unsupported(BLS_UNSUPPORTED))
597 }
598 }
599
600 fn bls12_381_g2_add(
602 &self,
603 a: ([u8; 48], [u8; 48], [u8; 48], [u8; 48]),
604 b: ([u8; 48], [u8; 48], [u8; 48], [u8; 48]),
605 ) -> Result<[u8; 192], CryptoError> {
606 #[cfg(feature = "blst")]
607 {
608 crate::bls_blst::g2_add(a, b)
609 }
610 #[cfg(not(feature = "blst"))]
611 {
612 let _ = (a, b);
613 Err(CryptoError::Unsupported(BLS_UNSUPPORTED))
614 }
615 }
616
617 #[allow(clippy::type_complexity)]
619 fn bls12_381_g2_msm(
620 &self,
621 pairs: &[(([u8; 48], [u8; 48], [u8; 48], [u8; 48]), [u8; 32])],
622 ) -> Result<[u8; 192], CryptoError> {
623 #[cfg(feature = "blst")]
624 {
625 crate::bls_blst::g2_msm(pairs)
626 }
627 #[cfg(not(feature = "blst"))]
628 {
629 let _ = pairs;
630 Err(CryptoError::Unsupported(BLS_UNSUPPORTED))
631 }
632 }
633
634 #[allow(clippy::type_complexity)]
636 fn bls12_381_pairing_check(
637 &self,
638 pairs: &[(
639 ([u8; 48], [u8; 48]),
640 ([u8; 48], [u8; 48], [u8; 48], [u8; 48]),
641 )],
642 ) -> Result<bool, CryptoError> {
643 #[cfg(feature = "blst")]
644 {
645 crate::bls_blst::pairing_check(pairs)
646 }
647 #[cfg(not(feature = "blst"))]
648 {
649 let _ = pairs;
650 Err(CryptoError::Unsupported(BLS_UNSUPPORTED))
651 }
652 }
653
654 fn bls12_381_fp_to_g1(&self, fp: &[u8; 48]) -> Result<[u8; 96], CryptoError> {
656 #[cfg(feature = "blst")]
657 {
658 crate::bls_blst::fp_to_g1(fp)
659 }
660 #[cfg(not(feature = "blst"))]
661 {
662 let _ = fp;
663 Err(CryptoError::Unsupported(BLS_UNSUPPORTED))
664 }
665 }
666
667 fn bls12_381_fp2_to_g2(&self, fp2: ([u8; 48], [u8; 48])) -> Result<[u8; 192], CryptoError> {
669 #[cfg(feature = "blst")]
670 {
671 crate::bls_blst::fp2_to_g2(fp2)
672 }
673 #[cfg(not(feature = "blst"))]
674 {
675 let _ = fp2;
676 Err(CryptoError::Unsupported(BLS_UNSUPPORTED))
677 }
678 }
679}
680
681fn pad_modexp_output(res_bytes: Vec<u8>, modulus_len: usize) -> Result<Vec<u8>, CryptoError> {
685 let mut out = vec![0u8; modulus_len];
686 if res_bytes.len() <= modulus_len {
687 let offset = modulus_len - res_bytes.len();
688 out[offset..].copy_from_slice(&res_bytes);
689 } else {
690 out.copy_from_slice(&res_bytes[res_bytes.len() - modulus_len..]);
691 }
692 Ok(out)
693}
694
695