-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new work queue hooked up everywhere excluding shplonk
- Loading branch information
1 parent
f534f92
commit ee6818a
Showing
3 changed files
with
155 additions
and
51 deletions.
There are no files selected for viewing
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
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
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,138 @@ | ||
#pragma once | ||
|
||
#include "barretenberg/honk/transcript/transcript.hpp" | ||
#include "barretenberg/plonk/proof_system/proving_key/proving_key.hpp" | ||
#include <cstddef> | ||
#include <memory> | ||
|
||
namespace proof_system::honk { | ||
|
||
// Currently only one type of work queue operation but there will likely be others related to Sumcheck | ||
enum WorkType { SCALAR_MULTIPLICATION }; | ||
|
||
template <typename Params> class work_queue { | ||
|
||
using CommitmentKey = typename Params::CK; | ||
using FF = typename Params::Fr; | ||
using Commitment = typename Params::C; | ||
|
||
public: | ||
struct work_item_info { | ||
uint32_t num_scalar_multiplications; | ||
}; | ||
|
||
struct work_item { | ||
WorkType work_type = SCALAR_MULTIPLICATION; | ||
std::span<FF> mul_scalars; | ||
std::string label; | ||
}; | ||
|
||
explicit work_queue(std::shared_ptr<proof_system::plonk::proving_key>& proving_key, | ||
proof_system::honk::ProverTranscript<FF>& prover_transcript, | ||
std::shared_ptr<CommitmentKey>& commitment_key) | ||
: key(proving_key) | ||
, transcript(prover_transcript) | ||
, commitment_key(commitment_key){}; | ||
|
||
work_queue(const work_queue& other) = default; | ||
work_queue(work_queue&& other) noexcept = default; | ||
work_queue& operator=(const work_queue& other) = delete; | ||
work_queue& operator=(work_queue&& other) = delete; | ||
~work_queue() = default; | ||
|
||
[[nodiscard]] work_item_info get_queued_work_item_info() const | ||
{ | ||
uint32_t scalar_mul_count = 0; | ||
for (const auto& item : work_item_queue) { | ||
if (item.work_type == WorkType::SCALAR_MULTIPLICATION) { | ||
++scalar_mul_count; | ||
} | ||
} | ||
return work_item_info{ scalar_mul_count }; | ||
}; | ||
|
||
[[nodiscard]] FF* get_scalar_multiplication_data(size_t work_item_number) const | ||
{ | ||
size_t count = 0; | ||
for (const auto& item : work_item_queue) { | ||
if (item.work_type == WorkType::SCALAR_MULTIPLICATION) { | ||
if (count == work_item_number) { | ||
return const_cast<FF*>(item.mul_scalars.data()); | ||
} | ||
++count; | ||
} | ||
} | ||
return nullptr; | ||
}; | ||
|
||
[[nodiscard]] size_t get_scalar_multiplication_size(size_t work_item_number) const | ||
{ | ||
size_t count = 0; | ||
for (const auto& item : work_item_queue) { | ||
if (item.work_type == WorkType::SCALAR_MULTIPLICATION) { | ||
if (count == work_item_number) { | ||
return item.mul_scalars.size(); | ||
} | ||
++count; | ||
} | ||
} | ||
return 0; | ||
}; | ||
|
||
void put_scalar_multiplication_data(const Commitment& result, size_t work_item_number) | ||
{ | ||
size_t count = 0; | ||
for (const auto& item : work_item_queue) { | ||
if (item.work_type == WorkType::SCALAR_MULTIPLICATION) { | ||
if (count == work_item_number) { | ||
transcript.send_to_verifier(item.label, result); | ||
return; | ||
} | ||
++count; | ||
} | ||
} | ||
}; | ||
|
||
void flush_queue() { work_item_queue = std::vector<work_item>(); }; | ||
|
||
void add_to_queue(const work_item& item) | ||
{ | ||
// Note: currently no difference between wasm and native but may be in the future | ||
#if defined(__wasm__) | ||
work_item_queue.push_back(item); | ||
#else | ||
work_item_queue.push_back(item); | ||
#endif | ||
}; | ||
|
||
void process_queue() | ||
{ | ||
for (const auto& item : work_item_queue) { | ||
switch (item.work_type) { | ||
|
||
case WorkType::SCALAR_MULTIPLICATION: { | ||
|
||
// Run pippenger multi-scalar multiplication. | ||
auto commitment = commitment_key->commit(item.mul_scalars); | ||
|
||
transcript.send_to_verifier(item.label, commitment); | ||
|
||
break; | ||
} | ||
default: { | ||
} | ||
} | ||
} | ||
work_item_queue = std::vector<work_item>(); | ||
}; | ||
|
||
[[nodiscard]] std::vector<work_item> get_queue() const { return work_item_queue; }; | ||
|
||
private: | ||
std::shared_ptr<proof_system::plonk::proving_key> key; | ||
// TODO(luke): Consider handling all transcript interactions in the prover rather than embedding them in the queue. | ||
proof_system::honk::ProverTranscript<FF>& transcript; | ||
std::shared_ptr<CommitmentKey>& commitment_key; | ||
std::vector<work_item> work_item_queue; | ||
}; | ||
} // namespace proof_system::honk |