chie-crypto 0.2.0

Cryptographic primitives for CHIE Protocol
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
//! Cryptographic accumulators for efficient set membership proofs.
//!
//! This module provides accumulator data structures that allow compact
//! representation of sets with efficient membership proofs. Useful for
//! tracking large sets of elements in the CHIE protocol (e.g., valid peers,
//! bandwidth proofs, revocation lists).
//!
//! # Features
//!
//! - Hash-based accumulators with constant-size digests
//! - Incremental accumulator updates
//! - Compact membership proofs
//! - Batch operations for multiple elements
//! - Serialization support
//!
//! # Examples
//!
//! ```
//! use chie_crypto::accumulator::{HashAccumulator, hash_element};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create a new accumulator
//! let mut acc = HashAccumulator::new();
//!
//! // Add elements
//! let elem1 = b"peer_id_1";
//! let elem2 = b"peer_id_2";
//! acc.add(elem1);
//! acc.add(elem2);
//!
//! // Generate membership proof
//! let proof = acc.prove(elem1)?;
//!
//! // Verify membership
//! assert!(acc.verify(elem1, &proof));
//! # Ok(())
//! # }
//! ```
//!
//! # Security Considerations
//!
//! - Hash-based accumulators provide collision resistance
//! - Proofs are binding but not hiding (elements are revealed)
//! - Use appropriate hash function (BLAKE3) for performance and security
//! - Accumulator state must be protected from tampering

use blake3;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// Serde helper for Vec<u8>
mod serde_bytes {
    use serde::{Deserialize, Deserializer, Serializer};

    pub fn serialize<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_bytes(bytes)
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
    where
        D: Deserializer<'de>,
    {
        <Vec<u8>>::deserialize(deserializer)
    }
}

// Serde helper for [u8; 32]
mod serde_bytes_32 {
    use serde::{Deserialize, Deserializer, Serializer};

    pub fn serialize<S>(bytes: &[u8; 32], serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_bytes(bytes)
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<[u8; 32], D::Error>
    where
        D: Deserializer<'de>,
    {
        let vec = <Vec<u8>>::deserialize(deserializer)?;
        vec.try_into()
            .map_err(|_| serde::de::Error::custom("Expected 32 bytes"))
    }
}

/// Accumulator errors.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AccumulatorError {
    /// Element not found in accumulator
    ElementNotFound,
    /// Invalid proof
    InvalidProof,
    /// Accumulator is empty
    EmptyAccumulator,
    /// Serialization error
    SerializationError(String),
    /// Element already exists
    ElementExists,
}

impl std::fmt::Display for AccumulatorError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::ElementNotFound => write!(f, "Element not found in accumulator"),
            Self::InvalidProof => write!(f, "Invalid membership proof"),
            Self::EmptyAccumulator => write!(f, "Accumulator is empty"),
            Self::SerializationError(e) => write!(f, "Serialization error: {}", e),
            Self::ElementExists => write!(f, "Element already exists in accumulator"),
        }
    }
}

impl std::error::Error for AccumulatorError {}

/// Accumulator result type.
pub type AccumulatorResult<T> = Result<T, AccumulatorError>;

/// Size of accumulator digest in bytes.
pub const ACCUMULATOR_DIGEST_SIZE: usize = 32;

/// Hash an element for accumulator inclusion.
pub fn hash_element(element: &[u8]) -> [u8; 32] {
    blake3::hash(element).into()
}

/// Accumulator digest.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct AccumulatorDigest {
    #[serde(with = "serde_bytes_32")]
    digest: [u8; ACCUMULATOR_DIGEST_SIZE],
}

impl AccumulatorDigest {
    /// Create from bytes.
    pub fn from_bytes(bytes: [u8; ACCUMULATOR_DIGEST_SIZE]) -> Self {
        Self { digest: bytes }
    }

