Skip to content

Commit

Permalink
Transcript updated #214
Browse files Browse the repository at this point in the history
  • Loading branch information
ETatuzova authored and martun committed Dec 6, 2023
1 parent 99c0581 commit 395c50b
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions include/nil/crypto3/zk/transcript/fiat_shamir.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,56 +205,71 @@ namespace nil {
typename std::enable_if_t<crypto3::hashes::is_poseidon<Hash>::value>> {

typedef Hash hash_type;
using field_type = nil::crypto3::algebra::curves::pallas::base_field_type;
using poseidon_policy = nil::crypto3::hashes::detail::mina_poseidon_policy<field_type>;
using permutation_type = nil::crypto3::hashes::detail::poseidon_permutation<poseidon_policy>;
using state_type = typename permutation_type::state_type;

fiat_shamir_heuristic_sequential() : state(hash<hash_type>(Hash::digest_type::zero())) {
fiat_shamir_heuristic_sequential() : state({0,0,0}), cur(1) {
}

template<typename InputRange>
fiat_shamir_heuristic_sequential(const InputRange &r) : state(hash<hash_type>(r)) {
fiat_shamir_heuristic_sequential(const InputRange &r) : state({0,0,0}), cur(1) {
BOOST_ASSERT_MSG(false, "Not supported");
}

template<typename InputIterator>
fiat_shamir_heuristic_sequential(InputIterator first, InputIterator last) :
state(hash<hash_type>(first, last)) {
fiat_shamir_heuristic_sequential(InputIterator first, InputIterator last){
BOOST_ASSERT_MSG(false, "Not supported");
}

void operator()(const typename hash_type::digest_type input){
auto tmp = pair_hash(state, state);
state = pair_hash(input, tmp);
state[cur] = input;
if( cur == 2 ){
state_type poseidon_state;
std::copy(state.begin(), state.end(), poseidon_state.begin());
permutation_type::permute(poseidon_state);

state[0] = poseidon_state[2];
state[1] = 0;
state[2] = 0;
cur = 1;
} else {
cur++;
}
}

template<typename InputRange>
void operator()(const InputRange &r) {
BOOST_ASSERT_MSG(false, "Not supported");
}

template<typename InputIterator>
void operator()(InputIterator first, InputIterator last) {
BOOST_ASSERT_MSG(false, "Not supported");
}

template<typename Field>
typename Field::value_type challenge() {
state = pair_hash(state, state);
return state;
state_type poseidon_state;
std::copy(state.begin(), state.end(), poseidon_state.begin());
permutation_type::permute(poseidon_state);

state[0] = poseidon_state[2];
state[1] = 0;
state[2] = 0;
cur = 1;
return state[0];
}

template<typename Integral>
Integral int_challenge() {

state = pair_hash(state, state);
auto c = challenge<field_type>();
nil::marshalling::status_type status;

std::cout << "State = " << std::hex << state << std::dec << std::endl;
nil::crypto3::multiprecision::cpp_int intermediate_result = nil::marshalling::pack(state, status);
nil::crypto3::multiprecision::cpp_int intermediate_result = nil::marshalling::pack(c, status);
Integral result = 0;
Integral factor = 1;
while (intermediate_result > 0) {
result += factor * (Integral)(intermediate_result%0x100);
factor *= 0x100;
intermediate_result = intermediate_result/0x100;
}
std::cout << "Integral challenge = " << std::hex << result << std::dec << std::endl;
return result;
}

Expand All @@ -270,23 +285,8 @@ 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<field_type>;
using permutation_type = nil::crypto3::hashes::detail::poseidon_permutation<poseidon_policy>;
using state_type = typename permutation_type::state_type;

std::vector<typename field_type::value_type> a = {0, a1, a2};

state_type poseidon_state;
std::copy(a.begin(), a.end(), poseidon_state.begin());
permutation_type::permute(poseidon_state);

std::vector<typename field_type::value_type> result(3);
std::copy(poseidon_state.begin(), poseidon_state.end(), result.begin());
return result[2];
}
std::vector<typename hash_type::digest_type> state;
std::size_t cur = 1;
};

} // namespace transcript
Expand Down

0 comments on commit 395c50b

Please sign in to comment.