cougr-core 1.0.0

Cougr - A Soroban-compatible ECS framework for on-chain gaming on Stellar
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
use soroban_sdk::{BytesN, Env};

use super::error::ZKError;
use super::traits::{bytes32_to_scalar, i32_to_scalar, u32_to_scalar, GameCircuit};
use super::types::{Groth16Proof, Scalar, VerificationKey};

/// Movement verification circuit interface.
///
/// Verifies that a player's move is valid (within maximum allowed distance)
/// without revealing the full game state. The circuit's public inputs are:
/// `[from_x, from_y, to_x, to_y, max_distance]`.
///
/// # Example
/// ```no_run
/// use cougr_core::zk::{G1Point, G2Point};
/// use cougr_core::zk::experimental::{Groth16Proof, MovementCircuit, VerificationKey};
/// use soroban_sdk::{BytesN, Env, Vec};
///
/// let env = Env::default();
/// let g1 = G1Point { bytes: BytesN::from_array(&env, &[0u8; 64]) };
/// let g2 = G2Point { bytes: BytesN::from_array(&env, &[0u8; 128]) };
/// let mut ic = Vec::new(&env);
/// for _ in 0..6 {
///     ic.push_back(g1.clone());
/// }
/// let vk = VerificationKey {
///     alpha: g1.clone(),
///     beta: g2.clone(),
///     gamma: g2.clone(),
///     delta: g2,
///     ic,
/// };
/// let proof = Groth16Proof { a: g1.clone(), b: vk.beta.clone(), c: g1 };
/// let circuit = MovementCircuit::new(vk, 10);
/// let _valid = circuit.verify_move(&env, &proof, 0, 0, 3, 4)?;
/// # Ok::<(), cougr_core::zk::ZKError>(())
/// ```
pub struct MovementCircuit {
    pub vk: VerificationKey,
    pub max_distance: u32,
}

impl GameCircuit for MovementCircuit {
    fn verification_key(&self) -> &VerificationKey {
        &self.vk
    }
}

impl MovementCircuit {
    /// Create a new movement circuit with the given verification key and max distance.
    pub fn new(vk: VerificationKey, max_distance: u32) -> Self {
        Self { vk, max_distance }
    }

    /// Verify a move from (from_x, from_y) to (to_x, to_y).
    ///
    /// The proof must demonstrate that the move is within `max_distance`.
    /// Public inputs are encoded as: `[from_x, from_y, to_x, to_y, max_distance]`.
    pub fn verify_move(
        &self,
        env: &Env,
        proof: &Groth16Proof,
        from_x: i32,
        from_y: i32,
        to_x: i32,
        to_y: i32,
    ) -> Result<bool, ZKError> {
        let public_inputs = alloc::vec![
            i32_to_scalar(env, from_x),
            i32_to_scalar(env, from_y),
            i32_to_scalar(env, to_x),
            i32_to_scalar(env, to_y),
            u32_to_scalar(env, self.max_distance),
        ];
        self.verify_with_inputs(env, proof, &public_inputs)
    }
}

/// Combat verification circuit interface.
///
/// Verifies damage calculation without revealing hidden player stats.
/// Public inputs: `[attacker_commitment, defender_commitment, damage_result]`.
pub struct CombatCircuit {
    pub vk: VerificationKey,
}

impl GameCircuit for CombatCircuit {
    fn verification_key(&self) -> &VerificationKey {
        &self.vk
    }
}

impl CombatCircuit {
    /// Create a new combat circuit with the given verification key.
    pub fn new(vk: VerificationKey) -> Self {
        Self { vk }
    }

    /// Verify a damage calculation.
    ///
    /// The proof demonstrates that `damage_result` was correctly computed
    /// from the hidden stats of the attacker and defender.
    pub fn verify_damage(
        &self,
        env: &Env,
        proof: &Groth16Proof,
        attacker_commitment: &BytesN<32>,
        defender_commitment: &BytesN<32>,
        damage_result: u32,
    ) -> Result<bool, ZKError> {
        let public_inputs = alloc::vec![
            bytes32_to_scalar(attacker_commitment),
            bytes32_to_scalar(defender_commitment),
            u32_to_scalar(env, damage_result),
        ];
        self.verify_with_inputs(env, proof, &public_inputs)
    }
}

