commonware-cryptography 2026.10.0

Generate keys, sign arbitrary messages, and deterministically verify signatures.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//! BLAKE3 implementation of the [Hasher] trait.
//!
//! This implementation uses the [blake3] crate to generate BLAKE3 digests. [Hasher::hash_across]
//! splits a message of at least 128 KiB along the BLAKE3 tree and hashes the subtrees across the
//! given strategy.
//!
//! # Example
//! ```rust
//! use commonware_cryptography::{Hasher, blake3::Blake3};
//! use commonware_parallel::Rayon;
//! use std::num::NonZeroUsize;
//!
//! // Hash data in a single shot
//! let digest = Blake3::hash(&[b"hello,", b"world!"]);
//! println!("digest: {:?}", digest);
//!
//! // Or stream data incrementally
//! let mut hasher = Blake3::default();
//! hasher.update(b"hello,");
//! hasher.update(b"world!");
//! let (_hasher, digest) = hasher.finalize();
//! println!("digest: {:?}", digest);
//!
//! // Hash a long message across a thread pool
//! let strategy = Rayon::new(NonZeroUsize::new(4).unwrap()).unwrap();
//! let message = vec![7u8; 1 << 20];
//! assert_eq!(
//!     Blake3::hash_across(&[&message], &strategy),
//!     Blake3::hash(&[&message]),
//! );
//! ```

use crate::Hasher;
use blake3::{
    CHUNK_LEN, Hash,
    hazmat::{ChainingValue, HasherExt as _, Mode, merge_subtrees_non_root, merge_subtrees_root},
};
use bytes::BufMut;
use commonware_codec::{Buf, Error as CodecError, FixedArray, FixedSize, Read, ReadExt, Write};
use commonware_formatting::Hex;
use commonware_math::algebra::Random;
use commonware_parallel::Strategy;
use commonware_utils::{Array, Span, sequence::FixedBytes};
use core::{
    cmp::Ordering,
    fmt::{Debug, Display},
    ops::Deref,
};
use rand_core::CryptoRng;
use zeroize::Zeroize;

/// Re-export [blake3::Hasher] as `CoreBlake3` for external use if needed.
pub type CoreBlake3 = blake3::Hasher;

const DIGEST_LENGTH: usize = blake3::OUT_LEN;

/// Minimum number of tasks a split message spreads across: [`Hasher::hash_across`] bounds the
/// subtree that each task hashes by the message's length divided by this.
const SUBTREES: usize = 8;

/// Lower clamp, in bytes, on the subtree bound: 16 chunks, the widest batch the SIMD kernels
/// compress at once.
const MIN_SUBTREE_LEN: usize = 16 * CHUNK_LEN;

/// Upper clamp, in bytes, on the subtree bound: 64 chunks, enough to amortize the cost of a fork.
const MAX_SUBTREE_LEN: usize = 64 * CHUNK_LEN;

/// Length, in bytes, of the shortest message [`Hasher::hash_across`] splits: [`SUBTREES`]
/// subtrees of [`MIN_SUBTREE_LEN`] bytes.
const MIN_SPLIT_LEN: usize = SUBTREES * MIN_SUBTREE_LEN;

/// Hash the `len`-byte concatenation of `parts`, which spans more than one chunk, across `strategy`
/// as subtrees of at most `len / SUBTREES` bytes, clamped to [`MIN_SUBTREE_LEN`] and
/// [`MAX_SUBTREE_LEN`].
fn hash_subtrees(parts: &[&[u8]], len: usize, strategy: &impl Strategy) -> Digest {
    let root = Node {
        parts,
        skip: 0,
        offset: 0,
        len,
        task_len: (len / SUBTREES).clamp(MIN_SUBTREE_LEN, MAX_SUBTREE_LEN),
    };
    let (left, right) = root.children(strategy);
    merge_subtrees_root(&left, &right, Mode::Hash).into()
}

/// A node of a message's BLAKE3 tree: the `len` bytes at byte `offset` of the message, which
/// start `skip` bytes into the first of `parts`. Subtrees of at most `task_len` bytes hash as one
/// task.
///
/// `parts` holds every byte of the node, and `skip` is at most the length of its first part.
#[derive(Clone, Copy)]
struct Node<'a> {
    parts: &'a [&'a [u8]],
    skip: usize,
    offset: usize,
    len: usize,
    task_len: usize,
}

