Skip to content

Commit 73d5f8a

Browse files
committed
napi: wrapper for lookups
1 parent d8dd45d commit 73d5f8a

File tree

2 files changed

+298
-1
lines changed

2 files changed

+298
-1
lines changed

plonk-napi/src/wrappers/lookups.rs

Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
use kimchi::circuits::lookup::{
2+
lookups::{
3+
LookupFeatures as KimchiLookupFeatures, LookupInfo as KimchiLookupInfo,
4+
LookupPatterns as KimchiLookupPatterns,
5+
},
6+
runtime_tables::{
7+
RuntimeTable as KimchiRuntimeTable, RuntimeTableCfg as KimchiRuntimeTableCfg,
8+
},
9+
tables::LookupTable as KimchiLookupTable,
10+
};
11+
use mina_curves::pasta::{Fp, Fq};
12+
use napi::bindgen_prelude::*;
13+
use napi_derive::napi;
14+
use paste::paste;
15+
use wasm_types::{FlatVector, FlatVectorElem};
16+
17+
use crate::{
18+
wasm_vector::{fp::WasmVecVecFp, fq::WasmVecVecFq},
19+
wrappers::field::{WasmPastaFp, WasmPastaFq},
20+
};
21+
22+
// -----------------
23+
// Lookup pattern and info wrappers
24+
// -----------------
25+
26+
#[napi(object)]
27+
#[derive(Clone, Copy, Debug, Default)]
28+
pub struct NapiLookupPatterns {
29+
pub xor: bool,
30+
pub lookup: bool,
31+
pub range_check: bool,
32+
pub foreign_field_mul: bool,
33+
}
34+
35+
impl From<KimchiLookupPatterns> for NapiLookupPatterns {
36+
fn from(value: KimchiLookupPatterns) -> Self {
37+
Self {
38+
xor: value.xor,
39+
lookup: value.lookup,
40+
range_check: value.range_check,
41+
foreign_field_mul: value.foreign_field_mul,
42+
}
43+
}
44+
}
45+
46+
impl From<NapiLookupPatterns> for KimchiLookupPatterns {
47+
fn from(value: NapiLookupPatterns) -> Self {
48+
KimchiLookupPatterns {
49+
xor: value.xor,
50+
lookup: value.lookup,
51+
range_check: value.range_check,
52+
foreign_field_mul: value.foreign_field_mul,
53+
}
54+
}
55+
}
56+
57+
#[napi(object)]
58+
#[derive(Clone, Debug, Default)]
59+
pub struct NapiLookupFeatures {
60+
pub patterns: NapiLookupPatterns,
61+
pub joint_lookup_used: bool,
62+
pub uses_runtime_tables: bool,
63+
}
64+
65+
impl From<KimchiLookupFeatures> for NapiLookupFeatures {
66+
fn from(value: KimchiLookupFeatures) -> Self {
67+
Self {
68+
patterns: value.patterns.into(),
69+
joint_lookup_used: value.joint_lookup_used,
70+
uses_runtime_tables: value.uses_runtime_tables,
71+
}
72+
}
73+
}
74+
75+
impl From<NapiLookupFeatures> for KimchiLookupFeatures {
76+
fn from(value: NapiLookupFeatures) -> Self {
77+
KimchiLookupFeatures {
78+
patterns: value.patterns.into(),
79+
joint_lookup_used: value.joint_lookup_used,
80+
uses_runtime_tables: value.uses_runtime_tables,
81+
}
82+
}
83+
}
84+
85+
#[napi(object)]
86+
#[derive(Clone, Debug, Default)]
87+
pub struct NapiLookupInfo {
88+
pub max_per_row: u32,
89+
pub max_joint_size: u32,
90+
pub features: NapiLookupFeatures,
91+
}
92+
93+
impl From<KimchiLookupInfo> for NapiLookupInfo {
94+
fn from(value: KimchiLookupInfo) -> Self {
95+
Self {
96+
max_per_row: value.max_per_row as u32,
97+
max_joint_size: value.max_joint_size as u32,
98+
features: value.features.into(),
99+
}
100+
}
101+
}
102+
103+
impl From<NapiLookupInfo> for KimchiLookupInfo {
104+
fn from(value: NapiLookupInfo) -> Self {
105+
KimchiLookupInfo {
106+
max_per_row: value.max_per_row as usize,
107+
max_joint_size: value.max_joint_size,
108+
features: value.features.into(),
109+
}
110+
}
111+
}
112+
113+
// -----------------
114+
// Lookup tables & runtime tables
115+
// -----------------
116+
117+
macro_rules! impl_lookup_wrappers {
118+
($name:ident, $field:ty, $wasm_field:ty, $vec_vec:ty) => {
119+
paste! {
120+
#[napi]
121+
#[derive(Clone)]
122+
pub struct [<NapiLookupTable $name:camel>] {
123+
id: i32,
124+
data: $vec_vec,
125+
}
126+
127+
#[napi]
128+
impl [<NapiLookupTable $name:camel>] {
129+
#[napi(constructor)]
130+
pub fn new(id: i32, data: External<$vec_vec>) -> Self {
131+
Self {
132+
id,
133+
data: data.as_ref().clone(),
134+
}
135+
}
136+
137+
#[napi(getter)]
138+
pub fn id(&self) -> i32 {
139+
self.id
140+
}
141+
142+
#[napi(setter)]
143+
pub fn set_id(&mut self, id: i32) {
144+
self.id = id;
145+
}
146+
147+
#[napi(getter)]
148+
pub fn data(&self) -> External<$vec_vec> {
149+
External::new(self.data.clone())
150+
}
151+
152+
#[napi(setter)]
153+
pub fn set_data(&mut self, data: External<$vec_vec>) {
154+
self.data = data.as_ref().clone();
155+
}
156+
}
157+
158+
impl From<KimchiLookupTable<$field>> for [<NapiLookupTable $name:camel>] {
159+
fn from(value: KimchiLookupTable<$field>) -> Self {
160+
Self {
161+
id: value.id,
162+
data: value.data.into(),
163+
}
164+
}
165+
}
166+
167+
impl From<[<NapiLookupTable $name:camel>]> for KimchiLookupTable<$field> {
168+
fn from(value: [<NapiLookupTable $name:camel>]) -> Self {
169+
Self {
170+
id: value.id,
171+
data: value.data.into(),
172+
}
173+
}
174+
}
175+
176+
#[napi]
177+
#[derive(Clone)]
178+
pub struct [<NapiRuntimeTableCfg $name:camel>] {
179+
id: i32,
180+
first_column: Vec<$field>,
181+
}
182+
183+
#[napi]
184+
impl [<NapiRuntimeTableCfg $name:camel>] {
185+
#[napi(constructor)]
186+
pub fn new(id: i32, first_column: Uint8Array) -> Result<Self> {
187+
let bytes = first_column.as_ref().to_vec();
188+
let elements: Vec<$field> = FlatVector::<$wasm_field>::from_bytes(bytes)
189+
.into_iter()
190+
.map(Into::into)
191+
.collect();
192+
Ok(Self { id, first_column: elements })
193+
}
194+
195+
#[napi(getter)]
196+
pub fn id(&self) -> i32 {
197+
self.id
198+
}
199+
200+
#[napi(setter)]
201+
pub fn set_id(&mut self, id: i32) {
202+
self.id = id;
203+
}
204+
205+
#[napi(getter)]
206+
pub fn first_column(&self) -> Result<Uint8Array> {
207+
let mut bytes = Vec::with_capacity(self.first_column.len() * <$wasm_field>::FLATTENED_SIZE);
208+
for value in &self.first_column {
209+
let element = <$wasm_field>::from(*value);
210+
bytes.extend(element.flatten());
211+
}
212+
Ok(Uint8Array::from(bytes))
213+
}
214+
}
215+
216+
impl From<KimchiRuntimeTableCfg<$field>> for [<NapiRuntimeTableCfg $name:camel>] {
217+
fn from(value: KimchiRuntimeTableCfg<$field>) -> Self {
218+
Self {
219+
id: value.id,
220+
first_column: value.first_column,
221+
}
222+
}
223+
}
224+
225+
impl From<[<NapiRuntimeTableCfg $name:camel>]> for KimchiRuntimeTableCfg<$field> {
226+
fn from(value: [<NapiRuntimeTableCfg $name:camel>]) -> Self {
227+
Self {
228+
id: value.id,
229+
first_column: value.first_column,
230+
}
231+
}
232+
}
233+
234+
#[napi]
235+
#[derive(Clone)]
236+
pub struct [<NapiRuntimeTable $name:camel>] {
237+
id: i32,
238+
data: Vec<$field>,
239+
}
240+
241+
#[napi]
242+
impl [<NapiRuntimeTable $name:camel>] {
243+
#[napi(constructor)]
244+
pub fn new(id: i32, data: Uint8Array) -> Result<Self> {
245+
let bytes = data.as_ref().to_vec();
246+
let elements: Vec<$field> = FlatVector::<$wasm_field>::from_bytes(bytes)
247+
.into_iter()
248+
.map(Into::into)
249+
.collect();
250+
Ok(Self { id, data: elements })
251+
}
252+
253+
#[napi(getter)]
254+
pub fn id(&self) -> i32 {
255+
self.id
256+
}
257+
258+
#[napi(setter)]
259+
pub fn set_id(&mut self, id: i32) {
260+
self.id = id;
261+
}
262+
263+
#[napi(getter)]
264+
pub fn data(&self) -> Result<Uint8Array> {
265+
let mut bytes = Vec::with_capacity(self.data.len() * <$wasm_field>::FLATTENED_SIZE);
266+
for value in &self.data {
267+
let element = <$wasm_field>::from(*value);
268+
bytes.extend(element.flatten());
269+
}
270+
Ok(Uint8Array::from(bytes))
271+
}
272+
}
273+
274+
impl From<KimchiRuntimeTable<$field>> for [<NapiRuntimeTable $name:camel>] {
275+
fn from(value: KimchiRuntimeTable<$field>) -> Self {
276+
Self {
277+
id: value.id,
278+
data: value.data,
279+
}
280+
}
281+
}
282+
283+
impl From<[<NapiRuntimeTable $name:camel>]> for KimchiRuntimeTable<$field> {
284+
fn from(value: [<NapiRuntimeTable $name:camel>]) -> Self {
285+
Self {
286+
id: value.id,
287+
data: value.data,
288+
}
289+
}
290+
}
291+
}
292+
};
293+
}
294+
295+
impl_lookup_wrappers!(Fp, Fp, WasmPastaFp, WasmVecVecFp);
296+
impl_lookup_wrappers!(Fq, Fq, WasmPastaFq, WasmVecVecFq);

plonk-napi/src/wrappers/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
pub(crate) mod feature_flags;
12
pub(crate) mod field;
23
pub(crate) mod group;
4+
pub(crate) mod lookups;
35
pub(crate) mod wires;
4-
pub(crate) mod feature_flags;

0 commit comments

Comments
 (0)