Skip to content

Commit ec4524e

Browse files
author
SyntheticBird45
committed
Add common ammounts commitment lookup table
- Implements `compute_zero_commitment` function in `cuprate-helper::crypto` module. - Added test that compare the function output with the correct calculation. - Use of a constant-time algorithm for the lookup table. - Added according documentation
1 parent 978d72b commit ec4524e

File tree

7 files changed

+142
-19
lines changed

7 files changed

+142
-19
lines changed

constants/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ rpc = []
1919
[dev-dependencies]
2020

2121
[lints]
22-
workspace = true
22+
workspace = true

helper/Cargo.toml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ atomic = ["dep:crossbeam"]
1616
asynch = ["dep:futures", "dep:rayon"]
1717
cast = []
1818
constants = []
19+
crypto = ["dep:curve25519-dalek", "dep:monero-serai", "std"]
1920
fs = ["dep:dirs"]
2021
num = []
2122
map = ["cast", "dep:monero-serai", "dep:cuprate-constants"]
@@ -26,12 +27,13 @@ tx = ["dep:monero-serai"]
2627
[dependencies]
2728
cuprate-constants = { path = "../constants", optional = true, features = ["block"] }
2829

29-
crossbeam = { workspace = true, optional = true }
30-
chrono = { workspace = true, optional = true, features = ["std", "clock"] }
31-
dirs = { workspace = true, optional = true }
32-
futures = { workspace = true, optional = true, features = ["std"] }
33-
monero-serai = { workspace = true, optional = true }
34-
rayon = { workspace = true, optional = true }
30+
chrono = { workspace = true, optional = true, features = ["std", "clock"] }
31+
crossbeam = { workspace = true, optional = true }
32+
curve25519-dalek = { workspace = true, optional = true }
33+
dirs = { workspace = true, optional = true }
34+
futures = { workspace = true, optional = true, features = ["std"] }
35+
monero-serai = { workspace = true, optional = true }
36+
rayon = { workspace = true, optional = true }
3537

3638
# This is kinda a stupid work around.
3739
# [thread] needs to activate one of these libs (windows|libc)

