Skip to main content

kimchi_napi/
poseidon.rs

1use crate::wrappers::field::{NapiPastaFp, NapiPastaFq};
2use mina_curves::pasta::{Fp, Fq};
3use mina_poseidon::{
4    constants::PlonkSpongeConstantsKimchi, pasta::FULL_ROUNDS, permutation::poseidon_block_cipher,
5};
6use napi::bindgen_prelude::*;
7use napi_derive::napi;
8use wasm_types::{FlatVector, FlatVectorElem};
9
10// fp
11
12#[napi(js_name = "caml_pasta_fp_poseidon_block_cipher")]
13pub fn caml_pasta_fp_poseidon_block_cipher(state: Uint8Array) -> Result<Uint8Array> {
14    let mut state_vec: Vec<Fp> = FlatVector::<NapiPastaFp>::from_bytes(state.to_vec())
15        .into_iter()
16        .map(Into::into)
17        .collect();
18
19    poseidon_block_cipher::<Fp, PlonkSpongeConstantsKimchi, FULL_ROUNDS>(
20        mina_poseidon::pasta::fp_kimchi::static_params(),
21        &mut state_vec,
22    );
23
24    let res: Vec<u8> = state_vec
25        .into_iter()
26        .map(NapiPastaFp)
27        .flat_map(FlatVectorElem::flatten)
28        .collect();
29
30    Ok(Uint8Array::from(res))
31}
32
33// fq
34
35#[napi(js_name = "caml_pasta_fq_poseidon_block_cipher")]
36pub fn caml_pasta_fq_poseidon_block_cipher(state: Uint8Array) -> Result<Uint8Array> {
37    let mut state_vec: Vec<Fq> = FlatVector::<NapiPastaFq>::from_bytes(state.to_vec())
38        .into_iter()
39        .map(Into::into)
40        .collect();
41
42    poseidon_block_cipher::<Fq, PlonkSpongeConstantsKimchi, FULL_ROUNDS>(
43        mina_poseidon::pasta::fq_kimchi::static_params(),
44        &mut state_vec,
45    );
46
47    let res: Vec<u8> = state_vec
48        .into_iter()
49        .map(NapiPastaFq)
50        .flat_map(FlatVectorElem::flatten)
51        .collect();
52
53    Ok(Uint8Array::from(res))
54}