/// Inventory verification circuit interface.
///
/// Proves a player has a specific item without revealing the full inventory.
/// Public inputs: `[inventory_root, item_id]`.
pub struct InventoryCircuit {
    pub vk: VerificationKey,
}

impl GameCircuit for InventoryCircuit {
    fn verification_key(&self) -> &VerificationKey {
        &self.vk
    }
}

impl InventoryCircuit {
    /// Create a new inventory circuit with the given verification key.
    pub fn new(vk: VerificationKey) -> Self {
        Self { vk }
    }

    /// Verify that an inventory contains a specific item.
    ///
    /// The proof demonstrates knowledge of a Merkle path from the item
    /// to the inventory root.
    pub fn verify_has_item(
        &self,
        env: &Env,
        proof: &Groth16Proof,
        inventory_root: &BytesN<32>,
        item_id: u32,
    ) -> Result<bool, ZKError> {
        let public_inputs = alloc::vec![
            bytes32_to_scalar(inventory_root),
            u32_to_scalar(env, item_id),
        ];
        self.verify_with_inputs(env, proof, &public_inputs)
    }
}

/// Turn sequence verification circuit interface.
///
/// Proves a sequence of game actions was executed in valid order
/// with valid state transitions.
/// Public inputs: `[initial_state_hash, final_state_hash, action_count]`.
pub struct TurnSequenceCircuit {
    pub vk: VerificationKey,
}

impl GameCircuit for TurnSequenceCircuit {
    fn verification_key(&self) -> &VerificationKey {
        &self.vk
    }
}

impl TurnSequenceCircuit {
    /// Create a new turn sequence circuit with the given verification key.
    pub fn new(vk: VerificationKey) -> Self {
        Self { vk }
    }

    /// Verify a sequence of turns.
    pub fn verify_sequence(
        &self,
        env: &Env,
        proof: &Groth16Proof,
        initial_state: &BytesN<32>,
        final_state: &BytesN<32>,
        action_count: u32,
    ) -> Result<bool, ZKError> {
        let public_inputs = alloc::vec![
            bytes32_to_scalar(initial_state),
            bytes32_to_scalar(final_state),
            u32_to_scalar(env, action_count),
        ];
        self.verify_with_inputs(env, proof, &public_inputs)
    }
}

/// Developer-defined circuit that wraps a VK and pre-encoded public inputs.
///
/// Use this when you have a custom circuit not covered by the pre-built ones.
///
/// # Example
/// ```no_run
/// use cougr_core::zk::{G1Point, G2Point};
/// use cougr_core::zk::experimental::{
///     bytes32_to_scalar, u32_to_scalar, CustomCircuit, GameCircuit, Groth16Proof, VerificationKey,
/// };
/// use soroban_sdk::{BytesN, Env, Vec};
///
/// let env = Env::default();
/// let g1 = G1Point { bytes: BytesN::from_array(&env, &[0u8; 64]) };
/// let g2 = G2Point { bytes: BytesN::from_array(&env, &[0u8; 128]) };
/// let mut ic = Vec::new(&env);
/// for _ in 0..3 {
///     ic.push_back(g1.clone());
/// }
/// let vk = VerificationKey {
///     alpha: g1.clone(),
///     beta: g2.clone(),
///     gamma: g2.clone(),
///     delta: g2,
///     ic,
/// };
/// let root = BytesN::from_array(&env, &[9u8; 32]);
/// let inputs = vec![u32_to_scalar(&env, 42), bytes32_to_scalar(&root)];
/// let circuit = CustomCircuit::new(vk, inputs);
/// let proof = Groth16Proof { a: g1.clone(), b: circuit.verification_key().beta.clone(), c: g1 };
/// let _valid = circuit.verify_with_inputs(&env, &proof, circuit.public_inputs())?;
/// # Ok::<(), cougr_core::zk::ZKError>(())
/// ```
pub struct CustomCircuit {
    vk: VerificationKey,
    public_inputs: alloc::vec::Vec<Scalar>,
}

