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
//! Privacy-preserving unlinkable tokens for anonymous credentials.
//!
//! This module provides unlinkable token credentials using commitments:
//! - Issuer signs committed values without seeing actual token data
//! - Tokens can be redeemed anonymously without linking to issuance
//! - Useful for anonymous bandwidth credits, reputation tokens, etc.
//!
//! Protocol:
//! 1. User creates token with random serial number
//! 2. User creates commitment to token (hash of serial + blinding factor)
//! 3. Issuer signs the commitment without seeing the serial
//! 4. User redeems by revealing token, blinding, and signature
//! 5. Verifier validates that commitment matches and signature is valid

use blake3;
use ed25519_dalek::{Signature, Signer, SigningKey, Verifier, VerifyingKey};
use rand::RngExt;
use thiserror::Error;
use zeroize::Zeroize;

/// Errors for unlinkable token operations.
#[derive(Debug, Error)]
pub enum BlindError {
    #[error("Invalid commitment")]
    InvalidCommitment,
    #[error("Invalid token signature")]
    InvalidSignature,
    #[error("Signature verification failed")]
    VerificationFailed,
    #[error("Invalid public key")]
    InvalidPublicKey,
    #[error("Token already spent")]
    TokenAlreadySpent,
}

pub type BlindResult<T> = Result<T, BlindError>;

/// An unlinkable token with a unique serial number.
#[derive(Clone, Debug)]
pub struct UnlinkableToken {
    /// Unique serial number (prevents double-spending)
    pub serial: [u8; 32],
    /// Token value/amount
    pub value: u64,
    /// Expiration timestamp (Unix timestamp)
    pub expiry: u64,
}

/// Blinding factor used to create commitment.
#[derive(Clone, Zeroize)]
#[zeroize(drop)]
pub struct BlindingFactor {
    factor: [u8; 32],
}

/// Commitment to a token (sent to issuer for signing).
#[derive(Clone, Debug)]
pub struct TokenCommitment {
    commitment: [u8; 32],
}

/// Signed token commitment from issuer.
#[derive(Clone, Debug)]
pub struct SignedCommitment {
    commitment: [u8; 32],
    signature: [u8; 64],
}

/// Redeemable token with all information needed for verification.
#[derive(Clone, Debug)]
pub struct RedeemableToken {
    pub token: UnlinkableToken,
    pub blinding_factor: [u8; 32],
    pub signature: [u8; 64],
}

impl UnlinkableToken {
    /// Create a new token with random serial number.
    pub fn new(value: u64, expiry: u64) -> Self {
        let mut rng = rand::rng();
        let mut serial = [0u8; 32];
        rng.fill(&mut serial);
        Self {
            serial,
            value,
            expiry,
        }
    }

    /// Create token with specific serial (for testing).
    pub fn with_serial(serial: [u8; 32], value: u64, expiry: u64) -> Self {
        Self {
            serial,
            value,
            expiry,
        }
    }

    /// Serialize token to bytes for hashing.
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut bytes = Vec::new();
        bytes.extend_from_slice(&self.serial);
        bytes.extend_from_slice(&self.value.to_le_bytes());
        bytes.extend_from_slice(&self.expiry.to_le_bytes());
        bytes
    }
}

impl BlindingFactor {
    /// Generate random blinding factor.
    pub fn generate() -> Self {
        let mut rng = rand::rng();
        let mut factor = [0u8; 32];
        rng.fill(&mut factor);
        Self { factor }
    }

    /// Create from bytes.
    pub fn from_bytes(bytes: [u8; 32]) -> Self {
        Self { factor: bytes }
    }

    /// Get bytes (warning: sensitive data).
    pub fn to_bytes(&self) -> [u8; 32] {
        self.factor
    }

    /// Create commitment to a token.
    pub fn commit(&self, token: &UnlinkableToken) -> TokenCommitment {
        let mut hasher = blake3::Hasher::new();
        hasher.update(b"TOKEN_COMMITMENT:");
        hasher.update(&token.to_bytes());
        hasher.update(&self.factor);
        TokenCommitment {
            commitment: *hasher.finalize().as_bytes(),
        }
    }

    /// Verify a commitment matches the token and blinding.
    pub fn verify_commitment(&self, token: &UnlinkableToken, commitment: &TokenCommitment) -> bool {
        let recomputed = self.commit(token);
        recomputed.commitment == commitment.commitment
    }
}

impl TokenCommitment {
    /// Get commitment bytes.
    pub fn as_bytes(&self) -> &[u8; 32] {
        &self.commitment
    }

    /// Create from bytes.
    pub fn from_bytes(bytes: [u8; 32]) -> Self {
        Self { commitment: bytes }
    }
}

/// Token issuer that signs commitments.
pub struct BlindSigner {
    signing_key: SigningKey,
}

impl BlindSigner {
    /// Create new issuer with random key.
    pub fn generate() -> Self {
        use rand_core06::OsRng;
        Self {
            signing_key: SigningKey::generate(&mut OsRng),
        }
    }