helper/src/crypto.rs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
//---------------------------------------------------------------------------------------------------- Pre-computation
13+
14+
/// This is the decomposed amount table containing the mandatory Pre-RCT amounts. It is used to pre-compute
15+
/// zero commitments at runtime.
16+
///
17+
/// Defined at:
18+
/// - <https://github.com/monero-project/monero/blob/893916ad091a92e765ce3241b94e706ad012b62a/src/ringct/rctOps.cpp#L44>
19+
#[rustfmt::skip]
20+
pub const ZERO_COMMITMENT_DECOMPOSED_AMOUNT: [u64; 172] = [
21+
1, 2, 3, 4, 5, 6, 7, 8, 9,
22+
10, 20, 30, 40, 50, 60, 70, 80, 90,
23+
100, 200, 300, 400, 500, 600, 700, 800, 900,
24+
1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000,
25+
10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000,
26+
100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000, 900000,
27+
1000000, 2000000, 3000000, 4000000, 5000000, 6000000, 7000000, 8000000, 9000000,
28+
10000000, 20000000, 30000000, 40000000, 50000000, 60000000, 70000000, 80000000, 90000000,
29+
100000000, 200000000, 300000000, 400000000, 500000000, 600000000, 700000000, 800000000, 900000000,
30+
1000000000, 2000000000, 3000000000, 4000000000, 5000000000, 6000000000, 7000000000, 8000000000, 9000000000,
31+
10000000000, 20000000000, 30000000000, 40000000000, 50000000000, 60000000000, 70000000000, 80000000000, 90000000000,
32+
100000000000, 200000000000, 300000000000, 400000000000, 500000000000, 600000000000, 700000000000, 800000000000, 900000000000,
33+
1000000000000, 2000000000000, 3000000000000, 4000000000000, 5000000000000, 6000000000000, 7000000000000, 8000000000000, 9000000000000,
34+
10000000000000, 20000000000000, 30000000000000, 40000000000000, 50000000000000, 60000000000000, 70000000000000, 80000000000000, 90000000000000,
35+
100000000000000, 200000000000000, 300000000000000, 400000000000000, 500000000000000, 600000000000000, 700000000000000, 800000000000000, 900000000000000,
36+
1000000000000000, 2000000000000000, 3000000000000000, 4000000000000000, 5000000000000000, 6000000000000000, 7000000000000000, 8000000000000000, 9000000000000000,
37+
10000000000000000, 20000000000000000, 30000000000000000, 40000000000000000, 50000000000000000, 60000000000000000, 70000000000000000, 80000000000000000, 90000000000000000,
38+
100000000000000000, 200000000000000000, 300000000000000000, 400000000000000000, 500000000000000000, 600000000000000000, 700000000000000000, 800000000000000000, 900000000000000000,
39+
1000000000000000000, 2000000000000000000, 3000000000000000000, 4000000000000000000, 5000000000000000000, 6000000000000000000, 7000000000000000000, 8000000000000000000, 9000000000000000000,
40+
10000000000000000000
41+
];
42+
43+
/// Runtime initialized [`H`] generator.
44+
static H_PRECOMP: LazyLock<VartimeEdwardsPrecomputation> =
45+
LazyLock::new(|| VartimeEdwardsPrecomputation::new([*H, ED25519_BASEPOINT_POINT]));
46+
47+
/// Runtime initialized zero commitment lookup table
48+
///
49+
/// # Invariant
50+
/// This function assumes that the [`ZERO_COMMITMENT_DECOMPOSED_AMOUNT`]
51+
/// table is sorted.
52+
pub static ZERO_COMMITMENT_LOOKUP_TABLE: LazyLock<[EdwardsPoint; 172]> = LazyLock::new(|| {
53+
let mut lookup_table: [EdwardsPoint; 172] = [ED25519_BASEPOINT_POINT; 172];
54+
55+
for (i, amount) in ZERO_COMMITMENT_DECOMPOSED_AMOUNT.into_iter().enumerate() {
56+
lookup_table[i] = ED25519_BASEPOINT_POINT + *H * Scalar::from(amount);
57+
}
58+
59+
lookup_table
60+
});
61+
62+
//---------------------------------------------------------------------------------------------------- Free functions
63+
64+
/// This function computes the zero commitment given a specific amount.
65+
///
66+
/// It will first attempt to lookup into the table of known Pre-RCT value.
67+
/// Compute it otherwise.
68+
#[expect(clippy::cast_possible_truncation)]
69+
pub fn compute_zero_commitment(amount: u64) -> EdwardsPoint {
70+
// OPTIMIZATION: Unlike monerod which execute a linear search across its lookup
71+
// table (O(n)). Cuprate is making use of an arithmetic based constant time
72+
// version (O(1)). It has been benchmarked in both hit and miss scenarios against
73+
// a binary search lookup (O(log2(n))). To understand the following algorithm it
74+
// is important to observe the pattern that follows the values of
75+
// [`ZERO_COMMITMENT_DECOMPOSED_AMOUNT`].
76+
77+
// First obtain the logarithm base 10 of the amount. and extend it back to obtain
78+
// the amount without its most significant digit.
79+
let Some(log) = amount.checked_ilog10() else {
80+
// amount = 0 so H component is 0.
81+
return ED25519_BASEPOINT_POINT;
82+
};
83+
let div = 10_u64.pow(log);
84+
85+
// Extract the most significant digit.
86+
let most_significant_digit = amount / div;
87+
88+
// If the *rounded* version is different than the exact amount. Then
89+
// there aren't only trailing zeroes behind the most significant digit.
90+
// The amount is not part of the table and can calculated apart.
91+
if most_significant_digit * div != amount {
92+
return H_PRECOMP.vartime_multiscalar_mul([Scalar::from(amount), Scalar::ONE]);
93+
}
94+
95+
// Calculating the index back by progressing within the powers of 10.
96+
// The index of the first value in the cached amount's row.
97+
let row_start = u64::from(log) * 9;
98+
// The index of the cached amount
99+
let index = (most_significant_digit - 1 + row_start) as usize;
100+
101+
ZERO_COMMITMENT_LOOKUP_TABLE[index]
102+
}
103+
104+
//---------------------------------------------------------------------------------------------------- Tests
105+
#[cfg(test)]
106+
mod test {
107+
use curve25519_dalek::{traits::VartimePrecomputedMultiscalarMul, Scalar};
108+
109+
use crate::crypto::{compute_zero_commitment, H_PRECOMP, ZERO_COMMITMENT_DECOMPOSED_AMOUNT};
110+
111+
#[test]
112+
/// Compare the output of `compute_zero_commitment` for all
113+
/// preRCT decomposed amounts against their actual computation.
114+
///
115+
/// Assert that the lookup table returns the correct commitments
116+
fn compare_lookup_with_computation() {
117+
for amount in ZERO_COMMITMENT_DECOMPOSED_AMOUNT {
118+
let commitment = H_PRECOMP.vartime_multiscalar_mul([Scalar::from(amount), Scalar::ONE]);
119+
assert!(commitment == compute_zero_commitment(amount));
120+
}
121+
}
122+
}