impl Node<'_> {
    /// Hash the chaining values of the children of a node that spans more than one chunk, in
    /// parallel across `strategy`.
    fn children(self, strategy: &impl Strategy) -> (ChainingValue, ChainingValue) {
        // The left child is the largest power-of-two number of chunks that leaves the right
        // child nonempty, which for a node of more than one chunk is the largest power of two
        // below its length. The right child starts in the first part that extends past it.
        let left_len = 1 << (self.len - 1).ilog2();
        let mut skip = self.skip + left_len;
        let mut index = 0;
        while skip >= self.parts[index].len() {
            skip -= self.parts[index].len();
            index += 1;
        }
        let left = Node {
            parts: &self.parts[..=index],
            len: left_len,
            ..self
        };
        let right = Node {
            parts: &self.parts[index..],
            skip,
            offset: self.offset + left_len,
            len: self.len - left_len,
            task_len: self.task_len,
        };
        strategy.join(
            || left.chaining_value(strategy),
            || right.chaining_value(strategy),
        )
    }

    /// Hash the node's chaining value, splitting it until every subtree fits in `task_len` bytes.
    fn chaining_value(self, strategy: &impl Strategy) -> ChainingValue {
        if self.len > self.task_len {
            let (left, right) = self.children(strategy);
            return merge_subtrees_non_root(&left, &right, Mode::Hash);
        }

        // Feed the node's bytes, which start `skip` bytes into the first part.
        let mut hasher = CoreBlake3::new();
        hasher.set_input_offset(self.offset as u64);
        let mut skip = self.skip;
        let mut remaining = self.len;
        for part in self.parts {
            let bytes = &part[skip..];
            let take = bytes.len().min(remaining);
            hasher.update(&bytes[..take]);
            remaining -= take;
            if remaining == 0 {
                break;
            }
            skip = 0;
        }
        hasher.finalize_non_root()
    }
}

/// BLAKE3 hasher.
#[derive(Debug, Default)]
pub struct Blake3 {
    hasher: CoreBlake3,
}

impl Hasher for Blake3 {
    type Digest = Digest;

    fn hash(parts: &[&[u8]]) -> Self::Digest {
        let mut hasher = CoreBlake3::new();
        for part in parts {
            hasher.update(part);
        }
        hasher.finalize().into()
    }

    fn hash_across(parts: &[&[u8]], strategy: &impl Strategy) -> Self::Digest {
        // A message shorter than `MIN_SPLIT_LEN` hashes on the calling thread. A total that
        // overflows `usize` (possible only when parts alias) hashes serially, since the streaming
        // hasher counts bytes in a `u64`.
        match parts
            .iter()
            .try_fold(0usize, |len, part| len.checked_add(part.len()))
        {
            Some(len) if len >= MIN_SPLIT_LEN => strategy.run(
                len,
                || Self::hash(parts),
                || hash_subtrees(parts, len, strategy),
            ),
            _ => Self::hash(parts),
        }
    }

    fn hash_pair(
        left: &[&[u8]],
        right: &[&[u8]],
        strategy: &impl Strategy,
    ) -> (Self::Digest, Self::Digest) {
        (
            Self::hash_across(left, strategy),
            Self::hash_across(right, strategy),
        )
    }

    fn update(&mut self, message: &[u8]) -> &mut Self {
        self.hasher.update(message);
        self
    }

    fn finalize(mut self) -> (Self, Self::Digest) {
        let finalized = self.hasher.finalize();
        self.hasher.reset();
        let array: [u8; DIGEST_LENGTH] = finalized.into();
        (self, Self::Digest::from(array))
    }
}

/// Digest of a BLAKE3 hashing operation.
#[derive(Clone, Copy, Eq, PartialEq, Hash, FixedArray)]
#[fixed_array(infallible)]
#[repr(transparent)]
pub struct Digest(pub [u8; DIGEST_LENGTH]);

impl Ord for Digest {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        FixedBytes::new(self.0).cmp(&FixedBytes::new(other.0))
    }
}

impl PartialOrd for Digest {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for Digest {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        // Generate random bytes and compute their Blake3 hash
        let len = u.int_in_range(0..=256)?;
        let data = u.bytes(len)?;
        Ok(Blake3::hash(&[data]))
    }
}

impl Write for Digest {
    fn write(&self, buf: &mut impl BufMut) {
        self.0.write(buf);
    }
}

impl Read for Digest {
    type Cfg = ();

    fn read_cfg(buf: &mut impl Buf, _: &()) -> Result<Self, CodecError> {
        let array = <[u8; DIGEST_LENGTH]>::read(buf)?;
        Ok(Self(array))
    }
}

impl FixedSize for Digest {
    const SIZE: usize = DIGEST_LENGTH;
}

impl Span for Digest {}

impl Array for Digest {}

impl From<Hash> for Digest {
    fn from(value: Hash) -> Self {
        Self(value.into())
    }
}

impl AsRef<[u8]> for Digest {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

impl Deref for Digest {
    type Target = [u8];
    fn deref(&self) -> &[u8] {
        &self.0
    }
}

impl Debug for Digest {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", Hex(&self.0))
    }
}

