-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(zkllvm-circuits/compute_shuffled_index_circuit): Move content of…
… implementation in imp folder.
- Loading branch information
1 parent
730297f
commit 351e50e
Showing
3 changed files
with
65 additions
and
101 deletions.
There are no files selected for viewing
53 changes: 2 additions & 51 deletions
53
vendor/zkllvm-metacraft-circuits/src/circuits/compute_shuffled_index.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,6 @@ | ||
#include <nil/crypto3/hash/algorithm/hash.hpp> | ||
#include <nil/crypto3/hash/sha2.hpp> | ||
|
||
#include <algorithm> | ||
#include <array> | ||
|
||
#include "../circuit_utils/circuit_byte_utils.h" | ||
#include "../utils/picosha2.h" | ||
|
||
using namespace circuit_byte_utils; | ||
#include "../circuits_imp/compute_shuffled_index_imp.h" | ||
|
||
[[circuit]] uint64_t compute_shuffled_index(uint64_t index, uint64_t index_count, std::array<Byte, 32> seed, | ||
int SHUFFLE_ROUND_COUNT = 90) { | ||
assert_true(index < index_count); | ||
|
||
std::array<Byte, 32 + 1 + 4> source_buffer {}; | ||
|
||
//!!! sha256_to_bytes_array(seed, source_buffer); | ||
std::copy(seed.begin(), seed.end(), source_buffer.begin()); | ||
|
||
// Swap or not (https://link.springer.com/content/pdf/10.1007%2F978-3-642-32009-5_1.pdf) | ||
// See the 'generalized domain' algorithm on page 3 | ||
for (Byte current_round = 0; current_round < SHUFFLE_ROUND_COUNT; current_round++) { | ||
source_buffer[32] = current_round; | ||
|
||
//!!! auto eth2digest = hash<hashes::sha2<256>>(source_buffer.begin(), source_buffer.begin() + 33); | ||
std::array<Byte, 32> eth2digest_bytes; | ||
picosha2::hash256(source_buffer.begin(), source_buffer.begin() + 33, eth2digest_bytes.begin(), | ||
eth2digest_bytes.end()); | ||
///!!! sha256_to_bytes_array(eth2digest, eth2digest_bytes); | ||
auto first8bytes = take_n_elements<Byte, eth2digest_bytes.size(), 8>(eth2digest_bytes); | ||
// PrintContainer(first8bytes); | ||
auto first8bytes_int = bytes_to_int<uint64_t>(first8bytes); | ||
auto pivot = first8bytes_int % index_count; | ||
uint64_t flip = (pivot + index_count - index) % index_count; | ||
auto position = std::max(index, flip); | ||
|
||
auto source_buffer_additional_bytes = int_to_bytes(uint32_t(position >> 8)); | ||
for (auto i = 0; i < 4; i++) { | ||
source_buffer[33 + i] = source_buffer_additional_bytes[i]; | ||
} | ||
///!!! auto source = hash<hashes::sha2<256>>(source_buffer.begin(), source_buffer.end()); | ||
std::array<Byte, 32> source_to_bytes; | ||
picosha2::hash256(source_buffer.begin(), source_buffer.end(), source_to_bytes.begin(), source_to_bytes.end()); | ||
///!!! sha256_to_bytes_array(source, source_to_bytes); | ||
auto byte_value = source_to_bytes[(position % 256) >> 3]; | ||
auto bit = (byte_value >> (position % 8)) % 2; | ||
|
||
if (bit != 0) { | ||
index = flip; | ||
} | ||
} | ||
|
||
return index; | ||
return compute_shuffled_index_imp(index, index_count, seed, SHUFFLE_ROUND_COUNT); | ||
} |
58 changes: 58 additions & 0 deletions
58
vendor/zkllvm-metacraft-circuits/src/circuits_imp/compute_shuffled_index_imp.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#pragma once | ||
|
||
#include <nil/crypto3/hash/algorithm/hash.hpp> | ||
#include <nil/crypto3/hash/sha2.hpp> | ||
|
||
#include <algorithm> | ||
#include <array> | ||
|
||
#include "compute_shuffled_index_imp.h" | ||
#include "../circuit_utils/circuit_byte_utils.h" | ||
#include "../utils/picosha2.h" | ||
|
||
using namespace circuit_byte_utils; | ||
|
||
uint64_t compute_shuffled_index_imp(uint64_t index, uint64_t index_count, std::array<Byte, 32> seed, | ||
int SHUFFLE_ROUND_COUNT = 90) { | ||
assert_true(index < index_count); | ||
|
||
std::array<Byte, 32 + 1 + 4> source_buffer {}; | ||
|
||
//!!! sha256_to_bytes_array(seed, source_buffer); | ||
std::copy(seed.begin(), seed.end(), source_buffer.begin()); | ||
|
||
// Swap or not (https://link.springer.com/content/pdf/10.1007%2F978-3-642-32009-5_1.pdf) | ||
// See the 'generalized domain' algorithm on page 3 | ||
for (Byte current_round = 0; current_round < SHUFFLE_ROUND_COUNT; current_round++) { | ||
source_buffer[32] = current_round; | ||
|
||
//!!! auto eth2digest = hash<hashes::sha2<256>>(source_buffer.begin(), source_buffer.begin() + 33); | ||
std::array<Byte, 32> eth2digest_bytes; | ||
picosha2::hash256(source_buffer.begin(), source_buffer.begin() + 33, eth2digest_bytes.begin(), | ||
eth2digest_bytes.end()); | ||
///!!! sha256_to_bytes_array(eth2digest, eth2digest_bytes); | ||
auto first8bytes = take_n_elements<Byte, eth2digest_bytes.size(), 8>(eth2digest_bytes); | ||
// PrintContainer(first8bytes); | ||
auto first8bytes_int = bytes_to_int<uint64_t>(first8bytes); | ||
auto pivot = first8bytes_int % index_count; | ||
uint64_t flip = (pivot + index_count - index) % index_count; | ||
auto position = std::max(index, flip); | ||
|
||
auto source_buffer_additional_bytes = int_to_bytes(uint32_t(position >> 8)); | ||
for (auto i = 0; i < 4; i++) { | ||
source_buffer[33 + i] = source_buffer_additional_bytes[i]; | ||
} | ||
///!!! auto source = hash<hashes::sha2<256>>(source_buffer.begin(), source_buffer.end()); | ||
std::array<Byte, 32> source_to_bytes; | ||
picosha2::hash256(source_buffer.begin(), source_buffer.end(), source_to_bytes.begin(), source_to_bytes.end()); | ||
///!!! sha256_to_bytes_array(source, source_to_bytes); | ||
auto byte_value = source_to_bytes[(position % 256) >> 3]; | ||
auto bit = (byte_value >> (position % 8)) % 2; | ||
|
||
if (bit != 0) { | ||
index = flip; | ||
} | ||
} | ||
|
||
return index; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters