Skip to content

Commit

Permalink
Refactor Poseidon usage
Browse files Browse the repository at this point in the history
  • Loading branch information
x-mass committed Apr 28, 2024
1 parent 25ea43e commit 3864daf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
25 changes: 18 additions & 7 deletions include/nil/crypto3/marshalling/containers/types/merkle_proof.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ namespace nil {
struct merkle_node_value<
TTypeBase,
GroupElementType,
typename std::enable_if<nil::crypto3::algebra::is_field_element<GroupElementType>::value>::type> {
typename std::enable_if<nil::crypto3::algebra::is_field_element<
GroupElementType
>::value>::type
> {
using type = field_element<TTypeBase, GroupElementType>;
};

Expand Down Expand Up @@ -170,14 +173,19 @@ namespace nil {
template<
typename GroupElementType,
typename Endianness,
typename std::enable_if<nil::crypto3::algebra::is_field_element<GroupElementType>::value, bool>::type = true>
typename merkle_node_value<nil::marshalling::field_type<Endianness>, GroupElementType>::type
typename std::enable_if<nil::crypto3::algebra::is_field_element<
GroupElementType
>::value, bool>::type = true>
typename merkle_node_value<
nil::marshalling::field_type<Endianness>,
GroupElementType
>::type
fill_merkle_node_value(const GroupElementType &node_value) {

using TTypeBase = nil::marshalling::field_type<Endianness>;

typename merkle_node_value<nil::marshalling::field_type<Endianness>, GroupElementType>::type filled_node_value =
field_element<TTypeBase, GroupElementType>(node_value);
typename merkle_node_value<nil::marshalling::field_type<Endianness>, GroupElementType>::type filled_node_value =
field_element<TTypeBase, GroupElementType>(node_value);
return filled_node_value;
}

Expand Down Expand Up @@ -214,9 +222,12 @@ namespace nil {
template<
typename GroupElementType,
typename Endianness,
typename std::enable_if<nil::crypto3::algebra::is_field_element<GroupElementType>::value, bool>::type = true>
typename std::enable_if<nil::crypto3::algebra::is_field_element<
GroupElementType
>::value, bool>::type = true>
GroupElementType make_merkle_node_value(const typename merkle_node_value<
nil::marshalling::field_type<Endianness>, GroupElementType>::type &filled_node_value) {
nil::marshalling::field_type<Endianness>, GroupElementType
>::type &filled_node_value) {
return filled_node_value.value();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ namespace nil {
nil::marshalling::types::integral<TTypeBase, octet_type>,
nil::marshalling::option::sequence_size_field_prefix<nil::marshalling::types::integral<TTypeBase, std::size_t>>
> filled_constraint_system_with_params_hash;
if constexpr(nil::crypto3::hashes::is_poseidon<typename CommonDataType::transcript_hash_type>::value){

if constexpr(nil::crypto3::algebra::is_field_element<
typename CommonDataType::transcript_hash_type::word_type
>::value) {
auto integral = typename CommonDataType::field_type::integral_type(common_data.vk.constraint_system_with_params_hash.data);
std::vector<unsigned char> blob;
export_bits(integral, std::back_inserter(blob), 8);
Expand Down Expand Up @@ -239,7 +242,9 @@ namespace nil {

typename CommonDataType::verification_key_type vk;
vk.fixed_values_commitment = fixed_values;
if constexpr(nil::crypto3::hashes::is_poseidon<typename CommonDataType::transcript_hash_type>::value){
if constexpr(nil::crypto3::algebra::is_field_element<
typename CommonDataType::transcript_hash_type::word_type
>::value) {
std::vector<std::uint8_t> blob;
for( std::size_t i = 0; i < std::get<13>(filled_common_data.value()).value().size(); i++){
blob.push_back(std::uint8_t(std::get<13>(filled_common_data.value()).value()[i].value()));
Expand Down
21 changes: 20 additions & 1 deletion test/merkle_proof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
#include <nil/marshalling/field_type.hpp>
#include <nil/marshalling/endianness.hpp>

#include <nil/crypto3/algebra/type_traits.hpp>
#include <nil/crypto3/multiprecision/cpp_int.hpp>
#include <nil/crypto3/multiprecision/number.hpp>

#include <nil/crypto3/hash/block_to_field_elements_wrapper.hpp>
#include <nil/crypto3/hash/sha2.hpp>
#include <nil/crypto3/hash/keccak.hpp>
#include <nil/crypto3/hash/poseidon.hpp>
Expand Down Expand Up @@ -105,8 +107,25 @@ void test_merkle_proof(std::size_t tree_depth) {
types::merkle_proof<nil::marshalling::field_type<Endianness>, merkle_proof_type>;

std::size_t leafs_number = std::pow(Arity, tree_depth);
// You can also lazy convert byte stream to field elements stream using <nil/crypto3/hash/block_to_field_elements_wrapper.hpp>
auto data = generate_random_data<std::uint8_t, LeafSize>(leafs_number);
merkle_tree_type tree = nil::crypto3::containers::make_merkle_tree<Hash, Arity>(data.begin(), data.end());
merkle_tree_type tree;

if constexpr (nil::crypto3::algebra::is_field_element<typename Hash::word_type>::value) {
// Populate the vector with wrappers, one for each block
std::vector<
nil::crypto3::hashes::block_to_field_elements_wrapper<
typename Hash::word_type::field_type,
std::array<std::uint8_t, LeafSize>
>
> wrappers;
for (const auto& inner_containers : data) {
wrappers.emplace_back(inner_containers);
}
tree = nil::crypto3::containers::make_merkle_tree<Hash, Arity>(wrappers.begin(), wrappers.end());
} else {
tree = nil::crypto3::containers::make_merkle_tree<Hash, Arity>(data.begin(), data.end());
}
std::size_t proof_idx = std::rand() % leafs_number;
merkle_proof_type proof(tree, proof_idx);

Expand Down

0 comments on commit 3864daf

Please sign in to comment.