    /// Get the digest bytes.
    pub fn as_bytes(&self) -> &[u8; ACCUMULATOR_DIGEST_SIZE] {
        &self.digest
    }
}

/// Membership proof for an element in the accumulator.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MembershipProof {
    element_hash: [u8; 32],
    #[serde(with = "serde_bytes")]
    witness: Vec<u8>,
}

impl MembershipProof {
    /// Serialize to bytes.
    pub fn to_bytes(&self) -> AccumulatorResult<Vec<u8>> {
        crate::codec::encode(self).map_err(|e| AccumulatorError::SerializationError(e.to_string()))
    }

    /// Deserialize from bytes.
    pub fn from_bytes(bytes: &[u8]) -> AccumulatorResult<Self> {
        crate::codec::decode(bytes).map_err(|e| AccumulatorError::SerializationError(e.to_string()))
    }
}

/// Hash-based accumulator using Merkle tree-like construction.
///
/// This accumulator maintains a set of elements and can generate
/// compact membership proofs. The accumulator value is a hash commitment
/// to all elements in the set.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HashAccumulator {
    elements: HashMap<[u8; 32], Vec<u8>>,
    digest: [u8; 32],
}

impl HashAccumulator {
    /// Create a new empty accumulator.
    pub fn new() -> Self {
        Self {
            elements: HashMap::new(),
            digest: [0u8; 32],
        }
    }

    /// Create an accumulator from a set of elements.
    pub fn from_elements(elements: &[&[u8]]) -> Self {
        let mut acc = Self::new();
        for elem in elements {
            acc.add(elem);
        }
        acc
    }

    /// Update the accumulator digest.
    fn update_digest(&mut self) {
        if self.elements.is_empty() {
            self.digest = [0u8; 32];
            return;
        }

        // Combine all element hashes in a deterministic order
        let mut sorted_hashes: Vec<_> = self.elements.keys().collect();
        sorted_hashes.sort();

        let mut hasher = blake3::Hasher::new();
        for hash in sorted_hashes {
            hasher.update(hash);
        }
        self.digest = hasher.finalize().into();
    }

    /// Add an element to the accumulator.
    ///
    /// Returns `true` if the element was newly added, `false` if it already existed.
    pub fn add(&mut self, element: &[u8]) -> bool {
        let hash = hash_element(element);
        let newly_added = self.elements.insert(hash, element.to_vec()).is_none();
        if newly_added {
            self.update_digest();
        }
        newly_added
    }

    /// Remove an element from the accumulator.
    ///
    /// Returns `true` if the element was present and removed, `false` otherwise.
    pub fn remove(&mut self, element: &[u8]) -> bool {
        let hash = hash_element(element);
        let was_present = self.elements.remove(&hash).is_some();
        if was_present {
            self.update_digest();
        }
        was_present
    }

    /// Check if an element is in the accumulator.
    pub fn contains(&self, element: &[u8]) -> bool {
        let hash = hash_element(element);
        self.elements.contains_key(&hash)
    }

    /// Generate a membership proof for an element.
    ///
    /// # Errors
    ///
    /// Returns `AccumulatorError::ElementNotFound` if the element is not in the accumulator.
    pub fn prove(&self, element: &[u8]) -> AccumulatorResult<MembershipProof> {
        let element_hash = hash_element(element);

        if !self.elements.contains_key(&element_hash) {
            return Err(AccumulatorError::ElementNotFound);
        }

        // The witness is all other element hashes
        let mut witness_hashes: Vec<_> = self
            .elements
            .keys()
            .filter(|&&h| h != element_hash)
            .collect();
        witness_hashes.sort();

        let mut witness = Vec::new();
        for hash in witness_hashes {
            witness.extend_from_slice(hash);
        }

        Ok(MembershipProof {
            element_hash,
            witness,
        })
    }

