Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -700,9 +700,9 @@ template <class... Ts> struct overloaded : Ts... {
};
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

inline std::ostream& operator<<(std::ostream& os, const MemoryTag& tag)
inline std::ostream& operator<<(std::ostream& os, const MemoryTagWrapper& tag)
{
os << std::to_string(tag);
os << tag.value;
return os;
}

Expand Down
24 changes: 12 additions & 12 deletions barretenberg/cpp/src/barretenberg/common/assert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ struct AssertGuard {
#define BB_ASSERT_EQ(actual, expected, ...) \
do { \
BB_BENCH_ASSERT("BB_ASSERT_EQ" #actual " == " #expected); \
auto _actual = (actual); \
auto _expected = (expected); \
const auto& _actual = (actual); \
const auto& _expected = (expected); \
if (!(BB_LIKELY(_actual == _expected))) { \
std::ostringstream oss; \
oss << "Assertion failed: (" #actual " == " #expected ")\n"; \
Expand All @@ -108,8 +108,8 @@ struct AssertGuard {
#define BB_ASSERT_NEQ(actual, expected, ...) \
do { \
BB_BENCH_ASSERT("BB_ASSERT_NEQ" #actual " != " #expected); \
auto _actual = (actual); \
auto _expected = (expected); \
const auto& _actual = (actual); \
const auto& _expected = (expected); \
if (!(BB_LIKELY(_actual != _expected))) { \
std::ostringstream oss; \
oss << "Assertion failed: (" #actual " != " #expected ")\n"; \
Expand All @@ -123,8 +123,8 @@ struct AssertGuard {
#define BB_ASSERT_GT(left, right, ...) \
do { \
BB_BENCH_ASSERT("BB_ASSERT_GT" #left " > " #right); \
auto _left = (left); \
auto _right = (right); \
const auto& _left = (left); \
const auto& _right = (right); \
if (!(BB_LIKELY(_left > _right))) { \
std::ostringstream oss; \
oss << "Assertion failed: (" #left " > " #right ")\n"; \
Expand All @@ -138,8 +138,8 @@ struct AssertGuard {
#define BB_ASSERT_GTE(left, right, ...) \
do { \
BB_BENCH_ASSERT("BB_ASSERT_GTE" #left " >= " #right); \
auto _left = (left); \
auto _right = (right); \
const auto& _left = (left); \
const auto& _right = (right); \
if (!(BB_LIKELY(_left >= _right))) { \
std::ostringstream oss; \
oss << "Assertion failed: (" #left " >= " #right ")\n"; \
Expand All @@ -153,8 +153,8 @@ struct AssertGuard {
#define BB_ASSERT_LT(left, right, ...) \
do { \
BB_BENCH_ASSERT("BB_ASSERT_LT" #left " < " #right); \
auto _left = (left); \
auto _right = (right); \
const auto& _left = (left); \
const auto& _right = (right); \
if (!(BB_LIKELY(_left < _right))) { \
std::ostringstream oss; \
oss << "Assertion failed: (" #left " < " #right ")\n"; \
Expand All @@ -168,8 +168,8 @@ struct AssertGuard {
#define BB_ASSERT_LTE(left, right, ...) \
do { \
BB_BENCH_ASSERT("BB_ASSERT_LTE" #left " <= " #right); \
auto _left = (left); \
auto _right = (right); \
const auto& _left = (left); \
const auto& _right = (right); \
if (!(BB_LIKELY(_left <= _right))) { \
std::ostringstream oss; \
oss << "Assertion failed: (" #left " <= " #right ")\n"; \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ inline bool is_protocol_contract_address(const AztecAddress& address)
inline std::optional<AztecAddress> get_derived_address(const ProtocolContracts& protocol_contracts,
const AztecAddress& canonical_address)
{
assert(is_protocol_contract_address(canonical_address) && "Protocol contract canonical address out of bounds");
BB_ASSERT(is_protocol_contract_address(canonical_address), "Protocol contract canonical address out of bounds");
AztecAddress derived_address =
protocol_contracts.derived_addresses.at(static_cast<uint32_t>(canonical_address) - 1);
if (derived_address.is_zero()) {
Expand Down
10 changes: 5 additions & 5 deletions barretenberg/cpp/src/barretenberg/vm2/common/tagged_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ uint8_t get_tag_bits(ValueTag tag)
return 0; // It is more useful for this to be 0 in the circuit
}

assert(false && "Invalid tag");
__builtin_unreachable();
return 0;
}

Expand All @@ -197,7 +197,7 @@ uint8_t get_tag_bytes(ValueTag tag)
return 0; // It is more useful for this to be 0 in the circuit
}

assert(false && "Invalid tag");
__builtin_unreachable();
return 0;
}

Expand All @@ -215,7 +215,7 @@ uint256_t get_tag_max_value(ValueTag tag)
return FF::modulus - 1;
}

assert(false && "Invalid tag");
__builtin_unreachable();
return 0;
}

Expand All @@ -226,9 +226,9 @@ TaggedValue::TaggedValue(TaggedValue::value_type value_)

TaggedValue TaggedValue::from_tag(ValueTag tag, FF value)
{
auto assert_bounds = [](const FF& value, uint8_t bits) {
auto assert_bounds = [tag](const FF& value, uint8_t bits) {
if (static_cast<uint256_t>(value).get_msb() >= bits) {
throw std::runtime_error("Value out of bounds");
throw ValueOutOfBounds(format("Value: ", value, " is out of bounds for tag: ", tag));
}
};

Expand Down
32 changes: 32 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm2/common/tagged_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cstdint>
#include <functional>
#include <ostream>
#include <string>
#include <variant>

Expand Down Expand Up @@ -38,6 +39,13 @@ class DivisionByZero : public TaggedValueException {
{}
};

class ValueOutOfBounds : public TaggedValueException {
public:
ValueOutOfBounds(const std::string& msg)
: TaggedValueException("Value out of bounds: " + msg)
{}
};

class CastException : public TaggedValueException {
public:
CastException(const std::string& msg)
Expand All @@ -56,6 +64,30 @@ enum class ValueTag {
MAX = U128,
};

inline std::ostream& operator<<(std::ostream& os, ValueTag tag)
{
switch (tag) {
case ValueTag::FF:
return os << "FF";
case ValueTag::U1:
return os << "U1";
case ValueTag::U8:
return os << "U8";
case ValueTag::U16:
return os << "U16";
case ValueTag::U32:
return os << "U32";
case ValueTag::U64:
return os << "U64";
case ValueTag::U128:
return os << "U128";
default:
return os << "Unknown";
}

__builtin_unreachable();
}

template <typename T> ValueTag tag_for_type()
{
if constexpr (std::is_same_v<T, FF>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const std::array<std::vector<uint8_t>, 257>& get_p_limbs_per_radix()

size_t get_p_limbs_per_radix_size(size_t radix)
{
assert(radix <= 256);
BB_ASSERT_LTE(radix, static_cast<decltype(radix)>(256), "Radix out of bounds");
return p_limbs_per_radix_sizes[radix];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ AvmProver::ProverPolynomials compute_polynomials(tracegen::TraceContainer& trace
// Polynomials that will be shifted need special care.
AVM_TRACK_TIME("proving/init_polys_to_be_shifted", ({
auto to_be_shifted = polys.get_to_be_shifted();
assert(to_be_shifted.size() == TO_BE_SHIFTED_COLUMNS_ARRAY.size());
BB_ASSERT_EQ(to_be_shifted.size(),
TO_BE_SHIFTED_COLUMNS_ARRAY.size(),
"To be shifted columns array size mismatch");

// NOTE: we can't parallelize because Polynomial construction uses parallelism.
for (size_t i = 0; i < to_be_shifted.size(); i++) {
Expand Down Expand Up @@ -98,7 +100,9 @@ void resize_inverses(AvmFlavor::ProverPolynomials& prover_polynomials,

const size_t num_rows = std::max<size_t>(src_selector.end_index(), dst_selector.end_index());
inverse_polynomial = AvmProver::Polynomial::create_non_parallel_zero_init(num_rows, MAX_AVM_TRACE_SIZE);
assert(prover_polynomials.get(static_cast<ColumnAndShifts>(inverses_col)).size() == num_rows);
BB_ASSERT_EQ(prover_polynomials.get(static_cast<ColumnAndShifts>(inverses_col)).size(),
num_rows,
"Inverse polynomial size mismatch");
}

std::shared_ptr<AvmProver::ProvingKey> proving_key_from_polynomials(AvmProver::ProverPolynomials& polynomials)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cassert>
#include <vector>

#include "barretenberg/common/assert.hpp"
#include "barretenberg/vm2/common/set.hpp"

namespace bb::avm2::simulation {
Expand Down Expand Up @@ -76,7 +77,7 @@ template <typename Event> class OneShotEventEmitter : public EventEmitterInterfa
virtual ~OneShotEventEmitter() = default;
void emit(Event&& event) override
{
assert(!has_emitted);
BB_ASSERT(!has_emitted, "Event already emitted");
has_emitted = true;
this->event = event;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void AddressDerivation::assert_derivation(const AztecAddress& address, const Con
EmbeddedCurvePoint preaddress_public_key = ecc.scalar_mul(EmbeddedCurvePoint::one(), preaddress);
EmbeddedCurvePoint address_point = ecc.add(preaddress_public_key, instance.public_keys.incoming_viewing_key);

assert(address == address_point.x());
BB_ASSERT_EQ(address, address_point.x(), "Address derivation mismatch");

// Cache this derivation so we don't repeat it
cached_derivations.insert(address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ std::vector<Operand> Addressing::resolve(const Instruction& instruction, MemoryI

// This represents either: (1) wrong info in the spec, or (2) a wrong witgen deserialization.
// Therefore, it is not an error the circuit should be able to prove.
assert(spec.num_addresses <= instruction.operands.size());
BB_ASSERT_LTE(spec.num_addresses, instruction.operands.size(), "Spec num addresses out of bounds");

// Check if there is any relative address.
bool has_relative_address = false;
Expand Down Expand Up @@ -86,7 +86,9 @@ std::vector<Operand> Addressing::resolve(const Instruction& instruction, MemoryI
// This should be guaranteed by instruction fetching and the wire format.
// The operand must fit in a MemoryAddress but does not need to be of the right tag.
// For instance, a 16-bit operand can be cast to a MemoryAddress and fit.
assert(FF(static_cast<MemoryAddress>(instruction.operands[i].as_ff())) == instruction.operands[i].as_ff());
// NOTE: Only asserting in debug builds because these convertions are in the hot path.
BB_ASSERT_DEBUG(FF(static_cast<MemoryAddress>(instruction.operands[i].as_ff())) ==
instruction.operands[i].as_ff());

// Guarantees at this point:
// - original operand is a valid address IF interpreted as a MemoryAddress.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ void BytecodeHasher::assert_public_bytecode_commitment(const BytecodeId& bytecod
inputs.insert(inputs.end(), bytecode_as_fields.begin(), bytecode_as_fields.end());

FF hash = hasher.hash(inputs);
assert(hash == public_bytecode_commitment);
// To please the compiler.
(void)hash;
(void)public_bytecode_commitment;
// This will throw an unexpected exception if it fails.
BB_ASSERT_EQ(hash, public_bytecode_commitment, "Public bytecode commitment hash mismatch");

events.emit({ .bytecode_id = bytecode_id,
.bytecode_length = bytecode_length_in_bytes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ BytecodeId TxBytecodeManager::get_bytecode(const AztecAddress& address)
std::optional<ContractClass> maybe_klass = contract_db.get_contract_class(current_class_id);
// Note: we don't need to silo and check the class id because the deployer contract guarantees
// that if a contract instance exists, the class has been registered.
assert(maybe_klass.has_value());
BB_ASSERT(maybe_klass.has_value(), "Contract class not found");
auto& klass = maybe_klass.value();
retrieval_event.contract_class = klass; // WARNING: this class has the whole bytecode.

// Bytecode hashing and decomposition, deduplicated by bytecode_id (commitment)
std::optional<FF> maybe_bytecode_commitment = contract_db.get_bytecode_commitment(current_class_id);
// If we reach this point, class ID and instance both exist which means bytecode commitment must exist.
assert(maybe_bytecode_commitment.has_value());
BB_ASSERT(maybe_bytecode_commitment.has_value(), "Bytecode commitment not found");
BytecodeId bytecode_id = maybe_bytecode_commitment.value();
retrieval_event.bytecode_id = bytecode_id;
debug("Bytecode for ", address, " successfully retrieved!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ void ClassIdDerivation::assert_derivation(const ContractClassWithCommitment& kla
klass.artifact_hash,
klass.private_functions_root,
klass.public_bytecode_commitment });
(void)computed_class_id; // Silence unused variable warning when assert is stripped out
assert(computed_class_id == klass.id);
// This will throw an unexpected exception if it fails.
BB_ASSERT_EQ(computed_class_id, klass.id, "Computed class ID mismatch");

// Cache this derivation so we don't repeat it
cached_derivations.insert(klass.id);
Expand Down
Loading
Loading