|
| 1 | +use crate::{ |
| 2 | + wrappers::group::{WasmGPallas, WasmGVesta}, |
| 3 | + wasm_vector::WasmVector, |
| 4 | +}; |
| 5 | +use napi_derive::napi; |
| 6 | +use paste::paste; |
| 7 | +use poly_commitment::commitment::PolyComm; |
| 8 | + |
| 9 | +macro_rules! impl_poly_comm { |
| 10 | + ( |
| 11 | + $wasm_g:ty, |
| 12 | + $g:ty, |
| 13 | + $field_name:ident |
| 14 | + ) => { |
| 15 | + paste! { |
| 16 | + #[napi] |
| 17 | + #[derive(Clone)] |
| 18 | + pub struct [<Wasm $field_name:camel PolyComm>] { |
| 19 | + #[napi(skip)] |
| 20 | + pub unshifted: WasmVector<$wasm_g>, |
| 21 | + pub shifted: Option<$wasm_g>, |
| 22 | + } |
| 23 | + |
| 24 | + #[napi] |
| 25 | + impl [<Wasm $field_name:camel PolyComm>] { |
| 26 | + #[napi(constructor)] |
| 27 | + pub fn new(unshifted: WasmVector<$wasm_g>, shifted: Option<$wasm_g>) -> Self { |
| 28 | + assert!( |
| 29 | + shifted.is_none(), |
| 30 | + "mina#14628: Shifted commitments are deprecated and must not be used", |
| 31 | + ); |
| 32 | + Self { unshifted, shifted } |
| 33 | + } |
| 34 | + |
| 35 | + #[napi(getter)] |
| 36 | + pub fn unshifted(&self) -> WasmVector<$wasm_g> { |
| 37 | + self.unshifted.clone() |
| 38 | + } |
| 39 | + |
| 40 | + #[napi(setter)] |
| 41 | + pub fn set_unshifted(&mut self, value: WasmVector<$wasm_g>) { |
| 42 | + self.unshifted = value; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + impl From<PolyComm<$g>> for [<Wasm $field_name:camel PolyComm>] { |
| 47 | + fn from(value: PolyComm<$g>) -> Self { |
| 48 | + let PolyComm { chunks } = value; |
| 49 | + let unshifted: Vec<$wasm_g> = chunks.into_iter().map(Into::into).collect(); |
| 50 | + Self { |
| 51 | + unshifted: unshifted.into(), |
| 52 | + shifted: None, |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + impl From<&PolyComm<$g>> for [<Wasm $field_name:camel PolyComm>] { |
| 58 | + fn from(value: &PolyComm<$g>) -> Self { |
| 59 | + let unshifted: Vec<$wasm_g> = value.chunks.iter().map(|chunk| (*chunk).into()).collect(); |
| 60 | + Self { |
| 61 | + unshifted: unshifted.into(), |
| 62 | + shifted: None, |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + impl From<[<Wasm $field_name:camel PolyComm>]> for PolyComm<$g> { |
| 68 | + fn from(value: [<Wasm $field_name:camel PolyComm>]) -> Self { |
| 69 | + let [<Wasm $field_name:camel PolyComm>] { unshifted, shifted } = value; |
| 70 | + assert!( |
| 71 | + shifted.is_none(), |
| 72 | + "mina#14628: Shifted commitments are deprecated and must not be used", |
| 73 | + ); |
| 74 | + PolyComm { |
| 75 | + chunks: Vec::<$wasm_g>::from(unshifted) |
| 76 | + .into_iter() |
| 77 | + .map(Into::into) |
| 78 | + .collect(), |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + impl From<&[<Wasm $field_name:camel PolyComm>]> for PolyComm<$g> { |
| 84 | + fn from(value: &[<Wasm $field_name:camel PolyComm>]) -> Self { |
| 85 | + assert!( |
| 86 | + value.shifted.is_none(), |
| 87 | + "mina#14628: Shifted commitments are deprecated and must not be used", |
| 88 | + ); |
| 89 | + PolyComm { |
| 90 | + chunks: value |
| 91 | + .unshifted |
| 92 | + .iter() |
| 93 | + .cloned() |
| 94 | + .map(Into::into) |
| 95 | + .collect(), |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + }; |
| 101 | +} |
| 102 | + |
| 103 | +pub mod pallas { |
| 104 | + use super::*; |
| 105 | + use mina_curves::pasta::Pallas as GAffine; |
| 106 | + |
| 107 | + impl_poly_comm!(WasmGPallas, GAffine, Fq); |
| 108 | +} |
| 109 | + |
| 110 | +pub mod vesta { |
| 111 | + use super::*; |
| 112 | + use mina_curves::pasta::Vesta as GAffine; |
| 113 | + |
| 114 | + impl_poly_comm!(WasmGVesta, GAffine, Fp); |
| 115 | +} |
0 commit comments