molpha-verifier 0.3.0-dev.2

Solana program library for verifying Molpha threshold-signed oracle updates
Documentation
//! Verify a Molpha [`Attestation`] using the 12-node registry fixture.
//!
//! Run:
//! ```text
//! cargo run -p molpha-verifier --example verify_attestation --features borsh
//! ```

use borsh::BorshDeserialize;
use molpha_verifier::{
    compute_message_hash, verify_attestation_compressed, Attestation, AttestationError,
    AttestationPayload, SchnorrSignature,
};

const FIXTURE_REGISTERED_NODE_COUNT: u32 = 12;
const FIXTURE_REDUNDANCY_BUFFER: u8 = 2;

/// 80-byte borsh encoding of the fixture `AttestationPayload`.
const FIXTURE_PAYLOAD_BORSH: [u8; 80] = [
    0xe1, 0xcd, 0x5b, 0x4f, 0x67, 0xac, 0xdc, 0x78, 0x68, 0xc3, 0xb1, 0x5f, 0x7b, 0x6c, 0xc2, 0xdc,
    0x27, 0x70, 0x54, 0x53, 0x71, 0x34, 0x2c, 0xab, 0x76, 0x62, 0x71, 0xbb, 0x3f, 0xd5, 0xe7, 0x34,
    0x0b, 0x0c, 0x5c, 0x4a, 0x0e, 0x67, 0x58, 0x69, 0xda, 0xc2, 0x27, 0x2a, 0x40, 0x04, 0x63, 0x65,
    0xa2, 0x9c, 0x8a, 0xe7, 0x63, 0x5e, 0x52, 0xc4, 0x94, 0xd8, 0x40, 0xda, 0x2e, 0xc8, 0x26, 0xcb,
    0x0c, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x7c, 0x06, 0xd6, 0x65, 0x00, 0x00, 0x00, 0x00,
];

/// 84-byte borsh encoding of the fixture aggregate Schnorr signature.
const FIXTURE_SIGNATURE_BORSH: [u8; 84] = [
    0x1b, 0x8d, 0xd2, 0x78, 0xb3, 0x67, 0xb3, 0x4d, 0x4e, 0xce, 0x69, 0xb8, 0x8c, 0x28, 0xff, 0x13,
    0x01, 0xb6, 0x72, 0x51, 0xfc, 0x3d, 0x79, 0x26, 0xac, 0xb5, 0x25, 0xd1, 0x1f, 0xd3, 0x17, 0x1d,
    0x51, 0xbe, 0x44, 0x69, 0x33, 0x1a, 0x9e, 0xb3, 0xed, 0x48, 0xb1, 0xd4, 0xe1, 0x1e, 0xc9, 0xa0,
    0xa5, 0x95, 0x2d, 0xf4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x0e, 0x1d,
];

