Skip to content

Commit

Permalink
comments and naming in response to review
Browse files Browse the repository at this point in the history
  • Loading branch information
ledwards2225 committed Jul 22, 2024
1 parent 336e99f commit 05cd650
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,19 +169,19 @@ template <typename Builder> bool UltraCircuitChecker::check_databus_read(auto& v

// Determine the type of read based on selector values
bool is_calldata_read = (values.q_l == 1);
bool is_calldata_2_read = (values.q_r == 1);
bool is_secondary_calldata_read = (values.q_r == 1);
bool is_return_data_read = (values.q_o == 1);
ASSERT(is_calldata_read || is_calldata_2_read || is_return_data_read);
ASSERT(is_calldata_read || is_secondary_calldata_read || is_return_data_read);

// Check that the claimed value is present in the calldata/return data at the corresponding index
FF bus_value;
if (is_calldata_read) {
auto calldata = builder.get_calldata();
bus_value = builder.get_variable(calldata[raw_read_idx]);
}
if (is_calldata_2_read) {
auto calldata_2 = builder.get_calldata_2();
bus_value = builder.get_variable(calldata_2[raw_read_idx]);
if (is_secondary_calldata_read) {
auto secondary_calldata = builder.get_secondary_calldata();
bus_value = builder.get_variable(secondary_calldata[raw_read_idx]);
}
if (is_return_data_read) {
auto return_data = builder.get_return_data();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,41 +49,51 @@ template <typename FF_> class DatabusLookupRelationImpl {
static constexpr size_t LENGTH = 5; // 1 + polynomial degree of this relation
static constexpr size_t NUM_BUS_COLUMNS = 3; // calldata, return data

static constexpr size_t INVERSE_SUBREL_LENGTH = 5; // deg + 1 of inverse correctness subrelation
static constexpr size_t LOOKUP_SUBREL_LENGTH = 5; // deg + 1 of log-deriv lookup subrelation

// Note: Inverse correctness subrelations are actually LENGTH-1; taking advantage would require additional work
static constexpr std::array<size_t, NUM_BUS_COLUMNS * 2> SUBRELATION_PARTIAL_LENGTHS{
LENGTH, // inverse polynomial correctness subrelation
LENGTH, // log-derivative lookup argument subrelation
LENGTH, // inverse polynomial correctness subrelation
LENGTH, // log-derivative lookup argument subrelation
LENGTH, // inverse polynomial correctness subrelation
LENGTH // log-derivative lookup argument subrelation
INVERSE_SUBREL_LENGTH, // inverse polynomial correctness subrelation (bus_idx 0)
LOOKUP_SUBREL_LENGTH, // log-derivative lookup argument subrelation (bus_idx 0)
INVERSE_SUBREL_LENGTH, // inverse polynomial correctness subrelation (bus_idx 1)
LOOKUP_SUBREL_LENGTH, // log-derivative lookup argument subrelation (bus_idx 1)
INVERSE_SUBREL_LENGTH, // inverse polynomial correctness subrelation (bus_idx 2)
LOOKUP_SUBREL_LENGTH // log-derivative lookup argument subrelation (bus_idx 2)
};

static constexpr size_t INVERSE_SUBREL_WITNESS_DEGREE = 4; // witness degree of inverse correctness subrelation
static constexpr size_t LOOKUP_SUBREL_WITNESS_DEGREE = 4; // witness degree of log-deriv lookup subrelation

/**
* @brief For ZK-Flavors: Upper bound on the degrees of subrelations considered as polynomials only in witness
polynomials,
* i.e. all selectors and public polynomials are treated as constants. The subrelation witness degree does not
* exceed the subrelation partial degree, which is given by LENGTH - 1 in this case.
* polynomials, i.e. all selectors and public polynomials are treated as constants. The subrelation witness degree
* does not exceed the subrelation partial degree, which is given by LENGTH - 1 in this case.
*/
static constexpr std::array<size_t, NUM_BUS_COLUMNS * 2> SUBRELATION_WITNESS_DEGREES{
LENGTH - 1, // inverse polynomial correctness subrelation
LENGTH - 1, // log-derivative lookup argument subrelation
LENGTH - 1, // inverse polynomial correctness subrelation
LENGTH - 1, // log-derivative lookup argument subrelation
LENGTH - 1, // inverse polynomial correctness subrelation
LENGTH - 1 // log-derivative lookup argument subrelation
INVERSE_SUBREL_WITNESS_DEGREE, // inverse polynomial correctness subrelation (bus_idx 0)
LOOKUP_SUBREL_WITNESS_DEGREE, // log-derivative lookup argument subrelation (bus_idx 0)
INVERSE_SUBREL_WITNESS_DEGREE, // inverse polynomial correctness subrelation (bus_idx 1)
LOOKUP_SUBREL_WITNESS_DEGREE, // log-derivative lookup argument subrelation (bus_idx 1)
INVERSE_SUBREL_WITNESS_DEGREE, // inverse polynomial correctness subrelation (bus_idx 2)
LOOKUP_SUBREL_WITNESS_DEGREE // log-derivative lookup argument subrelation (bus_idx 2)
};

static constexpr bool INVERSE_SUBREL_LIN_INDEPENDENT = true; // to be satisfied independently at each row
static constexpr bool LOOKUP_SUBREL_LIN_INDEPENDENT = false; // to be satisfied as a sum across all rows

// The lookup subrelations are "linearly dependent" in the sense that they establish the value of a sum across the
// entire execution trace rather than a per-row identity.
static constexpr std::array<bool, NUM_BUS_COLUMNS* 2> SUBRELATION_LINEARLY_INDEPENDENT = { true, false, true,
false, true, false };
static constexpr std::array<bool, NUM_BUS_COLUMNS* 2> SUBRELATION_LINEARLY_INDEPENDENT = {
INVERSE_SUBREL_LIN_INDEPENDENT, LOOKUP_SUBREL_LIN_INDEPENDENT, INVERSE_SUBREL_LIN_INDEPENDENT,
LOOKUP_SUBREL_LIN_INDEPENDENT, INVERSE_SUBREL_LIN_INDEPENDENT, LOOKUP_SUBREL_LIN_INDEPENDENT
};

template <typename AllEntities> inline static bool skip([[maybe_unused]] const AllEntities& in)
{
// Ensure the input does not contain a read gate or data that is being read
return in.q_busread.is_zero() && in.calldata_read_counts.is_zero() && in.calldata_2_read_counts.is_zero() &&
in.return_data_read_counts.is_zero();
return in.q_busread.is_zero() && in.calldata_read_counts.is_zero() &&
in.secondary_calldata_read_counts.is_zero() && in.return_data_read_counts.is_zero();
}

// Interface for easy access of databus components by column (bus_idx)
Expand All @@ -99,14 +109,14 @@ polynomials,
static auto& read_tags(const AllEntities& in) { return in.calldata_read_tags; }
};

// Specialization for calldata_2 (bus_idx = 1)
// Specialization for secondary_calldata (bus_idx = 1)
template <typename AllEntities> struct BusData</*bus_idx=*/1, AllEntities> {
static auto& values(const AllEntities& in) { return in.calldata_2; }
static auto& values(const AllEntities& in) { return in.secondary_calldata; }
static auto& selector(const AllEntities& in) { return in.q_r; }
static auto& inverses(AllEntities& in) { return in.calldata_2_inverses; }
static auto& inverses(const AllEntities& in) { return in.calldata_2_inverses; } // const version
static auto& read_counts(const AllEntities& in) { return in.calldata_2_read_counts; }
static auto& read_tags(const AllEntities& in) { return in.calldata_2_read_tags; }
static auto& inverses(AllEntities& in) { return in.secondary_calldata_inverses; }
static auto& inverses(const AllEntities& in) { return in.secondary_calldata_inverses; } // const version
static auto& read_counts(const AllEntities& in) { return in.secondary_calldata_read_counts; }
static auto& read_tags(const AllEntities& in) { return in.secondary_calldata_read_tags; }
};

// Specialization for return data (bus_idx = 2)
Expand Down Expand Up @@ -231,9 +241,9 @@ polynomials,
is_read = q_busread == 1 && polynomials.q_l[i] == 1;
nonzero_read_count = polynomials.calldata_read_counts[i] > 0;
}
if constexpr (bus_idx == 1) { // calldata_2
if constexpr (bus_idx == 1) { // secondary_calldata
is_read = q_busread == 1 && polynomials.q_r[i] == 1;
nonzero_read_count = polynomials.calldata_2_read_counts[i] > 0;
nonzero_read_count = polynomials.secondary_calldata_read_counts[i] > 0;
}
if constexpr (bus_idx == 2) { // return data
is_read = q_busread == 1 && polynomials.q_o[i] == 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ template <typename Builder> class databus {
public:
// The columns of the DataBus
bus_vector calldata{ BusId::CALLDATA };
bus_vector calldata_2{ BusId::CALLDATA_2 };
bus_vector secondary_calldata{ BusId::SECONDARY_CALLDATA };
bus_vector return_data{ BusId::RETURNDATA };
};
} // namespace bb::stdlib
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ struct BusVector {
*
*/
using DataBus = std::array<BusVector, 3>;
enum class BusId { CALLDATA, CALLDATA_2, RETURNDATA };
enum class BusId { CALLDATA, SECONDARY_CALLDATA, RETURNDATA };

} // namespace bb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ template <typename FF> void MegaCircuitBuilder_<FF>::add_gates_to_ensure_all_pol
auto read_idx = this->add_variable(raw_read_idx);
read_calldata(read_idx);

// Create an arbitrary calldata_2 read gate
add_public_calldata_2(this->add_variable(25)); // ensure there is at least one entry in calldata_2
raw_read_idx = static_cast<uint32_t>(get_calldata_2().size()) - 1; // read data that was just added
// Create an arbitrary secondary_calldata read gate
add_public_secondary_calldata(this->add_variable(25)); // ensure there is at least one entry in secondary_calldata
raw_read_idx = static_cast<uint32_t>(get_secondary_calldata().size()) - 1; // read data that was just added
read_idx = this->add_variable(raw_read_idx);
read_calldata_2(read_idx);
read_secondary_calldata(read_idx);

// Create an arbitrary return data read gate
add_public_return_data(this->add_variable(17)); // ensure there is at least one entry in return data
Expand Down Expand Up @@ -247,7 +247,7 @@ template <typename FF> void MegaCircuitBuilder_<FF>::apply_databus_selectors(con
block.q_3().emplace_back(0);
break;
}
case BusId::CALLDATA_2: {
case BusId::SECONDARY_CALLDATA: {
block.q_1().emplace_back(0);
block.q_2().emplace_back(1);
block.q_3().emplace_back(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,14 @@ template <typename FF> class MegaCircuitBuilder_ : public UltraCircuitBuilder_<M
void add_public_calldata(const uint32_t& in) { return append_to_bus_vector(BusId::CALLDATA, in); }

/**
* @brief Add a witness variable to the public calldata.
* @brief Add a witness variable to secondary_calldata.
* @details In practice this is used in aztec by the kernel circuit to recieve output from a function circuit
*
*/
void add_public_calldata_2(const uint32_t& in) { return append_to_bus_vector(BusId::CALLDATA_2, in); }
void add_public_secondary_calldata(const uint32_t& in)
{
return append_to_bus_vector(BusId::SECONDARY_CALLDATA, in);
}

/**
* @brief Add a witness variable to the public return_data.
Expand All @@ -197,14 +201,14 @@ template <typename FF> class MegaCircuitBuilder_ : public UltraCircuitBuilder_<M
};

/**
* @brief Read from calldata_2 and create a corresponding databus read gate
* @brief Read from secondary_calldata and create a corresponding databus read gate
*
* @param read_idx_witness_idx Witness index for the calldata_2 read index
* @param read_idx_witness_idx Witness index for the secondary_calldata read index
* @return uint32_t Witness index for the result of the read
*/
uint32_t read_calldata_2(const uint32_t& read_idx_witness_idx)
uint32_t read_secondary_calldata(const uint32_t& read_idx_witness_idx)
{
return read_bus_vector(BusId::CALLDATA_2, read_idx_witness_idx);
return read_bus_vector(BusId::SECONDARY_CALLDATA, read_idx_witness_idx);
};

/**
Expand All @@ -224,7 +228,7 @@ template <typename FF> class MegaCircuitBuilder_ : public UltraCircuitBuilder_<M
}

const BusVector& get_calldata() { return databus[static_cast<size_t>(BusId::CALLDATA)]; }
const BusVector& get_calldata_2() { return databus[static_cast<size_t>(BusId::CALLDATA_2)]; }
const BusVector& get_secondary_calldata() { return databus[static_cast<size_t>(BusId::SECONDARY_CALLDATA)]; }
const BusVector& get_return_data() { return databus[static_cast<size_t>(BusId::RETURNDATA)]; }

void create_poseidon2_external_gate(const poseidon2_external_gate_<FF>& in);
Expand Down
Loading

0 comments on commit 05cd650

Please sign in to comment.