plonk_wasm/
poseidon.rs

1//! This file defines wrapper for the Poseidon hash function that are used in
2//! the Mina codebase.
3//!
4//! It is a wrapper around the Poseidon implementation in the `mina_poseidon` crate.
5//! It is required as the native OCaml implementation of Mina does use the Rust
6//! implementation defined in the crate `mina_poseidon` instead of defining its
7//! own natively in OCaml for performance reasons. The bindings in OCaml can be
8//! found in `src/lib/crypto/kimchi_bindings/pasta_fp_poseidon` and
9//! `src/lib/crypto/kimchi_bindings/pasta_fq_poseidon` in the Mina codebase.
10
11use arkworks::{WasmPastaFp, WasmPastaFq};
12use mina_curves::pasta::{Fp, Fq};
13use mina_poseidon::{constants::PlonkSpongeConstantsKimchi, permutation::poseidon_block_cipher};
14use wasm_bindgen::prelude::*;
15use wasm_types::FlatVector as WasmFlatVector;
16
17// fp
18
19#[wasm_bindgen]
20pub fn caml_pasta_fp_poseidon_block_cipher(
21    state: WasmFlatVector<WasmPastaFp>,
22) -> WasmFlatVector<WasmPastaFp> {
23    let mut state_vec: Vec<Fp> = state.into_iter().map(Into::into).collect();
24    poseidon_block_cipher::<Fp, PlonkSpongeConstantsKimchi>(
25        mina_poseidon::pasta::fp_kimchi::static_params(),
26        &mut state_vec,
27    );
28    state_vec
29        .iter()
30        .map(|f| WasmPastaFp(*f))
31        .collect::<Vec<WasmPastaFp>>()
32        .into()
33}
34
35// fq
36
37#[wasm_bindgen]
38pub fn caml_pasta_fq_poseidon_block_cipher(
39    state: WasmFlatVector<WasmPastaFq>,
40) -> WasmFlatVector<WasmPastaFq> {
41    let mut state_vec: Vec<Fq> = state.into_iter().map(Into::into).collect();
42    poseidon_block_cipher::<Fq, PlonkSpongeConstantsKimchi>(
43        mina_poseidon::pasta::fq_kimchi::static_params(),
44        &mut state_vec,
45    );
46    state_vec
47        .iter()
48        .map(|f| WasmPastaFq(*f))
49        .collect::<Vec<WasmPastaFq>>()
50        .into()
51}