truthlinked-core 0.1.0

Core protocol primitives for the TruthLinked post-quantum blockchain.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Post-quantum identity helpers for TruthLinked accounts.
//!
//! The module provides ML-DSA signing, ML-KEM key material, deterministic account
//! derivation, and keyfile helpers shared by node and CLI code. Signing contexts
//! are domain-separated through constants from `truthlinked_core::constants`.

use fips203::ml_kem_768;
use fips203::traits::SerDes as KyberSerDes;
use fips203::traits::{Decaps, Encaps, KeyGen};
use fips204::ml_dsa_65;
use fips204::traits::{SerDes, Signer, Verifier};
use rand_chacha::ChaCha20Rng;
use rand_core::SeedableRng;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;

use crate::constants::{GENERIC_SIGN_CONTEXT, TX_SIGN_CONTEXT};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PQKeypair {
    pub secret: Vec<u8>,
    pub public: Vec<u8>,
}

#[derive(Clone)]
pub struct DualKeypair {
    pub dilithium_pk: ml_dsa_65::PublicKey,
    pub dilithium_sk: ml_dsa_65::PrivateKey,
    pub kyber_ek: ml_kem_768::EncapsKey,
    pub kyber_dk: ml_kem_768::DecapsKey,
    pub mnemonic: String,
}

impl PQKeypair {
    pub fn generate() -> Self {
        let (pk, sk) =
            ml_dsa_65::try_keygen_with_rng(&mut rand::thread_rng()).expect("Key generation failed");

        Self {
            secret: sk.into_bytes().to_vec(),
            public: pk.into_bytes().to_vec(),
        }
    }

    pub fn from_seed(seed: &[u8; 32]) -> Self {
        let mut rng = ChaCha20Rng::from_seed(*seed);
        let (pk, sk) = ml_dsa_65::try_keygen_with_rng(&mut rng).expect("Key generation failed");

        Self {
            secret: sk.into_bytes().to_vec(),
            public: pk.into_bytes().to_vec(),
        }
    }

    pub fn sign(&self, message: &[u8]) -> Vec<u8> {
        let sk_bytes: [u8; 4032] = self
            .secret
            .as_slice()
            .try_into()
            .expect("Invalid secret key length");
        let sk = ml_dsa_65::PrivateKey::try_from_bytes(sk_bytes).expect("Invalid secret key");

        sk.try_sign(message, GENERIC_SIGN_CONTEXT)
            .expect("Signing failed")
            .to_vec()
    }

    pub fn verify(message: &[u8], signature: &[u8], public_key: &[u8]) -> bool {
        let pk_bytes: [u8; 1952] = match public_key.try_into() {
            Ok(b) => b,
            Err(_) => return false,
        };

        let sig_bytes: [u8; 3309] = match signature.try_into() {
            Ok(b) => b,
            Err(_) => return false,
        };

        if let Ok(pk) = ml_dsa_65::PublicKey::try_from_bytes(pk_bytes) {
            pk.verify(message, &sig_bytes, GENERIC_SIGN_CONTEXT)
        } else {
            false
        }
    }

    pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), String> {
        let json = serde_json::to_string_pretty(self)
            .map_err(|e| format!("Failed to serialize: {}", e))?;
        fs::write(path, json).map_err(|e| format!("Failed to write file: {}", e))?;
        Ok(())
    }

    pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, String> {
        let json = fs::read_to_string(path).map_err(|e| format!("Failed to read file: {}", e))?;
        serde_json::from_str(&json).map_err(|e| format!("Failed to deserialize: {}", e))
    }
}

impl DualKeypair {
    pub fn generate() -> Self {
        let (pk, sk) =
            ml_dsa_65::try_keygen_with_rng(&mut rand::thread_rng()).expect("Key generation failed");
        let (ek, dk) = ml_kem_768::KG::try_keygen().expect("Kyber key generation failed");

        Self {
            dilithium_pk: pk,
            dilithium_sk: sk,
            kyber_ek: ek,
            kyber_dk: dk,
            mnemonic: String::from("random-generation"),
        }
    }

    pub fn from_mnemonic(mnemonic: String) -> Self {
        Self::from_mnemonic_with_passphrase(mnemonic, "")
    }

    pub fn from_mnemonic_with_passphrase(mnemonic: String, passphrase: &str) -> Self {
        let seed = Self::mnemonic_to_seed(&mnemonic, passphrase);

        let dilithium_seed = Self::derive_child_seed(&seed, b"dilithium");
        let mut rng = ChaCha20Rng::from_seed(dilithium_seed);
        let (pk, sk) = ml_dsa_65::try_keygen_with_rng(&mut rng).expect("Key generation failed");

        let kyber_seed = Self::derive_child_seed(&seed, b"kyber");
        let mut kyber_rng = ChaCha20Rng::from_seed(kyber_seed);
        let (ek, dk) = ml_kem_768::KG::try_keygen_with_rng(&mut kyber_rng)
            .expect("Kyber key generation failed");

        Self {
            dilithium_pk: pk,
            dilithium_sk: sk,
            kyber_ek: ek,
            kyber_dk: dk,
            mnemonic,
        }
    }