    /// Create from existing key.
    pub fn from_signing_key(signing_key: SigningKey) -> Self {
        Self { signing_key }
    }

    /// Create from bytes.
    pub fn from_bytes(bytes: &[u8; 32]) -> Self {
        Self {
            signing_key: SigningKey::from_bytes(bytes),
        }
    }

    /// Get public key.
    pub fn public_key(&self) -> BlindPublicKey {
        BlindPublicKey {
            verifying_key: self.signing_key.verifying_key(),
        }
    }

    /// Sign a token commitment (issuer doesn't see actual token).
    pub fn sign_commitment(&self, commitment: &TokenCommitment) -> SignedCommitment {
        let signature = self.signing_key.sign(&commitment.commitment);
        SignedCommitment {
            commitment: commitment.commitment,
            signature: signature.to_bytes(),
        }
    }

    /// Get signing key bytes (warning: secret).
    pub fn to_bytes(&self) -> [u8; 32] {
        self.signing_key.to_bytes()
    }
}

/// Public key for verifying redeemed tokens.
#[derive(Clone, Debug)]
pub struct BlindPublicKey {
    verifying_key: VerifyingKey,
}

impl BlindPublicKey {
    /// Create from bytes.
    pub fn from_bytes(bytes: &[u8; 32]) -> BlindResult<Self> {
        let verifying_key =
            VerifyingKey::from_bytes(bytes).map_err(|_| BlindError::InvalidPublicKey)?;
        Ok(Self { verifying_key })
    }

    /// Get public key bytes.
    pub fn to_bytes(&self) -> [u8; 32] {
        self.verifying_key.to_bytes()
    }

    /// Verify a redeemable token.
    ///
    /// Checks that:
    /// 1. The commitment is correctly formed from token + blinding
    /// 2. The signature on the commitment is valid
    pub fn verify_token(&self, redeemable: &RedeemableToken) -> BlindResult<()> {
        // Recompute commitment from token and blinding
        let blinding = BlindingFactor::from_bytes(redeemable.blinding_factor);
        let commitment = blinding.commit(&redeemable.token);

        // Verify signature on commitment
        let signature = Signature::from_bytes(&redeemable.signature);
        self.verifying_key
            .verify(&commitment.commitment, &signature)
            .map_err(|_| BlindError::VerificationFailed)?;

        Ok(())
    }

    /// Verify a signed commitment directly.
    pub fn verify_commitment(&self, signed: &SignedCommitment) -> BlindResult<()> {
        let signature = Signature::from_bytes(&signed.signature);
        self.verifying_key
            .verify(&signed.commitment, &signature)
            .map_err(|_| BlindError::VerificationFailed)?;
        Ok(())
    }
}

/// Complete unlinkable token protocol.
pub struct BlindSignatureProtocol;

impl BlindSignatureProtocol {
    /// User: Create a new token and commitment (Step 1-2).
    ///
    /// Returns (commitment to send to issuer, token, blinding factor to keep secret).
    pub fn create_token(
        value: u64,
        expiry: u64,
    ) -> (TokenCommitment, UnlinkableToken, BlindingFactor) {
        let token = UnlinkableToken::new(value, expiry);
        let blinding = BlindingFactor::generate();
        let commitment = blinding.commit(&token);
        (commitment, token, blinding)
    }

    /// Issuer: Sign a token commitment (Step 3).
    pub fn issue_token(issuer: &BlindSigner, commitment: &TokenCommitment) -> SignedCommitment {
        issuer.sign_commitment(commitment)
    }

    /// User: Create redeemable token (Step 4).
    pub fn prepare_redemption(
        token: UnlinkableToken,
        blinding: BlindingFactor,
        signed: SignedCommitment,
    ) -> RedeemableToken {
        RedeemableToken {
            token,
            blinding_factor: blinding.to_bytes(),
            signature: signed.signature,
        }
    }

