Skip to content

Commit

Permalink
move zero row spec to flavor
Browse files Browse the repository at this point in the history
  • Loading branch information
ledwards2225 committed Jul 16, 2023
1 parent f9ad66b commit b5b6dba
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 34 deletions.
7 changes: 3 additions & 4 deletions cpp/src/barretenberg/honk/composer/ultra_composer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ template <UltraFlavor Flavor> void UltraComposer_<Flavor>::compute_witness(Circu
circuit_constructor.w_4.emplace_back(circuit_constructor.zero_idx);
}

auto wire_polynomials =
construct_wire_polynomials_base<Flavor>(circuit_constructor, dyadic_circuit_size, zero_wire_offset);
auto wire_polynomials = construct_wire_polynomials_base<Flavor>(circuit_constructor, dyadic_circuit_size);

proving_key->w_l = wire_polynomials[0];
proving_key->w_r = wire_polynomials[1];
Expand Down Expand Up @@ -208,9 +207,9 @@ std::shared_ptr<typename Flavor::ProvingKey> UltraComposer_<Flavor>::compute_pro

proving_key = std::make_shared<ProvingKey>(dyadic_circuit_size, num_public_inputs);

construct_selector_polynomials<Flavor>(circuit_constructor, proving_key.get(), zero_wire_offset);
construct_selector_polynomials<Flavor>(circuit_constructor, proving_key.get());

compute_honk_generalized_sigma_permutations<Flavor>(circuit_constructor, proving_key.get(), zero_wire_offset);
compute_honk_generalized_sigma_permutations<Flavor>(circuit_constructor, proving_key.get());

compute_first_and_last_lagrange_polynomials<Flavor>(proving_key.get());