    fn mnemonic_to_seed(mnemonic: &str, passphrase: &str) -> [u8; 64] {
        use pbkdf2::pbkdf2_hmac;
        use sha2::Sha512;

        let salt = format!("mnemonic{}", passphrase);
        let mut seed = [0u8; 64];
        pbkdf2_hmac::<Sha512>(mnemonic.as_bytes(), salt.as_bytes(), 2048, &mut seed);
        seed
    }

    fn derive_child_seed(master_seed: &[u8], context: &[u8]) -> [u8; 32] {
        use hkdf::Hkdf;
        use sha2::Sha256;

        let hk = Hkdf::<Sha256>::new(Some(context), master_seed);
        let mut okm = [0u8; 32];
        hk.expand(b"truthlinked-v1", &mut okm)
            .expect("HKDF expand failed");
        okm
    }

    pub fn save_with_password<P: AsRef<Path>>(
        &self,
        path: P,
        password: Option<&str>,
    ) -> Result<(), String> {
        let data = serde_json::json!({
            "mnemonic": self.mnemonic,
            "dilithium_public": hex::encode(self.dilithium_pk.clone().into_bytes()),
            "kyber_encaps_key": hex::encode(self.kyber_ek.clone().into_bytes()),
            "dilithium_secret": hex::encode(self.dilithium_sk.clone().into_bytes()),
            "kyber_decaps_key": hex::encode(self.kyber_dk.clone().into_bytes()),
        });

        let json_str = serde_json::to_string_pretty(&data)
            .map_err(|e| format!("Failed to serialize keypair: {}", e))?;

        let final_data = if let Some(pwd) = password {
            let encrypted = Self::encrypt_keyfile(&json_str, pwd)?;
            serde_json::to_string_pretty(&serde_json::json!({
                "encrypted": true,
                "version": 1,
                "data": encrypted,
            }))
            .map_err(|e| format!("Failed to wrap encrypted keypair: {}", e))?
        } else {
            json_str
        };

        let path_ref = path.as_ref();
        fs::write(path_ref, final_data).map_err(|e| e.to_string())?;

        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mut perms = fs::metadata(path_ref)
                .map_err(|e| e.to_string())?
                .permissions();
            perms.set_mode(0o600);
            fs::set_permissions(path_ref, perms).map_err(|e| e.to_string())?;
        }

