Skip to content

Commit 7d127e0

Browse files
committed
napi: improve naming in gate_vector
1 parent 217dba7 commit 7d127e0

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

plonk-napi/src/gate_vector.rs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ impl From<&GateWires> for NapiGateWires {
5555
}
5656

5757
fn gate_type_from_i32(value: i32) -> Result<GateType> {
58+
// Ocaml/JS int are signed, so we use i32 here
5859
if value < 0 {
5960
return Err(Error::new(
6061
Status::InvalidArg,
@@ -92,24 +93,25 @@ fn gate_type_from_i32(value: i32) -> Result<GateType> {
9293
})
9394
}
9495

96+
// For convenience to not expose the GateType enum to JS
9597
fn gate_type_to_i32(value: GateType) -> i32 {
9698
value as i32
9799
}
98100

99101
macro_rules! impl_gate_support {
100-
($module:ident, $field:ty, $wasm_field:ty) => {
102+
($field_name:ident, $F:ty, $WasmF:ty) => {
101103
paste! {
102104
#[napi(object)]
103105
#[derive(Clone, Debug, Default)]
104-
pub struct [<Napi $module:camel Gate>] {
105-
pub typ: i32,
106+
pub struct [<Napi $field_name:camel Gate>] {
107+
pub typ: i32, // for convenience, we use i32 instead of GateType
106108
pub wires: NapiGateWires,
107-
pub coeffs: Vec<u8>,
109+
pub coeffs: Vec<u8>, // for now, serializing fields as flat bytes, but subject to changes
108110
}
109111

110-
impl [<Napi $module:camel Gate>] {
111-
fn into_inner(self) -> Result<CircuitGate<$field>> {
112-
let coeffs = WasmFlatVector::<$wasm_field>::from_bytes(self.coeffs)
112+
impl [<Napi $field_name:camel Gate>] {
113+
fn into_inner(self) -> Result<CircuitGate<$F>> {
114+
let coeffs = WasmFlatVector::<$WasmF>::from_bytes(self.coeffs)
113115
.into_iter()
114116
.map(Into::into)
115117
.collect();
@@ -121,12 +123,12 @@ macro_rules! impl_gate_support {
121123
})
122124
}
123125

124-
fn from_inner(value: &CircuitGate<$field>) -> Self {
126+
fn from_inner(value: &CircuitGate<$F>) -> Self {
125127
let coeffs = value
126128
.coeffs
127129
.iter()
128130
.cloned()
129-
.map($wasm_field::from)
131+
.map($WasmF::from)
130132
.flat_map(|elem| elem.flatten())
131133
.collect();
132134

@@ -140,54 +142,52 @@ macro_rules! impl_gate_support {
140142

141143
#[napi]
142144
#[derive(Clone, Default, Debug)]
143-
pub struct [<Napi $module:camel GateVector>](
144-
#[napi(skip)] pub Vec<CircuitGate<$field>>,
145+
pub struct [<Napi $field_name:camel GateVector>](
146+
#[napi(skip)] pub Vec<CircuitGate<$F>>,
145147
);
146148

147149
#[napi]
148-
pub fn [<caml_pasta_ $module:snake _plonk_gate_vector_create>]() -> [<Napi $module:camel GateVector>] {
149-
[<Napi $module:camel GateVector>](Vec::new())
150+
pub fn [<caml_pasta_ $field_name:snake _plonk_gate_vector_create>]() -> [<Napi $field_name:camel GateVector>] {
151+
[<Napi $field_name:camel GateVector>](Vec::new())
150152
}
151153

152154
#[napi]
153-
pub fn [<caml_pasta_ $module:snake _plonk_gate_vector_add>](
154-
vector: &mut [<Napi $module:camel GateVector>],
155-
gate: [<Napi $module:camel Gate>],
155+
pub fn [<caml_pasta_ $field_name:snake _plonk_gate_vector_add>](
156+
vector: &mut [<Napi $field_name:camel GateVector>],
157+
gate: [<Napi $field_name:camel Gate>],
156158
) -> Result<()> {
157159
vector.0.push(gate.into_inner()?);
158160
Ok(())
159161
}
160162

161163
#[napi]
162-
pub fn [<caml_pasta_ $module:snake _plonk_gate_vector_get>](
163-
vector: &[<Napi $module:camel GateVector>],
164+
pub fn [<caml_pasta_ $field_name:snake _plonk_gate_vector_get>](
165+
vector: &[<Napi $field_name:camel GateVector>],
164166
index: i32,
165-
) -> [<Napi $module:camel Gate>] {
166-
[<Napi $module:camel Gate>]::from_inner(&vector.0[index as usize])
167+
) -> [<Napi $field_name:camel Gate>] {
168+
[<Napi $field_name:camel Gate>]::from_inner(&vector.0[index as usize])
167169
}
168170

169171
#[napi]
170-
pub fn [<caml_pasta_ $module:snake _plonk_gate_vector_len>](
171-
vector: &[<Napi $module:camel GateVector>],
172+
pub fn [<caml_pasta_ $field_name:snake _plonk_gate_vector_len>](
173+
vector: &[<Napi $field_name:camel GateVector>],
172174
) -> i32 {
173175
vector.0.len() as i32
174176
}
175177

176178
#[napi]
177-
pub fn [<caml_pasta_ $module:snake _plonk_gate_vector_wrap>](
178-
vector: &mut [<Napi $module:camel GateVector>],
179+
pub fn [<caml_pasta_ $field_name:snake _plonk_gate_vector_wrap>](
180+
vector: &mut [<Napi $field_name:camel GateVector>],
179181
target: NapiWire,
180182
head: NapiWire,
181183
) {
182-
let row = target.row as usize;
183-
let col = target.col as usize;
184-
vector.0[row].wires[col] = KimchiWire::from(head);
184+
vector.0[target.row as usize].wires[target.col as usize] = KimchiWire::from(head);
185185
}
186186

187187
#[napi]
188-
pub fn [<caml_pasta_ $module:snake _plonk_gate_vector_digest>](
188+
pub fn [<caml_pasta_ $field_name:snake _plonk_gate_vector_digest>](
189189
public_input_size: i32,
190-
vector: &[<Napi $module:camel GateVector>],
190+
vector: &[<Napi $field_name:camel GateVector>],
191191
) -> Uint8Array {
192192
let bytes = Circuit::new(public_input_size as usize, &vector.0)
193193
.digest()
@@ -196,15 +196,15 @@ macro_rules! impl_gate_support {
196196
}
197197

198198
#[napi]
199-
pub fn [<caml_pasta_ $module:snake _plonk_circuit_serialize>](
199+
pub fn [<caml_pasta_ $field_name:snake _plonk_circuit_serialize>](
200200
public_input_size: i32,
201-
vector: &[<Napi $module:camel GateVector>],
201+
vector: &[<Napi $field_name:camel GateVector>],
202202
) -> Result<String> {
203203
let circuit = Circuit::new(public_input_size as usize, &vector.0);
204204
serde_json::to_string(&circuit).map_err(|err| {
205205
Error::new(
206206
Status::GenericFailure,
207-
format!("failed to serialize circuit: {}", err),
207+
format!("couldn't serialize constraints: {}", err),
208208
)
209209
})
210210
}

0 commit comments

Comments
 (0)