Skip to main content

qualia_core_db/crypto/network/
dual_sign.rs

1//! CRY-02.05 **PARTIAL** — dual ML-DSA-65 + Ed25519 proof over one payload.
2//!
3//! Both algorithms sign and verify the **same** caller `message` and `context`
4//! bytes. Binding is the existing length-delimited adapters
5//! ([`crate::crypto::network::mldsa`] SHA-384-compressed FIPS-204 ctx, and
6//! [`crate::crypto::network::ed25519`] versioned `u16be||label||u32be||ctx||u32be||msg`).
7//! This wave does **not** invent a deterministic-CBOR COSE_Sign1 object.
8//! [`cose_sign1_frozen`] is false: the CRY-02.05 COSE_Sign1 object freeze
9//! remains open.
10//!
11//! Dual proof: **both** signatures must verify. ML-DSA-only or Ed25519-only
12//! (including a stripped all-zero counterpart) is [`CryptoError::Unauthorized`].
13//! [`single_algorithm_is_enough`] is false.
14//!
15//! Draft until FND-03 freeze. No security proof is claimed.
16
17use crate::crypto::network::ed25519::{sign_with_context, verify_with_context};
18use crate::crypto::network::errors::CryptoError;
19use crate::crypto::network::mldsa;
20use crate::crypto::network::types::{
21    ED25519_PK_LEN, ED25519_SIG_LEN, ML_DSA_65_PK_LEN, ML_DSA_65_SIG_LEN, ML_DSA_65_SK_LEN,
22};
23
24/// Paired signatures over one bound payload. Both fields are required.
25#[repr(C)]
26#[derive(Clone, Copy, Debug, PartialEq, Eq)]
27pub struct DualProof {
28    pub mldsa_sig: [u8; ML_DSA_65_SIG_LEN],
29    pub ed25519_sig: [u8; ED25519_SIG_LEN],
30}
31
32/// Sign `message` under `context` with both ML-DSA-65 and Ed25519.
33///
34/// Same `message` and same `context` bytes are passed to both adapters.
35/// A failed call may leave `out` partially written; the caller must not
36/// treat it as a valid proof.
37pub fn sign_dual(
38    mldsa_sk: &[u8; ML_DSA_65_SK_LEN],
39    ed25519_seed: &[u8; 32],
40    message: &[u8],
41    context: &[u8],
42    out: &mut DualProof,
43) -> Result<(), CryptoError> {
44    mldsa::sign(mldsa_sk, message, context, &mut out.mldsa_sig)?;
45    sign_with_context(ed25519_seed, message, context, &mut out.ed25519_sig)?;
46    Ok(())
47}
48
49/// Verify ML-DSA-65 then Ed25519 over the same `message` and `context`.
50///
51/// Either signature failing is [`CryptoError::Unauthorized`]. This does not
52/// short-circuit into `Ok` after a single-algorithm success. Returning
53/// `Unauthorized` on the first failure (without attempting the second) is
54/// permitted; ML-DSA is checked first.
55pub fn verify_dual(
56    mldsa_pk: &[u8; ML_DSA_65_PK_LEN],
57    ed25519_pk: &[u8; ED25519_PK_LEN],
58    message: &[u8],
59    context: &[u8],
60    proof: &DualProof,
61) -> Result<(), CryptoError> {
62    mldsa::verify(mldsa_pk, message, context, &proof.mldsa_sig)?;
63    verify_with_context(ed25519_pk, message, context, &proof.ed25519_sig)?;
64    Ok(())
65}
66
67/// One algorithm is never sufficient for a dual proof.
68pub fn single_algorithm_is_enough() -> bool {
69    false
70}
71
72/// Honest: the CRY-02.05 COSE_Sign1 / deterministic-CBOR object is not frozen.
73pub fn cose_sign1_frozen() -> bool {
74    false
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80    use crate::crypto::network::ed25519::public_from_seed;
81    use crate::crypto::network::mldsa::generate_keypair;
82
83    const MSG: &[u8] = b"qdnf-dual-payload";
84    const CTX: &[u8] = b"qlink";
85    const SEED: [u8; 32] = [7u8; 32];
86
87    fn fresh_signed() -> ([u8; ML_DSA_65_PK_LEN], [u8; ED25519_PK_LEN], DualProof) {
88        let (sk, pk) = generate_keypair().unwrap();
89        let ed_pk = public_from_seed(&SEED);
90        let mut proof = DualProof {
91            mldsa_sig: [0u8; ML_DSA_65_SIG_LEN],
92            ed25519_sig: [0u8; ED25519_SIG_LEN],
93        };
94        sign_dual(&sk, &SEED, MSG, CTX, &mut proof).unwrap();
95        (pk, ed_pk, proof)
96    }
97
98    #[test]
99    fn generate_sign_verify_ok() {
100        let (mldsa_pk, ed_pk, proof) = fresh_signed();
101        assert!(verify_dual(&mldsa_pk, &ed_pk, MSG, CTX, &proof).is_ok());
102    }
103
104    #[test]
105    fn wrong_ed25519_sig_is_unauthorized() {
106        let (mldsa_pk, ed_pk, mut proof) = fresh_signed();
107        proof.ed25519_sig[0] ^= 0x01;
108        assert_eq!(
109            verify_dual(&mldsa_pk, &ed_pk, MSG, CTX, &proof),
110            Err(CryptoError::Unauthorized)
111        );
112        assert!(mldsa::verify(&mldsa_pk, MSG, CTX, &proof.mldsa_sig).is_ok());
113    }
114
115    #[test]
116    fn wrong_context_is_unauthorized() {
117        let (mldsa_pk, ed_pk, proof) = fresh_signed();
118        assert_eq!(
119            verify_dual(&mldsa_pk, &ed_pk, MSG, b"qsession", &proof),
120            Err(CryptoError::Unauthorized)
121        );
122    }
123
124    #[test]
125    fn single_algorithm_is_not_enough() {
126        assert!(!single_algorithm_is_enough());
127    }
128
129    #[test]
130    fn cose_sign1_is_not_frozen() {
131        assert!(!cose_sign1_frozen());
132    }
133
134    #[test]
135    fn stripped_ed25519_is_unauthorized() {
136        let (mldsa_pk, ed_pk, mut proof) = fresh_signed();
137        proof.ed25519_sig = [0u8; ED25519_SIG_LEN];
138        assert_eq!(
139            verify_dual(&mldsa_pk, &ed_pk, MSG, CTX, &proof),
140            Err(CryptoError::Unauthorized)
141        );
142        assert!(mldsa::verify(&mldsa_pk, MSG, CTX, &proof.mldsa_sig).is_ok());
143    }
144}