prover/
types.rs

1//! Common types used across the prover module
2
3use alloc::{collections::BTreeMap, string::String, vec::Vec};
4
5use serde::{Deserialize, Serialize};
6use wasm_bindgen::prelude::*;
7
8/// Field element size in bytes (BN254 scalar field)
9pub const FIELD_SIZE: usize = 32;
10
11/// Groth16 proof structure for serialization to JS
12#[derive(Clone, Debug, Serialize, Deserialize)]
13#[wasm_bindgen]
14pub struct Groth16Proof {
15    /// Proof point A (G1)
16    #[wasm_bindgen(skip)]
17    pub a: Vec<u8>,
18    /// Proof point B (G2)
19    #[wasm_bindgen(skip)]
20    pub b: Vec<u8>,
21    /// Proof point C (G1)
22    #[wasm_bindgen(skip)]
23    pub c: Vec<u8>,
24}
25
26#[wasm_bindgen]
27impl Groth16Proof {
28    /// Get proof point A as bytes
29    #[wasm_bindgen(getter)]
30    pub fn a(&self) -> Vec<u8> {
31        self.a.clone()
32    }
33
34    /// Get proof point B as bytes
35    #[wasm_bindgen(getter)]
36    pub fn b(&self) -> Vec<u8> {
37        self.b.clone()
38    }
39
40    /// Get proof point C as bytes
41    #[wasm_bindgen(getter)]
42    pub fn c(&self) -> Vec<u8> {
43        self.c.clone()
44    }
45
46    /// Get the full proof as concatenated bytes [A || B || C]
47    #[wasm_bindgen]
48    pub fn to_bytes(&self) -> Vec<u8> {
49        let capacity = self
50            .a
51            .len()
52            .saturating_add(self.b.len())
53            .saturating_add(self.c.len());
54        let mut bytes = Vec::with_capacity(capacity);
55        bytes.extend_from_slice(&self.a);
56        bytes.extend_from_slice(&self.b);
57        bytes.extend_from_slice(&self.c);
58        bytes
59    }
60}
61
62/// Circuit input builder for preparing witness inputs
63#[derive(Clone, Debug, Default, Serialize, Deserialize)]
64pub struct CircuitInputs {
65    /// Input signals as name -> value(s) mapping
66    /// Values are stored as hex strings for BigInt compatibility
67    #[serde(flatten)]
68    pub signals: BTreeMap<String, InputValue>,
69}
70
71/// Input value - either a single field element or an array
72#[derive(Clone, Debug, Serialize, Deserialize)]
73#[serde(untagged)]
74pub enum InputValue {
75    /// Single field element as hex string
76    Single(String),
77    /// Array of field elements as hex strings
78    Array(Vec<String>),
79}
80
81impl CircuitInputs {
82    /// Create new empty inputs
83    pub fn new() -> Self {
84        Self {
85            signals: BTreeMap::new(),
86        }
87    }
88
89    /// Set a single value input
90    pub fn set_single(&mut self, name: &str, value: &str) {
91        self.signals
92            .insert(String::from(name), InputValue::Single(String::from(value)));
93    }
94
95    /// Set an array value input
96    pub fn set_array(&mut self, name: &str, values: Vec<String>) {
97        self.signals
98            .insert(String::from(name), InputValue::Array(values));
99    }
100}