Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(zk): implement combined custom gate column builder #206

Merged
merged 13 commits into from
Dec 22, 2023
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 @@ -51,6 +51,7 @@ template <typename F,
class MixedRadixEvaluationDomain
: public UnivariateEvaluationDomain<F, MaxDegree> {
public:
using Base = UnivariateEvaluationDomain<F, MaxDegree>;
using Field = F;
using Evals = UnivariateEvaluations<F, MaxDegree>;
using DensePoly = UnivariateDensePolynomial<F, MaxDegree>;
Expand Down Expand Up @@ -89,7 +90,7 @@ class MixedRadixEvaluationDomain
Evals evals;
evals.evaluations_ = poly.coefficients_.coefficients_;
if (!this->offset_.IsOne()) {
this->DistributePowers(evals, this->offset_);
Base::DistributePowers(evals, this->offset_);
}
evals.evaluations_.resize(this->size_, F::Zero());
BestFFT(evals, this->group_gen_);
Expand All @@ -112,7 +113,7 @@ class MixedRadixEvaluationDomain
coeff *= this->size_inv_;
}
} else {
this->DistributePowersAndMulByConst(poly, this->offset_inv_,
Base::DistributePowersAndMulByConst(poly, this->offset_inv_,
this->size_inv_);
}
poly.coefficients_.RemoveHighDegreeZeros();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ template <typename F,
size_t MaxDegree = (size_t{1} << F::Config::kTwoAdicity) - 1>
class Radix2EvaluationDomain : public UnivariateEvaluationDomain<F, MaxDegree> {
public:
using Base = UnivariateEvaluationDomain<F, MaxDegree>;
using Field = F;
using Evals = UnivariateEvaluations<F, MaxDegree>;
using DensePoly = UnivariateDensePolynomial<F, MaxDegree>;
Expand Down Expand Up @@ -128,7 +129,7 @@ class Radix2EvaluationDomain : public UnivariateEvaluationDomain<F, MaxDegree> {
// https://github.com/arkworks-rs/algebra/blob/master/poly/src/domain/radix2/fft.rs#L28)
constexpr void DegreeAwareFFTInPlace(Evals& evals) const {
if (!this->offset_.IsOne()) {
this->DistributePowers(evals, this->offset_);
Base::DistributePowers(evals, this->offset_);
}
size_t n = this->size_;
uint32_t log_n = this->log_size_of_group_;
Expand Down Expand Up @@ -161,7 +162,7 @@ class Radix2EvaluationDomain : public UnivariateEvaluationDomain<F, MaxDegree> {

constexpr void InOrderFFTInPlace(Evals& evals) const {
if (!this->offset_.IsOne()) {
this->DistributePowers(evals, this->offset_);
Base::DistributePowers(evals, this->offset_);
}
FFTHelperInPlace(evals);
}
Expand All @@ -175,7 +176,7 @@ class Radix2EvaluationDomain : public UnivariateEvaluationDomain<F, MaxDegree> {
val *= this->size_inv_;
}
} else {
this->DistributePowersAndMulByConst(poly, this->offset_inv_,
Base::DistributePowersAndMulByConst(poly, this->offset_inv_,
this->size_inv_);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,18 +318,18 @@ class UnivariateEvaluationDomain : public EvaluationDomain<F, MaxDegree> {
}
}

protected:
// Multiply the i-th element of |poly_or_evals| with |g|ⁱ.
template <typename PolyOrEvals>
constexpr void DistributePowers(PolyOrEvals& poly_or_evals,
const F& g) const {
constexpr static void DistributePowers(PolyOrEvals& poly_or_evals,
const F& g) {
DistributePowersAndMulByConst(poly_or_evals, g, F::One());
}

protected:
// Multiply the i-th element of |poly_or_evals| with |c|*|g|ⁱ.
template <typename PolyOrEvals>
constexpr void DistributePowersAndMulByConst(PolyOrEvals& poly_or_evals,
const F& g, const F& c) const {
constexpr static void DistributePowersAndMulByConst(
PolyOrEvals& poly_or_evals, const F& g, const F& c) {
dongchangYoo marked this conversation as resolved.
Show resolved Hide resolved
#if defined(TACHYON_HAS_OPENMP)
size_t thread_nums = static_cast<size_t>(omp_get_max_threads());
#else
Expand Down
2 changes: 1 addition & 1 deletion tachyon/zk/expressions/evaluator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ tachyon_cc_library(
"//tachyon/zk/expressions:scaled_expression",
"//tachyon/zk/expressions:selector_expression",
"//tachyon/zk/expressions:sum_expression",
"//tachyon/zk/plonk/circuit:table",
"//tachyon/zk/plonk/circuit:ref_table",
],
)

Expand Down
5 changes: 3 additions & 2 deletions tachyon/zk/expressions/evaluator/simple_evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "tachyon/zk/expressions/scaled_expression.h"
#include "tachyon/zk/expressions/selector_expression.h"
#include "tachyon/zk/expressions/sum_expression.h"
#include "tachyon/zk/plonk/circuit/table.h"
#include "tachyon/zk/plonk/circuit/ref_table.h"

namespace tachyon::zk {

Expand All @@ -34,7 +34,8 @@ class SimpleEvaluator

SimpleEvaluator() = default;
SimpleEvaluator(int32_t idx, int32_t size, int32_t rot_scale,
const Table<Evals>& table, absl::Span<const Field> challenges)
const RefTable<Evals>& table,
absl::Span<const Field> challenges)
: idx_(idx),
size_(size),
rot_scale_(rot_scale),
Expand Down
6 changes: 3 additions & 3 deletions tachyon/zk/expressions/evaluator/simple_evaluator_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class SimpleEvaluatorTest : public EvaluatorTest {
challenges_.push_back(GF7::Random());
}

Table<Evals> columns(absl::MakeConstSpan(fixed_columns_),
absl::MakeConstSpan(advice_columns_),
absl::MakeConstSpan(instance_columns_));
RefTable<Evals> columns(absl::MakeConstSpan(fixed_columns_),
absl::MakeConstSpan(advice_columns_),
absl::MakeConstSpan(instance_columns_));
simple_evaluator_ = std::make_unique<SimpleEvaluator<Evals>>(
3, 4, 1, columns, absl::MakeConstSpan(challenges_));
}
Expand Down
8 changes: 8 additions & 0 deletions tachyon/zk/lookup/lookup_committed.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ class LookupCommitted {
permuted_table_poly_(std::move(permuted_table_poly)),
product_poly_(std::move(product_poly)) {}

const BlindedPolynomial<Poly>& permuted_input_poly() const {
return permuted_input_poly_;
}
const BlindedPolynomial<Poly>& permuted_table_poly() const {
return permuted_table_poly_;
}
const BlindedPolynomial<Poly>& product_poly() const { return product_poly_; }

BlindedPolynomial<Poly>&& TakePermutedInputPoly() && {
return std::move(permuted_input_poly_);
}
Expand Down
6 changes: 3 additions & 3 deletions tachyon/zk/lookup/test/compress_expression_test_setting.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class CompressExpressionTestSetting : public halo2::ProverTest {
void SetUp() override {
halo2::ProverTest::SetUp();

Table<Evals> columns(absl::MakeConstSpan(fixed_columns_),
absl::MakeConstSpan(advice_columns_),
absl::MakeConstSpan(instance_columns_));
RefTable<Evals> columns(absl::MakeConstSpan(fixed_columns_),
absl::MakeConstSpan(advice_columns_),
absl::MakeConstSpan(instance_columns_));
evaluator_ = {0, static_cast<int32_t>(prover_->domain()->size()), 1,
columns, absl::MakeConstSpan(challenges_)};
theta_ = F(2);
Expand Down
24 changes: 19 additions & 5 deletions tachyon/zk/plonk/circuit/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ tachyon_cc_library(
],
)

tachyon_cc_library(
name = "owned_table",
hdrs = ["owned_table.h"],
deps = [":table_base"],
)

tachyon_cc_library(
name = "phase",
hdrs = ["phase.h"],
Expand Down Expand Up @@ -180,6 +186,12 @@ tachyon_cc_library(
],
)

tachyon_cc_library(
name = "ref_table",
hdrs = ["ref_table.h"],
deps = [":table_base"],
)

tachyon_cc_library(
name = "region",
hdrs = ["region.h"],
Expand Down Expand Up @@ -214,8 +226,8 @@ tachyon_cc_library(
name = "rotation",
hdrs = ["rotation.h"],
deps = [
"//tachyon/base:bit_cast",
"//tachyon/base:logging",
"//tachyon/base/numerics:checked_math",
"//tachyon/base/strings:string_number_conversions",
"//tachyon/math/polynomials/univariate:univariate_evaluation_domain",
],
Expand Down Expand Up @@ -266,12 +278,14 @@ tachyon_cc_library(
)

tachyon_cc_library(
name = "table",
hdrs = ["table.h"],
name = "table_base",
hdrs = ["table_base.h"],
deps = [
":column_key",
"//tachyon/base:logging",
"//tachyon/base:ref",
"//tachyon/base/containers:container_util",
"@com_google_absl//absl/types:span",
],
)

Expand Down Expand Up @@ -301,17 +315,17 @@ tachyon_cc_unittest(
"column_key_unittest.cc",
"lookup_table_column_unittest.cc",
"phase_unittest.cc",
"ref_table_unittest.cc",
"region_column_unittest.cc",
"rotation_unittest.cc",
"selector_unittest.cc",
"table_unittest.cc",
],
deps = [
":column_key",
":lookup_table_column",
":ref_table",
":region_column",
":rotation",
":table",
"//tachyon/math/elliptic_curves/bn/bn254:g1",
"//tachyon/math/finite_fields/test:gf7",
"//tachyon/math/polynomials/univariate:univariate_polynomial",
Expand Down
46 changes: 46 additions & 0 deletions tachyon/zk/plonk/circuit/owned_table.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2020-2022 The Electric Coin Company
// Copyright 2022 The Halo2 developers
// Use of this source code is governed by a MIT/Apache-2.0 style license that
// can be found in the LICENSE-MIT.halo2 and the LICENCE-APACHE.halo2
// file.

#ifndef TACHYON_ZK_PLONK_CIRCUIT_OWNED_TABLE_H_
#define TACHYON_ZK_PLONK_CIRCUIT_OWNED_TABLE_H_

#include <utility>
#include <vector>

#include "tachyon/zk/plonk/circuit/table_base.h"

namespace tachyon::zk {

template <typename PolyOrEvals>
class OwnedTable : public TableBase<PolyOrEvals> {
public:
OwnedTable() = default;
OwnedTable(std::vector<PolyOrEvals>&& fixed_columns,
std::vector<PolyOrEvals>&& advice_columns,
std::vector<PolyOrEvals>&& instance_columns)
: fixed_columns_(std::move(fixed_columns)),
advice_columns_(std::move(advice_columns)),
instance_columns_(std::move(instance_columns)) {}

absl::Span<const PolyOrEvals> fixed_columns() const override {
return fixed_columns_;
}
absl::Span<const PolyOrEvals> advice_columns() const override {
return advice_columns_;
}
absl::Span<const PolyOrEvals> instance_columns() const override {
return instance_columns_;
}

protected:
std::vector<PolyOrEvals> fixed_columns_;
std::vector<PolyOrEvals> advice_columns_;
std::vector<PolyOrEvals> instance_columns_;
};

} // namespace tachyon::zk

#endif // TACHYON_ZK_PLONK_CIRCUIT_OWNED_TABLE_H_
45 changes: 45 additions & 0 deletions tachyon/zk/plonk/circuit/ref_table.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2020-2022 The Electric Coin Company
// Copyright 2022 The Halo2 developers
// Use of this source code is governed by a MIT/Apache-2.0 style license that
// can be found in the LICENSE-MIT.halo2 and the LICENCE-APACHE.halo2
// file.

#ifndef TACHYON_ZK_PLONK_CIRCUIT_REF_TABLE_H_
#define TACHYON_ZK_PLONK_CIRCUIT_REF_TABLE_H_

#include <vector>

#include "tachyon/zk/plonk/circuit/table_base.h"

namespace tachyon::zk {

template <typename PolyOrEvals>
class RefTable : public TableBase<PolyOrEvals> {
public:
RefTable() = default;
RefTable(absl::Span<const PolyOrEvals> fixed_columns,
absl::Span<const PolyOrEvals> advice_columns,
absl::Span<const PolyOrEvals> instance_columns)
: fixed_columns_(fixed_columns),
advice_columns_(advice_columns),
instance_columns_(instance_columns) {}

absl::Span<const PolyOrEvals> fixed_columns() const override {
return fixed_columns_;
}
absl::Span<const PolyOrEvals> advice_columns() const override {
return advice_columns_;
}
absl::Span<const PolyOrEvals> instance_columns() const override {
return instance_columns_;
}

protected:
absl::Span<const PolyOrEvals> fixed_columns_;
absl::Span<const PolyOrEvals> advice_columns_;
absl::Span<const PolyOrEvals> instance_columns_;
};

} // namespace tachyon::zk

#endif // TACHYON_ZK_PLONK_CIRCUIT_REF_TABLE_H_
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "tachyon/zk/plonk/circuit/table.h"
#include "tachyon/zk/plonk/circuit/ref_table.h"

#include <vector>

Expand All @@ -10,7 +10,7 @@
namespace tachyon::zk {

template <typename ColumnKeyType>
class TableTest : public testing::Test {
class RefTableTest : public testing::Test {
public:
constexpr static size_t kMaxDegree = (size_t{1} << 3) - 1;

Expand All @@ -37,20 +37,20 @@ class TableTest : public testing::Test {
};

using ColumnKeyTypes = testing::Types<ColumnKeyBase, AnyColumnKey>;
TYPED_TEST_SUITE(TableTest, ColumnKeyTypes);
TYPED_TEST_SUITE(RefTableTest, ColumnKeyTypes);

TYPED_TEST(TableTest, GetColumns) {
TYPED_TEST(RefTableTest, GetColumns) {
using ColumnKeyType = TypeParam;
using Evals = typename TableTest<ColumnKeyType>::Evals;
using Evals = typename RefTableTest<ColumnKeyType>::Evals;

std::vector<ColumnKeyType> targets = {
FixedColumnKey(1), AdviceColumnKey(1, kFirstPhase), InstanceColumnKey(1),
FixedColumnKey(2), AdviceColumnKey(2, kFirstPhase), InstanceColumnKey(2),
};

Table<Evals> table(absl::MakeConstSpan(this->fixed_columns_),
absl::MakeConstSpan(this->advice_columns_),
absl::MakeConstSpan(this->instance_columns_));
RefTable<Evals> table(absl::MakeConstSpan(this->fixed_columns_),
absl::MakeConstSpan(this->advice_columns_),
absl::MakeConstSpan(this->instance_columns_));

std::vector<base::Ref<const Evals>> evals_refs = table.GetColumns(targets);

Expand Down
13 changes: 7 additions & 6 deletions tachyon/zk/plonk/circuit/rotation.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

#include <string>

#include "tachyon/base/bit_cast.h"
#include "tachyon/base/logging.h"
#include "tachyon/base/numerics/checked_math.h"
#include "tachyon/base/strings/string_number_conversions.h"
#include "tachyon/math/polynomials/univariate/univariate_evaluation_domain.h"

Expand Down Expand Up @@ -42,12 +42,13 @@ class TACHYON_EXPORT Rotation {

std::string ToString() const { return base::NumberToString(value_); }

// Returns ((|idx| + |value_| * |scale|) % |size|).
// It fails when |idx| + |value_| * |scale| evaluates to be negative.
// Returns (|idx| + |value_| * |scale|) modulo |size|.
size_t GetIndex(int32_t idx, int32_t scale, int32_t size) const {
int32_t value = idx + value_ * scale;
CHECK_GE(value, int32_t{0});
return size_t{base::bit_cast<uint32_t>(value % size)};
CHECK_GT(size, 0);
base::CheckedNumeric<int32_t> value = value_;
int32_t result = ((idx + value * scale) % size).ValueOrDie();
if (result < 0) result += size;
return result;
}

template <typename Domain, typename F = typename Domain::F>
Expand Down
Loading