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
//! Zero-Knowledge Proof Composition Framework
//!
//! This module provides a framework for composing multiple zero-knowledge proofs
//! into complex protocols with AND/OR logic and proof aggregation.
//!
//! Perfect for building complex privacy-preserving protocols where you need to prove
//! multiple statements together (e.g., "I know a secret AND it's in a certain range").

use blake3;
use serde::{Deserialize, Serialize};
use thiserror::Error;

/// Errors that can occur in ZK proof operations
#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum ZkProofError {
    #[error("Proof verification failed")]
    VerificationFailed,
    #[error("Invalid proof composition")]
    InvalidComposition,
    #[error("Proof not found")]
    ProofNotFound,
    #[error("Invalid proof type")]
    InvalidProofType,
}

pub type ZkProofResult<T> = Result<T, ZkProofError>;

/// A zero-knowledge proof with metadata
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ZkProof {
    /// Unique identifier for this proof type
    proof_type: String,
    /// The actual proof data
    proof_data: Vec<u8>,
    /// Public inputs/statements
    public_inputs: Vec<Vec<u8>>,
    /// Optional metadata
    metadata: Vec<(String, Vec<u8>)>,
}

impl ZkProof {
    /// Create a new ZK proof
    pub fn new(
        proof_type: impl Into<String>,
        proof_data: Vec<u8>,
        public_inputs: Vec<Vec<u8>>,
    ) -> Self {
        Self {
            proof_type: proof_type.into(),
            proof_data,
            public_inputs,
            metadata: Vec::new(),
        }
    }

    /// Add metadata to the proof
    pub fn with_metadata(mut self, key: impl Into<String>, value: Vec<u8>) -> Self {
        self.metadata.push((key.into(), value));
        self
    }

    /// Get the proof type
    pub fn proof_type(&self) -> &str {
        &self.proof_type
    }

    /// Get the proof data
    pub fn proof_data(&self) -> &[u8] {
        &self.proof_data
    }

    /// Get the public inputs
    pub fn public_inputs(&self) -> &[Vec<u8>] {
        &self.public_inputs
    }

    /// Get metadata by key
    pub fn get_metadata(&self, key: &str) -> Option<&[u8]> {
        self.metadata
            .iter()
            .find(|(k, _)| k == key)
            .map(|(_, v)| v.as_slice())
    }

    /// Compute a hash commitment to this proof
    pub fn commitment(&self) -> [u8; 32] {
        let mut hasher = blake3::Hasher::new();
        hasher.update(b"ZK_PROOF_COMMITMENT:");
        hasher.update(self.proof_type.as_bytes());
        hasher.update(&self.proof_data);
        for input in &self.public_inputs {
            hasher.update(input);
        }
        *hasher.finalize().as_bytes()
    }
}

/// Composite proof combining multiple proofs with AND logic
///
/// All constituent proofs must verify for the composite to be valid
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AndProof {
    proofs: Vec<ZkProof>,
    /// Optional binding to ensure all proofs relate to the same statement
    binding: Option<[u8; 32]>,
}

impl AndProof {
    /// Create a new AND composition of proofs
    pub fn new(proofs: Vec<ZkProof>) -> Self {
        Self {
            proofs,
            binding: None,
        }
    }

    /// Add a binding value that links all proofs together
    pub fn with_binding(mut self, binding: [u8; 32]) -> Self {
        self.binding = Some(binding);
        self
    }

    /// Get all constituent proofs
    pub fn proofs(&self) -> &[ZkProof] {
        &self.proofs
    }

    /// Get a proof by type
    pub fn get_proof(&self, proof_type: &str) -> Option<&ZkProof> {
        self.proofs.iter().find(|p| p.proof_type() == proof_type)
    }

    /// Verify the binding if present
    pub fn verify_binding(&self) -> ZkProofResult<()> {
        if let Some(binding) = self.binding {
            // Compute expected binding from all proof commitments
            let mut hasher = blake3::Hasher::new();
            hasher.update(b"AND_PROOF_BINDING:");
            for proof in &self.proofs {
                hasher.update(&proof.commitment());
            }
            let expected = hasher.finalize();

            if expected.as_bytes() != &binding {
                return Err(ZkProofError::VerificationFailed);
            }
        }
        Ok(())
    }

    /// Compute the AND composition's commitment
    pub fn commitment(&self) -> [u8; 32] {
        let mut hasher = blake3::Hasher::new();
        hasher.update(b"AND_COMPOSITION:");
        for proof in &self.proofs {
            hasher.update(&proof.commitment());
        }
        if let Some(binding) = &self.binding {
            hasher.update(binding);
        }
        *hasher.finalize().as_bytes()
    }
}

