From 3d90cf2ca9b0aa1e43a51596c38bf45fa175ace7 Mon Sep 17 00:00:00 2001 From: "e.tatuzova" Date: Mon, 30 Oct 2023 16:15:25 +0400 Subject: [PATCH] Simplest zkllvm hash() function compatible transcript #214 --- .../nil/crypto3/zk/transcript/fiat_shamir.hpp | 40 +++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/include/nil/crypto3/zk/transcript/fiat_shamir.hpp b/include/nil/crypto3/zk/transcript/fiat_shamir.hpp index 3111b5151..fdb78e715 100644 --- a/include/nil/crypto3/zk/transcript/fiat_shamir.hpp +++ b/include/nil/crypto3/zk/transcript/fiat_shamir.hpp @@ -35,12 +35,19 @@ #include #include +#include +#include #include + +#include +#include #include #include +#include +#include +#include #include - namespace nil { namespace crypto3 { namespace zk { @@ -211,27 +218,31 @@ namespace nil { state(hash(first, last)) { } - - template + void operator()(const typename hash_type::digest_type input){ + auto tmp = pair_hash(state, state); + state = pair_hash(input, tmp); + } + + template void operator()(const InputRange &r) { - state = hash(r, hash(state)); + BOOST_ASSERT_MSG(false, "Not supported"); } template void operator()(InputIterator first, InputIterator last) { - state = hash(first, last, hash(state)); + BOOST_ASSERT_MSG(false, "Not supported"); } template typename Field::value_type challenge() { - state = hash(state); + state = pair_hash(state, state); return state; } template Integral int_challenge() { - state = hash(state); + state = pair_hash(state, state); Integral raw_result = state.data.template convert_to(); @@ -251,7 +262,22 @@ namespace nil { private: typename hash_type::digest_type state; + typename hash_type::digest_type pair_hash(typename hash_type::digest_type a1, typename hash_type::digest_type a2){ + using field_type = nil::crypto3::algebra::curves::pallas::base_field_type; + using poseidon_policy = nil::crypto3::hashes::detail::mina_poseidon_policy; + using permutation_type = nil::crypto3::hashes::detail::poseidon_permutation; + using state_type = typename permutation_type::state_type; + + std::vector a = {0, a1, a2}; + state_type poseidon_state; + std::copy(a.begin(), a.end(), poseidon_state.begin()); + permutation_type::permute(poseidon_state); + + std::vector result(3); + std::copy(poseidon_state.begin(), poseidon_state.end(), result.begin()); + return result[2]; + } }; } // namespace transcript