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 mina_curves::pasta::{Fp, Fq};
12use mina_poseidon::{constants::PlonkSpongeConstantsKimchi, permutation::poseidon_block_cipher};
13use wasm_bindgen::prelude::*;
14
15use crate::{
16    arkworks::{WasmPastaFp, WasmPastaFq},
17    wasm_flat_vector::WasmFlatVector,
18};
19
20// fp
21
22#[wasm_bindgen]
23pub fn caml_pasta_fp_poseidon_block_cipher(
24    state: WasmFlatVector<WasmPastaFp>,
25) -> WasmFlatVector<WasmPastaFp> {
26    let mut state_vec: Vec<Fp> = state.into_iter().map(Into::into).collect();
27    poseidon_block_cipher::<Fp, PlonkSpongeConstantsKimchi>(
28        mina_poseidon::pasta::fp_kimchi::static_params(),
29        &mut state_vec,
30    );
31    state_vec
32        .iter()
33        .map(|f| WasmPastaFp(*f))
34        .collect::<Vec<WasmPastaFp>>()
35        .into()
36}
37
38// fq
39
40#[wasm_bindgen]
41pub fn caml_pasta_fq_poseidon_block_cipher(
42    state: WasmFlatVector<WasmPastaFq>,
43) -> WasmFlatVector<WasmPastaFq> {
44    let mut state_vec: Vec<Fq> = state.into_iter().map(Into::into).collect();
45    poseidon_block_cipher::<Fq, PlonkSpongeConstantsKimchi>(
46        mina_poseidon::pasta::fq_kimchi::static_params(),
47        &mut state_vec,
48    );
49    state_vec
50        .iter()
51        .map(|f| WasmPastaFq(*f))
52        .collect::<Vec<WasmPastaFq>>()
53        .into()
54}