/// Composite proof combining multiple proofs with OR logic
///
/// At least one constituent proof must verify for the composite to be valid
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OrProof {
    /// The valid proof (only one is revealed)
    proof: ZkProof,
    /// Commitments to all possible proofs (for privacy)
    commitments: Vec<[u8; 32]>,
    /// Index of the revealed proof
    revealed_index: usize,
}

impl OrProof {
    /// Create a new OR composition (reveals one proof, hides others)
    pub fn new(all_proofs: Vec<ZkProof>, revealed_index: usize) -> ZkProofResult<Self> {
        if revealed_index >= all_proofs.len() {
            return Err(ZkProofError::InvalidComposition);
        }

        let commitments: Vec<[u8; 32]> = all_proofs.iter().map(|p| p.commitment()).collect();

        Ok(Self {
            proof: all_proofs[revealed_index].clone(),
            commitments,
            revealed_index,
        })
    }

    /// Get the revealed proof
    pub fn proof(&self) -> &ZkProof {
        &self.proof
    }

    /// Get all commitments
    pub fn commitments(&self) -> &[[u8; 32]] {
        &self.commitments
    }

    /// Get the revealed index
    pub fn revealed_index(&self) -> usize {
        self.revealed_index
    }

    /// Verify that the revealed proof matches its commitment
    pub fn verify_commitment(&self) -> ZkProofResult<()> {
        let commitment = self.proof.commitment();
        if commitment != self.commitments[self.revealed_index] {
            return Err(ZkProofError::VerificationFailed);
        }
        Ok(())
    }
}

/// Builder for creating composite proofs
pub struct ZkProofBuilder {
    proofs: Vec<ZkProof>,
}

impl ZkProofBuilder {
    /// Create a new proof builder
    pub fn new() -> Self {
        Self { proofs: Vec::new() }
    }

    /// Add a proof to the composition
    pub fn add_proof(mut self, proof: ZkProof) -> Self {
        self.proofs.push(proof);
        self
    }

    /// Build an AND composition
    pub fn build_and(self) -> AndProof {
        AndProof::new(self.proofs)
    }

    /// Build an AND composition with binding
    pub fn build_and_with_binding(self, binding: [u8; 32]) -> AndProof {
        AndProof::new(self.proofs).with_binding(binding)
    }

    /// Build an OR composition (reveal one proof)
    pub fn build_or(self, revealed_index: usize) -> ZkProofResult<OrProof> {
        OrProof::new(self.proofs, revealed_index)
    }
}

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

/// Trait for types that can be proven in zero-knowledge
pub trait ZkProvable {
    /// Generate a zero-knowledge proof
    fn prove(&self) -> ZkProofResult<ZkProof>;

    /// Verify a zero-knowledge proof
    fn verify(proof: &ZkProof) -> ZkProofResult<bool>;
}

