Skip to content

Commit

Permalink
resolve review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
maramihali committed Jul 31, 2023
1 parent 9343988 commit e106375
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 31 deletions.
2 changes: 1 addition & 1 deletion circuits/cpp/barretenberg/cpp/scripts/run_tests
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ docker run --rm -t $IMAGE_URI /bin/sh -c "\
cd /usr/src/barretenberg/cpp/srs_db; \
./download_ignition.sh $NUM_TRANSCRIPTS; \
cd /usr/src/barretenberg/cpp/build; \
./bin/grumpkin_srs_gen 1048576; \
./bin/grumpkin_srs_gen 1048576; \
for BIN in $TESTS; do ./bin/\$BIN $@; done"
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ template <typename Curve> class ProverCrs {
;

/**
* Returns the monomial points in a form to be consumed by scalar_multiplication pippenger algorithm.
* @brief Returns the monomial points in a form to be consumed by scalar_multiplication pippenger algorithm.
*/
virtual typename Curve::AffineElement* get_monomial_points() = 0;
virtual size_t get_monomial_size() const = 0;
Expand All @@ -36,16 +36,32 @@ template <> class VerifierCrs<curve::BN254> {

public:
virtual Curve::G2AffineElement get_g2x() const = 0;
/**
* @brief As the G_2 element of the CRS is fixed, we can precompute the operations performed on it during the
* pairing algorithm to optimise pairing computations.
*/
virtual barretenberg::pairing::miller_lines const* get_precomputed_g2_lines() const = 0;
/**
* @brief Returns the first G_1 element from the CRS, used by the Shplonk verifier to compute the final
* commtiment.
*/
virtual Curve::AffineElement get_first_g1() const = 0;
};

template <> class VerifierCrs<curve::Grumpkin> {
using Curve = curve::Grumpkin;

public:
/**
* @brief Returns the G_1 elements in the CRS after the pippenger point table has been applied on them
*
*/
virtual Curve::AffineElement* get_monomial_points() const = 0;
virtual size_t get_monomial_size() const = 0;
/**
* @brief Returns the first G_1 element from the CRS, used by the Shplonk verifier to compute the final
* commtiment.
*/
virtual Curve::AffineElement get_first_g1() const = 0;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ void init_grumpkin_crs_factory(std::string crs_path)
std::shared_ptr<factories::CrsFactory<curve::BN254>> get_crs_factory()
{
if (!crs_factory) {
throw_or_abort("You need vto initalize the global CRS with a call to init_crs_factory(...)!");
throw_or_abort("You need to initalize the global CRS with a call to init_crs_factory(...)!");
}
return crs_factory;
}

std::shared_ptr<factories::CrsFactory<curve::Grumpkin>> get_grumpkin_crs_factory()
{
if (!grumpkin_crs_factory) {
throw_or_abort("You need vto initalize the global CRS with a call to init_grumpkin_crs_factory(...)!");
throw_or_abort("You need to initalize the global CRS with a call to init_grumpkin_crs_factory(...)!");
}
return grumpkin_crs_factory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace stdlib {
template <typename Composer, typename Native>
uint_plookup<Composer, Native> uint_plookup<Composer, Native>::operator+(const uint_plookup& other) const
{

ASSERT(context == other.context || (context != nullptr && other.context == nullptr) ||
(context == nullptr && other.context != nullptr));
Composer* ctx = (context == nullptr) ? other.context : context;
Expand All @@ -26,15 +27,15 @@ uint_plookup<Composer, Native> uint_plookup<Composer, Native>::operator+(const u
const uint256_t overflow = sum >> width;
const uint256_t remainder = sum & MASK;

const add_quad_<typename Composer::FF> gate{
const add_quad_<FF> gate{
is_constant() ? ctx->zero_idx : witness_index,
other.is_constant() ? ctx->zero_idx : other.witness_index,
ctx->add_variable(remainder),
ctx->add_variable(overflow),
fr::one(),
fr::one(),
fr::neg_one(),
-fr(CIRCUIT_UINT_MAX_PLUS_ONE),
FF::one(),
FF::one(),
FF::neg_one(),
-FF(CIRCUIT_UINT_MAX_PLUS_ONE),
constants,
};

Expand All @@ -50,6 +51,7 @@ uint_plookup<Composer, Native> uint_plookup<Composer, Native>::operator+(const u
template <typename Composer, typename Native>
uint_plookup<Composer, Native> uint_plookup<Composer, Native>::operator-(const uint_plookup& other) const
{

ASSERT(context == other.context || (context != nullptr && other.context == nullptr) ||
(context == nullptr && other.context != nullptr));

Expand All @@ -71,15 +73,15 @@ uint_plookup<Composer, Native> uint_plookup<Composer, Native>::operator-(const u
const uint256_t overflow = difference >> width;
const uint256_t remainder = difference & MASK;

const add_quad_<typename Composer::FF> gate{
const add_quad_<FF> gate{
lhs_idx,
rhs_idx,
ctx->add_variable(remainder),
ctx->add_variable(overflow),
fr::one(),
fr::neg_one(),
fr::neg_one(),
-fr(CIRCUIT_UINT_MAX_PLUS_ONE),
FF::one(),
FF::neg_one(),
FF::neg_one(),
-FF(CIRCUIT_UINT_MAX_PLUS_ONE),
CIRCUIT_UINT_MAX_PLUS_ONE + constant_term,
};

Expand All @@ -95,6 +97,7 @@ uint_plookup<Composer, Native> uint_plookup<Composer, Native>::operator-(const u
template <typename Composer, typename Native>
uint_plookup<Composer, Native> uint_plookup<Composer, Native>::operator*(const uint_plookup& other) const
{

Composer* ctx = (context == nullptr) ? other.context : context;

if (is_constant() && other.is_constant()) {
Expand All @@ -113,16 +116,16 @@ uint_plookup<Composer, Native> uint_plookup<Composer, Native>::operator*(const u
const uint256_t overflow = product >> width;
const uint256_t remainder = product & MASK;

const mul_quad_<fr> gate{
const mul_quad_<FF> gate{
witness_index,
rhs_idx,
ctx->add_variable(remainder),
ctx->add_variable(overflow),
fr::one(),
FF::one(),
other.additive_constant,
additive_constant,
fr::neg_one(),
-fr(CIRCUIT_UINT_MAX_PLUS_ONE),
FF::neg_one(),
-FF(CIRCUIT_UINT_MAX_PLUS_ONE),
0,
};

Expand Down Expand Up @@ -180,12 +183,12 @@ std::pair<uint_plookup<Composer, Native>, uint_plookup<Composer, Native>> uint_p
// We want to force the divisor to be non-zero, as this is an error state
if (other.is_constant() && other.get_value() == 0) {
// TODO: should have an actual error handler!
const uint32_t one = ctx->add_variable(fr::one());
ctx->assert_equal_constant(one, fr::zero());
const uint32_t one = ctx->add_variable(FF::one());
ctx->assert_equal_constant(one, FF::zero());
ctx->failure("plookup_arithmetic: divide by zero!");
} else if (!other.is_constant()) {
const bool_t<Composer> is_divisor_zero = field_t<Composer>(other).is_zero();
ctx->assert_equal_constant(is_divisor_zero.witness_index, fr::zero(), "plookup_arithmetic: divide by zero!");
ctx->assert_equal_constant(is_divisor_zero.witness_index, FF::zero(), "plookup_arithmetic: divide by zero!");
}

if (is_constant() && other.is_constant()) {
Expand All @@ -210,31 +213,31 @@ std::pair<uint_plookup<Composer, Native>, uint_plookup<Composer, Native>> uint_p
const uint32_t quotient_idx = ctx->add_variable(q);
const uint32_t remainder_idx = ctx->add_variable(r);

const mul_quad_<fr> division_gate{
const mul_quad_<FF> division_gate{
quotient_idx, // q
divisor_idx, // b
dividend_idx, // a
remainder_idx, // r
fr::one(), // q_m.w_1.w_2 = q.b
FF::one(), // q_m.w_1.w_2 = q.b
other.additive_constant, // q_l.w_1 = q.b if b const
fr::zero(), // q_2.w_2 = 0
fr::neg_one(), // q_3.w_3 = -a
fr::one(), // q_4.w_4 = r
-fr(additive_constant) // q_c = -a if a const
FF::zero(), // q_2.w_2 = 0
FF::neg_one(), // q_3.w_3 = -a
FF::one(), // q_4.w_4 = r
-FF(additive_constant) // q_c = -a if a const
};
ctx->create_big_mul_gate(division_gate);

// (b + c_b - r) = d
const uint256_t delta = divisor - r;

const uint32_t delta_idx = ctx->add_variable(delta);
const add_triple_<fr> delta_gate{
const add_triple_<FF> delta_gate{
divisor_idx, // b
remainder_idx, // r
delta_idx, // d
fr::one(), // q_l = 1
fr::neg_one(), // q_r = -1
fr::neg_one(), // q_o = -1
FF::one(), // q_l = 1
FF::neg_one(), // q_r = -1
FF::neg_one(), // q_o = -1
other.additive_constant, // q_c = d if const
};
ctx->create_add_gate(delta_gate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace stdlib {

template <typename Composer, typename Native> class uint_plookup {
public:
using FF = typename Composer::FF;
static constexpr size_t width = sizeof(Native) * 8;

uint_plookup(const witness_t<Composer>& other);
Expand Down

0 comments on commit e106375

Please sign in to comment.