helper/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ pub mod time;
3030

3131
#[cfg(feature = "tx")]
3232
pub mod tx;
33+
34+
#[cfg(feature = "crypto")]
35+
pub mod crypto;
3336
//---------------------------------------------------------------------------------------------------- Private Usage
3437

3538
//----------------------------------------------------------------------------------------------------

storage/blockchain/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ service = ["dep:thread_local", "dep:rayon", "cuprate-helper/thread"]
2020
[dependencies]
2121
cuprate-database = { path = "../database" }
2222
cuprate-database-service = { path = "../service" }
23-
cuprate-helper = { path = "../../helper", features = ["fs", "map"] }
23+
cuprate-helper = { path = "../../helper", features = ["fs", "map", "crypto"] }
2424
cuprate-types = { path = "../../types", features = ["blockchain"] }
2525
cuprate-pruning = { path = "../../pruning" }
2626

storage/blockchain/src/ops/output.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
//! Output functions.
22
33
//---------------------------------------------------------------------------------------------------- Import
4-
use curve25519_dalek::{constants::ED25519_BASEPOINT_POINT, edwards::CompressedEdwardsY, Scalar};
5-
use monero_serai::{generators::H, transaction::Timelock};
4+
use curve25519_dalek::edwards::CompressedEdwardsY;
5+
use monero_serai::transaction::Timelock;
66

77
use cuprate_database::{
88
RuntimeError, {DatabaseRo, DatabaseRw},
99
};
10+
use cuprate_helper::crypto::compute_zero_commitment;
1011
use cuprate_helper::map::u64_to_timelock;
1112
use cuprate_types::OutputOnChain;
1213

@@ -155,9 +156,7 @@ pub fn output_to_output_on_chain(
155156
amount: Amount,
156157
table_tx_unlock_time: &impl DatabaseRo<TxUnlockTime>,
157158
) -> Result<OutputOnChain, RuntimeError> {
158-
// FIXME: implement lookup table for common values:
159-
// <https://github.com/monero-project/monero/blob/c8214782fb2a769c57382a999eaf099691c836e7/src/ringct/rctOps.cpp#L322>
160-
let commitment = ED25519_BASEPOINT_POINT + *H * Scalar::from(amount);
159+
let commitment = compute_zero_commitment(amount);
161160

162161
let time_lock = if output
163162
.output_flags

storage/blockchain/src/ops/tx.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
33
//---------------------------------------------------------------------------------------------------- Import
44
use bytemuck::TransparentWrapper;
5-
use curve25519_dalek::{constants::ED25519_BASEPOINT_POINT, Scalar};
65
use monero_serai::transaction::{Input, Timelock, Transaction};
76

87
use cuprate_database::{DatabaseRo, DatabaseRw, RuntimeError, StorableVec};
8+
use cuprate_helper::crypto::compute_zero_commitment;
99

1010
use crate::{
1111
ops::{
@@ -136,12 +136,9 @@ pub fn add_tx(
136136
.enumerate()
137137
.map(|(i, output)| {
138138
// Create commitment.
139-
// <https://github.com/Cuprate/cuprate/pull/102#discussion_r1559489302>
140-
// FIXME: implement lookup table for common values:
141-
// <https://github.com/monero-project/monero/blob/c8214782fb2a769c57382a999eaf099691c836e7/src/ringct/rctOps.cpp#L322>
139+
142140
let commitment = if miner_tx {
143-
ED25519_BASEPOINT_POINT
144-
+ *monero_serai::generators::H * Scalar::from(output.amount.unwrap_or(0))
141+
compute_zero_commitment(output.amount.unwrap_or(0))
145142
} else {
146143
proofs
147144
.as_ref()

0 commit comments

Comments
 (0)