impl GameCircuit for CustomCircuit {
    fn verification_key(&self) -> &VerificationKey {
        &self.vk
    }
}

impl CustomCircuit {
    /// Create a custom circuit with pre-encoded public inputs.
    pub fn new(vk: VerificationKey, public_inputs: alloc::vec::Vec<Scalar>) -> Self {
        Self { vk, public_inputs }
    }

    /// Start building a custom circuit with a fluent API.
    pub fn builder(vk: VerificationKey) -> CustomCircuitBuilder {
        CustomCircuitBuilder {
            vk,
            inputs: alloc::vec::Vec::new(),
        }
    }

    /// Get the pre-encoded public inputs.
    pub fn public_inputs(&self) -> &[Scalar] {
        &self.public_inputs
    }

    /// Verify the proof using the stored public inputs.
    pub fn verify(&self, env: &Env, proof: &Groth16Proof) -> Result<bool, ZKError> {
        self.verify_with_inputs(env, proof, &self.public_inputs)
    }
}

/// Builder for constructing `CustomCircuit` public inputs fluently.
pub struct CustomCircuitBuilder {
    vk: VerificationKey,
    inputs: alloc::vec::Vec<Scalar>,
}

impl CustomCircuitBuilder {
    /// Add a raw scalar to the public inputs.
    pub fn add_scalar(mut self, scalar: Scalar) -> Self {
        self.inputs.push(scalar);
        self
    }

    /// Add a u32 value as a scalar.
    pub fn add_u32(mut self, env: &Env, val: u32) -> Self {
        self.inputs.push(u32_to_scalar(env, val));
        self
    }

    /// Add an i32 value as a scalar.
    pub fn add_i32(mut self, env: &Env, val: i32) -> Self {
        self.inputs.push(i32_to_scalar(env, val));
        self
    }

    /// Add a BytesN<32> as a scalar.
    pub fn add_bytes32(mut self, val: &BytesN<32>) -> Self {
        self.inputs.push(bytes32_to_scalar(val));
        self
    }