    /// Verify a membership proof.
    pub fn verify(&self, element: &[u8], proof: &MembershipProof) -> bool {
        let element_hash = hash_element(element);

        if element_hash != proof.element_hash {
            return false;
        }

        // Reconstruct the digest from the proof
        let mut all_hashes = vec![element_hash];
        for chunk in proof.witness.chunks(32) {
            if chunk.len() == 32 {
                let hash: [u8; 32] = chunk.try_into().unwrap();
                all_hashes.push(hash);
            }
        }
        all_hashes.sort();

        let mut hasher = blake3::Hasher::new();
        for hash in all_hashes {
            hasher.update(&hash);
        }
        let computed_digest: [u8; 32] = hasher.finalize().into();

        computed_digest == self.digest
    }

    /// Get the current accumulator digest.
    pub fn digest(&self) -> AccumulatorDigest {
        AccumulatorDigest::from_bytes(self.digest)
    }

    /// Get the number of elements in the accumulator.
    pub fn len(&self) -> usize {
        self.elements.len()
    }

    /// Check if the accumulator is empty.
    pub fn is_empty(&self) -> bool {
        self.elements.is_empty()
    }

    /// Get all elements in the accumulator.
    pub fn elements(&self) -> Vec<Vec<u8>> {
        self.elements.values().cloned().collect()
    }

    /// Batch add multiple elements.
    pub fn add_batch(&mut self, elements: &[&[u8]]) -> usize {
        let mut added = 0;
        for elem in elements {
            let hash = hash_element(elem);
            if self.elements.insert(hash, elem.to_vec()).is_none() {
                added += 1;
            }
        }
        if added > 0 {
            self.update_digest();
        }
        added
    }

    /// Batch remove multiple elements.
    pub fn remove_batch(&mut self, elements: &[&[u8]]) -> usize {
        let mut removed = 0;
        for elem in elements {
            let hash = hash_element(elem);
            if self.elements.remove(&hash).is_some() {
                removed += 1;
            }
        }
        if removed > 0 {
            self.update_digest();
        }
        removed
    }

    /// Serialize to bytes.
    pub fn to_bytes(&self) -> AccumulatorResult<Vec<u8>> {
        crate::codec::encode(self).map_err(|e| AccumulatorError::SerializationError(e.to_string()))
    }

    /// Deserialize from bytes.
    pub fn from_bytes(bytes: &[u8]) -> AccumulatorResult<Self> {
        crate::codec::decode(bytes).map_err(|e| AccumulatorError::SerializationError(e.to_string()))
    }
}

impl Default for HashAccumulator {
    fn default() -> Self {
        Self::new()
    }
}

/// Compact accumulator that only stores the digest and element count.
///
/// This is useful when you only need to verify proofs but don't need
/// to generate new proofs or track individual elements.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CompactAccumulator {
    digest: [u8; 32],
    count: usize,
}

impl CompactAccumulator {
    /// Create from a full accumulator.
    pub fn from_accumulator(acc: &HashAccumulator) -> Self {
        Self {
            digest: acc.digest,
            count: acc.len(),
        }
    }

    /// Create from digest and count.
    pub fn new(digest: [u8; 32], count: usize) -> Self {
        Self { digest, count }
    }

    /// Get the digest.
    pub fn digest(&self) -> AccumulatorDigest {
        AccumulatorDigest::from_bytes(self.digest)
    }

    /// Get the element count.
    pub fn count(&self) -> usize {
        self.count
    }

    /// Verify a membership proof against this compact accumulator.
    pub fn verify(&self, element: &[u8], proof: &MembershipProof) -> bool {
        let element_hash = hash_element(element);

        if element_hash != proof.element_hash {
            return false;
        }

        // Reconstruct the digest from the proof
        let mut all_hashes = vec![element_hash];
        for chunk in proof.witness.chunks(32) {
            if chunk.len() == 32 {
                let hash: [u8; 32] = chunk.try_into().unwrap();
                all_hashes.push(hash);
            }
        }

        // Check that we have the right number of elements
        if all_hashes.len() != self.count {
            return false;
        }

        all_hashes.sort();

        let mut hasher = blake3::Hasher::new();
        for hash in all_hashes {
            hasher.update(&hash);
        }
        let computed_digest: [u8; 32] = hasher.finalize().into();

        computed_digest == self.digest
    }
}

