Skip to content

Commit

Permalink
feat(zkllvm-circuits/compute_shuffled_index_circuit): Move content of…
Browse files Browse the repository at this point in the history
… implementation in imp folder.
  • Loading branch information
HristoStaykov committed Jan 24, 2024
1 parent 730297f commit 351e50e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 101 deletions.
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);
}
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;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <nil/crypto3/hash/algorithm/hash.hpp>
#include <nil/crypto3/hash/sha2.hpp>
#include "circuit_utils/circuit_byte_utils.h"
#include "circuits_imp/compute_shuffled_index_imp.h"

#include <algorithm>
#include <array>
Expand All @@ -14,6 +15,8 @@
#include "utils/byte_utils.h"
#include "utils/file_utils.h"

#include "llvm/Support/JSON.h"

using llvm::yaml::Input;
using llvm::yaml::IO;
using llvm::yaml::MappingTraits;
Expand All @@ -24,54 +27,6 @@ using namespace file_utils;

using std::cout;

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;
}

struct TestInput {
std::string seed;
int count;
Expand Down Expand Up @@ -112,7 +67,7 @@ int main(int argc, char* argv[]) {
yin >> doc;

if (yin.error()) {
std::cerr << "Failes to process " << v.string() << "\n";
std::cerr << "Failed to process " << v.string() << "\n";
return false;
}

Expand All @@ -130,7 +85,7 @@ int main(int argc, char* argv[]) {
// std::cout << "\n";
std::vector<uint64_t> mapping_result;
for (size_t i = 0; i < doc.mapping.size(); i++) {
auto result = compute_shuffled_index(i, doc.mapping.size(), seed_bytes, SHUFFLE_ROUND_COUNT);
auto result = compute_shuffled_index_imp(i, doc.mapping.size(), seed_bytes, SHUFFLE_ROUND_COUNT);
mapping_result.push_back(result);
}
for (size_t i = 0; i < mapping_result.size(); i++) {
Expand Down

0 comments on commit 351e50e

Please sign in to comment.