    /// Verifier: Verify and redeem token (Step 5).
    pub fn verify_and_redeem(
        public_key: &BlindPublicKey,
        redeemable: &RedeemableToken,
        current_time: u64,
    ) -> BlindResult<()> {
        // Check expiry
        if current_time > redeemable.token.expiry {
            return Err(BlindError::VerificationFailed);
        }

        // Verify token
        public_key.verify_token(redeemable)?;

        Ok(())
    }
}

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

    #[test]
    fn test_unlinkable_token_flow() {
        let issuer = BlindSigner::generate();
        let public_key = issuer.public_key();

        // User creates token and commitment
        let (commitment, token, blinding) = BlindSignatureProtocol::create_token(100, u64::MAX);

        // Issuer signs commitment without seeing token
        let signed = BlindSignatureProtocol::issue_token(&issuer, &commitment);

        // User prepares redeemable token
        let redeemable = BlindSignatureProtocol::prepare_redemption(token, blinding, signed);

        // Verifier redeems token
        BlindSignatureProtocol::verify_and_redeem(&public_key, &redeemable, 0).unwrap();
    }

    #[test]
    fn test_commitment_verification() {
        let token = UnlinkableToken::new(50, u64::MAX);
        let blinding = BlindingFactor::generate();
        let commitment = blinding.commit(&token);

        // Correct blinding should verify
        assert!(blinding.verify_commitment(&token, &commitment));

        // Wrong blinding should not verify
        let wrong_blinding = BlindingFactor::generate();
        assert!(!wrong_blinding.verify_commitment(&token, &commitment));
    }

    #[test]
    fn test_different_tokens() {
        let issuer = BlindSigner::generate();
        let public_key = issuer.public_key();

        // Create two different tokens
        let (comm1, tok1, blind1) = BlindSignatureProtocol::create_token(100, u64::MAX);
        let (comm2, tok2, blind2) = BlindSignatureProtocol::create_token(200, u64::MAX);

        assert_ne!(tok1.serial, tok2.serial);
        assert_ne!(comm1.as_bytes(), comm2.as_bytes());

        // Sign both
        let signed1 = issuer.sign_commitment(&comm1);
        let signed2 = issuer.sign_commitment(&comm2);

        // Both should redeem correctly
        let redeem1 = BlindSignatureProtocol::prepare_redemption(tok1, blind1, signed1);
        let redeem2 = BlindSignatureProtocol::prepare_redemption(tok2, blind2, signed2);

        public_key.verify_token(&redeem1).unwrap();
        public_key.verify_token(&redeem2).unwrap();
    }

    #[test]
    fn test_wrong_blinding() {
        let issuer = BlindSigner::generate();
        let public_key = issuer.public_key();

        let (commitment, token, _correct_blinding) =
            BlindSignatureProtocol::create_token(100, u64::MAX);
        let signed = issuer.sign_commitment(&commitment);

        // Try to redeem with wrong blinding
        let wrong_blinding = BlindingFactor::generate();
        let wrong_redeemable = RedeemableToken {
            token,
            blinding_factor: wrong_blinding.to_bytes(),
            signature: signed.signature,
        };

        // Should fail verification
        assert!(public_key.verify_token(&wrong_redeemable).is_err());
    }

    #[test]
    fn test_expired_token() {
        let issuer = BlindSigner::generate();
        let public_key = issuer.public_key();

        // Create token that expires at time 1000
        let (commitment, token, blinding) = BlindSignatureProtocol::create_token(100, 1000);
        let signed = issuer.sign_commitment(&commitment);
        let redeemable = BlindSignatureProtocol::prepare_redemption(token, blinding, signed);

        // Should succeed before expiry
        BlindSignatureProtocol::verify_and_redeem(&public_key, &redeemable, 999).unwrap();

        // Should fail after expiry
        assert!(BlindSignatureProtocol::verify_and_redeem(&public_key, &redeemable, 1001).is_err());
    }

    #[test]
    fn test_unlinkability() {
        let issuer = BlindSigner::generate();

        // Same token value but different serials
        let (comm1, tok1, _) = BlindSignatureProtocol::create_token(100, u64::MAX);
        let (comm2, tok2, _) = BlindSignatureProtocol::create_token(100, u64::MAX);

        // Commitments should be different (unlinkable)
        assert_ne!(comm1.as_bytes(), comm2.as_bytes());
        assert_ne!(tok1.serial, tok2.serial);

        // Signatures should be different
        let sig1 = issuer.sign_commitment(&comm1);
        let sig2 = issuer.sign_commitment(&comm2);
        assert_ne!(sig1.signature, sig2.signature);
    }

    #[test]
    fn test_serialization() {
        let token = UnlinkableToken::new(123, 456);
        let bytes = token.to_bytes();
        assert_eq!(bytes.len(), 32 + 8 + 8);

        // Serial should be first 32 bytes
        assert_eq!(&bytes[..32], &token.serial);
    }

    #[test]
    fn test_key_serialization() {
        let issuer = BlindSigner::generate();
        let public_key = issuer.public_key();

        // Serialize and deserialize
        let issuer_bytes = issuer.to_bytes();
        let issuer2 = BlindSigner::from_bytes(&issuer_bytes);

        let pk_bytes = public_key.to_bytes();
        let pk2 = BlindPublicKey::from_bytes(&pk_bytes).unwrap();

        // Should work the same
        let (commitment, token, blinding) = BlindSignatureProtocol::create_token(100, u64::MAX);
        let signed = issuer2.sign_commitment(&commitment);
        let redeemable = BlindSignatureProtocol::prepare_redemption(token, blinding, signed);

        pk2.verify_token(&redeemable).unwrap();
    }

    #[test]
    fn test_blinding_factor_zeroize() {
        let factor = BlindingFactor::generate();
        let _bytes = factor.to_bytes();

        // Drop should zeroize (verified by type implementing Zeroize)
        drop(factor);
    }
}