molpha-verifier 0.1.0

Solana program library for verifying Molpha threshold-signed oracle updates
Documentation
//! Verify a Molpha [`DataUpdate`] using the EVM-compat fixture from
//! `tests/fixtures-json/verify-answer-evm.json`.
//!
//! Run:
//! ```text
//! cargo run -p molpha-verifier --example verify_data_update --features borsh
//! ```

use borsh::BorshDeserialize;
use molpha_verifier::{
    compute_message_hash, verify_data_update_compressed, DataUpdate, DataUpdateError,
};

/// 161-byte borsh encoding of the fixture `DataUpdate` (matches on-chain `SubmitDataUpdateArgs`).
const FIXTURE_BORSH: [u8; 161] = [
    0x73, 0x6f, 0x6c, 0x61, 0x6e, 0x61, 0x2d, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x2d, 0x6a, 0x6f,
    0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x01, 0x00, 0x00, 0x00, 0x73, 0x6f, 0x6c, 0x61, 0x6e, 0x61, 0x2d, 0x63, 0x6f, 0x6d, 0x70, 0x61,
    0x74, 0x2d, 0x76, 0x61, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x7b, 0xf1, 0x53, 0x65, 0x00, 0x00, 0x00, 0x00, 0x08, 0xc7, 0xe0, 0x99,
    0x60, 0x3c, 0xee, 0xd2, 0xa1, 0x13, 0xd7, 0x5a, 0x9d, 0x95, 0xe2, 0x0f, 0x92, 0x00, 0x6b, 0x06,
    0xc5, 0x49, 0x7a, 0xdd, 0x09, 0x81, 0x7d, 0xa8, 0x90, 0x8d, 0x39, 0x0d, 0xa5, 0xc6, 0xb9, 0x4f,
    0xea, 0x5d, 0xd5, 0xf9, 0x65, 0xd8, 0x67, 0x14, 0xb1, 0xd9, 0x9d, 0xcf, 0xaf, 0x1e, 0x72, 0xee,
    0x35, 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, 0x00,
    0xff,
];

/// Compressed secp256k1 pubkeys for nodes at bit positions 0–7 (`signersBitmap = 255`).
const FIXTURE_SIGNER_PUBKEYS: [[u8; 33]; 8] = [
    [
        0x03, 0xc0, 0x95, 0x27, 0xe9, 0x78, 0xf6, 0xea, 0x69, 0xf0, 0xc6, 0xb7, 0xac, 0x0f, 0xb6,
        0x3a, 0xd0, 0x81, 0xa8, 0xa2, 0x91, 0x15, 0x1c, 0x5a, 0x0b, 0x11, 0x5c, 0xce, 0x43, 0x57,
        0x51, 0xbe, 0x7d,
    ],
    [
        0x02, 0x64, 0xa7, 0x27, 0x04, 0xf3, 0x9f, 0x8d, 0xd1, 0x7f, 0x20, 0xd7, 0x1c, 0x5b, 0x21,
        0xf3, 0x7b, 0x58, 0x52, 0x65, 0x6b, 0xc0, 0x55, 0x54, 0x42, 0xbf, 0x72, 0x72, 0x22, 0xf2,
        0x9d, 0x7e, 0x58,
    ],
    [
        0x02, 0x75, 0xae, 0x1e, 0x3d, 0xac, 0x00, 0xeb, 0x7d, 0xf0, 0x2e, 0x9f, 0xe8, 0xd9, 0x70,
        0x9c, 0x8a, 0x2c, 0x09, 0xa1, 0x1e, 0xd4, 0xf7, 0xd9, 0xaa, 0x46, 0xa7, 0xde, 0xa6, 0xcf,
        0x37, 0x6d, 0x7f,
    ],
    [
        0x02, 0x6c, 0xe2, 0x5b, 0x3a, 0x16, 0x1a, 0xb8, 0xe0, 0xf0, 0x5e, 0x4c, 0xd1, 0xc7, 0x7b,
        0x77, 0x69, 0x6d, 0x26, 0xc6, 0x41, 0xeb, 0xde, 0xa4, 0xe8, 0x1a, 0xa8, 0x9a, 0x90, 0xf3,
        0x2c, 0xfc, 0x54,
    ],
    [
        0x03, 0x5b, 0x95, 0xd7, 0x03, 0x22, 0x8b, 0xef, 0xcc, 0xc3, 0x78, 0x62, 0x9d, 0xc1, 0x98,
        0x04, 0xce, 0xfe, 0x56, 0xc3, 0x3c, 0x64, 0x5f, 0xa4, 0xbc, 0x1a, 0xa0, 0xf3, 0x75, 0xe3,
        0xb4, 0xfa, 0x5e,
    ],
    [
        0x03, 0x99, 0x5e, 0x4b, 0xe0, 0xec, 0xd4, 0x22, 0xbf, 0x25, 0x0a, 0x3d, 0xa3, 0xa0, 0xb8,
        0x34, 0x2e, 0x52, 0x89, 0x3a, 0x3e, 0x06, 0x4f, 0xa6, 0x35, 0x55, 0x73, 0x78, 0xb5, 0x9a,
        0xfa, 0x8b, 0x50,
    ],
    [
        0x03, 0xec, 0x90, 0x6d, 0x0a, 0x1c, 0xfc, 0x3c, 0x7d, 0xec, 0x18, 0x08, 0x8c, 0x3d, 0x14,
        0x4f, 0x32, 0x15, 0x80, 0xec, 0xe0, 0xa6, 0xba, 0xe5, 0xce, 0xb2, 0x8d, 0xcf, 0x8d, 0xc6,
        0xe3, 0xda, 0x03,
    ],
    [
        0x03, 0x27, 0x5f, 0xcf, 0x98, 0x38, 0xb4, 0x7a, 0xac, 0xff, 0x25, 0x1f, 0x4f, 0x09, 0x9f,
        0x80, 0xc6, 0x4a, 0x1a, 0x9a, 0xed, 0xbd, 0xb6, 0x28, 0xc2, 0xc8, 0x7f, 0x2c, 0x5e, 0x12,
        0x3d, 0xd0, 0x40,
    ],
];

fn main() -> Result<(), DataUpdateError> {
    // 1. Decode the wire-format payload (e.g. from a `submit_data_update` instruction arg).
    let payload = DataUpdate::try_from_slice(&FIXTURE_BORSH).expect("fixture borsh must decode");

    println!(
        "decoded feed_id:        {:?}",
        ascii_prefix(&payload.feed_id)
    );
    println!("decoded value:          {:?}", ascii_prefix(&payload.value));
    println!("registry_version:       {}", payload.registry_version);
    println!("signatures_required:    {}", payload.signatures_required);
    println!("canonical_timestamp:    {}", payload.canonical_timestamp);

    let message_hash = compute_message_hash(&payload, payload.signatures_required);
    println!(
        "message_hash:           0x{}",
        message_hash
            .iter()
            .map(|b| format!("{b:02x}"))
            .collect::<String>()
    );

    // 2. Verify aggregate Schnorr signature.
    //
    // `node_count` is the registry size for `payload.registry_version`. For this fixture all eight
    // signers are selected (bitmap 0xff, threshold 8, redundancy_buffer 0).
    // `ordered_signers` are the signing nodes' compressed pubkeys in ascending bitmap-bit order.
    verify_data_update_compressed(&payload, 8, 0, &FIXTURE_SIGNER_PUBKEYS)?;

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

fn ascii_prefix(bytes: &[u8; 32]) -> String {
    bytes
        .iter()
        .take_while(|&&b| b != 0)
        .map(|&b| b as char)
        .collect()
}