drandite 0.1.0

Drand beacon verification, relay clients, and timelock encryption
Documentation
//! Offline Quicknet verification and timelock round trip through the facade.
//!
//! Round 1000 is historical and its signature is public: this example provides
//! no time delay. The fixed key and RNG seed are for reproducibility only.

use drandite::scheme::BlsUnchainedG1Rfc9380;
use drandite::tlock::crypto::{G2Projective, ibe};
use drandite::{ChainHash, Ciphertext, Timelock, Verifier, wire};
use hex_literal::hex;
use rand_chacha::ChaCha20Rng;
use rand_core::SeedableRng;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let pinned = ChainHash::new(hex!(
        "52db9ba70e0cc0f6eaf7803dd07447a1f5477735fd3f661792ba94600c84e971"
    ));
    let info = wire::v2::ChainInfo::from_json(
        r#"{
            "public_key": "83cf0f2896adee7eb8b5f01fcad3912212c437e0073e911fb90022d3e760183c8c4b450b6a0a6c3ac6a5776a2d1064510d1fec758c921cc22b0e17e63aaf4bcb5ed66304de9cf809bd274ca73bab4af5a6e9c76a4bc09e76eae8991ef5ece45a",
            "period": 3,
            "genesis_time": 1692803367,
            "genesis_seed": "f477d5c89f21a17c863a7f937c6a6d15859414d2be09cd448d4279af331c5d3e",
            "scheme": "bls-unchained-g1-rfc9380",
            "beacon_id": "quicknet"
        }"#,
    )?
    .try_canonicalize::<BlsUnchainedG1Rfc9380>(pinned)?;
    let beacon = wire::v2::Beacon::from_json(
        r#"{
            "round": 1000,
            "signature": "b44679b9a59af2ec876b1a6b1ad52ea9b1615fc3982b19576350f93447cb1125e342b73a8dd2bacbe47e4b6b63ed5e39"
        }"#,
    )?
    .try_canonicalize::<BlsUnchainedG1Rfc9380>()?;
    let verified = Verifier::new(&info).verify(beacon)?;
    assert_eq!(
        verified.randomness().get(),
        hex!("fe290beca10872ef2fb164d2aa4442de4566183ec51c56ff3cd603d930e54fdd")
    );

    let timelock = Timelock::new(&info);
    let key = [42; 32];
    let mut rng = ChaCha20Rng::from_seed([7; 32]);
    let ciphertext = timelock.encrypt(&mut rng, verified.round(), &key)?;
    let mut bytes = [0; ibe::Ciphertext::<G2Projective, 32>::encoded_len()];
    ciphertext.inner().write_bytes(&mut bytes)?;
    let restored = Ciphertext::new(
        ciphertext.round(),
        ciphertext.chain_hash(),
        ibe::Ciphertext::<G2Projective, 32>::from_bytes(&bytes)?,
    );
    let plaintext = timelock.decrypt(&restored, &verified)?;
    assert_eq!(*plaintext, key);
    println!(
        "Quicknet round {}: verified randomness; encrypted, encoded, decoded, and decrypted {} bytes",
        verified.round(),
        plaintext.len(),
    );
    Ok(())
}