/// Bloom filter-based probabilistic accumulator for fast membership testing.
///
/// This is not cryptographically secure but provides very fast membership
/// testing with a small false positive rate.
#[derive(Clone, Debug)]
pub struct BloomAccumulator {
    bits: Vec<bool>,
    num_hashes: usize,
    count: usize,
}

impl BloomAccumulator {
    /// Create a new Bloom filter with given capacity and false positive rate.
    ///
    /// # Arguments
    ///
    /// * `capacity` - Expected number of elements
    /// * `false_positive_rate` - Desired false positive rate (e.g., 0.01 for 1%)
    pub fn new(capacity: usize, false_positive_rate: f64) -> Self {
        let bits_per_element = -1.44 * false_positive_rate.log2();
        let num_bits = ((capacity as f64) * bits_per_element).ceil() as usize;
        let num_hashes = (bits_per_element * std::f64::consts::LN_2).ceil() as usize;

        Self {
            bits: vec![false; num_bits],
            num_hashes,
            count: 0,
        }
    }

    /// Add an element to the Bloom filter.
    pub fn add(&mut self, element: &[u8]) {
        let hash = blake3::hash(element);
        let hash_bytes = hash.as_bytes();

        for i in 0..self.num_hashes {
            let index = self.hash_index(hash_bytes, i);
            self.bits[index] = true;
        }
        self.count += 1;
    }

    /// Check if an element might be in the set (may have false positives).
    pub fn might_contain(&self, element: &[u8]) -> bool {
        let hash = blake3::hash(element);
        let hash_bytes = hash.as_bytes();

        for i in 0..self.num_hashes {
            let index = self.hash_index(hash_bytes, i);
            if !self.bits[index] {
                return false;
            }
        }
        true
    }

    fn hash_index(&self, hash: &[u8], i: usize) -> usize {
        let offset = (i * 8) % hash.len();
        let mut bytes = [0u8; 8];
        for j in 0..8 {
            bytes[j] = hash[(offset + j) % hash.len()];
        }
        let value = u64::from_le_bytes(bytes);
        (value as usize) % self.bits.len()
    }

    /// Get the number of elements added (approximate).
    pub fn count(&self) -> usize {
        self.count
    }

