Skip to main content

dsa_sign/
dsa_sign.rs

1// SPDX-License-Identifier: MIT
2//
3// Rivide Post-Quantum Cryptography Library
4// Copyright (C) 2026 Moh. Ananda Firmansyah Putra
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12//
13// The above copyright notice and this permission notice shall be included in all
14// copies or substantial portions of the Software.
15
16//! Demonstration example of ML-DSA-65 digital signatures in Rust.
17
18use rivide::dsa::MlDsa65;
19
20fn main() {
21    println!("Rivide Rust: ML-DSA-65 Digital Signature Demonstration");
22
23    // 1. Generate signing keypair
24    println!("\n[Signer] Generating ML-DSA-65 signature keypair...");
25    let signer = MlDsa65::keypair().expect("Failed to generate signer keypair");
26    println!(
27        "  Public Key : {} bytes",
28        signer.public_key.as_bytes().len()
29    );
30    println!(
31        "  Secret Key : {} bytes",
32        signer.secret_key.as_bytes().len()
33    );
34
35    // 2. Sign arbitrary message payload
36    let message = b"Post-quantum signed contract payload: Transfer $100,000 to Bob.";
37    println!(
38        "\n[Signer] Signing message: \"{}\"...",
39        std::str::from_utf8(message).unwrap()
40    );
41    let signature = MlDsa65::sign(message, &signer.secret_key).expect("Failed to sign message");
42    println!("  Signature  : {} bytes", signature.as_bytes().len());
43
44    // 3. [Verifier] Verify signature authenticity
45    println!("\n[Verifier] Verifying signature against public key...");
46    let is_valid = MlDsa65::verify(&signature, message, &signer.public_key);
47    if is_valid {
48        println!("[SUCCESS] Signature is VALID and AUTHENTIC!");
49    } else {
50        panic!("FATAL: Signature verification failed!");
51    }
52
53    // 4. [Verifier] Test tamper rejection
54    let tampered_msg = b"Post-quantum signed contract payload: Transfer $1,000,000 to Bob.";
55    println!(
56        "\n[Verifier] Testing tampered message: \"{}\"...",
57        std::str::from_utf8(tampered_msg).unwrap()
58    );
59    let is_tampered_valid = MlDsa65::verify(&signature, tampered_msg, &signer.public_key);
60    if !is_tampered_valid {
61        println!("[SUCCESS] Tampered message correctly REJECTED!");
62    } else {
63        panic!("FATAL: Tampered message was incorrectly accepted!");
64    }
65}