        Ok(())
    }

    fn encrypt_keyfile(plaintext: &str, password: &str) -> Result<String, String> {
        use aes_gcm::aead::Aead;
        use aes_gcm::{Aes256Gcm, KeyInit, Nonce};
        use rand::RngCore;

        let mut salt = [0u8; 32];
        rand::thread_rng().fill_bytes(&mut salt);

        let mut key = [0u8; 32];
        argon2::Argon2::default()
            .hash_password_into(password.as_bytes(), &salt, &mut key)
            .map_err(|e| format!("Argon2 failed: {}", e))?;

        let cipher =
            Aes256Gcm::new_from_slice(&key).map_err(|e| format!("Cipher init failed: {}", e))?;

        let mut nonce_bytes = [0u8; 12];
        rand::thread_rng().fill_bytes(&mut nonce_bytes);
        let nonce = Nonce::from_slice(&nonce_bytes);

        let ciphertext = cipher
            .encrypt(nonce, plaintext.as_bytes())
            .map_err(|e| format!("Encrypt failed: {}", e))?;

        let payload = serde_json::json!({
            "salt": hex::encode(salt),
            "nonce": hex::encode(nonce_bytes),
            "ciphertext": hex::encode(ciphertext),
        });

        Ok(payload.to_string())
    }

    fn decrypt_keyfile(payload: &str, password: &str) -> Result<String, String> {
        use aes_gcm::aead::Aead;
        use aes_gcm::{Aes256Gcm, KeyInit, Nonce};

        let obj: serde_json::Value = serde_json::from_str(payload)
            .map_err(|e| format!("Invalid encrypted keyfile: {}", e))?;

        let salt_hex = obj
            .get("salt")
            .and_then(|v| v.as_str())
            .ok_or("Missing salt")?;
        let nonce_hex = obj
            .get("nonce")
            .and_then(|v| v.as_str())
            .ok_or("Missing nonce")?;
        let ct_hex = obj
            .get("ciphertext")
            .and_then(|v| v.as_str())
            .ok_or("Missing ciphertext")?;

        let salt = hex::decode(salt_hex).map_err(|e| format!("Invalid salt: {}", e))?;
        let nonce = hex::decode(nonce_hex).map_err(|e| format!("Invalid nonce: {}", e))?;
        let ciphertext = hex::decode(ct_hex).map_err(|e| format!("Invalid ciphertext: {}", e))?;

        let mut key = [0u8; 32];
        argon2::Argon2::default()
            .hash_password_into(password.as_bytes(), &salt, &mut key)
            .map_err(|e| format!("Argon2 failed: {}", e))?;

        let cipher =
            Aes256Gcm::new_from_slice(&key).map_err(|e| format!("Cipher init failed: {}", e))?;

        let nonce = Nonce::from_slice(&nonce);
        let plaintext = cipher
            .decrypt(nonce, ciphertext.as_ref())
            .map_err(|e| format!("Decrypt failed: {}", e))?;

        String::from_utf8(plaintext).map_err(|e| format!("Invalid UTF-8: {}", e))
    }

    pub fn load_with_password<P: AsRef<Path>>(
        path: P,
        password: Option<&str>,
    ) -> Result<Self, String> {
        let path_ref = path.as_ref();
        let json =
            fs::read_to_string(path_ref).map_err(|e| format!("Failed to read file: {}", e))?;

        let wrapper: serde_json::Value =
            serde_json::from_str(&json).map_err(|e| format!("Failed to parse keypair: {}", e))?;

        let is_encrypted = wrapper
            .get("encrypted")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);

        let data_json = if is_encrypted {
            let pwd = password.ok_or("Password required")?;
            let payload = wrapper
                .get("data")
                .and_then(|v| v.as_str())
                .ok_or("Missing data")?;
            Self::decrypt_keyfile(payload, pwd)?
        } else {
            json
        };

        let obj: serde_json::Value = serde_json::from_str(&data_json)
            .map_err(|e| format!("Failed to parse keypair: {}", e))?;

        let mnemonic = obj
            .get("mnemonic")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        let dilithium_public = obj
            .get("dilithium_public")
            .and_then(|v| v.as_str())
            .ok_or("Missing dilithium_public")?;
        let dilithium_secret = obj
            .get("dilithium_secret")
            .and_then(|v| v.as_str())
            .ok_or("Missing dilithium_secret")?;

        let pk_bytes: [u8; 1952] = hex::decode(dilithium_public)
            .map_err(|e| e.to_string())?
            .try_into()
            .map_err(|_| "Invalid public key length")?;
        let sk_bytes: [u8; 4032] = hex::decode(dilithium_secret)
            .map_err(|e| e.to_string())?
            .try_into()
            .map_err(|_| "Invalid secret key length")?;

        let dilithium_pk = ml_dsa_65::PublicKey::try_from_bytes(pk_bytes)
            .map_err(|e| format!("Invalid public key: {:?}", e))?;
        let dilithium_sk = ml_dsa_65::PrivateKey::try_from_bytes(sk_bytes)
            .map_err(|e| format!("Invalid secret key: {:?}", e))?;

        let kyber_encaps_key = obj.get("kyber_encaps_key").and_then(|v| v.as_str());
        let kyber_decaps_key = obj.get("kyber_decaps_key").and_then(|v| v.as_str());

        let (kyber_ek, kyber_dk) = match (kyber_encaps_key, kyber_decaps_key) {
            (Some(ek_hex), Some(dk_hex)) => {
                let ek_bytes: [u8; 1184] = hex::decode(ek_hex)
                    .map_err(|e| e.to_string())?
                    .try_into()
                    .map_err(|_| "Invalid encaps key length")?;
                let dk_bytes: [u8; 2400] = hex::decode(dk_hex)
                    .map_err(|e| e.to_string())?
                    .try_into()
                    .map_err(|_| "Invalid decaps key length")?;
                let ek = ml_kem_768::EncapsKey::try_from_bytes(ek_bytes)
                    .map_err(|e| format!("Invalid encaps key: {:?}", e))?;
                let dk = ml_kem_768::DecapsKey::try_from_bytes(dk_bytes)
                    .map_err(|e| format!("Invalid decaps key: {:?}", e))?;
                (ek, dk)
            }
            _ => {
                if mnemonic.is_empty() {
                    return Err("Missing kyber keys and mnemonic".to_string());
                }
                let derived = Self::from_mnemonic(mnemonic.clone());
                (derived.kyber_ek, derived.kyber_dk)
            }
        };

        Ok(Self {
            dilithium_pk,
            dilithium_sk,
            kyber_ek,
            kyber_dk,
            mnemonic,
        })
    }

    pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, String> {
        let path_ref = path.as_ref();
        let json =
            fs::read_to_string(path_ref).map_err(|e| format!("Failed to read file: {}", e))?;

        let wrapper: serde_json::Value =
            serde_json::from_str(&json).map_err(|e| format!("Failed to parse keypair: {}", e))?;

        let is_encrypted = wrapper
            .get("encrypted")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);

        if is_encrypted {
            let password = rpassword::prompt_password("Enter password: ")
                .map_err(|e| format!("Failed to read password: {}", e))?;
            Self::load_with_password(path_ref, Some(&password))
        } else {
            Self::load_with_password(path_ref, None)
        }
    }

    pub fn sign_transaction(
        &self,
        tx: &crate::pq_execution::Transaction,
    ) -> Result<crate::pq_execution::Transaction, String> {
        let mut msg = Vec::new();
        msg.extend_from_slice(&(tx.genesis_fingerprint.len() as u32).to_le_bytes());
        msg.extend_from_slice(&tx.genesis_fingerprint);
        msg.extend_from_slice(&(tx.sender.len() as u32).to_le_bytes());
        msg.extend_from_slice(&tx.sender);
        msg.extend_from_slice(&tx.nonce.to_le_bytes());
        msg.extend_from_slice(&tx.timestamp.to_le_bytes());
        msg.extend_from_slice(&tx.expiration_height.to_le_bytes());

        let intent_bytes = postcard::to_allocvec(&tx.intent)
            .map_err(|e| format!("Failed to serialize intent: {}", e))?;
        msg.extend_from_slice(&(intent_bytes.len() as u32).to_le_bytes());
        msg.extend_from_slice(&intent_bytes);

        let signature = (&self.dilithium_sk)
            .try_sign(&msg, TX_SIGN_CONTEXT)
            .map_err(|e| format!("Signing failed: {:?}", e))?
            .to_vec();

        Ok(crate::pq_execution::Transaction {
            sender: tx.sender,
            intent: tx.intent.clone(),
            signature,
            nonce: tx.nonce,
            timestamp: tx.timestamp,
            genesis_fingerprint: tx.genesis_fingerprint,
            expiration_height: tx.expiration_height,
        })
    }
}