Expand Down
5 changes: 3 additions & 2 deletions cpp/src/barretenberg/honk/composer/ultra_composer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ template <UltraFlavor Flavor> class UltraComposer_ {
using PCSCommitmentKey = typename PCSParams::CommitmentKey;
using PCSVerificationKey = typename PCSParams::VerificationKey;

// offset due to placing zero wires at the start of execution trace
static constexpr size_t zero_wire_offset = Flavor::zero_row ? 1 : 0;

static constexpr std::string_view NAME_STRING = "UltraHonk";
static constexpr size_t NUM_WIRES = CircuitBuilder::NUM_WIRES;
std::shared_ptr<ProvingKey> proving_key;
Expand All @@ -40,8 +43,6 @@ template <UltraFlavor Flavor> class UltraComposer_ {
size_t lookups_size = 0; // total number of lookup gates
size_t tables_size = 0; // total number of table entries
size_t num_public_inputs = 0;
// WORKTODO: set as the length of an object zero_wires in circuit constructor?
const size_t zero_wire_offset = 1; // offset due to placing zero wires at the start of execution trace

UltraComposer_()
: crs_factory_(barretenberg::srs::get_crs_factory()){};
Expand Down
3 changes: 3 additions & 0 deletions cpp/src/barretenberg/honk/flavor/standard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class Standard {
using RelationUnivariates = decltype(create_relation_univariates_container<FF, Relations>());
using RelationValues = decltype(create_relation_values_container<FF, Relations>());

// Whether or not the first row of the execution trace is reserved for 0s to enable shifts
static constexpr bool zero_row = false;

private:
/**
* @brief A base class labelling precomputed entities and (ordered) subsets of interest.
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/barretenberg/honk/flavor/standard_grumpkin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ class StandardGrumpkin {
using RelationUnivariates = decltype(create_relation_univariates_container<FF, Relations>());
using RelationValues = decltype(create_relation_values_container<FF, Relations>());

// define utilities to extend univarates from RELATION_LENGTH to MAX_RELATION_LENGTH for each Relation
// using BarycentricUtils = decltype(create_barycentric_utils<FF, Relations, MAX_RELATION_LENGTH>());
// Whether or not the first row of the execution trace is reserved for 0s to enable shifts
static constexpr bool zero_row = false;

private:
/**
Expand Down
3 changes: 3 additions & 0 deletions cpp/src/barretenberg/honk/flavor/ultra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class Ultra {
using RelationUnivariates = decltype(create_relation_univariates_container<FF, Relations>());
using RelationValues = decltype(create_relation_values_container<FF, Relations>());

// Whether or not the first row of the execution trace is reserved for 0s to enable shifts
static constexpr bool zero_row = true;

private:
template <typename DataType, typename HandleType>
/**
Expand Down
3 changes: 3 additions & 0 deletions cpp/src/barretenberg/honk/flavor/ultra_grumpkin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class UltraGrumpkin {
using RelationUnivariates = decltype(create_relation_univariates_container<FF, Relations>());
using RelationValues = decltype(create_relation_values_container<FF, Relations>());

// Whether or not the first row of the execution trace is reserved for 0s to enable shifts
static constexpr bool zero_row = true;

private:
template <typename DataType, typename HandleType>
/**
Expand Down
6 changes: 6 additions & 0 deletions cpp/src/barretenberg/plonk/flavor/flavor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,26 @@ class Standard {
using CircuitBuilder = proof_system::StandardCircuitBuilder;
using ProvingKey = plonk::proving_key;
static constexpr size_t NUM_WIRES = CircuitBuilder::NUM_WIRES;
// Whether or not the first row of the execution trace is reserved for 0s to enable shifts
static constexpr bool zero_row = false;
};

class Turbo {
public:
using CircuitBuilder = proof_system::TurboCircuitBuilder;
using ProvingKey = plonk::proving_key;
static constexpr size_t NUM_WIRES = CircuitBuilder::NUM_WIRES;
// Whether or not the first row of the execution trace is reserved for 0s to enable shifts
static constexpr bool zero_row = false;
};

class Ultra {
public:
using CircuitBuilder = proof_system::UltraCircuitBuilder;
using ProvingKey = plonk::proving_key;
static constexpr size_t NUM_WIRES = CircuitBuilder::NUM_WIRES;
// Whether or not the first row of the execution trace is reserved for 0s to enable shifts
static constexpr bool zero_row = false;

/**
* @brief Create a manifest object
Expand Down
15 changes: 7 additions & 8 deletions cpp/src/barretenberg/proof_system/composer/composer_lib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ namespace proof_system {
*/
template <typename Flavor>
void construct_selector_polynomials(const typename Flavor::CircuitBuilder& circuit_constructor,
typename Flavor::ProvingKey* proving_key,
const size_t offset = 0)
typename Flavor::ProvingKey* proving_key)
{
const size_t zero_row_offset = Flavor::zero_row ? 1 : 0;
// Offset for starting to write selectors is input offset + num public inputs
const size_t gate_offset = offset + circuit_constructor.public_inputs.size();
const size_t gate_offset = zero_row_offset + circuit_constructor.public_inputs.size();
// const size_t offset = num_public_inputs +
// TODO(#398): Loose coupling here! Would rather build up pk from arithmetization
size_t selector_idx = 0; // TODO(#391) zip
Expand Down Expand Up @@ -58,10 +58,9 @@ void construct_selector_polynomials(const typename Flavor::CircuitBuilder& circu
* */
template <typename Flavor>
std::vector<barretenberg::polynomial> construct_wire_polynomials_base(
const typename Flavor::CircuitBuilder& circuit_constructor,
const size_t dyadic_circuit_size,
const size_t offset = 0)
const typename Flavor::CircuitBuilder& circuit_constructor, const size_t dyadic_circuit_size)
{
const size_t zero_row_offset = Flavor::zero_row ? 1 : 0;
std::span<const uint32_t> public_inputs = circuit_constructor.public_inputs;
const size_t num_public_inputs = public_inputs.size();

Expand All @@ -76,7 +75,7 @@ std::vector<barretenberg::polynomial> construct_wire_polynomials_base(

// Place all public inputs at the start of the first two wires, possibly offset by some value > 0.
// All selectors at these indices are set to 0, so these values are not constrained at all.
const size_t pub_input_offset = offset; // offset at which to start writing pub inputs
const size_t pub_input_offset = zero_row_offset; // offset at which to start writing pub inputs
if (wire_idx < 2) {
for (size_t i = 0; i < num_public_inputs; ++i) {
w_lagrange[i + pub_input_offset] = circuit_constructor.get_variable(public_inputs[i]);
Expand All @@ -86,7 +85,7 @@ std::vector<barretenberg::polynomial> construct_wire_polynomials_base(

// Assign the variable values (which are pointed-to by the `w_` wire_polynomials) to the wire witness
// polynomials `poly_w_`, shifted to make room for public inputs and the specified offset (possibly 0).
const size_t gate_offset = num_public_inputs + offset; // offset at which to start writing gates
const size_t gate_offset = num_public_inputs + pub_input_offset; // offset at which to start writing gates
for (size_t i = 0; i < circuit_constructor.num_gates; ++i) {
w_lagrange[i + gate_offset] = circuit_constructor.get_variable(wire[i]);
}
Expand Down
33 changes: 15 additions & 18 deletions cpp/src/barretenberg/proof_system/composer/permutation_lib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,15 @@ namespace {
* @tparam program_width Program width
* */
template <typename Flavor>
std::vector<CyclicPermutation> compute_wire_copy_cycles(const typename Flavor::CircuitBuilder& circuit_constructor,
const size_t offset = 0)
std::vector<CyclicPermutation> compute_wire_copy_cycles(const typename Flavor::CircuitBuilder& circuit_constructor)
{
// Reference circuit constructor members
const size_t num_gates = circuit_constructor.num_gates;
std::span<const uint32_t> public_inputs = circuit_constructor.public_inputs;
const size_t num_public_inputs = public_inputs.size();

// Define offsets for placement of public inputs and gates in execution trace
const size_t pub_inputs_offset = offset;
const size_t pub_inputs_offset = Flavor::zero_row ? 1 : 0;
const size_t gates_offset = num_public_inputs + pub_inputs_offset;

// Each variable represents one cycle
Expand All @@ -88,10 +87,10 @@ std::vector<CyclicPermutation> compute_wire_copy_cycles(const typename Flavor::C

// Add 'offset' many rows of wires to the zero index cycle. This will be used to ensure there are offset-many rows
// of zeros at the start of the execution trace.
for (size_t gate_idx = 0; gate_idx < offset; ++gate_idx) {
if (Flavor::zero_row) {
for (size_t wire_idx = 0; wire_idx < Flavor::NUM_WIRES; ++wire_idx) {
const auto wire_index = static_cast<uint32_t>(wire_idx);
const auto gate_index = static_cast<uint32_t>(gate_idx);
const uint32_t gate_index = 0; // place zeros at 0th index
const uint32_t zero_idx = circuit_constructor.zero_idx; // index of constant zero in variables
copy_cycles[zero_idx].emplace_back(cycle_node{ wire_index, gate_index });
}
Expand Down Expand Up @@ -151,12 +150,10 @@ std::vector<CyclicPermutation> compute_wire_copy_cycles(const typename Flavor::C
*/
template <typename Flavor, bool generalized>
PermutationMapping<Flavor::NUM_WIRES> compute_permutation_mapping(
const typename Flavor::CircuitBuilder& circuit_constructor,
typename Flavor::ProvingKey* proving_key,
const size_t offset = 0)
const typename Flavor::CircuitBuilder& circuit_constructor, typename Flavor::ProvingKey* proving_key)
{
// Compute wire copy cycles (cycles of permutations)
auto wire_copy_cycles = compute_wire_copy_cycles<Flavor>(circuit_constructor, offset);
auto wire_copy_cycles = compute_wire_copy_cycles<Flavor>(circuit_constructor);

PermutationMapping<Flavor::NUM_WIRES> mapping;

Expand Down Expand Up @@ -227,12 +224,13 @@ PermutationMapping<Flavor::NUM_WIRES> compute_permutation_mapping(
const auto num_public_inputs = static_cast<uint32_t>(circuit_constructor.public_inputs.size());

// The public inputs are placed at the top of the execution trace, potentially offset by some value.
// WORKTODO: we're now offsetting the sigmas; is there a corresponding thing for ids? I think not.
for (size_t i = offset; i < num_public_inputs + offset; ++i) {
mapping.sigmas[0][i].row_index = static_cast<uint32_t>(i);
mapping.sigmas[0][i].column_index = 0;
mapping.sigmas[0][i].is_public_input = true;
if (mapping.sigmas[0][i].is_tag) {
const size_t zero_row_offset = Flavor::zero_row ? 1 : 0;
for (size_t i = 0; i < num_public_inputs; ++i) {
size_t idx = i + zero_row_offset;
mapping.sigmas[0][idx].row_index = static_cast<uint32_t>(idx);
mapping.sigmas[0][idx].column_index = 0;
mapping.sigmas[0][idx].is_public_input = true;
if (mapping.sigmas[0][idx].is_tag) {
std::cerr << "MAPPING IS BOTH A TAG AND A PUBLIC INPUT" << std::endl;
}
}
Expand Down Expand Up @@ -535,10 +533,9 @@ void compute_plonk_generalized_sigma_permutations(const typename Flavor::Circuit
*/
template <typename Flavor>
void compute_honk_generalized_sigma_permutations(const typename Flavor::CircuitBuilder& circuit_constructor,
typename Flavor::ProvingKey* proving_key,
const size_t offset = 0)
typename Flavor::ProvingKey* proving_key)
{
auto mapping = compute_permutation_mapping<Flavor, true>(circuit_constructor, proving_key, offset);
auto mapping = compute_permutation_mapping<Flavor, true>(circuit_constructor, proving_key);

// Compute Honk-style sigma and ID polynomials from the corresponding mappings
compute_honk_style_permutation_lagrange_polynomials_from_mapping<Flavor>(
Expand Down

0 comments on commit b5b6dba

Please sign in to comment.