    /// Build the CustomCircuit.
    pub fn build(self) -> CustomCircuit {
        CustomCircuit {
            vk: self.vk,
            public_inputs: self.inputs,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::super::traits;
    use super::*;
    use soroban_sdk::{BytesN, Env, Vec};

    use super::super::types::{G1Point, G2Point};

    fn make_vk(env: &Env, ic_count: u32) -> VerificationKey {
        let g1 = G1Point {
            bytes: BytesN::from_array(env, &[0u8; 64]),
        };
        let g2 = G2Point {
            bytes: BytesN::from_array(env, &[0u8; 128]),
        };
        let mut ic = Vec::new(env);
        for _ in 0..ic_count {
            ic.push_back(g1.clone());
        }
        VerificationKey {
            alpha: g1,
            beta: g2.clone(),
            gamma: g2.clone(),
            delta: g2,
            ic,
        }
    }

    #[test]
    fn test_movement_circuit_creation() {
        let env = Env::default();
        let vk = make_vk(&env, 6); // 5 public inputs + 1
        let circuit = MovementCircuit::new(vk, 10);
        assert_eq!(circuit.max_distance, 10);
    }

    #[test]
    fn test_movement_circuit_wrong_ic_length() {
        let env = Env::default();
        let vk = make_vk(&env, 1); // wrong: needs 6 for 5 inputs
        let circuit = MovementCircuit::new(vk, 10);

        let g1 = G1Point {
            bytes: BytesN::from_array(&env, &[0u8; 64]),
        };
        let g2 = G2Point {
            bytes: BytesN::from_array(&env, &[0u8; 128]),
        };
        let proof = Groth16Proof {
            a: g1.clone(),
            b: g2,
            c: g1,
        };

        let result = circuit.verify_move(&env, &proof, 0, 0, 3, 4);
        assert_eq!(result, Err(ZKError::InvalidVerificationKey));
    }

    #[test]
    fn test_combat_circuit_creation() {
        let env = Env::default();
        let vk = make_vk(&env, 4);
        let circuit = CombatCircuit::new(vk);
        assert_eq!(circuit.vk.ic.len(), 4);
    }

    #[test]
    fn test_inventory_circuit_creation() {
        let env = Env::default();
        let vk = make_vk(&env, 3);
        let circuit = InventoryCircuit::new(vk);
        assert_eq!(circuit.vk.ic.len(), 3);
    }

    #[test]
    fn test_turn_sequence_circuit_creation() {
        let env = Env::default();
        let vk = make_vk(&env, 4);
        let circuit = TurnSequenceCircuit::new(vk);
        assert_eq!(circuit.vk.ic.len(), 4);
    }

    #[test]
    fn test_scalar_encoding_u32() {
        let env = Env::default();
        let scalar = traits::u32_to_scalar(&env, 42);
        assert_eq!(scalar.bytes.len(), 32);
    }

    #[test]
    fn test_scalar_encoding_i32() {
        let env = Env::default();
        let scalar = traits::i32_to_scalar(&env, -1);
        assert_eq!(scalar.bytes.len(), 32);
    }

    #[test]
    fn test_game_circuit_trait_on_movement() {
        let env = Env::default();
        let vk = make_vk(&env, 1); // wrong IC for 5 inputs
        let circuit = MovementCircuit::new(vk, 10);

        // Verify GameCircuit trait methods work
        assert_eq!(circuit.verification_key().ic.len(), 1);

        let g1 = G1Point {
            bytes: BytesN::from_array(&env, &[0u8; 64]),
        };
        let g2 = G2Point {
            bytes: BytesN::from_array(&env, &[0u8; 128]),
        };
        let proof = Groth16Proof {
            a: g1.clone(),
            b: g2,
            c: g1,
        };

        // verify_with_inputs should fail with wrong IC length
        let inputs = alloc::vec![traits::u32_to_scalar(&env, 1)];
        let result = circuit.verify_with_inputs(&env, &proof, &inputs);
        assert_eq!(result, Err(ZKError::InvalidVerificationKey));
    }

    #[test]
    fn test_custom_circuit_creation() {
        let env = Env::default();
        let vk = make_vk(&env, 3); // 2 public inputs + 1
        let inputs = alloc::vec![
            traits::u32_to_scalar(&env, 10),
            traits::u32_to_scalar(&env, 20),
        ];
        let circuit = CustomCircuit::new(vk, inputs);
        assert_eq!(circuit.public_inputs().len(), 2);
    }

    #[test]
    fn test_custom_circuit_builder() {
        let env = Env::default();
        let vk = make_vk(&env, 4); // 3 public inputs + 1
        let root = BytesN::from_array(&env, &[0xABu8; 32]);

        let circuit = CustomCircuit::builder(vk)
            .add_u32(&env, 42)
            .add_i32(&env, -5)
            .add_bytes32(&root)
            .build();

        assert_eq!(circuit.public_inputs().len(), 3);
        assert_eq!(circuit.verification_key().ic.len(), 4);
    }

    #[test]
    fn test_custom_circuit_verify_wrong_ic() {
        let env = Env::default();
        let vk = make_vk(&env, 1); // wrong IC
        let circuit = CustomCircuit::builder(vk)
            .add_u32(&env, 42)
            .add_u32(&env, 99)
            .build();

        let g1 = G1Point {
            bytes: BytesN::from_array(&env, &[0u8; 64]),
        };
        let g2 = G2Point {
            bytes: BytesN::from_array(&env, &[0u8; 128]),
        };
        let proof = Groth16Proof {
            a: g1.clone(),
            b: g2,
            c: g1,
        };

        let result = circuit.verify(&env, &proof);
        assert_eq!(result, Err(ZKError::InvalidVerificationKey));
    }
}