pub fn account_id_from_pubkey(pubkey: &[u8]) -> [u8; 32] {
    use sha2::{Digest, Sha256};
    let mut hasher = Sha256::new();
    hasher.update(b"truthlinked-account-id-v1");
    hasher.update(pubkey);
    hasher.finalize().into()
}

fn build_tx_signing_message(
    genesis_fingerprint: [u8; 32],
    sender: [u8; 32],
    timestamp: u64,
    expiration_height: u64,
    intent_bytes: &[u8],
) -> Vec<u8> {
    let mut msg = Vec::new();
    msg.extend_from_slice(&(genesis_fingerprint.len() as u32).to_le_bytes());
    msg.extend_from_slice(&genesis_fingerprint);
    msg.extend_from_slice(&(sender.len() as u32).to_le_bytes());
    msg.extend_from_slice(&sender);
    msg.extend_from_slice(&timestamp.to_le_bytes());
    msg.extend_from_slice(&expiration_height.to_le_bytes());
    msg.extend_from_slice(&(intent_bytes.len() as u32).to_le_bytes());
    msg.extend_from_slice(intent_bytes);
    msg
}

pub fn sign_transaction(
    genesis_fingerprint: [u8; 32],
    sender: [u8; 32],
    timestamp: u64,
    expiration_height: u64,
    intent: &[u8],
    dilithium_sk: &ml_dsa_65::PrivateKey,
) -> Vec<u8> {
    let msg = build_tx_signing_message(genesis_fingerprint, sender, timestamp, expiration_height, intent);

    dilithium_sk
        .try_sign(&msg, TX_SIGN_CONTEXT)
        .expect("Signing failed")
        .to_vec()
}

pub fn kyber_encapsulate(encaps_key: &ml_kem_768::EncapsKey) -> ([u8; 1088], [u8; 32]) {
    let (ssk, ct) = encaps_key.try_encaps().expect("Encapsulation failed");
    (ct.into_bytes(), ssk.into_bytes())
}

pub fn kyber_decapsulate(decaps_key: &ml_kem_768::DecapsKey, ciphertext: &[u8; 1088]) -> [u8; 32] {
    let ct = ml_kem_768::CipherText::try_from_bytes(*ciphertext).expect("Invalid ciphertext");
    decaps_key
        .try_decaps(&ct)
        .expect("Decapsulation failed")
        .into_bytes()
}