impl Display for Digest {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", Hex(&self.0))
    }
}

impl crate::Digest for Digest {
    const EMPTY: Self = Self([0u8; DIGEST_LENGTH]);
}

impl Random for Digest {
    fn random(mut rng: impl CryptoRng) -> Self {
        let mut array = [0u8; DIGEST_LENGTH];
        rng.fill_bytes(&mut array);
        Self(array)
    }
}

impl Zeroize for Digest {
    fn zeroize(&mut self) {
        self.0.zeroize();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use commonware_codec::{Copying, DecodeExt, Encode};
    use commonware_parallel::{
        Rayon, Sequential,
        mocks::{Explorer, explore},
        rayon::{ThreadPoolBuilder, Yield},
    };
    use commonware_utils::{NZUsize, TestRng};
    use rand::Rng as _;
    use std::sync::{
        Arc,
        atomic::{AtomicBool, Ordering::Relaxed},
        mpsc,
    };

    const HELLO_DIGEST: [u8; DIGEST_LENGTH] = commonware_formatting::hex!(
        "d74981efa70a0c880b8d8c1985d075dbcbf679b99a5f9914e5aaf96b831a9e24"
    );

    /// Return `len` random bytes, distinct per `seed`.
    fn random(len: usize, seed: u64) -> Vec<u8> {
        let mut bytes = vec![0; len];
        TestRng::new(seed).fill_bytes(&mut bytes);
        bytes
    }

    #[test]
    fn test_blake3() {
        let msg = b"hello world";

        // Generate initial hash
        let mut hasher = Blake3::default();
        hasher.update(msg);
        let (hasher, digest) = hasher.finalize();
        assert!(Digest::decode(Copying(&digest)).is_ok());
        assert_eq!(digest.as_ref(), HELLO_DIGEST);

        // Reuse the reset hasher
        let mut hasher = hasher;
        hasher.update(msg);
        let (_, digest) = hasher.finalize();
        assert!(Digest::decode(Copying(&digest)).is_ok());
        assert_eq!(digest.as_ref(), HELLO_DIGEST);

        // Test one-shot hasher
        let hash = Blake3::hash(&[msg]);
        assert_eq!(hash.as_ref(), HELLO_DIGEST);

        // Test multi-part one-shot hasher
        let hash = Blake3::hash(&[b"hello", b" world"]);
        assert_eq!(hash.as_ref(), HELLO_DIGEST);
    }

    /// Official BLAKE3 test vectors. Hashing 16 KiB or more in one update reaches the 16-way
    /// AVX-512 chunk kernel, and 32 KiB or more also reaches the 16-way parent kernel.
    #[test]
    fn test_official_vectors() {
        const VECTORS: [(usize, [u8; DIGEST_LENGTH]); 3] = [
            (
                16384,
                commonware_formatting::hex!(
                    "f875d6646de28985646f34ee13be9a576fd515f76b5b0a26bb324735041ddde4"
                ),
            ),
            (
                31744,
                commonware_formatting::hex!(
                    "62b6960e1a44bcc1eb1a611a8d6235b6b4b78f32e7abc4fb4c6cdcce94895c47"
                ),
            ),
            (
                102400,
                commonware_formatting::hex!(
                    "bc3e3d41a1146b069abffad3c0d44860cf664390afce4d9661f7902e7943e085"
                ),
            ),
        ];
        for (len, expected) in VECTORS {
            // The official input repeats the bytes 0 through 250.
            let input: Vec<u8> = (0..len).map(|i| (i % 251) as u8).collect();
            let mut hasher = Blake3::default();
            hasher.update(&input);
            let (_, digest) = hasher.finalize();
            assert_eq!(digest.as_ref(), expected, "len {len}");
        }
    }

    /// Messages around multiples of [`MIN_SUBTREE_LEN`] and around [`MIN_SPLIT_LEN`], given whole,
    /// cut inside chunks and subtrees, padded with empty parts, and paged, hash to the reference
    /// digest on the calling thread and across workers. Splitting into subtrees on the calling
    /// thread also matches, down to two chunks.
    #[test]
    fn test_hash_long_messages() {
        let data = random((1 << 20) + 5000, 0);
        for len in [
            0,
            1,
            CHUNK_LEN,
            CHUNK_LEN + 1,
            3 * CHUNK_LEN + 7,
            MIN_SUBTREE_LEN - 1,
            MIN_SUBTREE_LEN,
            MIN_SUBTREE_LEN + 1,
            MIN_SUBTREE_LEN + CHUNK_LEN,
            2 * MIN_SUBTREE_LEN - 1,
            2 * MIN_SUBTREE_LEN,
            2 * MIN_SUBTREE_LEN + 1,
            2 * MIN_SUBTREE_LEN + 3 * CHUNK_LEN + 7,
            3 * MIN_SUBTREE_LEN + 17,
            MIN_SPLIT_LEN - 1,
            MIN_SPLIT_LEN,
            MIN_SPLIT_LEN + MIN_SUBTREE_LEN + 1,
            3 * MIN_SPLIT_LEN + 17,
            (1 << 20) + 5000,
        ] {
            let message = &data[..len];
            let expected: Digest = blake3::hash(message).into();
            let (a, b) = (len / 3, len / 3 + MIN_SUBTREE_LEN.min(len - len / 3));
            let splits: [Vec<&[u8]>; 4] = [
                vec![message],
                vec![&message[..a], &message[a..b], &message[b..]],
                vec![&[], message, &[]],
                message.chunks(1000).collect(),
            ];
            for parts in &splits {
                assert_eq!(Blake3::hash(parts), expected, "len={len}");
                assert_eq!(
                    explore(|strategy| Blake3::hash_across(parts, strategy)),
                    expected,
                    "len={len}"
                );
                if len > CHUNK_LEN {
                    assert_eq!(
                        hash_subtrees(parts, len, &Sequential),
                        expected,
                        "len={len}"
                    );
                }
            }
        }
    }

    /// A message of at least [`MIN_SPLIT_LEN`] bytes is hashed across the strategy's pool, and a
    /// shorter one on the calling thread.
    #[test]
    fn test_hash_splits_long_messages() {
        // One worker, planned as four, counts the jobs it runs while looping. Splitting a message
        // from outside the pool hands it one job, whose nested forks stay on the worker.
        let pool = Arc::new(ThreadPoolBuilder::new().num_threads(1).build().unwrap());
        let strategy =
            Explorer::parallel(&Rayon::with_pool(pool.clone()).with_parallelism(NZUsize!(4)));
        let data = random(2 * MIN_SPLIT_LEN, 0);
        for (len, jobs) in [
            (MIN_SPLIT_LEN - 1, 0),
            (MIN_SPLIT_LEN, 1),
            (2 * MIN_SPLIT_LEN, 1),
        ] {
            let done = Arc::new(AtomicBool::new(false));
            let (started, ready) = mpsc::channel();
            let (sender, receiver) = mpsc::channel();
            pool.spawn({
                let done = done.clone();
                let pool = pool.clone();
                move || {
                    started.send(()).unwrap();
                    let mut jobs = 0;
                    while !done.load(Relaxed) {
                        if pool.yield_now() == Some(Yield::Executed) {
                            jobs += 1;
                        }
                    }
                    sender.send(jobs).unwrap();
                }
            });
            ready.recv().unwrap();

            // Hash, then release the worker and compare its job count.
            let message = &data[..len];
            let digest = Blake3::hash_across(&[message], &strategy);
            done.store(true, Relaxed);
            assert_eq!(digest, blake3::hash(message).into(), "len={len}");
            assert_eq!(receiver.recv().unwrap(), jobs, "len={len}");
        }
    }

    #[test]
    fn test_blake3_len() {
        assert_eq!(Digest::SIZE, DIGEST_LENGTH);
    }

    #[test]
    fn test_codec() {
        let msg = b"hello world";
        let mut hasher = Blake3::default();
        hasher.update(msg);
        let (_, digest) = hasher.finalize();

        let encoded = digest.encode();
        assert_eq!(encoded.len(), DIGEST_LENGTH);
        assert_eq!(encoded, digest.as_ref());

        let decoded = Digest::decode(encoded).unwrap();
        assert_eq!(digest, decoded);
    }

    #[test]
    fn test_digest_ord() {
        let a = Digest([0; DIGEST_LENGTH]);
        let mut b = a;
        b.0[DIGEST_LENGTH - 1] = 1;

        // The first byte decides even when the remaining bytes order the other way.
        let mut c = a;
        c.0[0] = 0x7f;
        c.0[1..].fill(0xff);
        let mut d = a;
        d.0[0] = 0x80;
        for (a, b, expected) in [
            (a, a, Ordering::Equal),
            (a, b, Ordering::Less),
            (b, a, Ordering::Greater),
            (c, d, Ordering::Less),
            (d, c, Ordering::Greater),
        ] {
            assert_eq!(a.cmp(&b), expected);
            assert_eq!(a.partial_cmp(&b), Some(expected));
        }
    }

    #[cfg(feature = "arbitrary")]
    mod conformance {
        use super::*;
        use commonware_codec::conformance::CodecConformance;

        commonware_conformance::conformance_tests! {
            CodecConformance<Digest>,
        }
    }
}