molpha-verifier 0.2.0

Solana program library for verifying Molpha threshold-signed oracle updates
Documentation
//! Verify a Molpha [`DataUpdate`] whose raw value is longer than 32 bytes.
//!
//! The signed message commits to the value as `keccak256(raw_value)` + `raw_value.len()`; those
//! fields are derived from the raw bytes at verify time and are not stored on the wire payload.
//! Fixture constants are generated by the in-repo test signer
//! (`cargo test --lib regenerate_example_fixture -- --ignored --nocapture`).
//!
//! 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,
};

/// The raw feed value carried alongside the signed payload — 63 bytes, longer than the old
/// 32-byte limit.
const FIXTURE_RAW_VALUE: &[u8] = b"molpha raw value that is decidedly longer than thirty-two bytes";

/// 129-byte borsh encoding of the fixture `DataUpdate` (matches on-chain `SubmitDataUpdateArgs`).
const FIXTURE_BORSH: [u8; 129] = [
    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, 0x7b, 0xf1, 0x53, 0x65, 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0xa7, 0x9c,
    0x31, 0x79, 0x81, 0x80, 0xd1, 0x21, 0x6c, 0x6c, 0x82, 0xa4, 0x18, 0x5b, 0xba, 0xce, 0xde, 0x5b,
    0xc5, 0xfe, 0x1d, 0x71, 0xdf, 0x18, 0xe8, 0x23, 0x0c, 0xfa, 0x52, 0xd7, 0x8e, 0x60, 0xbe, 0x5b,
    0x10, 0x8c, 0x35, 0x58, 0xdc, 0xb9, 0x15, 0xe9, 0xa1, 0x90, 0xb5, 0x28, 0xe2, 0x46, 0xc0, 0xef,
    0xca, 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] = [
    [
        0x02, 0xc9, 0x25, 0xe5, 0x4b, 0xba, 0x3c, 0x00, 0x10, 0xdb, 0x34, 0xec, 0x62, 0xcb, 0x39,
        0xa8, 0xfd, 0x47, 0xd2, 0x12, 0xd7, 0xea, 0x92, 0x11, 0xc9, 0x89, 0x0f, 0x3b, 0x6e, 0x03,
        0x1a, 0x1b, 0x5f,
    ],
    [
        0x03, 0x10, 0x94, 0x1d, 0x85, 0x30, 0x89, 0x47, 0x15, 0x1b, 0x41, 0x9a, 0x27, 0xef, 0x53,
        0x84, 0x27, 0x71, 0x95, 0x3a, 0x89, 0x3c, 0x20, 0x09, 0x40, 0x19, 0x4a, 0x92, 0x67, 0x4a,
        0xf3, 0xcf, 0xdf,
    ],
    [
        0x02, 0x29, 0x7d, 0x73, 0x25, 0x1d, 0x4d, 0xcc, 0x8b, 0x52, 0xa4, 0x8e, 0x45, 0x9e, 0x45,
        0x52, 0x01, 0xa3, 0x8b, 0xf3, 0x07, 0x91, 0x98, 0x28, 0xc5, 0xf1, 0x3b, 0xa8, 0x40, 0x19,
        0xa8, 0x5b, 0x24,
    ],
    [
        0x03, 0x8e, 0x12, 0x77, 0x05, 0x62, 0xb3, 0xcd, 0x20, 0x12, 0xda, 0x26, 0x6e, 0x56, 0x5b,
        0x2c, 0x92, 0x17, 0x48, 0xfe, 0xb2, 0x74, 0x54, 0x6a, 0x31, 0x19, 0x3e, 0x09, 0x39, 0x6e,
        0xad, 0x50, 0xa4,
    ],
    [
        0x03, 0xab, 0x20, 0xa8, 0x1c, 0xde, 0xde, 0xec, 0xa9, 0xd3, 0xe2, 0xa3, 0x51, 0xb4, 0x73,
        0x1f, 0xa9, 0xd7, 0x26, 0xb6, 0xf4, 0x93, 0x92, 0x9f, 0x73, 0x57, 0xb4, 0xe5, 0x7d, 0xca,
        0x93, 0x4c, 0xc7,
    ],
    [
        0x03, 0x77, 0x5f, 0xf4, 0x31, 0x0d, 0xb1, 0x95, 0xf9, 0x65, 0x0d, 0xbb, 0xba, 0x52, 0x84,
        0xfa, 0x52, 0x07, 0x27, 0x49, 0xb7, 0xd1, 0xa6, 0xe9, 0xfe, 0x35, 0x05, 0xfd, 0x8a, 0x63,
        0xb0, 0xb2, 0xbd,
    ],
    [
        0x03, 0x0f, 0x99, 0xa2, 0x00, 0x9d, 0x2a, 0x37, 0x9f, 0x19, 0xd1, 0xfe, 0x8a, 0xf1, 0xd1,
        0x73, 0xd6, 0xa6, 0x27, 0x5b, 0x19, 0xb4, 0xc5, 0x1e, 0xbd, 0x75, 0xd1, 0xfe, 0x7c, 0x4d,
        0x68, 0xb8, 0x4d,
    ],
    [
        0x03, 0x7f, 0xa6, 0x86, 0x6d, 0x55, 0xbb, 0x94, 0x7b, 0x21, 0xe4, 0xe8, 0x26, 0x42, 0x1f,
        0xe5, 0xd7, 0x04, 0x1f, 0x25, 0x73, 0xa2, 0xea, 0x80, 0xd7, 0x9b, 0x51, 0x9e, 0xfc, 0x9d,
        0xeb, 0xe7, 0x2d,
    ],
];

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!(
        "raw value ({} bytes):   {:?}",
        FIXTURE_RAW_VALUE.len(),
        core::str::from_utf8(FIXTURE_RAW_VALUE).unwrap()
    );
    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, FIXTURE_RAW_VALUE);
    println!("message_hash:           0x{}", hex(&message_hash));

    // 2. Verify the 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, FIXTURE_RAW_VALUE, 8, 0, &FIXTURE_SIGNER_PUBKEYS)?;

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

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

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