/// Compressed secp256k1 pubkeys for signers at bit positions 0, 2, 3, 4, 9, 10, 11.
const FIXTURE_SIGNER_PUBKEYS: [[u8; 33]; 7] = [
    [
        0x03, 0x04, 0xb2, 0x3a, 0xff, 0xb9, 0xae, 0xb2, 0x80, 0xd6, 0xa2, 0x75, 0xb8, 0x65, 0xe6,
        0x3b, 0x1f, 0x27, 0xb0, 0xd5, 0x01, 0x6e, 0x35, 0x6d, 0xdb, 0xfe, 0x8b, 0xd2, 0x5b, 0x27,
        0xd1, 0x7e, 0x5f,
    ],
    [
        0x02, 0xdc, 0x2d, 0x88, 0xad, 0x9d, 0x1c, 0x4f, 0xc7, 0x6b, 0xc5, 0xaf, 0x00, 0xc3, 0x90,
        0x20, 0x08, 0xa0, 0xbe, 0x5f, 0x8f, 0x10, 0x48, 0xd1, 0xd5, 0xb3, 0xfb, 0xc7, 0x19, 0xfc,
        0x7a, 0xd5, 0xec,
    ],
    [
        0x02, 0xc2, 0x6e, 0xd5, 0xda, 0x51, 0x58, 0xfd, 0x27, 0xe5, 0xaf, 0xc0, 0x5f, 0x88, 0xeb,
        0xe4, 0x4b, 0xcb, 0xf0, 0x90, 0xae, 0x9b, 0xc5, 0xe7, 0x02, 0x4d, 0xf0, 0xd5, 0x7e, 0xa4,
        0xcd, 0x7a, 0x44,
    ],
    [
        0x02, 0x85, 0x07, 0x3b, 0x91, 0x57, 0xfb, 0xd6, 0x77, 0x95, 0x9b, 0xf9, 0x12, 0xac, 0x07,
        0x95, 0x8c, 0x4a, 0x62, 0x5d, 0xcc, 0xd7, 0x4f, 0xa1, 0x3c, 0x92, 0x9e, 0x3d, 0xbb, 0x8d,
        0x3d, 0xbd, 0x41,
    ],
    [
        0x02, 0x58, 0xbf, 0x41, 0xcf, 0xea, 0x2b, 0x1d, 0x34, 0x4c, 0xc3, 0x0b, 0xb7, 0x35, 0xa1,
        0x32, 0xc1, 0x75, 0x5b, 0x11, 0x2d, 0xb5, 0x8f, 0xaa, 0x7e, 0x4c, 0x44, 0x65, 0x95, 0x2e,
        0x00, 0x04, 0xbf,
    ],
    [
        0x02, 0x5d, 0xc1, 0x4d, 0x6b, 0xc2, 0x04, 0x42, 0xbe, 0x79, 0xf5, 0x1c, 0xf5, 0x20, 0x33,
        0xc3, 0x96, 0x7b, 0xcc, 0xdd, 0xc5, 0xd3, 0x66, 0x95, 0x95, 0x13, 0x73, 0x20, 0xdf, 0xe5,
        0xc6, 0xab, 0xfc,
    ],
    [
        0x03, 0xdc, 0xa6, 0x3a, 0x35, 0xd0, 0x48, 0xf7, 0x94, 0x5c, 0x95, 0x9d, 0x61, 0x8c, 0x2f,
        0xe8, 0xee, 0x5d, 0x40, 0x00, 0x29, 0x19, 0xa4, 0x6d, 0xff, 0x81, 0x27, 0x9c, 0x04, 0xb9,
        0x71, 0xe6, 0x06,
    ],
];

fn main() -> Result<(), AttestationError> {
    // 1. Decode the wire-format attestation (e.g. from instruction args).
    let payload = AttestationPayload::try_from_slice(&FIXTURE_PAYLOAD_BORSH)
        .expect("fixture payload borsh must decode");
    let signature = SchnorrSignature::try_from_slice(&FIXTURE_SIGNATURE_BORSH)
        .expect("fixture signature borsh must decode");
    let attestation = Attestation {
        payload: payload.clone(),
        signature: signature.clone(),
    };
    let mut attestation_borsh = FIXTURE_PAYLOAD_BORSH.to_vec();
    attestation_borsh.extend_from_slice(&FIXTURE_SIGNATURE_BORSH);
    let decoded_attestation = Attestation::try_from_slice(&attestation_borsh)
        .expect("fixture attestation borsh must decode");
    assert_eq!(decoded_attestation, attestation);

    println!(
        "decoded source_id:        0x{}",
        hex_encode(&attestation.payload.source_id)
    );
    println!(
        "decoded value:            0x{}",
        hex_encode(&attestation.payload.value)
    );
    println!(
        "registry_version:         {}",
        attestation.payload.registry_version
    );
    println!(
        "signatures_required:      {}",
        attestation.payload.signatures_required
    );
    println!(
        "canonical_timestamp:      {}",
        attestation.payload.canonical_timestamp
    );

    let message_hash = compute_message_hash(
        &attestation.payload,
        attestation.signature.signers_bitmap,
        attestation.payload.signatures_required,
    );
    println!("message_hash:             0x{}", hex_encode(&message_hash));

    // 2. Verify aggregate Schnorr signature.
    //
    // `node_count` is the registry size for `payload.registry_version`. Seven signers at bitmap
    // positions 0, 2, 3, 4, 9, 10, 11 (signersBitmap = 3613); threshold 5; redundancy_buffer 2.
    // `ordered_signers` are the signing nodes' compressed pubkeys in ascending bitmap-bit order.
    verify_attestation_compressed(
        &attestation,
        FIXTURE_REGISTERED_NODE_COUNT,
        FIXTURE_REDUNDANCY_BUFFER,
        &FIXTURE_SIGNER_PUBKEYS,
    )?;

    println!("aggregate Schnorr signature: OK");
    Ok(())
}

fn hex_encode(bytes: &[u8]) -> String {
    bytes.iter().map(|b| format!("{b:02x}")).collect()
}