1mod aggregate;
9pub mod curve;
10mod entropy;
11mod hash;
12pub use entropy::{MAX_RANDOM_BYTES, fill_random};
13mod keys;
14mod secret_bytes;
15mod signature_metadata;
16mod signatures;
17pub use ruint::aliases::U256;
19pub use secret_bytes::SecretBytes;
20pub use signature_metadata::{SignatureMetadata, legacy_chain_id, legacy_chain_v, normalized_v};
21
22pub use aggregate::{MAX_AGGREGATE_KEYS, OrderedKeyAggregate};
23pub use hash::{
24 MESSAGE_PREFIX, hash_message, hmac_sha256, hmac_sha512, keccak256, ripemd160, sha256, sha512,
25 verify_hmac_sha256, verify_hmac_sha512,
26};
27pub use keys::{PublicKey, SecretKey};
28pub use signatures::{RecoverableSignature, SchnorrPublicKey, SchnorrSignature};
29
30pub fn recover_message_signer(
34 message: &[u8],
35 signature: &RecoverableSignature,
36) -> Result<quai_primitives::Address, CryptoError> {
37 signature
38 .recover_prehash(&hash_message(message))
39 .map(PublicKey::address)
40}
41
42#[derive(Clone, Copy, Debug, Eq, PartialEq)]
44#[non_exhaustive]
45pub enum CryptoError {
46 InvalidScalar,
48 HashLimit,
50 InvalidSecretKey,
52 InvalidKeyCount,
54 InvalidKeyAggregation,
56 InvalidSecretKeySet,
58 InvalidPublicKey,
60 InvalidSignature,
62 HighS,
64 InvalidRecoveryId,
66 UnsupportedRecoveryId,
68 SigningFailed,
70 VerificationFailed,
72 RandomnessUnavailable,
74 RandomRequestTooLarge,
76}
77
78impl core::fmt::Display for CryptoError {
79 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
80 f.write_str(match self {
81 Self::InvalidScalar => "invalid secp256k1 scalar",
82 Self::HashLimit => "hash input exceeds resource limit",
83 Self::InvalidSecretKey => "invalid secp256k1 secret key",
84 Self::InvalidKeyCount => "invalid ordered aggregation key count",
85 Self::InvalidKeyAggregation => "invalid ordered public key aggregate",
86 Self::InvalidSecretKeySet => "secret keys do not match ordered aggregation context",
87 Self::InvalidPublicKey => "invalid secp256k1 public key",
88 Self::InvalidSignature => "invalid signature",
89 Self::HighS => "noncanonical high-S signature",
90 Self::InvalidRecoveryId => "invalid recovery identifier",
91 Self::UnsupportedRecoveryId => {
92 "recovery identifier cannot be represented by Quai parity"
93 }
94 Self::SigningFailed => "signature generation failed",
95 Self::VerificationFailed => "signature verification failed",
96 Self::RandomnessUnavailable => "cryptographic randomness unavailable",
97 Self::RandomRequestTooLarge => "random output exceeds resource limit",
98 })
99 }
100}
101impl std::error::Error for CryptoError {}