Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit bec804d

Browse files
committed
Changed interface for proof_of_work #11
Added parallel implementation for proof_of_work for non-poseidon trascript, #11 Update for grinding, synchronizing with crypto3 #11 Renamed int_be to to_byte_array #11
1 parent c0e5955 commit bec804d

File tree

14 files changed

+124
-160
lines changed

14 files changed

+124
-160
lines changed

CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@ project(parallel-crypto3)
44
list(APPEND CMAKE_MODULE_PATH
55
"${CMAKE_CURRENT_LIST_DIR}/cmake/modules/share/modules/cmake")
66

7+
# This is useful due to some build systems (Ninja in particular) are piping
8+
# compiler output and compiler switches it's output to plain text
9+
option (FORCE_COLORED_OUTPUT "Always produce ANSI-colored output (GNU/Clang only)." FALSE)
10+
if (${FORCE_COLORED_OUTPUT})
11+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
12+
add_compile_options (-fdiagnostics-color=always)
13+
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
14+
add_compile_options (-fcolor-diagnostics)
15+
endif ()
16+
endif ()
17+
18+
# The file compile_commands.json is generated in build directory, so LSP could
19+
# pick it up and guess all include paths, defines and other stuff.
20+
# If Nix is used, LSP could not guess the locations of implicit include
21+
# directories, so we need to include them explicitly.
22+
if(CMAKE_EXPORT_COMPILE_COMMANDS)
23+
set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES
24+
${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
25+
endif()
26+
727
# TODO: check what is actually required here
828
include(CMConfig)
929
include(CMDeploy)

libs/parallel-zk/include/nil/crypto3/zk/commitments/batched_commitment.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,7 @@ namespace nil {
199199
}
200200

201201
void eval_polys() {
202-
for(auto it = _polys.begin(); it != _polys.end(); ++it) {
203-
std::size_t k = it->first;
204-
const auto& poly = it->second;
205-
202+
for(auto const &[k, poly] : _polys) {
206203
_z.set_batch_size(k, poly.size());
207204
auto const &point = _points.at(k);
208205

libs/parallel-zk/include/nil/crypto3/zk/commitments/detail/polynomial/basic_fri.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ namespace nil {
157157
std::size_t lambda,
158158
std::size_t expand_factor,
159159
bool use_grinding = false,
160-
std::size_t grinding_parameter = 0xFFFF
160+
std::size_t grinding_parameter = 16
161161
): lambda(lambda)
162162
, use_grinding(use_grinding)
163163
, grinding_parameter(grinding_parameter)
@@ -174,7 +174,7 @@ namespace nil {
174174
std::size_t lambda,
175175
std::size_t expand_factor,
176176
bool use_grinding = false,
177-
std::size_t grinding_parameter = 0xFFFF
177+
std::size_t grinding_parameter = 16
178178
): lambda(lambda)
179179
, use_grinding(use_grinding)
180180
, grinding_parameter(grinding_parameter)
@@ -192,7 +192,7 @@ namespace nil {
192192
std::size_t expand_factor,
193193
std::size_t lambda,
194194
bool use_grinding = false,
195-
std::size_t grinding_parameter = 0xFFFF
195+
std::size_t grinding_parameter = 16
196196
) : lambda(lambda)
197197
, use_grinding(use_grinding)
198198
, grinding_parameter(grinding_parameter)

libs/parallel-zk/include/nil/crypto3/zk/commitments/detail/polynomial/proof_of_work.hpp

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//---------------------------------------------------------------------------//
22
// Copyright (c) 2023 Elena Tatuzova <[email protected]>
3+
// Copyright (c) 2024 Vasiliy Olekhov <[email protected]>
34
//
45
// MIT License
56
//
@@ -47,37 +48,66 @@ namespace nil {
4748
using transcript_type = transcript::fiat_shamir_heuristic_sequential<transcript_hash_type>;
4849
using output_type = OutType;
4950

50-
static inline OutType generate(transcript_type &transcript, OutType mask=0xFFFF) {
51-
output_type proof_of_work = std::rand();
52-
output_type result;
53-
std::vector<std::uint8_t> bytes(4);
51+
static inline std::array<std::uint8_t, sizeof(OutType)>
52+
to_byte_array(OutType v) {
53+
std::array<std::uint8_t, sizeof(OutType)> bytes;
54+
for(int i = sizeof(v)-1; i>=0; --i) {
55+
bytes[i] = v & 0xFF;
56+
v >>= 8;
57+
}
58+
return bytes;
59+
}
60+
61+
static inline OutType generate(transcript_type &transcript, std::size_t grinding_bits = 16) {
62+
BOOST_ASSERT_MSG(grinding_bits < 64, "Grinding parameter should be bits, not mask");
63+
output_type mask = grinding_bits > 0 ? ( 1ULL << grinding_bits ) - 1 : 0;
64+
output_type pow_seed = std::rand();
65+
66+
/* Enough work for ~ two minutes on 48 cores, keccak<512> */
67+
std::size_t per_block = 1 << 30;
68+
69+
std::atomic<bool> challenge_found = false;
70+
std::atomic<std::size_t> pow_value_offset;
5471

5572
while( true ) {
56-
transcript_type tmp_transcript = transcript;
57-
bytes[0] = std::uint8_t((proof_of_work&0xFF000000)>>24);
58-
bytes[1] = std::uint8_t((proof_of_work&0x00FF0000)>>16);
59-
bytes[2] = std::uint8_t((proof_of_work&0x0000FF00)>>8);
60-
bytes[3] = std::uint8_t(proof_of_work&0x000000FF);
61-
62-
tmp_transcript(bytes);
63-
result = tmp_transcript.template int_challenge<output_type>();
64-
if ((result & mask) == 0)
73+
wait_for_all(parallel_run_in_chunks<void>(
74+
per_block,
75+
[&transcript, &pow_seed, &challenge_found, &pow_value_offset, &mask](std::size_t pow_start, std::size_t pow_finish) {
76+
std::size_t i = pow_start;
77+
while ( i < pow_finish ) {
78+
if (challenge_found) {
79+
break;
80+
}
81+
transcript_type tmp_transcript = transcript;
82+
tmp_transcript(to_byte_array(pow_seed + i));
83+
OutType pow_result = tmp_transcript.template int_challenge<OutType>();
84+
if ( ((pow_result & mask) == 0) && !challenge_found ) {
85+
bool expected = false;
86+
if (challenge_found.compare_exchange_strong(expected, true)) {
87+
pow_value_offset = i;
88+
}
89+
break;
90+
}
91+
++i;
92+
}
93+
}, ThreadPool::PoolLevel::LOW));
94+
95+
if (challenge_found) {
6596
break;
66-
proof_of_work++;
97+
}
98+
pow_seed += per_block;
6799
}
68-
transcript(bytes);
69-
result = transcript.template int_challenge<output_type>();
70-
return proof_of_work;
100+
101+
transcript(to_byte_array(pow_seed + (std::size_t)pow_value_offset));
102+
transcript.template int_challenge<OutType>();
103+
return pow_seed + (std::size_t)pow_value_offset;
71104
}
72105

73-
static inline bool verify(transcript_type &transcript, output_type proof_of_work, OutType mask=0xFFFF) {
74-
std::vector<std::uint8_t> bytes(4);
75-
bytes[0] = std::uint8_t((proof_of_work&0xFF000000)>>24);
76-
bytes[1] = std::uint8_t((proof_of_work&0x00FF0000)>>16);
77-
bytes[2] = std::uint8_t((proof_of_work&0x0000FF00)>>8);
78-
bytes[3] = std::uint8_t(proof_of_work&0x000000FF);
79-
transcript(bytes);
106+
static inline bool verify(transcript_type &transcript, output_type proof_of_work, std::size_t grinding_bits = 16) {
107+
BOOST_ASSERT_MSG(grinding_bits < 64, "Grinding parameter should be bits, not mask");
108+
transcript(to_byte_array(proof_of_work));
80109
output_type result = transcript.template int_challenge<output_type>();
110+
output_type mask = grinding_bits > 0 ? ( 1ULL << grinding_bits ) - 1 : 0;
81111
return ((result & mask) == 0);
82112
}
83113
};
@@ -104,8 +134,8 @@ namespace nil {
104134
((integral_type(1) << GrindingBits) - 1) << (FieldType::modulus_bits - GrindingBits)
105135
: 0);
106136

107-
/* Enough work for ~ two minutes on 48 cores */
108-
std::size_t per_block = 1<<23;
137+
/* Enough work for ~ two minutes on 48 cores, poseidon<pallas> */
138+
std::size_t per_block = 1 << 23;
109139

110140
std::atomic<bool> challenge_found = false;
111141
std::atomic<std::size_t> pow_value_offset;

libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kzg.hpp

Lines changed: 13 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -352,43 +352,18 @@ namespace nil {
352352
/* The procedure of updating the transcript is subject to review and change
353353
* #295 */
354354

355-
nil::marshalling::status_type status;
356-
357-
for (const auto &commit: public_key.commits) {
358-
std::vector<uint8_t> byteblob =
359-
nil::marshalling::pack<nil::marshalling::option::big_endian>(commit, status);
360-
BOOST_ASSERT(status == nil::marshalling::status_type::success);
361-
transcript(
362-
::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper<
363-
typename CommitmentSchemeType::transcript_hash_type::word_type,
364-
decltype(byteblob)
365-
>(byteblob)
366-
);
355+
356+
for (const auto &commit : public_key.commits) {
357+
transcript(commit);
367358
}
368-
for (const auto &S: public_key.S) {
369-
for (const auto &s: S) {
370-
std::vector<uint8_t> byteblob =
371-
nil::marshalling::pack<nil::marshalling::option::big_endian>(s, status);
372-
BOOST_ASSERT(status == nil::marshalling::status_type::success);
373-
transcript(
374-
::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper<
375-
typename CommitmentSchemeType::transcript_hash_type::word_type,
376-
decltype(byteblob)
377-
>(byteblob)
378-
);
359+
for (const auto &S : public_key.S) {
360+
for (const auto &s : S) {
361+
transcript(s);
379362
}
380363
}
381364
for (const auto &r: public_key.r) {
382365
for (std::size_t i = 0; i < r.size(); ++i) {
383-
std::vector<uint8_t> byteblob =
384-
nil::marshalling::pack<nil::marshalling::option::big_endian>(r[i], status);
385-
BOOST_ASSERT(status == nil::marshalling::status_type::success);
386-
transcript(
387-
::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper<
388-
typename CommitmentSchemeType::transcript_hash_type::word_type,
389-
decltype(byteblob)
390-
>(byteblob)
391-
);
366+
transcript(r[i]);
392367
}
393368
}
394369
}
@@ -730,41 +705,21 @@ namespace nil {
730705
* #295 */
731706

732707
// Push commitments to transcript
733-
transcript(::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper<
734-
typename CommitmentSchemeType::transcript_hash_type::word_type,
735-
decltype(_commitments[batch_ind])
736-
>(_commitments[batch_ind]));
708+
709+
transcript(_commitments[batch_ind]);
737710

738711
// Push evaluation points to transcript
739-
for (std::size_t i = 0; i < this->_z.get_batch_size(batch_ind); i++) {
740-
for (std::size_t j = 0; j < this->_z.get_poly_points_number(batch_ind, i); j++) {
741-
nil::marshalling::status_type status;
742-
std::vector<uint8_t> byteblob =
743-
nil::marshalling::pack<endianness>(this->_z.get(batch_ind, i, j), status);
744-
BOOST_ASSERT(status == nil::marshalling::status_type::success);
745-
transcript(
746-
::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper<
747-
typename CommitmentSchemeType::transcript_hash_type::word_type,
748-
decltype(byteblob)
749-
>(byteblob)
750-
);
712+
for(std::size_t i = 0; i < this->_z.get_batch_size(batch_ind); i++) {
713+
for(std::size_t j = 0; j < this->_z.get_poly_points_number(batch_ind, i); j++) {
714+
transcript(this->_z.get(batch_ind, i, j));
751715
}
752716
}
753717

754718
// Push U polynomials to transcript
755719
for (std::size_t i = 0; i < this->_points[batch_ind].size(); i++) {
756720
auto poly = this->get_U(batch_ind, i);
757721
for (std::size_t j = 0; j < poly.size(); ++j) {
758-
nil::marshalling::status_type status;
759-
std::vector<uint8_t> byteblob =
760-
nil::marshalling::pack<endianness>(poly[j], status);
761-
BOOST_ASSERT(status == nil::marshalling::status_type::success);
762-
transcript(
763-
::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper<
764-
typename CommitmentSchemeType::transcript_hash_type::word_type,
765-
decltype(byteblob)
766-
>(byteblob)
767-
);
722+
transcript(poly[j]);
768723
}
769724
}
770725
}

0 commit comments

Comments
 (0)