|
| 1 | +//! Crypto related functions and runtime initialized constants |
| 2 | +
|
| 3 | +//---------------------------------------------------------------------------------------------------- Use |
| 4 | +use std::sync::LazyLock; |
| 5 | + |
| 6 | +use curve25519_dalek::{ |
| 7 | + constants::ED25519_BASEPOINT_POINT, edwards::VartimeEdwardsPrecomputation, |
| 8 | + traits::VartimePrecomputedMultiscalarMul, EdwardsPoint, Scalar, |
| 9 | +}; |
| 10 | +use monero_serai::generators::H; |
| 11 | + |
| 12 | +/// This is the decomposed amount table containing the mandatory Pre-RCT amounts. It is use to pre-compute |
| 13 | +/// zero commitments at runtime. |
| 14 | +/// |
| 15 | +/// Defined at: |
| 16 | +/// - <https://github.com/monero-project/monero/blob/893916ad091a92e765ce3241b94e706ad012b62a/src/ringct/rctOps.cpp#L44> |
| 17 | +#[rustfmt::skip] |
| 18 | +const ZERO_COMMITMENT_DECOMPOSED_AMOUNT: [u64; 172] = [ |
| 19 | + 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 20 | + 10, 20, 30, 40, 50, 60, 70, 80, 90, |
| 21 | + 100, 200, 300, 400, 500, 600, 700, 800, 900, |
| 22 | + 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, |
| 23 | + 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, |
| 24 | + 100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000, 900000, |
| 25 | + 1000000, 2000000, 3000000, 4000000, 5000000, 6000000, 7000000, 8000000, 9000000, |
| 26 | + 10000000, 20000000, 30000000, 40000000, 50000000, 60000000, 70000000, 80000000, 90000000, |
| 27 | + 100000000, 200000000, 300000000, 400000000, 500000000, 600000000, 700000000, 800000000, 900000000, |
| 28 | + 1000000000, 2000000000, 3000000000, 4000000000, 5000000000, 6000000000, 7000000000, 8000000000, 9000000000, |
| 29 | + 10000000000, 20000000000, 30000000000, 40000000000, 50000000000, 60000000000, 70000000000, 80000000000, 90000000000, |
| 30 | + 100000000000, 200000000000, 300000000000, 400000000000, 500000000000, 600000000000, 700000000000, 800000000000, 900000000000, |
| 31 | + 1000000000000, 2000000000000, 3000000000000, 4000000000000, 5000000000000, 6000000000000, 7000000000000, 8000000000000, 9000000000000, |
| 32 | + 10000000000000, 20000000000000, 30000000000000, 40000000000000, 50000000000000, 60000000000000, 70000000000000, 80000000000000, 90000000000000, |
| 33 | + 100000000000000, 200000000000000, 300000000000000, 400000000000000, 500000000000000, 600000000000000, 700000000000000, 800000000000000, 900000000000000, |
| 34 | + 1000000000000000, 2000000000000000, 3000000000000000, 4000000000000000, 5000000000000000, 6000000000000000, 7000000000000000, 8000000000000000, 9000000000000000, |
| 35 | + 10000000000000000, 20000000000000000, 30000000000000000, 40000000000000000, 50000000000000000, 60000000000000000, 70000000000000000, 80000000000000000, 90000000000000000, |
| 36 | + 100000000000000000, 200000000000000000, 300000000000000000, 400000000000000000, 500000000000000000, 600000000000000000, 700000000000000000, 800000000000000000, 900000000000000000, |
| 37 | + 1000000000000000000, 2000000000000000000, 3000000000000000000, 4000000000000000000, 5000000000000000000, 6000000000000000000, 7000000000000000000, 8000000000000000000, 9000000000000000000, |
| 38 | + 10000000000000000000 |
| 39 | +]; |
| 40 | + |
| 41 | +/// Runtime initialized H generator. |
| 42 | +static H_PRECOMP: LazyLock<VartimeEdwardsPrecomputation> = |
| 43 | + LazyLock::new(|| VartimeEdwardsPrecomputation::new([*H, ED25519_BASEPOINT_POINT])); |
| 44 | + |
| 45 | +/// Runtime initialized zero commitment lookup table |
| 46 | +/// |
| 47 | +/// ASSUMPTION: This function assume that the [`ZERO_COMMITMENT_DECOMPOSED_AMOUNT`] |
| 48 | +/// table is sorted. |
| 49 | +pub static ZERO_COMMITMENT_LOOKUP_TABLE: LazyLock<[(u64, EdwardsPoint); 172]> = |
| 50 | + LazyLock::new(|| { |
| 51 | + let mut lookup_table: [(u64, EdwardsPoint); 172] = [(0_u64, ED25519_BASEPOINT_POINT); 172]; |
| 52 | + |
| 53 | + for (i, amount) in ZERO_COMMITMENT_DECOMPOSED_AMOUNT.into_iter().enumerate() { |
| 54 | + lookup_table[i] = ( |
| 55 | + amount, |
| 56 | + (ED25519_BASEPOINT_POINT + *H * Scalar::from(amount)), |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + lookup_table |
| 61 | + }); |
| 62 | + |
| 63 | +/// This function compute the zero commitment given a specific amount. |
| 64 | +/// |
| 65 | +/// It will first attempt to lookup into the table of known Pre-RCT value. |
| 66 | +/// Compute it otherwise. |
| 67 | +pub fn compute_zero_commitments(amount: u64) -> EdwardsPoint { |
| 68 | + // OPTIMIZATION: We first make an arithmetic check that the value lies between valid pre-computed amounts. This permit to avoid the lookup cost if possible |
| 69 | + if amount > 10 && amount % 10 != 0 { |
| 70 | + return H_PRECOMP.vartime_multiscalar_mul([Scalar::from(amount), Scalar::from(1_u8)]); |
| 71 | + } |
| 72 | + |
| 73 | + // Binary search in lookup table (O(log2(n)) instead of O(n)) |
| 74 | + let (mut start, mut end, mut c): (usize, usize, Option<EdwardsPoint>) = |
| 75 | + (0, (ZERO_COMMITMENT_LOOKUP_TABLE.len() - 1) / 2, None); |
| 76 | + while start <= end { |
| 77 | + let mid = (start + end) / 2; |
| 78 | + let lookup = ZERO_COMMITMENT_LOOKUP_TABLE[mid].0; |
| 79 | + match amount { |
| 80 | + a if a == lookup => { |
| 81 | + c = Some(ZERO_COMMITMENT_LOOKUP_TABLE[mid].1); |
| 82 | + } |
| 83 | + a if a < lookup => { |
| 84 | + end = mid - 1; |
| 85 | + } |
| 86 | + _ => { |
| 87 | + start = mid + 1; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Compute the zero commitment if not found. |
| 93 | + c.unwrap_or_else(|| { |
| 94 | + H_PRECOMP.vartime_multiscalar_mul([Scalar::from(amount), Scalar::from(1_u8)]) |
| 95 | + }) |
| 96 | +} |
0 commit comments