Skip to main content

veil_crypto/
lib.rs

1//! Veil Crypto Library
2//!
3//! Pure Rust implementation of Veil's cryptographic primitives for RingCT transactions.
4//! This library reimplements the Veil-specific functions from the C secp256k1 fork.
5
6#![deny(clippy::all)]
7#![warn(clippy::pedantic)]
8#![warn(missing_docs)]
9
10pub mod ecdh;
11pub mod ecdsa;  // ECDSA signing for CT inputs
12pub mod keyimage;
13pub mod pedersen;
14pub mod borromean;
15pub mod rangeproof;
16pub mod mlsag;  // Pure Rust implementation!
17pub mod utils;  // Pure Rust utilities
18// TODO: Reimplement these in pure Rust (currently use C FFI)
19// pub mod stealth;
20
21// Re-export main functions for easy access
22pub use ecdh::ecdh_veil;
23pub use ecdsa::{ecdsa_sign, ecdsa_sign_compact, ecdsa_get_public_key};
24pub use keyimage::get_keyimage;
25pub use pedersen::{pedersen_blind_sum, pedersen_commit};
26pub use borromean::{borromean_hash, borromean_sign, borromean_verify};
27pub use rangeproof::{rangeproof_sign, rangeproof_verify, rangeproof_rewind, RangeProofSignResult, RangeProofVerifyResult, RangeProofRewindResult};
28pub use mlsag::{prepare_mlsag, generate_mlsag, verify_mlsag, PrepareMlsagResult, GenerateMlsagResult};
29pub use utils::{derive_pubkey, point_add_scalar, point_multiply, private_add};
30
31// Re-export k256 types for users who need them
32pub use k256::{PublicKey, SecretKey};
33
34/// Error types for Veil crypto operations
35#[derive(Debug, Clone, PartialEq, Eq)]
36pub enum VeilCryptoError {
37    /// Invalid public key
38    InvalidPublicKey,
39    /// Invalid secret key
40    InvalidSecretKey,
41    /// Invalid signature
42    InvalidSignature,
43    /// Invalid commitment
44    InvalidCommitment,
45    /// Invalid range proof
46    InvalidRangeProof,
47    /// MLSAG generation failed
48    MlsagGenerationFailed,
49    /// MLSAG verification failed
50    MlsagVerificationFailed,
51    /// Invalid input parameter
52    InvalidInput(String),
53    /// Signature creation/verification error
54    SignatureError(String),
55    /// Generic error with message
56    Other(String),
57}
58
59impl std::fmt::Display for VeilCryptoError {
60    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61        match self {
62            Self::InvalidPublicKey => write!(f, "Invalid public key"),
63            Self::InvalidSecretKey => write!(f, "Invalid secret key"),
64            Self::InvalidSignature => write!(f, "Invalid signature"),
65            Self::InvalidCommitment => write!(f, "Invalid commitment"),
66            Self::InvalidRangeProof => write!(f, "Invalid range proof"),
67            Self::MlsagGenerationFailed => write!(f, "MLSAG generation failed"),
68            Self::MlsagVerificationFailed => write!(f, "MLSAG verification failed"),
69            Self::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
70            Self::SignatureError(msg) => write!(f, "Signature error: {}", msg),
71            Self::Other(msg) => write!(f, "{}", msg),
72        }
73    }
74}
75
76impl std::error::Error for VeilCryptoError {}
77
78/// Result type for Veil crypto operations
79pub type Result<T> = std::result::Result<T, VeilCryptoError>;
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    #[test]
86    fn test_error_display() {
87        let err = VeilCryptoError::InvalidPublicKey;
88        assert_eq!(format!("{}", err), "Invalid public key");
89    }
90}