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 @@ -4,23 +4,23 @@
// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
// =====================

#include "../../circuit_builders/circuit_builders.hpp"
#include "../circuit_builders/circuit_builders.hpp"
#include "uint.hpp"

using namespace bb;

namespace bb::stdlib {

template <typename Builder, typename Native>
uint_plookup<Builder, Native> uint_plookup<Builder, Native>::operator+(const uint_plookup& other) const
uint<Builder, Native> uint<Builder, Native>::operator+(const uint& other) const
{

ASSERT(context == other.context || (context != nullptr && other.context == nullptr) ||
(context == nullptr && other.context != nullptr));
Builder* ctx = (context == nullptr) ? other.context : context;

if (is_constant() && other.is_constant()) {
return uint_plookup<Builder, Native>(context, (additive_constant + other.additive_constant) & MASK);
return uint<Builder, Native>(context, (additive_constant + other.additive_constant) & MASK);
}

// N.B. We assume that additive_constant is nonzero ONLY if value is constant
Expand All @@ -45,15 +45,15 @@ uint_plookup<Builder, Native> uint_plookup<Builder, Native>::operator+(const uin

ctx->create_balanced_add_gate(gate);

uint_plookup<Builder, Native> result(ctx);
uint<Builder, Native> result(ctx);
result.witness_index = gate.c;
result.witness_status = WitnessStatus::WEAK_NORMALIZED;

return result;
}

template <typename Builder, typename Native>
uint_plookup<Builder, Native> uint_plookup<Builder, Native>::operator-(const uint_plookup& other) const
uint<Builder, Native> uint<Builder, Native>::operator-(const uint& other) const
{

ASSERT(context == other.context || (context != nullptr && other.context == nullptr) ||
Expand All @@ -62,7 +62,7 @@ uint_plookup<Builder, Native> uint_plookup<Builder, Native>::operator-(const uin
Builder* ctx = (context == nullptr) ? other.context : context;

if (is_constant() && other.is_constant()) {
return uint_plookup<Builder, Native>(context, (additive_constant - other.additive_constant) & MASK);
return uint<Builder, Native>(context, (additive_constant - other.additive_constant) & MASK);
}

// N.B. We assume that additive_constant is nonzero ONLY if value is constant
Expand Down Expand Up @@ -91,20 +91,20 @@ uint_plookup<Builder, Native> uint_plookup<Builder, Native>::operator-(const uin

ctx->create_balanced_add_gate(gate);

uint_plookup<Builder, Native> result(ctx);
uint<Builder, Native> result(ctx);
result.witness_index = gate.c;
result.witness_status = WitnessStatus::WEAK_NORMALIZED;

return result;
}

template <typename Builder, typename Native>
uint_plookup<Builder, Native> uint_plookup<Builder, Native>::operator*(const uint_plookup& other) const
uint<Builder, Native> uint<Builder, Native>::operator*(const uint& other) const
{
Builder* ctx = (context == nullptr) ? other.context : context;

if (is_constant() && other.is_constant()) {
return uint_plookup<Builder, Native>(context, (additive_constant * other.additive_constant) & MASK);
return uint<Builder, Native>(context, (additive_constant * other.additive_constant) & MASK);
}
if (is_constant() && !other.is_constant()) {
return other * (*this);
Expand Down Expand Up @@ -137,7 +137,7 @@ uint_plookup<Builder, Native> uint_plookup<Builder, Native>::operator*(const uin
// discard the high bits
ctx->decompose_into_default_range(gate.d, width);

uint_plookup<Builder, Native> result(ctx);
uint<Builder, Native> result(ctx);
result.accumulators = constrain_accumulators(ctx, gate.c);
result.witness_index = gate.c;
result.witness_status = WitnessStatus::OK;
Expand All @@ -146,20 +146,19 @@ uint_plookup<Builder, Native> uint_plookup<Builder, Native>::operator*(const uin
}

template <typename Builder, typename Native>
uint_plookup<Builder, Native> uint_plookup<Builder, Native>::operator/(const uint_plookup& other) const
uint<Builder, Native> uint<Builder, Native>::operator/(const uint& other) const
{
return divmod(other).first;
}

template <typename Builder, typename Native>
uint_plookup<Builder, Native> uint_plookup<Builder, Native>::operator%(const uint_plookup& other) const
uint<Builder, Native> uint<Builder, Native>::operator%(const uint& other) const
{
return divmod(other).second;
}

template <typename Builder, typename Native>
std::pair<uint_plookup<Builder, Native>, uint_plookup<Builder, Native>> uint_plookup<Builder, Native>::divmod(
const uint_plookup& other) const
std::pair<uint<Builder, Native>, uint<Builder, Native>> uint<Builder, Native>::divmod(const uint& other) const
{
/**
* divmod: returns (a / b) and (a % b)
Expand Down Expand Up @@ -194,12 +193,12 @@ std::pair<uint_plookup<Builder, Native>, uint_plookup<Builder, Native>> uint_plo
}

if (is_constant() && other.is_constant()) {
const uint_plookup<Builder, Native> remainder(ctx, additive_constant % other.additive_constant);
const uint_plookup<Builder, Native> quotient(ctx, additive_constant / other.additive_constant);
const uint<Builder, Native> remainder(ctx, additive_constant % other.additive_constant);
const uint<Builder, Native> quotient(ctx, additive_constant / other.additive_constant);
return std::make_pair(quotient, remainder);
} else if (witness_index == other.witness_index) {
const uint_plookup<Builder, Native> remainder(context, 0);
const uint_plookup<Builder, Native> quotient(context, 1);
const uint<Builder, Native> remainder(context, 0);
const uint<Builder, Native> quotient(context, 1);
return std::make_pair(quotient, remainder);
}

Expand Down Expand Up @@ -246,25 +245,25 @@ std::pair<uint_plookup<Builder, Native>, uint_plookup<Builder, Native>> uint_plo

// validate delta is in the correct range
ctx->decompose_into_default_range(delta_idx, width);
uint_plookup<Builder, Native> quotient(ctx);
uint<Builder, Native> quotient(ctx);
quotient.witness_index = quotient_idx;
quotient.accumulators = constrain_accumulators(ctx, quotient.witness_index);
quotient.witness_status = WitnessStatus::OK;

uint_plookup<Builder, Native> remainder(ctx);
uint<Builder, Native> remainder(ctx);
remainder.witness_index = remainder_idx;
remainder.accumulators = constrain_accumulators(ctx, remainder.witness_index);
remainder.witness_status = WitnessStatus::OK;

return std::make_pair(quotient, remainder);
}
template class uint_plookup<bb::UltraCircuitBuilder, uint8_t>;
template class uint_plookup<bb::MegaCircuitBuilder, uint8_t>;
template class uint_plookup<bb::UltraCircuitBuilder, uint16_t>;
template class uint_plookup<bb::MegaCircuitBuilder, uint16_t>;
template class uint_plookup<bb::UltraCircuitBuilder, uint32_t>;
template class uint_plookup<bb::MegaCircuitBuilder, uint32_t>;
template class uint_plookup<bb::UltraCircuitBuilder, uint64_t>;
template class uint_plookup<bb::MegaCircuitBuilder, uint64_t>;
template class uint<bb::UltraCircuitBuilder, uint8_t>;
template class uint<bb::MegaCircuitBuilder, uint8_t>;
template class uint<bb::UltraCircuitBuilder, uint16_t>;
template class uint<bb::MegaCircuitBuilder, uint16_t>;
template class uint<bb::UltraCircuitBuilder, uint32_t>;
template class uint<bb::MegaCircuitBuilder, uint32_t>;
template class uint<bb::UltraCircuitBuilder, uint64_t>;
template class uint<bb::MegaCircuitBuilder, uint64_t>;

} // namespace bb::stdlib
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
// =====================

#include "../../circuit_builders/circuit_builders.hpp"
#include "../circuit_builders/circuit_builders.hpp"
#include "uint.hpp"

using namespace bb;

namespace bb::stdlib {

template <typename Builder, typename Native>
bool_t<Builder> uint_plookup<Builder, Native>::operator>(const uint_plookup& other) const
template <typename Builder, typename Native> bool_t<Builder> uint<Builder, Native>::operator>(const uint& other) const
{
Builder* ctx = (context == nullptr) ? other.context : context;

Expand Down Expand Up @@ -40,26 +39,22 @@ bool_t<Builder> uint_plookup<Builder, Native>::operator>(const uint_plookup& oth
return result;
}

template <typename Builder, typename Native>
bool_t<Builder> uint_plookup<Builder, Native>::operator<(const uint_plookup& other) const
template <typename Builder, typename Native> bool_t<Builder> uint<Builder, Native>::operator<(const uint& other) const
{
return other > *this;
}

template <typename Builder, typename Native>
bool_t<Builder> uint_plookup<Builder, Native>::operator>=(const uint_plookup& other) const
template <typename Builder, typename Native> bool_t<Builder> uint<Builder, Native>::operator>=(const uint& other) const
{
return (!(other > *this)).normalize();
}

template <typename Builder, typename Native>
bool_t<Builder> uint_plookup<Builder, Native>::operator<=(const uint_plookup& other) const
template <typename Builder, typename Native> bool_t<Builder> uint<Builder, Native>::operator<=(const uint& other) const
{
return (!(*this > other)).normalize();
}

template <typename Builder, typename Native>
bool_t<Builder> uint_plookup<Builder, Native>::operator==(const uint_plookup& other) const
template <typename Builder, typename Native> bool_t<Builder> uint<Builder, Native>::operator==(const uint& other) const
{
// casting to a field type will ensure that lhs / rhs are both normalized
const field_t<Builder> lhs = static_cast<field_t<Builder>>(*this);
Expand All @@ -68,23 +63,22 @@ bool_t<Builder> uint_plookup<Builder, Native>::operator==(const uint_plookup& ot
return (lhs == rhs).normalize();
}

template <typename Builder, typename Native>
bool_t<Builder> uint_plookup<Builder, Native>::operator!=(const uint_plookup& other) const
template <typename Builder, typename Native> bool_t<Builder> uint<Builder, Native>::operator!=(const uint& other) const
{
return (!(*this == other)).normalize();
}

template <typename Builder, typename Native> bool_t<Builder> uint_plookup<Builder, Native>::operator!() const
template <typename Builder, typename Native> bool_t<Builder> uint<Builder, Native>::operator!() const
{
return (field_t<Builder>(*this).is_zero()).normalize();
}

template class uint_plookup<bb::UltraCircuitBuilder, uint8_t>;
template class uint_plookup<bb::MegaCircuitBuilder, uint8_t>;
template class uint_plookup<bb::UltraCircuitBuilder, uint16_t>;
template class uint_plookup<bb::MegaCircuitBuilder, uint16_t>;
template class uint_plookup<bb::UltraCircuitBuilder, uint32_t>;
template class uint_plookup<bb::MegaCircuitBuilder, uint32_t>;
template class uint_plookup<bb::UltraCircuitBuilder, uint64_t>;
template class uint_plookup<bb::MegaCircuitBuilder, uint64_t>;
template class uint<bb::UltraCircuitBuilder, uint8_t>;
template class uint<bb::MegaCircuitBuilder, uint8_t>;
template class uint<bb::UltraCircuitBuilder, uint16_t>;
template class uint<bb::MegaCircuitBuilder, uint16_t>;
template class uint<bb::UltraCircuitBuilder, uint32_t>;
template class uint<bb::MegaCircuitBuilder, uint32_t>;
template class uint<bb::UltraCircuitBuilder, uint64_t>;
template class uint<bb::MegaCircuitBuilder, uint64_t>;
} // namespace bb::stdlib
Loading
Loading