/// Helper to create a binding value for AND compositions
pub fn create_binding(proofs: &[&ZkProof]) -> [u8; 32] {
    let mut hasher = blake3::Hasher::new();
    hasher.update(b"AND_PROOF_BINDING:");
    for proof in proofs {
        hasher.update(&proof.commitment());
    }
    *hasher.finalize().as_bytes()
}

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

    fn create_test_proof(proof_type: &str, data: &[u8]) -> ZkProof {
        ZkProof::new(proof_type, data.to_vec(), vec![b"public_input".to_vec()])
    }

    #[test]
    fn test_zkproof_creation() {
        let proof = create_test_proof("range", b"proof_data");
        assert_eq!(proof.proof_type(), "range");
        assert_eq!(proof.proof_data(), b"proof_data");
        assert_eq!(proof.public_inputs().len(), 1);
    }

    #[test]
    fn test_zkproof_metadata() {
        let proof = create_test_proof("range", b"proof_data")
            .with_metadata("key1", b"value1".to_vec())
            .with_metadata("key2", b"value2".to_vec());

        assert_eq!(proof.get_metadata("key1"), Some(b"value1".as_slice()));
        assert_eq!(proof.get_metadata("key2"), Some(b"value2".as_slice()));
        assert_eq!(proof.get_metadata("key3"), None);
    }

    #[test]
    fn test_zkproof_commitment() {
        let proof1 = create_test_proof("range", b"proof_data");
        let proof2 = create_test_proof("range", b"proof_data");
        let proof3 = create_test_proof("range", b"different_data");

        // Same data should produce same commitment
        assert_eq!(proof1.commitment(), proof2.commitment());

        // Different data should produce different commitment
        assert_ne!(proof1.commitment(), proof3.commitment());
    }

    #[test]
    fn test_and_proof_basic() {
        let proof1 = create_test_proof("range", b"proof1");
        let proof2 = create_test_proof("membership", b"proof2");
        let proof3 = create_test_proof("signature", b"proof3");

        let and_proof = AndProof::new(vec![proof1, proof2, proof3]);

        assert_eq!(and_proof.proofs().len(), 3);
        assert!(and_proof.get_proof("range").is_some());
        assert!(and_proof.get_proof("membership").is_some());
        assert!(and_proof.get_proof("signature").is_some());
        assert!(and_proof.get_proof("nonexistent").is_none());
    }

    #[test]
    fn test_and_proof_binding() {
        let proof1 = create_test_proof("range", b"proof1");
        let proof2 = create_test_proof("membership", b"proof2");

        // Clone for binding computation since we need to move them into AndProof
        let binding = create_binding(&[&proof1, &proof2]);
        let and_proof = AndProof::new(vec![proof1.clone(), proof2.clone()]).with_binding(binding);

        // Correct binding should verify
        assert!(and_proof.verify_binding().is_ok());
    }

    #[test]
    fn test_and_proof_invalid_binding() {
        let proof1 = create_test_proof("range", b"proof1");
        let proof2 = create_test_proof("membership", b"proof2");

        // Create with wrong binding
        let wrong_binding = [0u8; 32];
        let and_proof = AndProof::new(vec![proof1, proof2]).with_binding(wrong_binding);

        // Wrong binding should fail
        assert!(and_proof.verify_binding().is_err());
    }

    #[test]
    fn test_or_proof_basic() {
        let proof1 = create_test_proof("option1", b"proof1");
        let proof2 = create_test_proof("option2", b"proof2");
        let proof3 = create_test_proof("option3", b"proof3");

        let or_proof = OrProof::new(vec![proof1, proof2.clone(), proof3], 1).unwrap();

        assert_eq!(or_proof.revealed_index(), 1);
        assert_eq!(or_proof.proof().proof_type(), "option2");
        assert_eq!(or_proof.commitments().len(), 3);
    }

    #[test]
    fn test_or_proof_commitment_verification() {
        let proof1 = create_test_proof("option1", b"proof1");
        let proof2 = create_test_proof("option2", b"proof2");

        let or_proof = OrProof::new(vec![proof1, proof2], 0).unwrap();

        // Commitment should verify
        assert!(or_proof.verify_commitment().is_ok());
    }

    #[test]
    fn test_or_proof_invalid_index() {
        let proof1 = create_test_proof("option1", b"proof1");
        let proof2 = create_test_proof("option2", b"proof2");

        // Index out of bounds should error
        let result = OrProof::new(vec![proof1, proof2], 5);
        assert!(result.is_err());
    }

    #[test]
    fn test_proof_builder_and() {
        let proof1 = create_test_proof("range", b"proof1");
        let proof2 = create_test_proof("membership", b"proof2");

        let and_proof = ZkProofBuilder::new()
            .add_proof(proof1)
            .add_proof(proof2)
            .build_and();

        assert_eq!(and_proof.proofs().len(), 2);
    }

    #[test]
    fn test_proof_builder_and_with_binding() {
        let proof1 = create_test_proof("range", b"proof1");
        let proof2 = create_test_proof("membership", b"proof2");

        let binding = create_binding(&[&proof1, &proof2]);
        let and_proof = ZkProofBuilder::new()
            .add_proof(proof1.clone())
            .add_proof(proof2.clone())
            .build_and_with_binding(binding);

        assert!(and_proof.verify_binding().is_ok());
    }

    #[test]
    fn test_proof_builder_or() {
        let proof1 = create_test_proof("option1", b"proof1");
        let proof2 = create_test_proof("option2", b"proof2");
        let proof3 = create_test_proof("option3", b"proof3");

        let or_proof = ZkProofBuilder::new()
            .add_proof(proof1)
            .add_proof(proof2)
            .add_proof(proof3)
            .build_or(1)
            .unwrap();

        assert_eq!(or_proof.revealed_index(), 1);
        assert_eq!(or_proof.proof().proof_type(), "option2");
    }

    #[test]
    fn test_serialization() {
        let proof1 = create_test_proof("range", b"proof1");
        let proof2 = create_test_proof("membership", b"proof2");

        let and_proof = AndProof::new(vec![proof1, proof2]);

        // Serialize with bincode
        let serialized = crate::codec::encode(&and_proof).unwrap();
        let deserialized: AndProof = crate::codec::decode(&serialized).unwrap();

        assert_eq!(deserialized.proofs().len(), 2);
        assert_eq!(deserialized.commitment(), and_proof.commitment());
    }
}