Skip to main content

zip_plus/
utils.rs

1use crate::pcs_transcript::PcsProverTranscript;
2use rand::{rngs::StdRng, seq::SliceRandom};
3use rand_core::SeedableRng;
4use zinc_transcript::traits::Transcribable;
5
6/// Reorder the elements in slice using the given randomness seed
7pub(super) fn shuffle_seeded<T>(slice: &mut [T], seed: u64) {
8    let mut rng = StdRng::seed_from_u64(seed);
9    slice.shuffle(&mut rng);
10}
11
12/// Formats a number with spaces as thousands separators, e.g. 1234567 becomes
13/// "1 234 567".
14#[allow(clippy::unwrap_used)]
15fn fmt_thousands(n: usize) -> String {
16    let s = n.to_string();
17    s.as_bytes()
18        .rchunks(3)
19        .rev()
20        .map(|c| std::str::from_utf8(c).unwrap())
21        .collect::<Vec<_>>()
22        .join(" ")
23}
24
25/// Prints proof size (before and after compression) to stderr.
26pub fn eprint_proof_size(label: impl std::fmt::Display, proof: &impl Transcribable) {
27    let mut transcript = PcsProverTranscript::new_from_commitments(std::iter::empty());
28    transcript
29        .write(proof)
30        .expect("transcribing proof should not fail");
31    let raw = transcript.stream.into_inner();
32
33    eprint_bytes_size(label, &raw);
34}
35
36/// Prints byte slice size (before and after compression) to stderr.
37pub fn eprint_bytes_size(label: impl std::fmt::Display, raw: &[u8]) {
38    macro_rules! print {
39        ($details:expr, $size_bytes:expr) => {
40            eprintln!(
41                "    Proof size ({label}, {}): {} bytes ({} KiB)",
42                $details,
43                fmt_thousands($size_bytes),
44                $size_bytes.div_ceil(1024),
45            );
46        };
47    }
48    print!("raw", raw.len());
49
50    let zstd = zstd::encode_all(raw, 22).expect("zstd compression failed");
51    print!("zstd-22", zstd.len());
52}