Skip to content

Commit d4e09e2

Browse files
committed
napi: inline functions for GateVector struct
1 parent 2831552 commit d4e09e2

File tree

1 file changed

+29
-62
lines changed

1 file changed

+29
-62
lines changed

plonk-napi/src/gate_vector.rs

Lines changed: 29 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -140,86 +140,37 @@ macro_rules! impl_gate_support {
140140

141141
#[napi]
142142
#[derive(Clone, Default, Debug)]
143-
pub struct [<Napi $module:camel GateVector>] {
144-
#[napi(skip)]
145-
pub inner: Vec<CircuitGate<$field>>,
146-
}
147-
148-
#[napi]
149-
impl [<Napi $module:camel GateVector>] {
150-
#[napi(constructor)]
151-
pub fn new() -> Self {
152-
Self { inner: Vec::new() }
153-
}
154-
155-
#[napi]
156-
pub fn add(&mut self, gate: [<Napi $module:camel Gate>]) -> Result<()> {
157-
self.inner.push(gate.into_inner()?);
158-
Ok(())
159-
}
160-
161-
#[napi]
162-
pub fn get(&self, index: i32) -> [<Napi $module:camel Gate>] {
163-
[<Napi $module:camel Gate>]::from_inner(&self.inner[index as usize])
164-
}
165-
166-
#[napi]
167-
pub fn len(&self) -> i32 {
168-
self.inner.len() as i32
169-
}
143+
pub struct [<Napi $module:camel GateVector>](
144+
#[napi(skip)] pub Vec<CircuitGate<$field>>);
170145

171-
#[napi]
172-
pub fn wrap(&mut self, target: NapiWire, head: NapiWire) {
173-
let row = target.row as usize;
174-
let col = target.col as usize;
175-
self.inner[row].wires[col] = KimchiWire::from(head);
176-
}
177-
178-
#[napi]
179-
pub fn digest(&self, public_input_size: i32) -> Vec<u8> {
180-
Circuit::new(public_input_size as usize, &self.inner)
181-
.digest()
182-
.to_vec()
183-
}
184-
185-
#[napi]
186-
pub fn serialize(&self, public_input_size: i32) -> Result<String> {
187-
let circuit = Circuit::new(public_input_size as usize, &self.inner);
188-
serde_json::to_string(&circuit).map_err(|err| {
189-
Error::new(
190-
Status::GenericFailure,
191-
format!("failed to serialize circuit: {}", err),
192-
)
193-
})
194-
}
195-
}
196146

197147
#[napi]
198148
pub fn [<caml_pasta_ $module:snake _plonk_gate_vector_create>]() -> [<Napi $module:camel GateVector>] {
199-
[<Napi $module:camel GateVector>]::new()
149+
[<Napi $module:camel GateVector>(Vec::new())]
200150
}
201151

202152
#[napi]
203153
pub fn [<caml_pasta_ $module:snake _plonk_gate_vector_add>](
204154
vector: &mut [<Napi $module:camel GateVector>],
205155
gate: [<Napi $module:camel Gate>],
206156
) -> Result<()> {
207-
vector.add(gate)
157+
vector.0.push(gate.into_inner()?);
158+
Ok(())
208159
}
209160

210161
#[napi]
211162
pub fn [<caml_pasta_ $module:snake _plonk_gate_vector_get>](
212163
vector: &[<Napi $module:camel GateVector>],
213164
index: i32,
214165
) -> [<Napi $module:camel Gate>] {
215-
vector.get(index)
166+
[<Napi $module:camel Gate>]::from_inner(&self.0[index as usize])
216167
}
217168

218169
#[napi]
219170
pub fn [<caml_pasta_ $module:snake _plonk_gate_vector_len>](
220171
vector: &[<Napi $module:camel GateVector>],
221172
) -> i32 {
222-
vector.len()
173+
vector.0.len() as i32
223174
}
224175

225176
#[napi]
@@ -228,27 +179,43 @@ macro_rules! impl_gate_support {
228179
target: NapiWire,
229180
head: NapiWire,
230181
) {
231-
vector.wrap(target, head);
232-
}
182+
let row = target.row as usize;
183+
let col = target.col as usize;
184+
self.0[row].wires[col] = KimchiWire::from(head);
185+
}
233186

234187
#[napi]
235188
pub fn [<caml_pasta_ $module:snake _plonk_gate_vector_digest>](
236189
public_input_size: i32,
237190
vector: &[<Napi $module:camel GateVector>],
238191
) -> Vec<u8> {
239-
vector.digest(public_input_size)
192+
Circuit::new(public_input_size as usize, &self.0)
193+
.digest()
194+
.to_vec()
240195
}
241196

242197
#[napi]
243198
pub fn [<caml_pasta_ $module:snake _plonk_circuit_serialize>](
244199
public_input_size: i32,
245200
vector: &[<Napi $module:camel GateVector>],
246201
) -> Result<String> {
247-
vector.serialize(public_input_size)
202+
let circuit = Circuit::new(public_input_size as usize, &self.inner);
203+
serde_json::to_string(&circuit).map_err(|err| {
204+
Error::new(
205+
Status::GenericFailure,
206+
format!("failed to serialize circuit: {}", err),
207+
)
208+
})
248209
}
249210
}
250211
};
251212
}
252213

253-
impl_gate_support!(fp, Fp, WasmPastaFp);
254-
impl_gate_support!(fq, Fq, WasmPastaFq);
214+
pub mod fp {
215+
use super::*;
216+
impl_gate_support!(fp, Fp, WasmPastaFp);
217+
}
218+
pub mod fq {
219+
use super::*;
220+
impl_gate_support!(fq, Fq, WasmPastaFq);
221+
}

0 commit comments

Comments
 (0)