xand_ledger/
crypto.rs

1use crate::{
2    transactions::txn_input_keys_to_hash_curve_points, verify_zkplmt, TransactionOutput,
3    VectorTuple, ZkPlmtError,
4};
5use alloc::vec::Vec;
6use curve25519_dalek::ristretto::RistrettoPoint;
7pub use zkplmt::bulletproofs::BulletRangeProof;
8use zkplmt::{
9    bulletproofs::{bullet_range_verify, Bases},
10    core::CryptoSystemParameters,
11    models::Proof,
12};
13
14lazy_static::lazy_static! {
15    static ref CRYPTO_SYSTEM: CryptoSystemParameters = serde_scale::from_slice(include_bytes!(concat!(env!("OUT_DIR"), "/crypto_params"))).unwrap();
16}
17
18#[allow(non_snake_case)]
19pub(crate) fn get_K() -> RistrettoPoint {
20    CRYPTO_SYSTEM.K
21}
22
23#[allow(non_snake_case)]
24pub(crate) fn get_L() -> RistrettoPoint {
25    CRYPTO_SYSTEM.L
26}
27
28#[allow(non_snake_case)]
29pub(crate) fn get_G() -> RistrettoPoint {
30    CRYPTO_SYSTEM.G
31}
32
33pub(crate) fn get_bases() -> &'static Bases {
34    &CRYPTO_SYSTEM.bases
35}
36
37/// For computationally heavy operations that may need different implementations
38/// in various contexts (e.g. WASM vs Native)
39pub trait LedgerCrypto {
40    fn verify_bulletproof(proof: &BulletRangeProof) -> bool;
41
42    fn verify_zkplmt(
43        message: &[u8],
44        tuples: &[VectorTuple],
45        proof: &Proof,
46    ) -> Result<(), ZkPlmtError>;
47
48    fn txn_input_keys_to_hash_curve_points(input: &[TransactionOutput]) -> Vec<RistrettoPoint>;
49}
50
51/// Reference implementation of LedgerCrypto
52pub struct DefaultCryptoImpl;
53
54impl LedgerCrypto for DefaultCryptoImpl {
55    fn verify_bulletproof(proof: &BulletRangeProof) -> bool {
56        bullet_range_verify(proof, get_bases())
57    }
58
59    fn verify_zkplmt(
60        message: &[u8],
61        tuples: &[VectorTuple],
62        proof: &Proof,
63    ) -> Result<(), ZkPlmtError> {
64        verify_zkplmt(message, tuples, proof)
65    }
66
67    fn txn_input_keys_to_hash_curve_points(input: &[TransactionOutput]) -> Vec<RistrettoPoint> {
68        txn_input_keys_to_hash_curve_points(input)
69    }
70}