Skip to content

Commit e353417

Browse files
committed
persistent signature cache for CI
1 parent eaee0f3 commit e353417

5 files changed

Lines changed: 102 additions & 20 deletions

File tree

.github/workflows/rust.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ jobs:
4848
- name: Cache benchmark signers
4949
uses: actions/cache@v4
5050
with:
51-
path: target/signers-cache
51+
path: .signers-cache
5252
key: benchmark-signers-${{ hashFiles('crates/xmss/**', 'crates/utils/**', 'crates/backend/**') }}
53+
restore-keys: benchmark-signers-
5354
- name: Build
5455
run: cargo build --release --all --verbose
5556
env:
@@ -59,6 +60,7 @@ jobs:
5960
run: cargo test --release --all --verbose
6061
env:
6162
RUSTFLAGS: ${{ matrix.rustflags }}
63+
SIGNERS_CACHE_DIR: ${{ github.workspace }}/.signers-cache
6264

6365
cargo-clippy:
6466
runs-on: ubuntu-latest

Cargo.lock

Lines changed: 68 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ rec_aggregation = { path = "crates/rec_aggregation" }
6262
backend = { path = "crates/backend" }
6363

6464
# External
65+
sha3 = "0.11.0"
6566
clap = { version = "4.5.59", features = ["derive"] }
6667
rand = "0.10.0"
6768
rayon = "1.11.0"

crates/xmss/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ backend.workspace = true
1414
serde.workspace = true
1515
lz4_flex.workspace = true
1616
postcard.workspace = true
17+
sha3.workspace = true
1718

1819
[dev-dependencies]
1920
postcard.workspace = true

crates/xmss/src/signers_cache.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1+
use backend::*;
2+
use rand::rngs::StdRng;
3+
use rand::{RngExt, SeedableRng};
4+
use serde::{Deserialize, Serialize};
5+
use sha3::{Digest, Sha3_256};
16
use std::fs;
2-
use std::hash::{DefaultHasher, Hash, Hasher};
37
use std::path::PathBuf;
48
use std::sync::OnceLock;
59
use std::sync::atomic::{AtomicUsize, Ordering};
610
use std::time::Instant;
711

8-
use backend::*;
9-
use rand::rngs::StdRng;
10-
use rand::{RngExt, SeedableRng};
11-
use serde::{Deserialize, Serialize};
12-
1312
use crate::*;
1413

1514
static SIGNERS_CACHE: OnceLock<Vec<(XmssPublicKey, XmssSignature)>> = OnceLock::new();
@@ -19,7 +18,7 @@ pub fn get_benchmark_signatures() -> &'static Vec<(XmssPublicKey, XmssSignature)
1918
}
2019

2120
pub const BENCHMARK_SLOT: u32 = 111;
22-
pub const NUM_BENCHMARK_SIGNERS: usize = 10000;
21+
pub const NUM_BENCHMARK_SIGNERS: usize = 10_000;
2322

2423
pub fn message_for_benchmark() -> [F; MESSAGE_LEN_FE] {
2524
std::array::from_fn(F::from_usize)
@@ -30,19 +29,33 @@ struct SignersCacheFile {
3029
signatures: Vec<(XmssPublicKey, XmssSignature)>,
3130
}
3231

33-
fn cache_footprint(first_pubkey: &XmssPublicKey) -> u64 {
34-
let mut hasher = DefaultHasher::new();
35-
NUM_BENCHMARK_SIGNERS.hash(&mut hasher);
36-
BENCHMARK_SLOT.hash(&mut hasher);
37-
message_for_benchmark().hash(&mut hasher);
38-
first_pubkey.merkle_root.hash(&mut hasher);
39-
hasher.finish()
32+
fn cache_footprint(first_pubkey: &XmssPublicKey) -> u128 {
33+
let mut hasher = Sha3_256::new();
34+
hasher.update(NUM_BENCHMARK_SIGNERS.to_le_bytes());
35+
hasher.update(BENCHMARK_SLOT.to_le_bytes());
36+
for f in message_for_benchmark() {
37+
hasher.update(f.as_canonical_u32().to_le_bytes());
38+
}
39+
for f in first_pubkey.merkle_root {
40+
hasher.update(f.as_canonical_u32().to_le_bytes());
41+
}
42+
let hash = hasher.finalize();
43+
u128::from_le_bytes(hash[..16].try_into().unwrap())
44+
}
45+
46+
fn cache_dir() -> PathBuf {
47+
// In CI, set SIGNERS_CACHE_DIR to a path outside target/
48+
if let Ok(dir) = std::env::var("SIGNERS_CACHE_DIR") {
49+
PathBuf::from(dir)
50+
} else {
51+
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../target/signers-cache")
52+
}
4053
}
4154

4255
fn cache_path(first_pubkey: &XmssPublicKey) -> PathBuf {
4356
let footprint = cache_footprint(first_pubkey);
44-
let file = format!("benchmark_signers_cache_{footprint:016x}.bin");
45-
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(format!("../../target/signers-cache/{file}"))
57+
let file = format!("benchmark_signers_cache_{footprint:032x}.bin");
58+
cache_dir().join(file)
4659
}
4760

4861
fn compute_signer(index: usize) -> (XmssPublicKey, XmssSignature) {

0 commit comments

Comments
 (0)