    /// Get the size of the Bloom filter in bits.
    pub fn size(&self) -> usize {
        self.bits.len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_hash_accumulator_basic() {
        let mut acc = HashAccumulator::new();
        assert!(acc.is_empty());

        let elem1 = b"element1";
        let elem2 = b"element2";

        assert!(acc.add(elem1));
        assert!(acc.add(elem2));
        assert_eq!(acc.len(), 2);

        assert!(acc.contains(elem1));
        assert!(acc.contains(elem2));
        assert!(!acc.contains(b"element3"));
    }

    #[test]
    fn test_hash_accumulator_proof() {
        let mut acc = HashAccumulator::new();
        let elem1 = b"peer_id_1";
        let elem2 = b"peer_id_2";
        let elem3 = b"peer_id_3";

        acc.add(elem1);
        acc.add(elem2);
        acc.add(elem3);

        let proof1 = acc.prove(elem1).unwrap();
        assert!(acc.verify(elem1, &proof1));

        let proof2 = acc.prove(elem2).unwrap();
        assert!(acc.verify(elem2, &proof2));

        // Wrong element should fail
        assert!(!acc.verify(b"wrong", &proof1));
    }

    #[test]
    fn test_hash_accumulator_remove() {
        let mut acc = HashAccumulator::new();
        let elem1 = b"element1";
        let elem2 = b"element2";

        acc.add(elem1);
        acc.add(elem2);

        assert!(acc.remove(elem1));
        assert!(!acc.contains(elem1));
        assert!(acc.contains(elem2));

        // Removing again should return false
        assert!(!acc.remove(elem1));
    }

    #[test]
    fn test_hash_accumulator_from_elements() {
        let elements = vec![b"elem1".as_ref(), b"elem2".as_ref(), b"elem3".as_ref()];
        let acc = HashAccumulator::from_elements(&elements);

        assert_eq!(acc.len(), 3);
        for elem in &elements {
            assert!(acc.contains(elem));
        }
    }

    #[test]
    fn test_hash_accumulator_batch_operations() {
        let mut acc = HashAccumulator::new();
        let elements = vec![b"elem1".as_ref(), b"elem2".as_ref(), b"elem3".as_ref()];

        let added = acc.add_batch(&elements);
        assert_eq!(added, 3);
        assert_eq!(acc.len(), 3);

        let removed = acc.remove_batch(&elements[0..2]);
        assert_eq!(removed, 2);
        assert_eq!(acc.len(), 1);
    }

    #[test]
    fn test_compact_accumulator() {
        let mut acc = HashAccumulator::new();
        acc.add(b"elem1");
        acc.add(b"elem2");
        acc.add(b"elem3");

        let proof = acc.prove(b"elem1").unwrap();

        let compact = CompactAccumulator::from_accumulator(&acc);
        assert_eq!(compact.count(), 3);
        assert!(compact.verify(b"elem1", &proof));
    }

    #[test]
    fn test_bloom_accumulator() {
        let mut bloom = BloomAccumulator::new(1000, 0.01);

        let elements = vec![b"elem1", b"elem2", b"elem3"];
        for elem in &elements {
            bloom.add(*elem);
        }

        for elem in &elements {
            assert!(bloom.might_contain(*elem));
        }

        // Element not added should probably not be present
        // (may have false positives, but unlikely with low rate)
        let not_added = b"definitely_not_added_unique_12345";
        // We can't assert false here due to false positives, but we can test it exists
        let _ = bloom.might_contain(not_added);
    }

    #[test]
    fn test_accumulator_serialization() {
        let mut acc = HashAccumulator::new();
        acc.add(b"elem1");
        acc.add(b"elem2");

        let bytes = acc.to_bytes().unwrap();
        let restored = HashAccumulator::from_bytes(&bytes).unwrap();

        assert_eq!(acc.digest(), restored.digest());
        assert_eq!(acc.len(), restored.len());
        assert!(restored.contains(b"elem1"));
        assert!(restored.contains(b"elem2"));
    }

    #[test]
    fn test_proof_serialization() {
        let mut acc = HashAccumulator::new();
        acc.add(b"elem1");
        acc.add(b"elem2");

        let proof = acc.prove(b"elem1").unwrap();
        let bytes = proof.to_bytes().unwrap();
        let restored = MembershipProof::from_bytes(&bytes).unwrap();

        assert!(acc.verify(b"elem1", &restored));
    }

    #[test]
    fn test_accumulator_digest_changes() {
        let mut acc = HashAccumulator::new();
        let digest1 = acc.digest();

        acc.add(b"elem1");
        let digest2 = acc.digest();
        assert_ne!(digest1, digest2);

        acc.add(b"elem2");
        let digest3 = acc.digest();
        assert_ne!(digest2, digest3);

        acc.remove(b"elem1");
        let digest4 = acc.digest();
        assert_ne!(digest3, digest4);
    }

    #[test]
    fn test_proof_not_found() {
        let acc = HashAccumulator::new();
        assert!(acc.prove(b"nonexistent").is_err());
    }

    #[test]
    fn test_duplicate_add() {
        let mut acc = HashAccumulator::new();
        assert!(acc.add(b"elem1"));
        assert!(!acc.add(b"elem1")); // Second add should return false
        assert_eq!(acc.len(), 1);
    }
}