{"record":{"id":"40b517e6a134e02f","repo":"diem/diem","slug":"error-40b517","errorCode":null,"errorMessage":"{:?}","messagePattern":"\\{:\\?\\}","errorType":"error_code","errorClass":"CryptoMaterialError","httpStatus":null,"severity":"error","filePath":"crates/diem-crypto/src/traits.rs","lineNumber":25,"sourceCode":"//! [`bls12381`] modules.\n\nuse crate::hash::CryptoHash;\nuse anyhow::Result;\nuse core::convert::{From, TryFrom};\nuse rand::{rngs::StdRng, CryptoRng, RngCore, SeedableRng};\nuse serde::{de::DeserializeOwned, Serialize};\nuse std::{fmt::Debug, hash::Hash};\nuse thiserror::Error;\n\n/// An error type for key and signature validation issues, see [`ValidCryptoMaterial`][ValidCryptoMaterial].\n///\n/// This enum reflects there are two interesting causes of validation\n/// failure for the ingestion of key or signature material: deserialization errors\n/// (often, due to mangled material or curve equation failure for ECC) and\n/// validation errors (material recognizable but unacceptable for use,\n/// e.g. unsafe).\n#[derive(Clone, Debug, PartialEq, Eq, Error)]\n#[error(\"{:?}\", self)]\npub enum CryptoMaterialError {\n    /// Struct to be signed does not serialize correctly.\n    SerializationError,\n    /// Key or signature material does not deserialize correctly.\n    DeserializationError,\n    /// Key or signature material deserializes, but is otherwise not valid.\n    ValidationError,\n    /// Key, threshold or signature material does not have the expected size.\n    WrongLengthError,\n    /// Part of the signature or key is not canonical resulting to malleability issues.\n    CanonicalRepresentationError,\n    /// A curve point (i.e., a public key) lies on a small group.\n    SmallSubgroupError,\n    /// A curve point (i.e., a public key) does not satisfy the curve equation.\n    PointNotOnCurveError,\n    /// BitVec errors in accountable multi-sig schemes.\n    BitVecError(String),\n}","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/diem/diem/blob/fc4714a8ea273b6efe8b13dbce72ea60aad9a16c/crates/diem-crypto/src/traits.rs#L7-L43","documentation":"This is the Display output of `CryptoMaterialError`, an enum in diem-crypto that models the two causes of key/signature validation failure: deserialization errors (mangled material, curve equation failures) and validation errors (material parses but is unsafe or unacceptable). Because the enum derives `#[error(\"{:?}\", self)]`, the error message is just the Debug repr of the variant (e.g. `DeserializationError`), so you see the literal string `{:?}` in generic reporting contexts. It is thrown whenever key or signature material fails to load or validate.","triggerScenarios":"Calling deserialization/validation APIs such as `Ed25519PrivateKey::from_encoded_string`, `try_from(&[u8])`, `from_bytes`, or `ValidCryptoMaterial::validating` checks with malformed, wrong-length, or curve-invalid bytes.","commonSituations":"Pasting hex/base64 keys with whitespace or wrong encoding, truncating key material when copying from config, loading keys generated by a different curve or library version, signature bytes corrupted in transit.","solutions":["Check the concrete variant in the Debug output: DeserializationError means the bytes are mangled; a validation variant means the material parses but is rejected.","Re-encode the key material in the exact format the API expects (hex vs base64, correct length) and retry.","Regenerate the key/secret if the source material is corrupted or from an incompatible scheme."],"exampleFix":"// before\nlet key = Ed25519PrivateKey::from_encoded_string(key_hex.trim().trim_start_matches(\"0x\"))?;\n// after\nlet key = Ed25519PrivateKey::from_encoded_string(key_hex.trim())\n    .map_err(|e| format!(\"invalid private key: {:?}\", e))?;","handlingStrategy":"validation","validationCode":"fn valid_key_material(bytes: &[u8]) -> Result<(), String> {\n    if bytes.len() != 32 {\n        return Err(format!(\"expected 32 bytes, got {}\", bytes.len()));\n    }\n    hex::decode(hex::encode(bytes)).map_err(|e| format!(\"not valid hex material: {}\", e))?;\n    Ok(())\n}\n// call before Ed25519PrivateKey::try_from(bytes)","typeGuard":"fn is_crypto_material_error(e: &(dyn std::error::Error + 'static)) -> Option<&diem_crypto::CryptoMaterialError> {\n    e.downcast_ref::<diem_crypto::CryptoMaterialError>()\n}","tryCatchPattern":"match Ed25519PrivateKey::from_encoded_string(s) {\n    Ok(k) => k,\n    Err(e @ CryptoMaterialError::DeserializationError) => { log::warn!(\"mangled key material: {:?}\", e); return Err(e.into()); }\n    Err(e) => { log::error!(\"key validation rejected: {:?}\", e); return Err(e.into()); }\n}","preventionTips":["Trim and normalize encoding (hex/base64, optional 0x prefix) before parsing key material","Assert key byte length matches the scheme (32 bytes for Ed25519) before calling the API","Store keys in a single canonical encoding in config and never round-trip through lossy string ops","Log the Debug form of CryptoMaterialError to distinguish deserialization vs validation failures"],"tags":["crypto","deserialization","validation","rust"],"backgroundTag":"key-deserialization-failed","analyzedSha":"fc4714a8ea273b6efe8b13dbce72ea60aad9a16c","analyzedAt":"2026-09-04T21:07:05.890Z","contentChangedAt":"2026-09-04T21:07:05.890Z","schemaVersion":2},"datasetVersion":"2026-09-12T02:17:10.037Z"}