From 9f275ba3e9fa109240313efddf33c102c09aa2bf Mon Sep 17 00:00:00 2001 From: batzor <32958247+batzor@users.noreply.github.com> Date: Wed, 26 Jun 2024 15:20:32 +0900 Subject: [PATCH 1/7] perf(zk): remove simple selector check in arithmetic operators Checking on each operator is costly as it double checks nested expressions. For example, when evaluating `(expr_a + expr_b) + expr_c` it will perform the check on `expr_a` and `expr_b` twice. Instead, we should check before doing operation when necessary to avoid double checks. In the relevant parts, we already do the check before doing operations. --- tachyon/zk/expressions/expression_factory.h | 6 ----- tachyon/zk/expressions/expression_unittest.cc | 24 ++----------------- 2 files changed, 2 insertions(+), 28 deletions(-) diff --git a/tachyon/zk/expressions/expression_factory.h b/tachyon/zk/expressions/expression_factory.h index ac3f90e90..834a467fb 100644 --- a/tachyon/zk/expressions/expression_factory.h +++ b/tachyon/zk/expressions/expression_factory.h @@ -90,8 +90,6 @@ std::unique_ptr> operator+( template std::unique_ptr> operator+(std::unique_ptr>&& lhs, std::unique_ptr>&& rhs) { - CHECK(!(lhs->ContainsSimpleSelector() && rhs->ContainsSimpleSelector())) - << "attempted to use a simple selector in an addition"; return ExpressionFactory::Sum(std::move(lhs), std::move(rhs)); } @@ -105,8 +103,6 @@ std::unique_ptr> operator-( template std::unique_ptr> operator-(std::unique_ptr>&& lhs, std::unique_ptr>&& rhs) { - CHECK(!(lhs->ContainsSimpleSelector() && rhs->ContainsSimpleSelector())) - << "attempted to use a simple selector in a subtraction"; return ExpressionFactory::Sum(std::move(lhs), operator-(std::move(rhs))); } @@ -120,8 +116,6 @@ std::unique_ptr> operator*( template std::unique_ptr> operator*(std::unique_ptr>&& lhs, std::unique_ptr>&& rhs) { - CHECK(!(lhs->ContainsSimpleSelector() && rhs->ContainsSimpleSelector())) - << "attempted to use a simple selector in a production"; return ExpressionFactory::Product(std::move(lhs), std::move(rhs)); } diff --git a/tachyon/zk/expressions/expression_unittest.cc b/tachyon/zk/expressions/expression_unittest.cc index 73e5a14f2..84cbe4fd1 100644 --- a/tachyon/zk/expressions/expression_unittest.cc +++ b/tachyon/zk/expressions/expression_unittest.cc @@ -51,11 +51,7 @@ TEST_F(ExpressionTest, ArithmeticOperatorWithClone) { std::unique_ptr> right = base::UniformElement(expressions_)->Clone(); - if (left->ContainsSimpleSelector() && right->ContainsSimpleSelector()) { - EXPECT_DEATH(left + right, ""); - EXPECT_DEATH(left - right, ""); - EXPECT_DEATH(left * right, ""); - } else { + if (!left->ContainsSimpleSelector() && !right->ContainsSimpleSelector()) { std::unique_ptr> add = left + right; EXPECT_EQ(*add->ToSum()->left(), *left); EXPECT_EQ(*add->ToSum()->right(), *right); @@ -92,23 +88,7 @@ TEST_F(ExpressionTest, ArithmeticOperatorWithMove) { std::unique_ptr> right = base::UniformElement(expressions_)->Clone(); - if (left->ContainsSimpleSelector() && right->ContainsSimpleSelector()) { - { - std::unique_ptr> left_tmp = left->Clone(); - std::unique_ptr> right_tmp = right->Clone(); - EXPECT_DEATH(std::move(left_tmp) + std::move(right_tmp), ""); - } - { - std::unique_ptr> left_tmp = left->Clone(); - std::unique_ptr> right_tmp = right->Clone(); - EXPECT_DEATH(std::move(left_tmp) - std::move(right_tmp), ""); - } - { - std::unique_ptr> left_tmp = left->Clone(); - std::unique_ptr> right_tmp = right->Clone(); - EXPECT_DEATH(std::move(left_tmp) * std::move(right_tmp), ""); - } - } else { + if (!left->ContainsSimpleSelector() && !right->ContainsSimpleSelector()) { { std::unique_ptr> left_tmp = left->Clone(); std::unique_ptr> right_tmp = right->Clone(); From 9226ef6cbffedd70cc323de4a33c7484373c7b49 Mon Sep 17 00:00:00 2001 From: batzor <32958247+batzor@users.noreply.github.com> Date: Thu, 27 Jun 2024 15:57:49 +0900 Subject: [PATCH 2/7] refac(zk): separate selector replacer from expressions --- tachyon/zk/expressions/BUILD.bazel | 2 - tachyon/zk/expressions/evaluator/BUILD.bazel | 1 + .../expressions/evaluator/selector_replacer.h | 100 ++++++++---------- .../evaluator/selector_replacer_unittest.cc | 27 ++--- tachyon/zk/expressions/expression.h | 5 - tachyon/zk/expressions/expression_factory.h | 1 - .../zk/plonk/constraint_system/BUILD.bazel | 1 + .../constraint_system/constraint_system.h | 9 +- 8 files changed, 65 insertions(+), 81 deletions(-) diff --git a/tachyon/zk/expressions/BUILD.bazel b/tachyon/zk/expressions/BUILD.bazel index 2aae08aa5..1f9ad3703 100644 --- a/tachyon/zk/expressions/BUILD.bazel +++ b/tachyon/zk/expressions/BUILD.bazel @@ -44,7 +44,6 @@ tachyon_cc_library( hdrs = ["expression.h"], deps = [ ":expression_type", - "//tachyon/base:ref", "//tachyon/base/containers:container_util", "//tachyon/zk/plonk/constraint_system:selector", ], @@ -64,7 +63,6 @@ tachyon_cc_library( ":scaled_expression", ":selector_expression", ":sum_expression", - "//tachyon/zk/expressions/evaluator:selector_replacer", "//tachyon/zk/expressions/evaluator:simple_selector_extractor", "//tachyon/zk/expressions/evaluator:simple_selector_finder", ], diff --git a/tachyon/zk/expressions/evaluator/BUILD.bazel b/tachyon/zk/expressions/evaluator/BUILD.bazel index c4855b69e..c74d5c69c 100644 --- a/tachyon/zk/expressions/evaluator/BUILD.bazel +++ b/tachyon/zk/expressions/evaluator/BUILD.bazel @@ -7,6 +7,7 @@ tachyon_cc_library( hdrs = ["selector_replacer.h"], deps = [ "//tachyon/base:logging", + "//tachyon/base:ref", "//tachyon/zk/expressions:evaluator", "//tachyon/zk/expressions:negated_expression", "//tachyon/zk/expressions:product_expression", diff --git a/tachyon/zk/expressions/evaluator/selector_replacer.h b/tachyon/zk/expressions/evaluator/selector_replacer.h index eb9a73e21..b6a55e9e9 100644 --- a/tachyon/zk/expressions/evaluator/selector_replacer.h +++ b/tachyon/zk/expressions/evaluator/selector_replacer.h @@ -11,6 +11,7 @@ #include #include "tachyon/base/logging.h" +#include "tachyon/base/ref.h" #include "tachyon/zk/expressions/evaluator.h" #include "tachyon/zk/expressions/negated_expression.h" #include "tachyon/zk/expressions/product_expression.h" @@ -21,67 +22,52 @@ namespace tachyon::zk { template -class SelectorsReplacer : public Evaluator>> { - public: - SelectorsReplacer( - const std::vector>>& replacements, - bool must_be_non_simple) - : replacements_(replacements), must_be_non_simple_(must_be_non_simple) {} - - // Evaluator methods - std::unique_ptr> Evaluate(const Expression* input) override { - switch (input->type()) { - case ExpressionType::kConstant: - case ExpressionType::kFixed: - case ExpressionType::kAdvice: - case ExpressionType::kInstance: - case ExpressionType::kChallenge: - return input->Clone(); - case ExpressionType::kSelector: { - plonk::Selector selector = input->ToSelector()->selector(); - if (must_be_non_simple_) { - // Simple selectors are prohibited from appearing in - // expressions in the lookup argument by |ConstraintSystem|. - if (selector.is_simple()) { - LOG(DFATAL) << "Simple selector found in lookup argument"; - } +std::unique_ptr> ReplaceSelectors( + const Expression* input, + const std::vector>>& replacements, + bool must_be_non_simple) { + switch (input->type()) { + case ExpressionType::kConstant: + case ExpressionType::kFixed: + case ExpressionType::kAdvice: + case ExpressionType::kInstance: + case ExpressionType::kChallenge: + return input->Clone(); + case ExpressionType::kSelector: { + plonk::Selector selector = input->ToSelector()->selector(); + if (must_be_non_simple) { + // Simple selectors are prohibited from appearing in + // expressions in the lookup argument by |ConstraintSystem|. + if (selector.is_simple()) { + LOG(DFATAL) << "Simple selector found in lookup argument"; } - return replacements_[selector.index()]->Clone(); - } - case ExpressionType::kNegated: - return ExpressionFactory::Negated( - Evaluate(input->ToNegated()->expr())); - case ExpressionType::kSum: { - const SumExpression* sum = input->ToSum(); - return ExpressionFactory::Sum(Evaluate(sum->left()), - Evaluate(sum->right())); - } - case ExpressionType::kProduct: { - const ProductExpression* product = input->ToProduct(); - return ExpressionFactory::Product(Evaluate(product->left()), - Evaluate(product->right())); - } - case ExpressionType::kScaled: { - const ScaledExpression* scaled = input->ToScaled(); - return ExpressionFactory::Scaled(Evaluate(scaled->expr()), - scaled->scale()); } + return replacements[selector.index()]->Clone(); + } + case ExpressionType::kNegated: + return ExpressionFactory::Negated(ReplaceSelectors( + input->ToNegated()->expr(), replacements, must_be_non_simple)); + case ExpressionType::kSum: { + const SumExpression* sum = input->ToSum(); + return ExpressionFactory::Sum( + ReplaceSelectors(sum->left(), replacements, must_be_non_simple), + ReplaceSelectors(sum->right(), replacements, must_be_non_simple)); + } + case ExpressionType::kProduct: { + const ProductExpression* product = input->ToProduct(); + return ExpressionFactory::Product( + ReplaceSelectors(product->left(), replacements, must_be_non_simple), + ReplaceSelectors(product->right(), replacements, must_be_non_simple)); + } + case ExpressionType::kScaled: { + const ScaledExpression* scaled = input->ToScaled(); + return ExpressionFactory::Scaled( + ReplaceSelectors(scaled->expr(), replacements, must_be_non_simple), + scaled->scale()); } - NOTREACHED(); - return nullptr; } - - private: - const std::vector>>& replacements_; - const bool must_be_non_simple_; -}; - -template -std::unique_ptr> Expression::ReplaceSelectors( - const std::vector>>& replacements, - bool must_be_non_simple) const { - SelectorsReplacer replacer(replacements, must_be_non_simple); - return Evaluate(&replacer); + NOTREACHED(); + return nullptr; } } // namespace tachyon::zk diff --git a/tachyon/zk/expressions/evaluator/selector_replacer_unittest.cc b/tachyon/zk/expressions/evaluator/selector_replacer_unittest.cc index 854f24603..0dccf9985 100644 --- a/tachyon/zk/expressions/evaluator/selector_replacer_unittest.cc +++ b/tachyon/zk/expressions/evaluator/selector_replacer_unittest.cc @@ -13,9 +13,8 @@ class SelectorReplacerTest : public EvaluatorTest {}; TEST_F(SelectorReplacerTest, Constant) { GF7 value = GF7::Random(); - std::unique_ptr> expr = - ExpressionFactory::Constant(value); - EXPECT_EQ(*expr, *expr->ReplaceSelectors({}, false)); + Expr expr = ExpressionFactory::Constant(value); + EXPECT_EQ(*expr, *ReplaceSelectors(expr.get(), {}, false)); } TEST_F(SelectorReplacerTest, Selector) { @@ -29,11 +28,13 @@ TEST_F(SelectorReplacerTest, Selector) { replacements.emplace_back(owned_replacements[0].get()); replacements.emplace_back(owned_replacements[1].get()); - EXPECT_DEBUG_DEATH(expr->ReplaceSelectors(replacements, true), ""); - EXPECT_EQ(*expr->ReplaceSelectors(replacements, false), *replacements[1]); + EXPECT_DEBUG_DEATH(ReplaceSelectors(expr.get(), replacements, true), ""); + EXPECT_EQ(*ReplaceSelectors(expr.get(), replacements, false), + *replacements[1]); expr = ExpressionFactory::Selector(plonk::Selector::Complex(1)); - EXPECT_EQ(*expr->ReplaceSelectors(replacements, false), *replacements[1]); + EXPECT_EQ(*ReplaceSelectors(expr.get(), replacements, false), + *replacements[1]); } TEST_F(SelectorReplacerTest, Fixed) { @@ -49,7 +50,7 @@ TEST_F(SelectorReplacerTest, Fixed) { plonk::FixedQuery query(1, Rotation(test.rotation), plonk::FixedColumnKey(test.column_index)); Expr expr = ExpressionFactory::Fixed(query); - EXPECT_EQ(*expr, *expr->ReplaceSelectors({}, false)); + EXPECT_EQ(*expr, *ReplaceSelectors(expr.get(), {}, false)); } } @@ -67,7 +68,7 @@ TEST_F(SelectorReplacerTest, Advice) { 1, Rotation(test.rotation), plonk::AdviceColumnKey(test.column_index, plonk::Phase(0))); Expr expr = ExpressionFactory::Advice(query); - EXPECT_EQ(*expr, *expr->ReplaceSelectors({}, false)); + EXPECT_EQ(*expr, *ReplaceSelectors(expr.get(), {}, false)); } } @@ -84,21 +85,21 @@ TEST_F(SelectorReplacerTest, Instance) { plonk::InstanceQuery query(1, Rotation(test.rotation), plonk::InstanceColumnKey(test.column_index)); Expr expr = ExpressionFactory::Instance(query); - EXPECT_EQ(*expr, *expr->ReplaceSelectors({}, false)); + EXPECT_EQ(*expr, *ReplaceSelectors(expr.get(), {}, false)); } } TEST_F(SelectorReplacerTest, Challenges) { Expr expr = ExpressionFactory::Challenge(plonk::Challenge(1, plonk::Phase(0))); - EXPECT_EQ(*expr, *expr->ReplaceSelectors({}, false)); + EXPECT_EQ(*expr, *ReplaceSelectors(expr.get(), {}, false)); } TEST_F(SelectorReplacerTest, Negated) { GF7 value = GF7::Random(); Expr expr = ExpressionFactory::Negated(ExpressionFactory::Constant(value)); - EXPECT_EQ(*expr, *expr->ReplaceSelectors({}, false)); + EXPECT_EQ(*expr, *ReplaceSelectors(expr.get(), {}, false)); } TEST_F(SelectorReplacerTest, Sum) { @@ -114,7 +115,7 @@ TEST_F(SelectorReplacerTest, Product) { GF7 b = GF7::Random(); Expr expr = ExpressionFactory::Product( ExpressionFactory::Constant(a), ExpressionFactory::Constant(b)); - EXPECT_EQ(*expr, *expr->ReplaceSelectors({}, false)); + EXPECT_EQ(*expr, *ReplaceSelectors(expr.get(), {}, false)); } TEST_F(SelectorReplacerTest, Scaled) { @@ -122,7 +123,7 @@ TEST_F(SelectorReplacerTest, Scaled) { GF7 b = GF7::Random(); Expr expr = ExpressionFactory::Scaled(ExpressionFactory::Constant(a), b); - EXPECT_EQ(*expr, *expr->ReplaceSelectors({}, false)); + EXPECT_EQ(*expr, *ReplaceSelectors(expr.get(), {}, false)); } } // namespace tachyon::zk diff --git a/tachyon/zk/expressions/expression.h b/tachyon/zk/expressions/expression.h index 2495763d4..021b78ef3 100644 --- a/tachyon/zk/expressions/expression.h +++ b/tachyon/zk/expressions/expression.h @@ -15,7 +15,6 @@ #include "tachyon/base/containers/container_util.h" #include "tachyon/base/logging.h" -#include "tachyon/base/ref.h" #include "tachyon/zk/expressions/expression_type.h" #include "tachyon/zk/plonk/constraint_system/selector.h" @@ -104,10 +103,6 @@ class Expression { }); } - std::unique_ptr> ReplaceSelectors( - const std::vector>>& replacements, - bool must_be_non_simple) const; - template Evaluated Evaluate(Evaluator* evaluator) const { return evaluator->Evaluate(this); diff --git a/tachyon/zk/expressions/expression_factory.h b/tachyon/zk/expressions/expression_factory.h index 834a467fb..a14201ec2 100644 --- a/tachyon/zk/expressions/expression_factory.h +++ b/tachyon/zk/expressions/expression_factory.h @@ -15,7 +15,6 @@ #include "tachyon/zk/expressions/advice_expression.h" #include "tachyon/zk/expressions/challenge_expression.h" #include "tachyon/zk/expressions/constant_expression.h" -#include "tachyon/zk/expressions/evaluator/selector_replacer.h" #include "tachyon/zk/expressions/evaluator/simple_selector_extractor.h" #include "tachyon/zk/expressions/evaluator/simple_selector_finder.h" #include "tachyon/zk/expressions/expression.h" diff --git a/tachyon/zk/plonk/constraint_system/BUILD.bazel b/tachyon/zk/plonk/constraint_system/BUILD.bazel index 012ccf08e..79a7467e7 100644 --- a/tachyon/zk/plonk/constraint_system/BUILD.bazel +++ b/tachyon/zk/plonk/constraint_system/BUILD.bazel @@ -42,6 +42,7 @@ tachyon_cc_library( "//tachyon/base/functional:callback", "//tachyon/base/strings:string_util", "//tachyon/zk/base:row_types", + "//tachyon/zk/expressions/evaluator:selector_replacer", "//tachyon/zk/expressions/evaluator:simple_selector_finder", "//tachyon/zk/lookup:lookup_argument", "//tachyon/zk/lookup:type", diff --git a/tachyon/zk/plonk/constraint_system/constraint_system.h b/tachyon/zk/plonk/constraint_system/constraint_system.h index 4a0c03aa7..9188af95a 100644 --- a/tachyon/zk/plonk/constraint_system/constraint_system.h +++ b/tachyon/zk/plonk/constraint_system/constraint_system.h @@ -29,6 +29,7 @@ #include "tachyon/base/logging.h" #include "tachyon/base/strings/string_util.h" #include "tachyon/zk/base/row_types.h" +#include "tachyon/zk/expressions/evaluator/selector_replacer.h" #include "tachyon/zk/expressions/evaluator/simple_selector_finder.h" #include "tachyon/zk/lookup/lookup_argument.h" #include "tachyon/zk/lookup/type.h" @@ -482,7 +483,8 @@ class ConstraintSystem { for (Gate& gate : gates_) { for (std::unique_ptr>& expression : gate.polys()) { - expression = expression->ReplaceSelectors(selector_replacements, false); + expression = + ReplaceSelectors(expression.get(), selector_replacements, false); } } for (lookup::Argument& lookup : lookups_) { @@ -490,12 +492,13 @@ class ConstraintSystem { lookup.inputs_expressions()) { for (std::unique_ptr>& expression : input_expressions) { expression = - expression->ReplaceSelectors(selector_replacements, true); + ReplaceSelectors(expression.get(), selector_replacements, true); } } for (std::unique_ptr>& expression : lookup.table_expressions()) { - expression = expression->ReplaceSelectors(selector_replacements, true); + expression = + ReplaceSelectors(expression.get(), selector_replacements, true); } } From 672eca91fce327dfd2aafc329854316ed7c8a93f Mon Sep 17 00:00:00 2001 From: batzor <32958247+batzor@users.noreply.github.com> Date: Thu, 27 Jun 2024 16:04:12 +0900 Subject: [PATCH 3/7] refac(zk): separate simple selector finder from expressions --- tachyon/zk/expressions/BUILD.bazel | 2 +- .../evaluator/selector_replacer_unittest.cc | 3 +- .../evaluator/simple_selector_finder.h | 70 ++++++++----------- .../simple_selector_finder_unittest.cc | 28 ++++---- tachyon/zk/expressions/expression.h | 3 - tachyon/zk/expressions/expression_factory.h | 1 - tachyon/zk/expressions/expression_unittest.cc | 7 +- .../zk/plonk/constraint_system/BUILD.bazel | 1 + .../constraint_system/constraint_system.h | 4 +- .../plonk/constraint_system/virtual_cells.h | 3 +- 10 files changed, 58 insertions(+), 64 deletions(-) diff --git a/tachyon/zk/expressions/BUILD.bazel b/tachyon/zk/expressions/BUILD.bazel index 1f9ad3703..b6f9f7ef4 100644 --- a/tachyon/zk/expressions/BUILD.bazel +++ b/tachyon/zk/expressions/BUILD.bazel @@ -64,7 +64,6 @@ tachyon_cc_library( ":selector_expression", ":sum_expression", "//tachyon/zk/expressions/evaluator:simple_selector_extractor", - "//tachyon/zk/expressions/evaluator:simple_selector_finder", ], ) @@ -165,5 +164,6 @@ tachyon_cc_unittest( "//tachyon/math/finite_fields/test:finite_field_test", "//tachyon/math/finite_fields/test:gf7", "//tachyon/math/polynomials/univariate:univariate_polynomial", + "//tachyon/zk/expressions/evaluator:simple_selector_finder", ], ) diff --git a/tachyon/zk/expressions/evaluator/selector_replacer_unittest.cc b/tachyon/zk/expressions/evaluator/selector_replacer_unittest.cc index 0dccf9985..4f0a85c60 100644 --- a/tachyon/zk/expressions/evaluator/selector_replacer_unittest.cc +++ b/tachyon/zk/expressions/evaluator/selector_replacer_unittest.cc @@ -2,6 +2,7 @@ #include +#include "tachyon/zk/expressions/evaluator/simple_selector_finder.h" #include "tachyon/zk/expressions/evaluator/test/evaluator_test.h" #include "tachyon/zk/expressions/expression_factory.h" @@ -107,7 +108,7 @@ TEST_F(SelectorReplacerTest, Sum) { GF7 b = GF7::Random(); Expr expr = ExpressionFactory::Sum(ExpressionFactory::Constant(a), ExpressionFactory::Constant(b)); - EXPECT_FALSE(expr->ContainsSimpleSelector()); + EXPECT_FALSE(ContainsSimpleSelector(expr.get())); } TEST_F(SelectorReplacerTest, Product) { diff --git a/tachyon/zk/expressions/evaluator/simple_selector_finder.h b/tachyon/zk/expressions/evaluator/simple_selector_finder.h index fe0321225..02ec759ea 100644 --- a/tachyon/zk/expressions/evaluator/simple_selector_finder.h +++ b/tachyon/zk/expressions/evaluator/simple_selector_finder.h @@ -17,47 +17,39 @@ namespace tachyon::zk { template -class SimpleSelectorFinder : public Evaluator { - public: - // Evaluator methods - bool Evaluate(const Expression* input) override { - switch (input->type()) { - case ExpressionType::kConstant: - return false; - case ExpressionType::kSelector: - return input->ToSelector()->selector().is_simple(); - case ExpressionType::kFixed: - return false; - case ExpressionType::kAdvice: - return false; - case ExpressionType::kInstance: - return false; - case ExpressionType::kChallenge: - return false; - case ExpressionType::kNegated: - return Evaluate(input->ToNegated()->expr()); - case ExpressionType::kSum: { - const SumExpression* sum = input->ToSum(); - return Evaluate(sum->left()) || Evaluate(sum->right()); - } - case ExpressionType::kProduct: { - const ProductExpression* product = input->ToProduct(); - return Evaluate(product->left()) || Evaluate(product->right()); - } - case ExpressionType::kScaled: { - const ScaledExpression* scaled = input->ToScaled(); - return Evaluate(scaled->expr()); - } +bool ContainsSimpleSelector(const Expression* input) { + switch (input->type()) { + case ExpressionType::kConstant: + return false; + case ExpressionType::kSelector: + return input->ToSelector()->selector().is_simple(); + case ExpressionType::kFixed: + return false; + case ExpressionType::kAdvice: + return false; + case ExpressionType::kInstance: + return false; + case ExpressionType::kChallenge: + return false; + case ExpressionType::kNegated: + return ContainsSimpleSelector(input->ToNegated()->expr()); + case ExpressionType::kSum: { + const SumExpression* sum = input->ToSum(); + return ContainsSimpleSelector(sum->left()) || + ContainsSimpleSelector(sum->right()); + } + case ExpressionType::kProduct: { + const ProductExpression* product = input->ToProduct(); + return ContainsSimpleSelector(product->left()) || + ContainsSimpleSelector(product->right()); + } + case ExpressionType::kScaled: { + const ScaledExpression* scaled = input->ToScaled(); + return ContainsSimpleSelector(scaled->expr()); } - NOTREACHED(); - return false; } -}; - -template -bool Expression::ContainsSimpleSelector() const { - SimpleSelectorFinder finder; - return Evaluate(&finder); + NOTREACHED(); + return false; } } // namespace tachyon::zk diff --git a/tachyon/zk/expressions/evaluator/simple_selector_finder_unittest.cc b/tachyon/zk/expressions/evaluator/simple_selector_finder_unittest.cc index 778bbbdfe..a465995ad 100644 --- a/tachyon/zk/expressions/evaluator/simple_selector_finder_unittest.cc +++ b/tachyon/zk/expressions/evaluator/simple_selector_finder_unittest.cc @@ -15,14 +15,14 @@ TEST_F(SimpleSelectorFinderTest, Constant) { GF7 value = GF7::Random(); std::unique_ptr> expr = ExpressionFactory::Constant(value); - EXPECT_FALSE(expr->ContainsSimpleSelector()); + EXPECT_FALSE(ContainsSimpleSelector(expr.get())); } TEST_F(SimpleSelectorFinderTest, Selector) { Expr expr = ExpressionFactory::Selector(plonk::Selector::Simple(1)); - EXPECT_TRUE(expr->ContainsSimpleSelector()); + EXPECT_TRUE(ContainsSimpleSelector(expr.get())); expr = ExpressionFactory::Selector(plonk::Selector::Complex(1)); - EXPECT_FALSE(expr->ContainsSimpleSelector()); + EXPECT_FALSE(ContainsSimpleSelector(expr.get())); } TEST_F(SimpleSelectorFinderTest, Fixed) { @@ -38,7 +38,7 @@ TEST_F(SimpleSelectorFinderTest, Fixed) { plonk::FixedQuery query(1, Rotation(test.rotation), plonk::FixedColumnKey(test.column_index)); Expr expr = ExpressionFactory::Fixed(query); - EXPECT_FALSE(expr->ContainsSimpleSelector()); + EXPECT_FALSE(ContainsSimpleSelector(expr.get())); } } @@ -56,7 +56,7 @@ TEST_F(SimpleSelectorFinderTest, Advice) { 1, Rotation(test.rotation), plonk::AdviceColumnKey(test.column_index, plonk::Phase(0))); Expr expr = ExpressionFactory::Advice(query); - EXPECT_FALSE(expr->ContainsSimpleSelector()); + EXPECT_FALSE(ContainsSimpleSelector(expr.get())); } } @@ -73,21 +73,21 @@ TEST_F(SimpleSelectorFinderTest, Instance) { plonk::InstanceQuery query(1, Rotation(test.rotation), plonk::InstanceColumnKey(test.column_index)); Expr expr = ExpressionFactory::Instance(query); - EXPECT_FALSE(expr->ContainsSimpleSelector()); + EXPECT_FALSE(ContainsSimpleSelector(expr.get())); } } TEST_F(SimpleSelectorFinderTest, Challenges) { Expr expr = ExpressionFactory::Challenge(plonk::Challenge(1, plonk::Phase(0))); - EXPECT_FALSE(expr->ContainsSimpleSelector()); + EXPECT_FALSE(ContainsSimpleSelector(expr.get())); } TEST_F(SimpleSelectorFinderTest, Negated) { GF7 value = GF7::Random(); Expr expr = ExpressionFactory::Negated(ExpressionFactory::Constant(value)); - EXPECT_FALSE(expr->ContainsSimpleSelector()); + EXPECT_FALSE(ContainsSimpleSelector(expr.get())); } TEST_F(SimpleSelectorFinderTest, Sum) { @@ -95,11 +95,11 @@ TEST_F(SimpleSelectorFinderTest, Sum) { GF7 b = GF7::Random(); Expr expr = ExpressionFactory::Sum(ExpressionFactory::Constant(a), ExpressionFactory::Constant(b)); - EXPECT_FALSE(expr->ContainsSimpleSelector()); + EXPECT_FALSE(ContainsSimpleSelector(expr.get())); expr = ExpressionFactory::Sum( ExpressionFactory::Constant(a), ExpressionFactory::Selector(plonk::Selector::Simple(1))); - EXPECT_TRUE(expr->ContainsSimpleSelector()); + EXPECT_TRUE(ContainsSimpleSelector(expr.get())); } TEST_F(SimpleSelectorFinderTest, Product) { @@ -107,11 +107,11 @@ TEST_F(SimpleSelectorFinderTest, Product) { GF7 b = GF7::Random(); Expr expr = ExpressionFactory::Product( ExpressionFactory::Constant(a), ExpressionFactory::Constant(b)); - EXPECT_FALSE(expr->ContainsSimpleSelector()); + EXPECT_FALSE(ContainsSimpleSelector(expr.get())); expr = ExpressionFactory::Product( ExpressionFactory::Constant(a), ExpressionFactory::Selector(plonk::Selector::Simple(1))); - EXPECT_TRUE(expr->ContainsSimpleSelector()); + EXPECT_TRUE(ContainsSimpleSelector(expr.get())); } TEST_F(SimpleSelectorFinderTest, Scaled) { @@ -119,10 +119,10 @@ TEST_F(SimpleSelectorFinderTest, Scaled) { GF7 b = GF7::Random(); Expr expr = ExpressionFactory::Scaled(ExpressionFactory::Constant(a), b); - EXPECT_FALSE(expr->ContainsSimpleSelector()); + EXPECT_FALSE(ContainsSimpleSelector(expr.get())); expr = ExpressionFactory::Scaled( ExpressionFactory::Selector(plonk::Selector::Simple(1)), GF7(3)); - EXPECT_TRUE(expr->ContainsSimpleSelector()); + EXPECT_TRUE(ContainsSimpleSelector(expr.get())); } } // namespace tachyon::zk diff --git a/tachyon/zk/expressions/expression.h b/tachyon/zk/expressions/expression.h index 021b78ef3..248927fef 100644 --- a/tachyon/zk/expressions/expression.h +++ b/tachyon/zk/expressions/expression.h @@ -90,9 +90,6 @@ class Expression { return ExpressionFactory::Negated(Clone()); } - // Returns whether or not this expression contains a simple selector. - bool ContainsSimpleSelector() const; - // Extracts a simple selector from this gate, if present. std::optional ExtractSimpleSelector() const; diff --git a/tachyon/zk/expressions/expression_factory.h b/tachyon/zk/expressions/expression_factory.h index a14201ec2..f86dee94d 100644 --- a/tachyon/zk/expressions/expression_factory.h +++ b/tachyon/zk/expressions/expression_factory.h @@ -16,7 +16,6 @@ #include "tachyon/zk/expressions/challenge_expression.h" #include "tachyon/zk/expressions/constant_expression.h" #include "tachyon/zk/expressions/evaluator/simple_selector_extractor.h" -#include "tachyon/zk/expressions/evaluator/simple_selector_finder.h" #include "tachyon/zk/expressions/expression.h" #include "tachyon/zk/expressions/fixed_expression.h" #include "tachyon/zk/expressions/instance_expression.h" diff --git a/tachyon/zk/expressions/expression_unittest.cc b/tachyon/zk/expressions/expression_unittest.cc index 84cbe4fd1..a53f9b6a7 100644 --- a/tachyon/zk/expressions/expression_unittest.cc +++ b/tachyon/zk/expressions/expression_unittest.cc @@ -3,6 +3,7 @@ #include "tachyon/base/random.h" #include "tachyon/math/finite_fields/test/finite_field_test.h" #include "tachyon/math/finite_fields/test/gf7.h" +#include "tachyon/zk/expressions/evaluator/simple_selector_finder.h" #include "tachyon/zk/expressions/expression_factory.h" namespace tachyon::zk { @@ -51,7 +52,8 @@ TEST_F(ExpressionTest, ArithmeticOperatorWithClone) { std::unique_ptr> right = base::UniformElement(expressions_)->Clone(); - if (!left->ContainsSimpleSelector() && !right->ContainsSimpleSelector()) { + if (!ContainsSimpleSelector(left.get()) && + !ContainsSimpleSelector(right.get())) { std::unique_ptr> add = left + right; EXPECT_EQ(*add->ToSum()->left(), *left); EXPECT_EQ(*add->ToSum()->right(), *right); @@ -88,7 +90,8 @@ TEST_F(ExpressionTest, ArithmeticOperatorWithMove) { std::unique_ptr> right = base::UniformElement(expressions_)->Clone(); - if (!left->ContainsSimpleSelector() && !right->ContainsSimpleSelector()) { + if (!ContainsSimpleSelector(left.get()) && + !ContainsSimpleSelector(right.get())) { { std::unique_ptr> left_tmp = left->Clone(); std::unique_ptr> right_tmp = right->Clone(); diff --git a/tachyon/zk/plonk/constraint_system/BUILD.bazel b/tachyon/zk/plonk/constraint_system/BUILD.bazel index 79a7467e7..c923144ba 100644 --- a/tachyon/zk/plonk/constraint_system/BUILD.bazel +++ b/tachyon/zk/plonk/constraint_system/BUILD.bazel @@ -150,6 +150,7 @@ tachyon_cc_library( ":selector", ":virtual_cell", "//tachyon/zk/expressions:expression_factory", + "//tachyon/zk/expressions/evaluator:simple_selector_finder", "//tachyon/zk/plonk/layout:lookup_table_column", ], ) diff --git a/tachyon/zk/plonk/constraint_system/constraint_system.h b/tachyon/zk/plonk/constraint_system/constraint_system.h index 9188af95a..c767f0150 100644 --- a/tachyon/zk/plonk/constraint_system/constraint_system.h +++ b/tachyon/zk/plonk/constraint_system/constraint_system.h @@ -208,7 +208,7 @@ class ConstraintSystem { switch (lookup_type_) { case lookup::Type::kHalo2: { for (const lookup::Pair>>& pair : pairs) { - CHECK(!pair.input()->ContainsSimpleSelector()) + CHECK(!ContainsSimpleSelector(pair.input().get())) << "expression containing simple selector " "supplied to lookup argument"; } @@ -223,7 +223,7 @@ class ConstraintSystem { table_expressions.reserve(pairs.size()); for (lookup::Pair>>& pair : pairs) { - CHECK(!pair.input()->ContainsSimpleSelector()) + CHECK(!ContainsSimpleSelector(pair.input().get())) << "expression containing simple selector " "supplied to lookup argument"; diff --git a/tachyon/zk/plonk/constraint_system/virtual_cells.h b/tachyon/zk/plonk/constraint_system/virtual_cells.h index 2600ff4c0..77864ae28 100644 --- a/tachyon/zk/plonk/constraint_system/virtual_cells.h +++ b/tachyon/zk/plonk/constraint_system/virtual_cells.h @@ -5,6 +5,7 @@ #include #include +#include "tachyon/zk/expressions/evaluator/simple_selector_finder.h" #include "tachyon/zk/expressions/expression_factory.h" #include "tachyon/zk/plonk/constraint_system/selector.h" #include "tachyon/zk/plonk/constraint_system/virtual_cell.h" @@ -93,7 +94,7 @@ class VirtualCells { const lookup::Pair>, LookupTableColumn>& pair, Rotation at) { - CHECK(!pair.input()->ContainsSimpleSelector()) + CHECK(!ContainsSimpleSelector(pair.input().get())) << "expression containing simple selector supplied to lookup argument"; return QueryFixed(pair.table().column(), at); From fd8a389e94bb7b1aa6e945d54b15d2cd7886a5ab Mon Sep 17 00:00:00 2001 From: batzor <32958247+batzor@users.noreply.github.com> Date: Thu, 27 Jun 2024 16:07:07 +0900 Subject: [PATCH 4/7] refac(zk): separate simple selector extractor from expressions --- tachyon/zk/expressions/BUILD.bazel | 1 - .../evaluator/simple_selector_extractor.h | 100 ++++++++---------- .../simple_selector_extractor_unittest.cc | 28 ++--- tachyon/zk/expressions/expression.h | 4 - tachyon/zk/expressions/expression_factory.h | 1 - .../zk/plonk/constraint_system/BUILD.bazel | 1 + .../constraint_system/constraint_system.h | 4 +- 7 files changed, 64 insertions(+), 75 deletions(-) diff --git a/tachyon/zk/expressions/BUILD.bazel b/tachyon/zk/expressions/BUILD.bazel index b6f9f7ef4..fb08ef9df 100644 --- a/tachyon/zk/expressions/BUILD.bazel +++ b/tachyon/zk/expressions/BUILD.bazel @@ -63,7 +63,6 @@ tachyon_cc_library( ":scaled_expression", ":selector_expression", ":sum_expression", - "//tachyon/zk/expressions/evaluator:simple_selector_extractor", ], ) diff --git a/tachyon/zk/expressions/evaluator/simple_selector_extractor.h b/tachyon/zk/expressions/evaluator/simple_selector_extractor.h index 793299ac0..06b3dc6df 100644 --- a/tachyon/zk/expressions/evaluator/simple_selector_extractor.h +++ b/tachyon/zk/expressions/evaluator/simple_selector_extractor.h @@ -15,64 +15,56 @@ namespace tachyon::zk { template -class SimpleSelectorExtractor - : public Evaluator> { - public: - // Evaluator methods - std::optional Evaluate(const Expression* input) override { - auto op = [](const std::optional& left, - const std::optional& right) - -> std::optional { - CHECK(!(left.has_value() && right.has_value())) - << "two simple selectors cannot be in the same expression"; - if (left.has_value()) return left; - if (right.has_value()) return right; - return std::nullopt; - }; +std::optional ExtractSimpleSelector( + const Expression* input) { + auto op = [](const std::optional& left, + const std::optional& right) + -> std::optional { + CHECK(!(left.has_value() && right.has_value())) + << "two simple selectors cannot be in the same expression"; + if (left.has_value()) return left; + if (right.has_value()) return right; + return std::nullopt; + }; - switch (input->type()) { - case ExpressionType::kConstant: - return std::nullopt; - case ExpressionType::kSelector: { - plonk::Selector selector = input->ToSelector()->selector(); - if (selector.is_simple()) { - return selector; - } - return std::nullopt; - } - case ExpressionType::kFixed: - return std::nullopt; - case ExpressionType::kAdvice: - return std::nullopt; - case ExpressionType::kInstance: - return std::nullopt; - case ExpressionType::kChallenge: - return std::nullopt; - case ExpressionType::kNegated: { - return Evaluate(input->ToNegated()->expr()); - } - case ExpressionType::kSum: { - const SumExpression* sum = input->ToSum(); - return op(Evaluate(sum->left()), Evaluate(sum->right())); - } - case ExpressionType::kProduct: { - const ProductExpression* product = input->ToProduct(); - return op(Evaluate(product->left()), Evaluate(product->right())); - } - case ExpressionType::kScaled: { - const ScaledExpression* scaled = input->ToScaled(); - return Evaluate(scaled->expr()); + switch (input->type()) { + case ExpressionType::kConstant: + return std::nullopt; + case ExpressionType::kSelector: { + plonk::Selector selector = input->ToSelector()->selector(); + if (selector.is_simple()) { + return selector; } + return std::nullopt; + } + case ExpressionType::kFixed: + return std::nullopt; + case ExpressionType::kAdvice: + return std::nullopt; + case ExpressionType::kInstance: + return std::nullopt; + case ExpressionType::kChallenge: + return std::nullopt; + case ExpressionType::kNegated: { + return ExtractSimpleSelector(input->ToNegated()->expr()); + } + case ExpressionType::kSum: { + const SumExpression* sum = input->ToSum(); + return op(ExtractSimpleSelector(sum->left()), + ExtractSimpleSelector(sum->right())); + } + case ExpressionType::kProduct: { + const ProductExpression* product = input->ToProduct(); + return op(ExtractSimpleSelector(product->left()), + ExtractSimpleSelector(product->right())); + } + case ExpressionType::kScaled: { + const ScaledExpression* scaled = input->ToScaled(); + return ExtractSimpleSelector(scaled->expr()); } - NOTREACHED(); - return std::nullopt; } -}; - -template -std::optional Expression::ExtractSimpleSelector() const { - SimpleSelectorExtractor extractor; - return Evaluate(&extractor); + NOTREACHED(); + return std::nullopt; } } // namespace tachyon::zk diff --git a/tachyon/zk/expressions/evaluator/simple_selector_extractor_unittest.cc b/tachyon/zk/expressions/evaluator/simple_selector_extractor_unittest.cc index 6f9c420fa..626c82af2 100644 --- a/tachyon/zk/expressions/evaluator/simple_selector_extractor_unittest.cc +++ b/tachyon/zk/expressions/evaluator/simple_selector_extractor_unittest.cc @@ -15,16 +15,16 @@ TEST_F(SimpleSelectorExtractorTest, Constant) { GF7 value = GF7::Random(); std::unique_ptr> expr = ExpressionFactory::Constant(value); - EXPECT_FALSE(expr->ExtractSimpleSelector().has_value()); + EXPECT_FALSE(ExtractSimpleSelector(expr.get()).has_value()); } TEST_F(SimpleSelectorExtractorTest, Selector) { plonk::Selector expected_selector = plonk::Selector::Simple(1); Expr expr = ExpressionFactory::Selector(expected_selector); - std::optional selector = expr->ExtractSimpleSelector(); + std::optional selector = ExtractSimpleSelector(expr.get()); EXPECT_EQ(selector.value(), expected_selector); expr = ExpressionFactory::Selector(plonk::Selector::Complex(1)); - EXPECT_FALSE(expr->ExtractSimpleSelector().has_value()); + EXPECT_FALSE(ExtractSimpleSelector(expr.get()).has_value()); } TEST_F(SimpleSelectorExtractorTest, Fixed) { @@ -40,7 +40,7 @@ TEST_F(SimpleSelectorExtractorTest, Fixed) { plonk::FixedQuery query(1, Rotation(test.rotation), plonk::FixedColumnKey(test.column_index)); Expr expr = ExpressionFactory::Fixed(query); - EXPECT_FALSE(expr->ExtractSimpleSelector().has_value()); + EXPECT_FALSE(ExtractSimpleSelector(expr.get()).has_value()); } } @@ -58,7 +58,7 @@ TEST_F(SimpleSelectorExtractorTest, Advice) { 1, Rotation(test.rotation), plonk::AdviceColumnKey(test.column_index, plonk::Phase(0))); Expr expr = ExpressionFactory::Advice(query); - EXPECT_FALSE(expr->ExtractSimpleSelector().has_value()); + EXPECT_FALSE(ExtractSimpleSelector(expr.get()).has_value()); } } @@ -75,21 +75,21 @@ TEST_F(SimpleSelectorExtractorTest, Instance) { plonk::InstanceQuery query(1, Rotation(test.rotation), plonk::InstanceColumnKey(test.column_index)); Expr expr = ExpressionFactory::Instance(query); - EXPECT_FALSE(expr->ExtractSimpleSelector().has_value()); + EXPECT_FALSE(ExtractSimpleSelector(expr.get()).has_value()); } } TEST_F(SimpleSelectorExtractorTest, Challenges) { Expr expr = ExpressionFactory::Challenge(plonk::Challenge(1, plonk::Phase(0))); - EXPECT_FALSE(expr->ExtractSimpleSelector().has_value()); + EXPECT_FALSE(ExtractSimpleSelector(expr.get()).has_value()); } TEST_F(SimpleSelectorExtractorTest, Negated) { GF7 value = GF7::Random(); Expr expr = ExpressionFactory::Negated(ExpressionFactory::Constant(value)); - EXPECT_FALSE(expr->ExtractSimpleSelector().has_value()); + EXPECT_FALSE(ExtractSimpleSelector(expr.get()).has_value()); } TEST_F(SimpleSelectorExtractorTest, Sum) { @@ -97,12 +97,12 @@ TEST_F(SimpleSelectorExtractorTest, Sum) { GF7 b = GF7::Random(); Expr expr = ExpressionFactory::Sum(ExpressionFactory::Constant(a), ExpressionFactory::Constant(b)); - EXPECT_FALSE(expr->ExtractSimpleSelector().has_value()); + EXPECT_FALSE(ExtractSimpleSelector(expr.get()).has_value()); plonk::Selector expected_selector = plonk::Selector::Simple(1); expr = ExpressionFactory::Sum( ExpressionFactory::Constant(a), ExpressionFactory::Selector(expected_selector)); - std::optional selector = expr->ExtractSimpleSelector(); + std::optional selector = ExtractSimpleSelector(expr.get()); EXPECT_EQ(selector.value(), expected_selector); } @@ -111,12 +111,12 @@ TEST_F(SimpleSelectorExtractorTest, Product) { GF7 b = GF7::Random(); Expr expr = ExpressionFactory::Product( ExpressionFactory::Constant(a), ExpressionFactory::Constant(b)); - EXPECT_FALSE(expr->ExtractSimpleSelector().has_value()); + EXPECT_FALSE(ExtractSimpleSelector(expr.get()).has_value()); plonk::Selector expected_selector = plonk::Selector::Simple(1); expr = ExpressionFactory::Product( ExpressionFactory::Constant(a), ExpressionFactory::Selector(expected_selector)); - std::optional selector = expr->ExtractSimpleSelector(); + std::optional selector = ExtractSimpleSelector(expr.get()); EXPECT_EQ(selector.value(), expected_selector); } @@ -125,11 +125,11 @@ TEST_F(SimpleSelectorExtractorTest, Scaled) { GF7 b = GF7::Random(); Expr expr = ExpressionFactory::Scaled(ExpressionFactory::Constant(a), b); - EXPECT_FALSE(expr->ExtractSimpleSelector().has_value()); + EXPECT_FALSE(ExtractSimpleSelector(expr.get()).has_value()); plonk::Selector expected_selector = plonk::Selector::Simple(1); expr = ExpressionFactory::Scaled( ExpressionFactory::Selector(expected_selector), GF7(3)); - std::optional selector = expr->ExtractSimpleSelector(); + std::optional selector = ExtractSimpleSelector(expr.get()); EXPECT_EQ(selector.value(), expected_selector); } diff --git a/tachyon/zk/expressions/expression.h b/tachyon/zk/expressions/expression.h index 248927fef..895b58e94 100644 --- a/tachyon/zk/expressions/expression.h +++ b/tachyon/zk/expressions/expression.h @@ -8,7 +8,6 @@ #define TACHYON_ZK_EXPRESSIONS_EXPRESSION_H_ #include -#include #include #include #include @@ -90,9 +89,6 @@ class Expression { return ExpressionFactory::Negated(Clone()); } - // Extracts a simple selector from this gate, if present. - std::optional ExtractSimpleSelector() const; - static std::vector> CloneExpressions( const std::vector>& expressions) { return base::CreateVector(expressions.size(), [&expressions](size_t i) { diff --git a/tachyon/zk/expressions/expression_factory.h b/tachyon/zk/expressions/expression_factory.h index f86dee94d..b03537b0a 100644 --- a/tachyon/zk/expressions/expression_factory.h +++ b/tachyon/zk/expressions/expression_factory.h @@ -15,7 +15,6 @@ #include "tachyon/zk/expressions/advice_expression.h" #include "tachyon/zk/expressions/challenge_expression.h" #include "tachyon/zk/expressions/constant_expression.h" -#include "tachyon/zk/expressions/evaluator/simple_selector_extractor.h" #include "tachyon/zk/expressions/expression.h" #include "tachyon/zk/expressions/fixed_expression.h" #include "tachyon/zk/expressions/instance_expression.h" diff --git a/tachyon/zk/plonk/constraint_system/BUILD.bazel b/tachyon/zk/plonk/constraint_system/BUILD.bazel index c923144ba..46f551b0d 100644 --- a/tachyon/zk/plonk/constraint_system/BUILD.bazel +++ b/tachyon/zk/plonk/constraint_system/BUILD.bazel @@ -43,6 +43,7 @@ tachyon_cc_library( "//tachyon/base/strings:string_util", "//tachyon/zk/base:row_types", "//tachyon/zk/expressions/evaluator:selector_replacer", + "//tachyon/zk/expressions/evaluator:simple_selector_extractor", "//tachyon/zk/expressions/evaluator:simple_selector_finder", "//tachyon/zk/lookup:lookup_argument", "//tachyon/zk/lookup:type", diff --git a/tachyon/zk/plonk/constraint_system/constraint_system.h b/tachyon/zk/plonk/constraint_system/constraint_system.h index c767f0150..3dc548486 100644 --- a/tachyon/zk/plonk/constraint_system/constraint_system.h +++ b/tachyon/zk/plonk/constraint_system/constraint_system.h @@ -30,6 +30,7 @@ #include "tachyon/base/strings/string_util.h" #include "tachyon/zk/base/row_types.h" #include "tachyon/zk/expressions/evaluator/selector_replacer.h" +#include "tachyon/zk/expressions/evaluator/simple_selector_extractor.h" #include "tachyon/zk/expressions/evaluator/simple_selector_finder.h" #include "tachyon/zk/lookup/lookup_argument.h" #include "tachyon/zk/lookup/type.h" @@ -445,7 +446,8 @@ class ConstraintSystem { std::vector degrees(selectors.size(), size_t{0}); for (const Gate& gate : gates_) { for (const std::unique_ptr>& expression : gate.polys()) { - std::optional selector = expression->ExtractSimpleSelector(); + std::optional selector = + ExtractSimpleSelector(expression.get()); if (selector.has_value()) { degrees[selector->index()] = std::max(degrees[selector->index()], expression->Degree()); From 3c5144cd9997a3056afa5b84de5b3942417ee741 Mon Sep 17 00:00:00 2001 From: batzor <32958247+batzor@users.noreply.github.com> Date: Thu, 27 Jun 2024 16:09:35 +0900 Subject: [PATCH 5/7] refac(zk): separate identifier evaluator from expressions --- tachyon/zk/expressions/advice_expression.h | 5 -- tachyon/zk/expressions/challenge_expression.h | 4 - tachyon/zk/expressions/constant_expression.h | 4 - tachyon/zk/expressions/evaluator/BUILD.bazel | 19 +++++ tachyon/zk/expressions/evaluator/identifier.h | 77 +++++++++++++++++++ tachyon/zk/expressions/expression.h | 9 --- tachyon/zk/expressions/fixed_expression.h | 5 -- tachyon/zk/expressions/instance_expression.h | 5 -- tachyon/zk/expressions/negated_expression.h | 6 -- tachyon/zk/expressions/product_expression.h | 8 -- tachyon/zk/expressions/scaled_expression.h | 4 - tachyon/zk/expressions/selector_expression.h | 4 - tachyon/zk/expressions/sum_expression.h | 8 -- .../zk/plonk/constraint_system/BUILD.bazel | 1 + .../constraint_system/constraint_system.h | 3 +- 15 files changed, 99 insertions(+), 63 deletions(-) create mode 100644 tachyon/zk/expressions/evaluator/identifier.h diff --git a/tachyon/zk/expressions/advice_expression.h b/tachyon/zk/expressions/advice_expression.h index a38963f53..5fe97c08f 100644 --- a/tachyon/zk/expressions/advice_expression.h +++ b/tachyon/zk/expressions/advice_expression.h @@ -42,11 +42,6 @@ class AdviceExpression : public Expression { query_.ToString()); } - void WriteIdentifier(std::ostream& out) const override { - out << "advice[" << query_.column().index() << "][" - << query_.rotation().value() << "]"; - } - bool operator==(const Expression& other) const override { if (!Expression::operator==(other)) return false; const AdviceExpression* advice = other.ToAdvice(); diff --git a/tachyon/zk/expressions/challenge_expression.h b/tachyon/zk/expressions/challenge_expression.h index 8c54e6bab..230ae9b6d 100644 --- a/tachyon/zk/expressions/challenge_expression.h +++ b/tachyon/zk/expressions/challenge_expression.h @@ -44,10 +44,6 @@ class ChallengeExpression : public Expression { challenge_.ToString()); } - void WriteIdentifier(std::ostream& out) const override { - out << "challenge[" << challenge_.index() << "]"; - } - bool operator==(const Expression& other) const override { if (!Expression::operator==(other)) return false; const ChallengeExpression* challenge = other.ToChallenge(); diff --git a/tachyon/zk/expressions/constant_expression.h b/tachyon/zk/expressions/constant_expression.h index e4618dbf9..1e513c72b 100644 --- a/tachyon/zk/expressions/constant_expression.h +++ b/tachyon/zk/expressions/constant_expression.h @@ -42,10 +42,6 @@ class ConstantExpression : public Expression { value_.ToString()); } - void WriteIdentifier(std::ostream& out) const override { - out << value_.ToString(); - } - bool operator==(const Expression& other) const override { if (!Expression::operator==(other)) return false; const ConstantExpression* constant = other.ToConstant(); diff --git a/tachyon/zk/expressions/evaluator/BUILD.bazel b/tachyon/zk/expressions/evaluator/BUILD.bazel index c74d5c69c..c6a98f9ef 100644 --- a/tachyon/zk/expressions/evaluator/BUILD.bazel +++ b/tachyon/zk/expressions/evaluator/BUILD.bazel @@ -2,6 +2,25 @@ load("//bazel:tachyon_cc.bzl", "tachyon_cc_library", "tachyon_cc_unittest") package(default_visibility = ["//visibility:public"]) +tachyon_cc_library( + name = "identifier", + hdrs = ["identifier.h"], + deps = [ + "//tachyon/base:logging", + "//tachyon/zk/expressions:advice_expression", + "//tachyon/zk/expressions:challenge_expression", + "//tachyon/zk/expressions:constant_expression", + "//tachyon/zk/expressions:evaluator", + "//tachyon/zk/expressions:fixed_expression", + "//tachyon/zk/expressions:instance_expression", + "//tachyon/zk/expressions:negated_expression", + "//tachyon/zk/expressions:product_expression", + "//tachyon/zk/expressions:scaled_expression", + "//tachyon/zk/expressions:selector_expression", + "//tachyon/zk/expressions:sum_expression", + ], +) + tachyon_cc_library( name = "selector_replacer", hdrs = ["selector_replacer.h"], diff --git a/tachyon/zk/expressions/evaluator/identifier.h b/tachyon/zk/expressions/evaluator/identifier.h new file mode 100644 index 000000000..7ca47d0f0 --- /dev/null +++ b/tachyon/zk/expressions/evaluator/identifier.h @@ -0,0 +1,77 @@ +#ifndef TACHYON_ZK_EXPRESSIONS_EVALUATOR_IDENTIFIER_H_ +#define TACHYON_ZK_EXPRESSIONS_EVALUATOR_IDENTIFIER_H_ + +#include +#include + +#include "tachyon/base/logging.h" +#include "tachyon/zk/expressions/advice_expression.h" +#include "tachyon/zk/expressions/challenge_expression.h" +#include "tachyon/zk/expressions/constant_expression.h" +#include "tachyon/zk/expressions/expression.h" +#include "tachyon/zk/expressions/expression_type.h" +#include "tachyon/zk/expressions/fixed_expression.h" +#include "tachyon/zk/expressions/instance_expression.h" +#include "tachyon/zk/expressions/negated_expression.h" +#include "tachyon/zk/expressions/product_expression.h" +#include "tachyon/zk/expressions/scaled_expression.h" +#include "tachyon/zk/expressions/selector_expression.h" +#include "tachyon/zk/expressions/sum_expression.h" + +namespace tachyon::zk { + +template +std::string Identifier(const Expression* input) { + std::ostringstream ss; + switch (input->type()) { + case ExpressionType::kConstant: + ss << input->ToConstant()->value().ToString(); + return ss.str(); + case ExpressionType::kFixed: + ss << "fixed[" << input->ToFixed()->query().column().index() << "][" + << input->ToFixed()->query().rotation().value() << "]"; + return ss.str(); + case ExpressionType::kAdvice: + ss << "advice[" << input->ToAdvice()->query().column().index() << "][" + << input->ToFixed()->query().rotation().value() << "]"; + return ss.str(); + case ExpressionType::kInstance: + ss << "instance[" << input->ToInstance()->query().column().index() << "][" + << input->ToFixed()->query().rotation().value() << "]"; + return ss.str(); + case ExpressionType::kChallenge: + ss << "challenge[" << input->ToChallenge()->challenge().index() << "]"; + return ss.str(); + case ExpressionType::kSelector: + ss << "selector[" << input->ToSelector()->selector().index() << "]"; + return ss.str(); + case ExpressionType::kNegated: + ss << "(-"; + ss << Identifier(input->ToNegated()->expr()); + ss << ")"; + return ss.str(); + case ExpressionType::kSum: + ss << "("; + ss << Identifier(input->ToSum()->left()); + ss << "+"; + ss << Identifier(input->ToSum()->right()); + ss << ")"; + return ss.str(); + case ExpressionType::kProduct: + ss << "("; + ss << Identifier(input->ToProduct()->left()); + ss << "*"; + ss << Identifier(input->ToProduct()->right()); + ss << ")"; + return ss.str(); + case ExpressionType::kScaled: + ss << "*" << input->ToScaled()->scale().ToString(); + return ss.str(); + } + NOTREACHED(); + return ""; +} + +} // namespace tachyon::zk + +#endif // TACHYON_ZK_EXPRESSIONS_EVALUATOR_IDENTIFIER_H_ diff --git a/tachyon/zk/expressions/expression.h b/tachyon/zk/expressions/expression.h index 895b58e94..9d7b8ab2e 100644 --- a/tachyon/zk/expressions/expression.h +++ b/tachyon/zk/expressions/expression.h @@ -8,7 +8,6 @@ #define TACHYON_ZK_EXPRESSIONS_EXPRESSION_H_ #include -#include #include #include @@ -72,14 +71,6 @@ class Expression { virtual std::unique_ptr Clone() const = 0; - virtual void WriteIdentifier(std::ostream& out) const = 0; - - std::string Identifier() const { - std::ostringstream ss; - WriteIdentifier(ss); - return ss.str(); - } - virtual bool operator==(const Expression& other) const { return type_ == other.type_; } diff --git a/tachyon/zk/expressions/fixed_expression.h b/tachyon/zk/expressions/fixed_expression.h index c86daff2b..eca9f899b 100644 --- a/tachyon/zk/expressions/fixed_expression.h +++ b/tachyon/zk/expressions/fixed_expression.h @@ -42,11 +42,6 @@ class FixedExpression : public Expression { query_.ToString()); } - void WriteIdentifier(std::ostream& out) const override { - out << "fixed[" << query_.column().index() << "][" - << query_.rotation().value() << "]"; - } - bool operator==(const Expression& other) const override { if (!Expression::operator==(other)) return false; const FixedExpression* fixed = other.ToFixed(); diff --git a/tachyon/zk/expressions/instance_expression.h b/tachyon/zk/expressions/instance_expression.h index 486f0b022..2e55b6463 100644 --- a/tachyon/zk/expressions/instance_expression.h +++ b/tachyon/zk/expressions/instance_expression.h @@ -42,11 +42,6 @@ class InstanceExpression : public Expression { query_.ToString()); } - void WriteIdentifier(std::ostream& out) const override { - out << "instance[" << query_.column().index() << "][" - << query_.rotation().value() << "]"; - } - bool operator==(const Expression& other) const override { if (!Expression::operator==(other)) return false; const InstanceExpression* instance = other.ToInstance(); diff --git a/tachyon/zk/expressions/negated_expression.h b/tachyon/zk/expressions/negated_expression.h index 63ca5df73..6c2cdd91c 100644 --- a/tachyon/zk/expressions/negated_expression.h +++ b/tachyon/zk/expressions/negated_expression.h @@ -47,12 +47,6 @@ class NegatedExpression : public Expression { expr_->ToString()); } - void WriteIdentifier(std::ostream& out) const override { - out << "(-"; - expr_->WriteIdentifier(out); - out << ")"; - } - bool operator==(const Expression& other) const override { if (!Expression::operator==(other)) return false; const NegatedExpression* negated = other.ToNegated(); diff --git a/tachyon/zk/expressions/product_expression.h b/tachyon/zk/expressions/product_expression.h index 4b52fe5ab..959d47313 100644 --- a/tachyon/zk/expressions/product_expression.h +++ b/tachyon/zk/expressions/product_expression.h @@ -51,14 +51,6 @@ class ProductExpression : public Expression { left_->ToString(), right_->ToString()); } - void WriteIdentifier(std::ostream& out) const override { - out << "("; - left_->WriteIdentifier(out); - out << "*"; - right_->WriteIdentifier(out); - out << ")"; - } - bool operator==(const Expression& other) const override { if (!Expression::operator==(other)) return false; const ProductExpression* product = other.ToProduct(); diff --git a/tachyon/zk/expressions/scaled_expression.h b/tachyon/zk/expressions/scaled_expression.h index c7f6e76e5..ea4f2088a 100644 --- a/tachyon/zk/expressions/scaled_expression.h +++ b/tachyon/zk/expressions/scaled_expression.h @@ -48,10 +48,6 @@ class ScaledExpression : public Expression { expr_->ToString(), scale_.ToString()); } - void WriteIdentifier(std::ostream& out) const override { - out << "*" << scale_.ToString(); - } - bool operator==(const Expression& other) const override { if (!Expression::operator==(other)) return false; const ScaledExpression* scaled = other.ToScaled(); diff --git a/tachyon/zk/expressions/selector_expression.h b/tachyon/zk/expressions/selector_expression.h index 48d1595de..315af47bb 100644 --- a/tachyon/zk/expressions/selector_expression.h +++ b/tachyon/zk/expressions/selector_expression.h @@ -43,10 +43,6 @@ class SelectorExpression : public Expression { selector_.ToString()); } - void WriteIdentifier(std::ostream& out) const override { - out << "selector[" << selector_.index() << "]"; - } - bool operator==(const Expression& other) const override { if (!Expression::operator==(other)) return false; const SelectorExpression* selector = other.ToSelector(); diff --git a/tachyon/zk/expressions/sum_expression.h b/tachyon/zk/expressions/sum_expression.h index e266234f6..48cbdaa69 100644 --- a/tachyon/zk/expressions/sum_expression.h +++ b/tachyon/zk/expressions/sum_expression.h @@ -55,14 +55,6 @@ class SumExpression : public Expression { left_->ToString(), right_->ToString()); } - void WriteIdentifier(std::ostream& out) const override { - out << "("; - left_->WriteIdentifier(out); - out << "+"; - right_->WriteIdentifier(out); - out << ")"; - } - bool operator==(const Expression& other) const override { if (!Expression::operator==(other)) return false; const SumExpression* sum = other.ToSum(); diff --git a/tachyon/zk/plonk/constraint_system/BUILD.bazel b/tachyon/zk/plonk/constraint_system/BUILD.bazel index 46f551b0d..cac40bac2 100644 --- a/tachyon/zk/plonk/constraint_system/BUILD.bazel +++ b/tachyon/zk/plonk/constraint_system/BUILD.bazel @@ -42,6 +42,7 @@ tachyon_cc_library( "//tachyon/base/functional:callback", "//tachyon/base/strings:string_util", "//tachyon/zk/base:row_types", + "//tachyon/zk/expressions/evaluator:identifier", "//tachyon/zk/expressions/evaluator:selector_replacer", "//tachyon/zk/expressions/evaluator:simple_selector_extractor", "//tachyon/zk/expressions/evaluator:simple_selector_finder", diff --git a/tachyon/zk/plonk/constraint_system/constraint_system.h b/tachyon/zk/plonk/constraint_system/constraint_system.h index 3dc548486..49cb0a1de 100644 --- a/tachyon/zk/plonk/constraint_system/constraint_system.h +++ b/tachyon/zk/plonk/constraint_system/constraint_system.h @@ -29,6 +29,7 @@ #include "tachyon/base/logging.h" #include "tachyon/base/strings/string_util.h" #include "tachyon/zk/base/row_types.h" +#include "tachyon/zk/expressions/evaluator/identifier.h" #include "tachyon/zk/expressions/evaluator/selector_replacer.h" #include "tachyon/zk/expressions/evaluator/simple_selector_extractor.h" #include "tachyon/zk/expressions/evaluator/simple_selector_finder.h" @@ -726,7 +727,7 @@ class ConstraintSystem { std::vector>>&& table_expressions) { std::stringstream table_expressions_ss; for (const std::unique_ptr>& expr : table_expressions) { - table_expressions_ss << expr->Identifier(); + table_expressions_ss << Identifier(expr.get()); } std::string table_expressions_identifier = table_expressions_ss.str(); From a10ad7410a0ffb9c83ce19cfe543413036a147f0 Mon Sep 17 00:00:00 2001 From: batzor <32958247+batzor@users.noreply.github.com> Date: Thu, 27 Jun 2024 10:10:05 +0900 Subject: [PATCH 6/7] refac(zk): move plonk expressions to plonk namespace --- tachyon/c/zk/plonk/halo2/buffer_reader.h | 13 +- tachyon/zk/expressions/BUILD.bazel | 63 -------- tachyon/zk/expressions/expression.h | 43 ++++-- tachyon/zk/expressions/expression_factory.h | 25 --- tachyon/zk/expressions/expression_type.cc | 16 +- tachyon/zk/expressions/expression_type.h | 20 +-- tachyon/zk/expressions/expression_unittest.cc | 111 ++++++-------- .../product_expression_unittest.cc | 5 +- .../zk/expressions/sum_expression_unittest.cc | 5 +- tachyon/zk/lookup/BUILD.bazel | 24 +-- tachyon/zk/lookup/proving_evaluator.h | 17 +- .../zk/lookup/proving_evaluator_unittest.cc | 23 +-- tachyon/zk/lookup/verifying_evaluator.h | 19 +-- .../zk/plonk/constraint_system/BUILD.bazel | 12 +- .../constraint_system/constraint_system.h | 8 +- .../selector_compressor_unittest.cc | 2 +- .../plonk/constraint_system/virtual_cells.h | 4 +- tachyon/zk/plonk/expressions/BUILD.bazel | 87 +++++++++++ .../expressions/advice_expression.h | 21 +-- .../expressions/advice_expression_unittest.cc | 10 +- .../expressions/challenge_expression.h | 21 +-- .../challenge_expression_unittest.cc | 9 +- .../expressions/evaluator/BUILD.bazel | 20 +-- .../plonk/expressions/evaluator/identifier.h | 86 +++++++++++ .../expressions/evaluator/selector_replacer.h | 14 +- .../evaluator/selector_replacer_unittest.cc | 36 ++--- .../evaluator/simple_selector_extractor.h | 23 ++- .../simple_selector_extractor_unittest.cc | 44 +++--- .../evaluator/simple_selector_finder.h | 12 +- .../simple_selector_finder_unittest.cc | 39 +++-- .../expressions/evaluator/test/BUILD.bazel | 2 +- .../evaluator/test/evaluator_test.h | 10 +- .../zk/plonk/expressions/expression_factory.h | 52 +++++++ .../plonk/expressions/expression_unittest.cc | 145 ++++++++++++++++++ .../expressions/fixed_expression.h | 21 +-- .../expressions/fixed_expression_unittest.cc | 8 +- .../expressions/instance_expression.h | 21 +-- .../instance_expression_unittest.cc | 8 +- .../expressions/selector_expression.h | 21 +-- .../selector_expression_unittest.cc | 8 +- .../zk/plonk/halo2/stringifiers/BUILD.bazel | 10 +- .../stringifiers/expression_stringifier.h | 10 +- tachyon/zk/plonk/vanishing/BUILD.bazel | 14 +- tachyon/zk/plonk/vanishing/graph_evaluator.h | 12 +- .../vanishing/graph_evaluator_unittest.cc | 4 +- 45 files changed, 734 insertions(+), 444 deletions(-) create mode 100644 tachyon/zk/plonk/expressions/BUILD.bazel rename tachyon/zk/{ => plonk}/expressions/advice_expression.h (75%) rename tachyon/zk/{ => plonk}/expressions/advice_expression_unittest.cc (62%) rename tachyon/zk/{ => plonk}/expressions/challenge_expression.h (77%) rename tachyon/zk/{ => plonk}/expressions/challenge_expression_unittest.cc (67%) rename tachyon/zk/{ => plonk}/expressions/evaluator/BUILD.bazel (75%) create mode 100644 tachyon/zk/plonk/expressions/evaluator/identifier.h rename tachyon/zk/{ => plonk}/expressions/evaluator/selector_replacer.h (86%) rename tachyon/zk/{ => plonk}/expressions/evaluator/selector_replacer_unittest.cc (73%) rename tachyon/zk/{ => plonk}/expressions/evaluator/simple_selector_extractor.h (73%) rename tachyon/zk/{ => plonk}/expressions/evaluator/simple_selector_extractor_unittest.cc (70%) rename tachyon/zk/{ => plonk}/expressions/evaluator/simple_selector_finder.h (82%) rename tachyon/zk/{ => plonk}/expressions/evaluator/simple_selector_finder_unittest.cc (70%) rename tachyon/zk/{ => plonk}/expressions/evaluator/test/BUILD.bazel (89%) rename tachyon/zk/{ => plonk}/expressions/evaluator/test/evaluator_test.h (74%) create mode 100644 tachyon/zk/plonk/expressions/expression_factory.h create mode 100644 tachyon/zk/plonk/expressions/expression_unittest.cc rename tachyon/zk/{ => plonk}/expressions/fixed_expression.h (75%) rename tachyon/zk/{ => plonk}/expressions/fixed_expression_unittest.cc (71%) rename tachyon/zk/{ => plonk}/expressions/instance_expression.h (75%) rename tachyon/zk/{ => plonk}/expressions/instance_expression_unittest.cc (70%) rename tachyon/zk/{ => plonk}/expressions/selector_expression.h (77%) rename tachyon/zk/{ => plonk}/expressions/selector_expression_unittest.cc (68%) diff --git a/tachyon/c/zk/plonk/halo2/buffer_reader.h b/tachyon/c/zk/plonk/halo2/buffer_reader.h index 4ecede280..f8ddd0067 100644 --- a/tachyon/c/zk/plonk/halo2/buffer_reader.h +++ b/tachyon/c/zk/plonk/halo2/buffer_reader.h @@ -295,24 +295,27 @@ class BufferReader>> { static std::unique_ptr> Read( const tachyon::base::ReadOnlyBuffer& buffer) { uint8_t kind = BufferReader::Read(buffer); + // NOTE(batzor): this switch statement is hardcoded to be compliant with + // halo2 rust implementation. + // https://github.com/kroma-network/halo2/blob/4ad135/halo2_proofs/src/plonk/circuit.rs#L993 switch (kind) { case 0: return tachyon::zk::ExpressionFactory::Constant( BufferReader::Read(buffer)); case 1: - return tachyon::zk::ExpressionFactory::Selector( + return tachyon::zk::plonk::ExpressionFactory::Selector( BufferReader::Read(buffer)); case 2: - return tachyon::zk::ExpressionFactory::Fixed( + return tachyon::zk::plonk::ExpressionFactory::Fixed( BufferReader::Read(buffer)); case 3: - return tachyon::zk::ExpressionFactory::Advice( + return tachyon::zk::plonk::ExpressionFactory::Advice( BufferReader::Read(buffer)); case 4: - return tachyon::zk::ExpressionFactory::Instance( + return tachyon::zk::plonk::ExpressionFactory::Instance( BufferReader::Read(buffer)); case 5: - return tachyon::zk::ExpressionFactory::Challenge( + return tachyon::zk::plonk::ExpressionFactory::Challenge( BufferReader::Read(buffer)); case 6: return tachyon::zk::ExpressionFactory::Negated( diff --git a/tachyon/zk/expressions/BUILD.bazel b/tachyon/zk/expressions/BUILD.bazel index fb08ef9df..2a213f1d5 100644 --- a/tachyon/zk/expressions/BUILD.bazel +++ b/tachyon/zk/expressions/BUILD.bazel @@ -2,27 +2,6 @@ load("//bazel:tachyon_cc.bzl", "tachyon_cc_library", "tachyon_cc_unittest") package(default_visibility = ["//visibility:public"]) -tachyon_cc_library( - name = "advice_expression", - hdrs = ["advice_expression.h"], - deps = [ - ":expression", - "//tachyon/zk/plonk/constraint_system:query", - "@com_google_absl//absl/memory", - ], -) - -tachyon_cc_library( - name = "challenge_expression", - hdrs = ["challenge_expression.h"], - deps = [ - ":expression", - "//tachyon/zk/plonk/constraint_system:challenge", - "@com_google_absl//absl/memory", - "@com_google_absl//absl/strings", - ], -) - tachyon_cc_library( name = "constant_expression", hdrs = ["constant_expression.h"], @@ -45,7 +24,6 @@ tachyon_cc_library( deps = [ ":expression_type", "//tachyon/base/containers:container_util", - "//tachyon/zk/plonk/constraint_system:selector", ], ) @@ -53,15 +31,10 @@ tachyon_cc_library( name = "expression_factory", hdrs = ["expression_factory.h"], deps = [ - ":advice_expression", - ":challenge_expression", ":constant_expression", - ":fixed_expression", - ":instance_expression", ":negated_expression", ":product_expression", ":scaled_expression", - ":selector_expression", ":sum_expression", ], ) @@ -73,26 +46,6 @@ tachyon_cc_library( deps = ["//tachyon/base:logging"], ) -tachyon_cc_library( - name = "fixed_expression", - hdrs = ["fixed_expression.h"], - deps = [ - ":expression", - "//tachyon/zk/plonk/constraint_system:query", - "@com_google_absl//absl/memory", - ], -) - -tachyon_cc_library( - name = "instance_expression", - hdrs = ["instance_expression.h"], - deps = [ - ":expression", - "//tachyon/zk/plonk/constraint_system:query", - "@com_google_absl//absl/memory", - ], -) - tachyon_cc_library( name = "negated_expression", hdrs = ["negated_expression.h"], @@ -123,16 +76,6 @@ tachyon_cc_library( ], ) -tachyon_cc_library( - name = "selector_expression", - hdrs = ["selector_expression.h"], - deps = [ - ":expression", - "@com_google_absl//absl/memory", - "@com_google_absl//absl/strings", - ], -) - tachyon_cc_library( name = "sum_expression", hdrs = ["sum_expression.h"], @@ -146,16 +89,11 @@ tachyon_cc_library( tachyon_cc_unittest( name = "expression_unittests", srcs = [ - "advice_expression_unittest.cc", - "challenge_expression_unittest.cc", "constant_expression_unittest.cc", "expression_unittest.cc", - "fixed_expression_unittest.cc", - "instance_expression_unittest.cc", "negated_expression_unittest.cc", "product_expression_unittest.cc", "scaled_expression_unittest.cc", - "selector_expression_unittest.cc", "sum_expression_unittest.cc", ], deps = [ @@ -163,6 +101,5 @@ tachyon_cc_unittest( "//tachyon/math/finite_fields/test:finite_field_test", "//tachyon/math/finite_fields/test:gf7", "//tachyon/math/polynomials/univariate:univariate_polynomial", - "//tachyon/zk/expressions/evaluator:simple_selector_finder", ], ) diff --git a/tachyon/zk/expressions/expression.h b/tachyon/zk/expressions/expression.h index 9d7b8ab2e..3a1998025 100644 --- a/tachyon/zk/expressions/expression.h +++ b/tachyon/zk/expressions/expression.h @@ -14,7 +14,6 @@ #include "tachyon/base/containers/container_util.h" #include "tachyon/base/logging.h" #include "tachyon/zk/expressions/expression_type.h" -#include "tachyon/zk/plonk/constraint_system/selector.h" namespace tachyon::zk { @@ -28,31 +27,33 @@ template class ConstantExpression; template -class SelectorExpression; +class NegatedExpression; template -class FixedExpression; +class SumExpression; template -class AdviceExpression; +class ProductExpression; template -class InstanceExpression; +class ScaledExpression; +namespace plonk { template -class ChallengeExpression; +class SelectorExpression; template -class NegatedExpression; +class FixedExpression; template -class SumExpression; +class AdviceExpression; template -class ProductExpression; +class InstanceExpression; template -class ScaledExpression; +class ChallengeExpression; +} // namespace plonk // A Expression represents a polynomial. template @@ -104,11 +105,6 @@ class Expression { } DEFINE_CONVERSION_METHOD(Constant) - DEFINE_CONVERSION_METHOD(Selector) - DEFINE_CONVERSION_METHOD(Fixed) - DEFINE_CONVERSION_METHOD(Advice) - DEFINE_CONVERSION_METHOD(Instance) - DEFINE_CONVERSION_METHOD(Challenge) DEFINE_CONVERSION_METHOD(Negated) DEFINE_CONVERSION_METHOD(Sum) DEFINE_CONVERSION_METHOD(Product) @@ -116,6 +112,23 @@ class Expression { #undef DEFINE_CONVERSION_METHOD +#define DEFINE_NAMESPACED_CONVERSION_METHOD(ns, type) \ + ns::type##Expression* To##type() { \ + CHECK_EQ(type_, ExpressionType::k##type); \ + return static_cast*>(this); \ + } \ + \ + const ns::type##Expression* To##type() const { \ + CHECK_EQ(type_, ExpressionType::k##type); \ + return static_cast*>(this); \ + } + DEFINE_NAMESPACED_CONVERSION_METHOD(plonk, Selector) + DEFINE_NAMESPACED_CONVERSION_METHOD(plonk, Fixed) + DEFINE_NAMESPACED_CONVERSION_METHOD(plonk, Advice) + DEFINE_NAMESPACED_CONVERSION_METHOD(plonk, Instance) + DEFINE_NAMESPACED_CONVERSION_METHOD(plonk, Challenge) +#undef DEFINE_NAMESPACED_CONVERSION_METHOD + protected: explicit Expression(ExpressionType type) : type_(type) {} diff --git a/tachyon/zk/expressions/expression_factory.h b/tachyon/zk/expressions/expression_factory.h index b03537b0a..798db032a 100644 --- a/tachyon/zk/expressions/expression_factory.h +++ b/tachyon/zk/expressions/expression_factory.h @@ -12,16 +12,11 @@ #include "absl/memory/memory.h" -#include "tachyon/zk/expressions/advice_expression.h" -#include "tachyon/zk/expressions/challenge_expression.h" #include "tachyon/zk/expressions/constant_expression.h" #include "tachyon/zk/expressions/expression.h" -#include "tachyon/zk/expressions/fixed_expression.h" -#include "tachyon/zk/expressions/instance_expression.h" #include "tachyon/zk/expressions/negated_expression.h" #include "tachyon/zk/expressions/product_expression.h" #include "tachyon/zk/expressions/scaled_expression.h" -#include "tachyon/zk/expressions/selector_expression.h" #include "tachyon/zk/expressions/sum_expression.h" namespace tachyon::zk { @@ -35,26 +30,6 @@ class ExpressionFactory { return absl::WrapUnique(new ConstantExpression(value)); } - static std::unique_ptr Selector(plonk::Selector selector) { - return absl::WrapUnique(new SelectorExpression(selector)); - } - - static std::unique_ptr Fixed(const plonk::FixedQuery& query) { - return absl::WrapUnique(new FixedExpression(query)); - } - - static std::unique_ptr Advice(const plonk::AdviceQuery& query) { - return absl::WrapUnique(new AdviceExpression(query)); - } - - static std::unique_ptr Instance(const plonk::InstanceQuery& query) { - return absl::WrapUnique(new InstanceExpression(query)); - } - - static std::unique_ptr Challenge(plonk::Challenge challenge) { - return absl::WrapUnique(new ChallengeExpression(challenge)); - } - static std::unique_ptr Negated(std::unique_ptr value) { return absl::WrapUnique(new NegatedExpression(std::move(value))); } diff --git a/tachyon/zk/expressions/expression_type.cc b/tachyon/zk/expressions/expression_type.cc index 5b84c0b19..9e4259b71 100644 --- a/tachyon/zk/expressions/expression_type.cc +++ b/tachyon/zk/expressions/expression_type.cc @@ -14,6 +14,14 @@ std::string_view ExpressionTypeToString(ExpressionType type) { switch (type) { case ExpressionType::kConstant: return "Constant"; + case ExpressionType::kNegated: + return "Negated"; + case ExpressionType::kSum: + return "Sum"; + case ExpressionType::kProduct: + return "Product"; + case ExpressionType::kScaled: + return "Scaled"; case ExpressionType::kSelector: return "Selector"; case ExpressionType::kFixed: @@ -24,14 +32,6 @@ std::string_view ExpressionTypeToString(ExpressionType type) { return "Instance"; case ExpressionType::kChallenge: return "Challenge"; - case ExpressionType::kNegated: - return "Negated"; - case ExpressionType::kSum: - return "Sum"; - case ExpressionType::kProduct: - return "Product"; - case ExpressionType::kScaled: - return "Scaled"; } NOTREACHED(); return ""; diff --git a/tachyon/zk/expressions/expression_type.h b/tachyon/zk/expressions/expression_type.h index 7721e7c64..2c7123b0b 100644 --- a/tachyon/zk/expressions/expression_type.h +++ b/tachyon/zk/expressions/expression_type.h @@ -15,16 +15,16 @@ namespace tachyon::zk { enum class ExpressionType { - kConstant, - kSelector, - kFixed, - kAdvice, - kInstance, - kChallenge, - kNegated, - kSum, - kProduct, - kScaled + kConstant = 1, + kNegated = 2, + kSum = 3, + kProduct = 4, + kScaled = 5, + kSelector = 21, + kFixed = 22, + kAdvice = 23, + kInstance = 24, + kChallenge = 25, }; TACHYON_EXPORT std::string_view ExpressionTypeToString(ExpressionType type); diff --git a/tachyon/zk/expressions/expression_unittest.cc b/tachyon/zk/expressions/expression_unittest.cc index a53f9b6a7..37e976774 100644 --- a/tachyon/zk/expressions/expression_unittest.cc +++ b/tachyon/zk/expressions/expression_unittest.cc @@ -3,7 +3,6 @@ #include "tachyon/base/random.h" #include "tachyon/math/finite_fields/test/finite_field_test.h" #include "tachyon/math/finite_fields/test/gf7.h" -#include "tachyon/zk/expressions/evaluator/simple_selector_finder.h" #include "tachyon/zk/expressions/expression_factory.h" namespace tachyon::zk { @@ -16,18 +15,6 @@ class ExpressionTest : public math::FiniteFieldTest { public: void SetUp() override { expressions_.push_back(ExpressionFactory::Constant(F(1))); - expressions_.push_back( - ExpressionFactory::Selector(plonk::Selector::Simple(1))); - expressions_.push_back( - ExpressionFactory::Selector(plonk::Selector::Complex(2))); - expressions_.push_back(ExpressionFactory::Fixed( - plonk::FixedQuery(1, Rotation(1), plonk::FixedColumnKey(1)))); - expressions_.push_back(ExpressionFactory::Advice(plonk::AdviceQuery( - 1, Rotation(1), plonk::AdviceColumnKey(1, plonk::Phase(0))))); - expressions_.push_back(ExpressionFactory::Instance( - plonk::InstanceQuery(1, Rotation(1), plonk::InstanceColumnKey(1)))); - expressions_.push_back( - ExpressionFactory::Challenge(plonk::Challenge(1, plonk::Phase(0)))); expressions_.push_back( ExpressionFactory::Negated(ExpressionFactory::Constant(F(1)))); expressions_.push_back( @@ -52,26 +39,23 @@ TEST_F(ExpressionTest, ArithmeticOperatorWithClone) { std::unique_ptr> right = base::UniformElement(expressions_)->Clone(); - if (!ContainsSimpleSelector(left.get()) && - !ContainsSimpleSelector(right.get())) { - std::unique_ptr> add = left + right; - EXPECT_EQ(*add->ToSum()->left(), *left); - EXPECT_EQ(*add->ToSum()->right(), *right); - EXPECT_TRUE(left); - EXPECT_TRUE(right); + std::unique_ptr> add = left + right; + EXPECT_EQ(*add->ToSum()->left(), *left); + EXPECT_EQ(*add->ToSum()->right(), *right); + EXPECT_TRUE(left); + EXPECT_TRUE(right); - std::unique_ptr> sub = left - right; - EXPECT_EQ(*sub->ToSum()->left(), *left); - EXPECT_EQ(*sub->ToSum()->right()->ToNegated()->expr(), *right); - EXPECT_TRUE(left); - EXPECT_TRUE(right); + std::unique_ptr> sub = left - right; + EXPECT_EQ(*sub->ToSum()->left(), *left); + EXPECT_EQ(*sub->ToSum()->right()->ToNegated()->expr(), *right); + EXPECT_TRUE(left); + EXPECT_TRUE(right); - std::unique_ptr> mul = left * right; - EXPECT_EQ(*mul->ToProduct()->left(), *left); - EXPECT_EQ(*mul->ToProduct()->right(), *right); - EXPECT_TRUE(left); - EXPECT_TRUE(right); - } + std::unique_ptr> mul = left * right; + EXPECT_EQ(*mul->ToProduct()->left(), *left); + EXPECT_EQ(*mul->ToProduct()->right(), *right); + EXPECT_TRUE(left); + EXPECT_TRUE(right); F scale = F::Random(); std::unique_ptr> scaled = left * scale; @@ -90,40 +74,37 @@ TEST_F(ExpressionTest, ArithmeticOperatorWithMove) { std::unique_ptr> right = base::UniformElement(expressions_)->Clone(); - if (!ContainsSimpleSelector(left.get()) && - !ContainsSimpleSelector(right.get())) { - { - std::unique_ptr> left_tmp = left->Clone(); - std::unique_ptr> right_tmp = right->Clone(); - std::unique_ptr> add = - std::move(left_tmp) + std::move(right_tmp); - EXPECT_EQ(*add->ToSum()->left(), *left); - EXPECT_EQ(*add->ToSum()->right(), *right); - EXPECT_FALSE(left_tmp); - EXPECT_FALSE(right_tmp); - } - - { - std::unique_ptr> left_tmp = left->Clone(); - std::unique_ptr> right_tmp = right->Clone(); - std::unique_ptr> sub = - std::move(left_tmp) - std::move(right_tmp); - EXPECT_EQ(*sub->ToSum()->left(), *left); - EXPECT_EQ(*sub->ToSum()->right()->ToNegated()->expr(), *right); - EXPECT_FALSE(left_tmp); - EXPECT_FALSE(right_tmp); - } - - { - std::unique_ptr> left_tmp = left->Clone(); - std::unique_ptr> right_tmp = right->Clone(); - std::unique_ptr> mul = - std::move(left_tmp) * std::move(right_tmp); - EXPECT_EQ(*mul->ToProduct()->left(), *left); - EXPECT_EQ(*mul->ToProduct()->right(), *right); - EXPECT_FALSE(left_tmp); - EXPECT_FALSE(right_tmp); - } + { + std::unique_ptr> left_tmp = left->Clone(); + std::unique_ptr> right_tmp = right->Clone(); + std::unique_ptr> add = + std::move(left_tmp) + std::move(right_tmp); + EXPECT_EQ(*add->ToSum()->left(), *left); + EXPECT_EQ(*add->ToSum()->right(), *right); + EXPECT_FALSE(left_tmp); + EXPECT_FALSE(right_tmp); + } + + { + std::unique_ptr> left_tmp = left->Clone(); + std::unique_ptr> right_tmp = right->Clone(); + std::unique_ptr> sub = + std::move(left_tmp) - std::move(right_tmp); + EXPECT_EQ(*sub->ToSum()->left(), *left); + EXPECT_EQ(*sub->ToSum()->right()->ToNegated()->expr(), *right); + EXPECT_FALSE(left_tmp); + EXPECT_FALSE(right_tmp); + } + + { + std::unique_ptr> left_tmp = left->Clone(); + std::unique_ptr> right_tmp = right->Clone(); + std::unique_ptr> mul = + std::move(left_tmp) * std::move(right_tmp); + EXPECT_EQ(*mul->ToProduct()->left(), *left); + EXPECT_EQ(*mul->ToProduct()->right(), *right); + EXPECT_FALSE(left_tmp); + EXPECT_FALSE(right_tmp); } { diff --git a/tachyon/zk/expressions/product_expression_unittest.cc b/tachyon/zk/expressions/product_expression_unittest.cc index e1ebbf4d7..0b84321d3 100644 --- a/tachyon/zk/expressions/product_expression_unittest.cc +++ b/tachyon/zk/expressions/product_expression_unittest.cc @@ -7,7 +7,6 @@ #include "tachyon/math/finite_fields/test/finite_field_test.h" #include "tachyon/math/finite_fields/test/gf7.h" #include "tachyon/zk/expressions/constant_expression.h" -#include "tachyon/zk/expressions/selector_expression.h" namespace tachyon::zk { @@ -18,8 +17,8 @@ class ProductExpressionTest : public math::FiniteFieldTest {}; TEST_F(ProductExpressionTest, DegreeComplexity) { std::unique_ptr> left = ConstantExpression::CreateForTesting(F::One()); - std::unique_ptr> right = - SelectorExpression::CreateForTesting(plonk::Selector::Simple(1)); + std::unique_ptr> right = + ConstantExpression::CreateForTesting(F::Zero()); size_t left_degree = left->Degree(); uint64_t left_complexity = left->Complexity(); diff --git a/tachyon/zk/expressions/sum_expression_unittest.cc b/tachyon/zk/expressions/sum_expression_unittest.cc index 6f94ec393..4181571bb 100644 --- a/tachyon/zk/expressions/sum_expression_unittest.cc +++ b/tachyon/zk/expressions/sum_expression_unittest.cc @@ -5,7 +5,6 @@ #include "tachyon/math/finite_fields/test/finite_field_test.h" #include "tachyon/math/finite_fields/test/gf7.h" #include "tachyon/zk/expressions/constant_expression.h" -#include "tachyon/zk/expressions/selector_expression.h" namespace tachyon::zk { @@ -16,8 +15,8 @@ class SumExpressionTest : public math::FiniteFieldTest {}; TEST_F(SumExpressionTest, DegreeComplexity) { std::unique_ptr> left = ConstantExpression::CreateForTesting(F::One()); - std::unique_ptr> right = - SelectorExpression::CreateForTesting(plonk::Selector::Simple(1)); + std::unique_ptr> right = + ConstantExpression::CreateForTesting(F::One()); size_t left_degree = left->Degree(); uint64_t left_complexity = left->Complexity(); diff --git a/tachyon/zk/lookup/BUILD.bazel b/tachyon/zk/lookup/BUILD.bazel index a43bdf0f5..796191edd 100644 --- a/tachyon/zk/lookup/BUILD.bazel +++ b/tachyon/zk/lookup/BUILD.bazel @@ -21,18 +21,18 @@ tachyon_cc_library( name = "proving_evaluator", hdrs = ["proving_evaluator.h"], deps = [ - "//tachyon/zk/expressions:advice_expression", - "//tachyon/zk/expressions:challenge_expression", "//tachyon/zk/expressions:constant_expression", "//tachyon/zk/expressions:evaluator", - "//tachyon/zk/expressions:fixed_expression", - "//tachyon/zk/expressions:instance_expression", "//tachyon/zk/expressions:negated_expression", "//tachyon/zk/expressions:product_expression", "//tachyon/zk/expressions:scaled_expression", - "//tachyon/zk/expressions:selector_expression", "//tachyon/zk/expressions:sum_expression", "//tachyon/zk/plonk/base:multi_phase_ref_table", + "//tachyon/zk/plonk/expressions:advice_expression", + "//tachyon/zk/plonk/expressions:challenge_expression", + "//tachyon/zk/plonk/expressions:fixed_expression", + "//tachyon/zk/plonk/expressions:instance_expression", + "//tachyon/zk/plonk/expressions:selector_expression", ], ) @@ -59,18 +59,18 @@ tachyon_cc_library( name = "verifying_evaluator", hdrs = ["verifying_evaluator.h"], deps = [ - "//tachyon/zk/expressions:advice_expression", - "//tachyon/zk/expressions:challenge_expression", "//tachyon/zk/expressions:constant_expression", "//tachyon/zk/expressions:evaluator", - "//tachyon/zk/expressions:fixed_expression", - "//tachyon/zk/expressions:instance_expression", "//tachyon/zk/expressions:negated_expression", "//tachyon/zk/expressions:product_expression", "//tachyon/zk/expressions:scaled_expression", - "//tachyon/zk/expressions:selector_expression", "//tachyon/zk/expressions:sum_expression", "//tachyon/zk/plonk/base:multi_phase_evaluations", + "//tachyon/zk/plonk/expressions:advice_expression", + "//tachyon/zk/plonk/expressions:challenge_expression", + "//tachyon/zk/plonk/expressions:fixed_expression", + "//tachyon/zk/plonk/expressions:instance_expression", + "//tachyon/zk/plonk/expressions:selector_expression", ], ) @@ -79,7 +79,7 @@ tachyon_cc_unittest( srcs = ["proving_evaluator_unittest.cc"], deps = [ ":proving_evaluator", - "//tachyon/zk/expressions:expression_factory", - "//tachyon/zk/expressions/evaluator/test:evaluator_test", + "//tachyon/zk/plonk/expressions:expression_factory", + "//tachyon/zk/plonk/expressions/evaluator/test:evaluator_test", ], ) diff --git a/tachyon/zk/lookup/proving_evaluator.h b/tachyon/zk/lookup/proving_evaluator.h index 1b1924b57..192f135b0 100644 --- a/tachyon/zk/lookup/proving_evaluator.h +++ b/tachyon/zk/lookup/proving_evaluator.h @@ -11,18 +11,18 @@ #include "absl/types/span.h" -#include "tachyon/zk/expressions/advice_expression.h" -#include "tachyon/zk/expressions/challenge_expression.h" #include "tachyon/zk/expressions/constant_expression.h" #include "tachyon/zk/expressions/evaluator.h" -#include "tachyon/zk/expressions/fixed_expression.h" -#include "tachyon/zk/expressions/instance_expression.h" #include "tachyon/zk/expressions/negated_expression.h" #include "tachyon/zk/expressions/product_expression.h" #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/base/multi_phase_ref_table.h" +#include "tachyon/zk/plonk/expressions/advice_expression.h" +#include "tachyon/zk/plonk/expressions/challenge_expression.h" +#include "tachyon/zk/plonk/expressions/fixed_expression.h" +#include "tachyon/zk/plonk/expressions/instance_expression.h" +#include "tachyon/zk/plonk/expressions/selector_expression.h" namespace tachyon::zk::lookup { @@ -63,21 +63,22 @@ class ProvingEvaluator break; case ExpressionType::kFixed: { - const FixedExpression* fixed_expr = input->ToFixed(); + const plonk::FixedExpression* fixed_expr = input->ToFixed(); const plonk::FixedQuery& query = fixed_expr->query(); const Evals& evals = table_.GetFixedColumns()[query.column().index()]; return evals[query.rotation().GetIndex(idx_, rot_scale_, size_)]; } case ExpressionType::kAdvice: { - const AdviceExpression* advice_expr = input->ToAdvice(); + const plonk::AdviceExpression* advice_expr = input->ToAdvice(); const plonk::AdviceQuery& query = advice_expr->query(); const Evals& evals = table_.GetAdviceColumns()[query.column().index()]; return evals[query.rotation().GetIndex(idx_, rot_scale_, size_)]; } case ExpressionType::kInstance: { - const InstanceExpression* instance_expr = input->ToInstance(); + const plonk::InstanceExpression* instance_expr = + input->ToInstance(); const plonk::InstanceQuery& query = instance_expr->query(); const Evals& evals = table_.GetInstanceColumns()[query.column().index()]; diff --git a/tachyon/zk/lookup/proving_evaluator_unittest.cc b/tachyon/zk/lookup/proving_evaluator_unittest.cc index bf6d1be5d..17ed614dc 100644 --- a/tachyon/zk/lookup/proving_evaluator_unittest.cc +++ b/tachyon/zk/lookup/proving_evaluator_unittest.cc @@ -2,16 +2,20 @@ #include -#include "tachyon/zk/expressions/evaluator/test/evaluator_test.h" -#include "tachyon/zk/expressions/expression_factory.h" +#include "tachyon/zk/plonk/expressions/evaluator/test/evaluator_test.h" +#include "tachyon/zk/plonk/expressions/expression_factory.h" namespace tachyon::zk::lookup { namespace { +constexpr size_t kMaxDegree = 5; + +using GF7 = math::GF7; +using Evals = math::UnivariateEvaluations; using Expr = std::unique_ptr>; -class ProvingEvaluatorTest : public EvaluatorTest { +class ProvingEvaluatorTest : public plonk::EvaluatorTest { public: void SetUp() override { std::vector evaluations; @@ -46,7 +50,8 @@ TEST_F(ProvingEvaluatorTest, Constant) { } TEST_F(ProvingEvaluatorTest, Selector) { - Expr expr = ExpressionFactory::Selector(plonk::Selector::Simple(1)); + Expr expr = + plonk::ExpressionFactory::Selector(plonk::Selector::Simple(1)); EXPECT_DEATH(proving_evaluator_->Evaluate(expr.get()), ""); } @@ -70,7 +75,7 @@ TEST_F(ProvingEvaluatorTest, Fixed) { RowIndex row_index = query.rotation().GetIndex(idx, rot_scale, size); const GF7& expected = fixed_columns_[test.column_index][row_index]; - Expr expr = ExpressionFactory::Fixed(query); + Expr expr = plonk::ExpressionFactory::Fixed(query); GF7 evaluated = proving_evaluator_->Evaluate(expr.get()); EXPECT_EQ(evaluated, expected); } @@ -97,7 +102,7 @@ TEST_F(ProvingEvaluatorTest, Advice) { RowIndex row_index = query.rotation().GetIndex(idx, rot_scale, size); const GF7& expected = advice_columns_[test.column_index][row_index]; - Expr expr = ExpressionFactory::Advice(query); + Expr expr = plonk::ExpressionFactory::Advice(query); GF7 evaluated = proving_evaluator_->Evaluate(expr.get()); EXPECT_EQ(evaluated, expected); } @@ -123,7 +128,7 @@ TEST_F(ProvingEvaluatorTest, Instance) { RowIndex row_index = query.rotation().GetIndex(idx, rot_scale, size); const GF7& expected = instance_columns_[test.column_index][row_index]; - Expr expr = ExpressionFactory::Instance(query); + Expr expr = plonk::ExpressionFactory::Instance(query); GF7 evaluated = proving_evaluator_->Evaluate(expr.get()); EXPECT_EQ(evaluated, expected); } @@ -131,8 +136,8 @@ TEST_F(ProvingEvaluatorTest, Instance) { TEST_F(ProvingEvaluatorTest, Challenges) { for (size_t i = 0; i < challenges_.size(); ++i) { - Expr expr = - ExpressionFactory::Challenge(plonk::Challenge(i, plonk::Phase(0))); + Expr expr = plonk::ExpressionFactory::Challenge( + plonk::Challenge(i, plonk::Phase(0))); GF7 evaluated = proving_evaluator_->Evaluate(expr.get()); GF7 expected = challenges_[i]; EXPECT_EQ(evaluated, expected); diff --git a/tachyon/zk/lookup/verifying_evaluator.h b/tachyon/zk/lookup/verifying_evaluator.h index 65d4b8a83..ebe34b4da 100644 --- a/tachyon/zk/lookup/verifying_evaluator.h +++ b/tachyon/zk/lookup/verifying_evaluator.h @@ -7,18 +7,18 @@ #ifndef TACHYON_ZK_LOOKUP_VERIFYING_EVALUATOR_H_ #define TACHYON_ZK_LOOKUP_VERIFYING_EVALUATOR_H_ -#include "tachyon/zk/expressions/advice_expression.h" -#include "tachyon/zk/expressions/challenge_expression.h" #include "tachyon/zk/expressions/constant_expression.h" #include "tachyon/zk/expressions/evaluator.h" -#include "tachyon/zk/expressions/fixed_expression.h" -#include "tachyon/zk/expressions/instance_expression.h" #include "tachyon/zk/expressions/negated_expression.h" #include "tachyon/zk/expressions/product_expression.h" #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/base/multi_phase_evaluations.h" +#include "tachyon/zk/plonk/expressions/advice_expression.h" +#include "tachyon/zk/plonk/expressions/challenge_expression.h" +#include "tachyon/zk/plonk/expressions/fixed_expression.h" +#include "tachyon/zk/plonk/expressions/instance_expression.h" +#include "tachyon/zk/plonk/expressions/selector_expression.h" namespace tachyon::zk::lookup { @@ -39,25 +39,26 @@ class VerifyingEvaluator : public Evaluator { break; case ExpressionType::kFixed: { - const FixedExpression* fixed_expr = input->ToFixed(); + const plonk::FixedExpression* fixed_expr = input->ToFixed(); const plonk::FixedQuery& query = fixed_expr->query(); return data_.fixed_evals[query.index()]; } case ExpressionType::kAdvice: { - const AdviceExpression* advice_expr = input->ToAdvice(); + const plonk::AdviceExpression* advice_expr = input->ToAdvice(); const plonk::AdviceQuery& query = advice_expr->query(); return data_.advice_evals[query.index()]; } case ExpressionType::kInstance: { - const InstanceExpression* instance_expr = input->ToInstance(); + const plonk::InstanceExpression* instance_expr = input->ToInstance(); const plonk::InstanceQuery& query = instance_expr->query(); return data_.instance_evals[query.index()]; } case ExpressionType::kChallenge: { - const ChallengeExpression* challenge_expr = input->ToChallenge(); + const plonk::ChallengeExpression* challenge_expr = + input->ToChallenge(); plonk::Challenge challenge = challenge_expr->challenge(); return data_.challenges[challenge.index()]; } diff --git a/tachyon/zk/plonk/constraint_system/BUILD.bazel b/tachyon/zk/plonk/constraint_system/BUILD.bazel index cac40bac2..d7c12e4c2 100644 --- a/tachyon/zk/plonk/constraint_system/BUILD.bazel +++ b/tachyon/zk/plonk/constraint_system/BUILD.bazel @@ -42,12 +42,12 @@ tachyon_cc_library( "//tachyon/base/functional:callback", "//tachyon/base/strings:string_util", "//tachyon/zk/base:row_types", - "//tachyon/zk/expressions/evaluator:identifier", - "//tachyon/zk/expressions/evaluator:selector_replacer", - "//tachyon/zk/expressions/evaluator:simple_selector_extractor", - "//tachyon/zk/expressions/evaluator:simple_selector_finder", "//tachyon/zk/lookup:lookup_argument", "//tachyon/zk/lookup:type", + "//tachyon/zk/plonk/expressions/evaluator:identifier", + "//tachyon/zk/plonk/expressions/evaluator:selector_replacer", + "//tachyon/zk/plonk/expressions/evaluator:simple_selector_extractor", + "//tachyon/zk/plonk/expressions/evaluator:simple_selector_finder", "//tachyon/zk/plonk/keys:c_proving_key_impl_base_forward", "//tachyon/zk/plonk/layout:lookup_table_column", "//tachyon/zk/plonk/permutation:permutation_argument", @@ -151,8 +151,8 @@ tachyon_cc_library( deps = [ ":selector", ":virtual_cell", - "//tachyon/zk/expressions:expression_factory", - "//tachyon/zk/expressions/evaluator:simple_selector_finder", + "//tachyon/zk/plonk/expressions:expression_factory", + "//tachyon/zk/plonk/expressions/evaluator:simple_selector_finder", "//tachyon/zk/plonk/layout:lookup_table_column", ], ) diff --git a/tachyon/zk/plonk/constraint_system/constraint_system.h b/tachyon/zk/plonk/constraint_system/constraint_system.h index 49cb0a1de..787570ede 100644 --- a/tachyon/zk/plonk/constraint_system/constraint_system.h +++ b/tachyon/zk/plonk/constraint_system/constraint_system.h @@ -29,10 +29,6 @@ #include "tachyon/base/logging.h" #include "tachyon/base/strings/string_util.h" #include "tachyon/zk/base/row_types.h" -#include "tachyon/zk/expressions/evaluator/identifier.h" -#include "tachyon/zk/expressions/evaluator/selector_replacer.h" -#include "tachyon/zk/expressions/evaluator/simple_selector_extractor.h" -#include "tachyon/zk/expressions/evaluator/simple_selector_finder.h" #include "tachyon/zk/lookup/lookup_argument.h" #include "tachyon/zk/lookup/type.h" #include "tachyon/zk/plonk/constraint_system/constraint.h" @@ -41,6 +37,10 @@ #include "tachyon/zk/plonk/constraint_system/query.h" #include "tachyon/zk/plonk/constraint_system/selector_compressor.h" #include "tachyon/zk/plonk/constraint_system/virtual_cells.h" +#include "tachyon/zk/plonk/expressions/evaluator/identifier.h" +#include "tachyon/zk/plonk/expressions/evaluator/selector_replacer.h" +#include "tachyon/zk/plonk/expressions/evaluator/simple_selector_extractor.h" +#include "tachyon/zk/plonk/expressions/evaluator/simple_selector_finder.h" #include "tachyon/zk/plonk/keys/c_proving_key_impl_base_forward.h" #include "tachyon/zk/plonk/layout/lookup_table_column.h" #include "tachyon/zk/plonk/permutation/permutation_argument.h" diff --git a/tachyon/zk/plonk/constraint_system/selector_compressor_unittest.cc b/tachyon/zk/plonk/constraint_system/selector_compressor_unittest.cc index 69ce9cee3..b3956e9d7 100644 --- a/tachyon/zk/plonk/constraint_system/selector_compressor_unittest.cc +++ b/tachyon/zk/plonk/constraint_system/selector_compressor_unittest.cc @@ -6,7 +6,7 @@ #include "tachyon/base/functional/callback.h" #include "tachyon/math/finite_fields/test/finite_field_test.h" #include "tachyon/math/finite_fields/test/gf7.h" -#include "tachyon/zk/expressions/expression_factory.h" +#include "tachyon/zk/plonk/expressions/expression_factory.h" namespace tachyon::zk::plonk { diff --git a/tachyon/zk/plonk/constraint_system/virtual_cells.h b/tachyon/zk/plonk/constraint_system/virtual_cells.h index 77864ae28..2f77da4db 100644 --- a/tachyon/zk/plonk/constraint_system/virtual_cells.h +++ b/tachyon/zk/plonk/constraint_system/virtual_cells.h @@ -5,10 +5,10 @@ #include #include -#include "tachyon/zk/expressions/evaluator/simple_selector_finder.h" -#include "tachyon/zk/expressions/expression_factory.h" #include "tachyon/zk/plonk/constraint_system/selector.h" #include "tachyon/zk/plonk/constraint_system/virtual_cell.h" +#include "tachyon/zk/plonk/expressions/evaluator/simple_selector_finder.h" +#include "tachyon/zk/plonk/expressions/expression_factory.h" #include "tachyon/zk/plonk/layout/lookup_table_column.h" namespace tachyon::zk::plonk { diff --git a/tachyon/zk/plonk/expressions/BUILD.bazel b/tachyon/zk/plonk/expressions/BUILD.bazel new file mode 100644 index 000000000..6ae61d0de --- /dev/null +++ b/tachyon/zk/plonk/expressions/BUILD.bazel @@ -0,0 +1,87 @@ +load("//bazel:tachyon_cc.bzl", "tachyon_cc_library", "tachyon_cc_unittest") + +package(default_visibility = ["//visibility:public"]) + +tachyon_cc_library( + name = "advice_expression", + hdrs = ["advice_expression.h"], + deps = [ + "//tachyon/zk/expressions:expression", + "//tachyon/zk/plonk/constraint_system:query", + "@com_google_absl//absl/memory", + ], +) + +tachyon_cc_library( + name = "challenge_expression", + hdrs = ["challenge_expression.h"], + deps = [ + "//tachyon/zk/expressions:expression", + "//tachyon/zk/plonk/constraint_system:challenge", + "@com_google_absl//absl/memory", + "@com_google_absl//absl/strings", + ], +) + +tachyon_cc_library( + name = "expression_factory", + hdrs = ["expression_factory.h"], + deps = [ + ":advice_expression", + ":challenge_expression", + ":fixed_expression", + ":instance_expression", + ":selector_expression", + "//tachyon/zk/expressions:expression_factory", + ], +) + +tachyon_cc_library( + name = "fixed_expression", + hdrs = ["fixed_expression.h"], + deps = [ + "//tachyon/zk/expressions:expression", + "//tachyon/zk/plonk/constraint_system:query", + "@com_google_absl//absl/memory", + ], +) + +tachyon_cc_library( + name = "instance_expression", + hdrs = ["instance_expression.h"], + deps = [ + "//tachyon/zk/expressions:expression", + "//tachyon/zk/plonk/constraint_system:query", + "@com_google_absl//absl/memory", + ], +) + +tachyon_cc_library( + name = "selector_expression", + hdrs = ["selector_expression.h"], + deps = [ + "//tachyon/zk/expressions:expression", + "//tachyon/zk/plonk/constraint_system:selector", + "@com_google_absl//absl/memory", + "@com_google_absl//absl/strings", + ], +) + +tachyon_cc_unittest( + name = "expression_unittests", + srcs = [ + "advice_expression_unittest.cc", + "challenge_expression_unittest.cc", + "expression_unittest.cc", + "fixed_expression_unittest.cc", + "instance_expression_unittest.cc", + "selector_expression_unittest.cc", + ], + deps = [ + ":expression_factory", + "//tachyon/math/finite_fields/test:finite_field_test", + "//tachyon/math/finite_fields/test:gf7", + "//tachyon/math/polynomials/univariate:univariate_polynomial", + "//tachyon/zk/plonk/expressions/evaluator:simple_selector_finder", + ], +) diff --git a/tachyon/zk/expressions/advice_expression.h b/tachyon/zk/plonk/expressions/advice_expression.h similarity index 75% rename from tachyon/zk/expressions/advice_expression.h rename to tachyon/zk/plonk/expressions/advice_expression.h index 5fe97c08f..06fde1824 100644 --- a/tachyon/zk/expressions/advice_expression.h +++ b/tachyon/zk/plonk/expressions/advice_expression.h @@ -4,8 +4,8 @@ // can be found in the LICENSE-MIT.halo2 and the LICENCE-APACHE.halo2 // file. -#ifndef TACHYON_ZK_EXPRESSIONS_ADVICE_EXPRESSION_H_ -#define TACHYON_ZK_EXPRESSIONS_ADVICE_EXPRESSION_H_ +#ifndef TACHYON_ZK_PLONK_EXPRESSIONS_ADVICE_EXPRESSION_H_ +#define TACHYON_ZK_PLONK_EXPRESSIONS_ADVICE_EXPRESSION_H_ #include #include @@ -15,17 +15,20 @@ #include "tachyon/zk/expressions/expression.h" #include "tachyon/zk/plonk/constraint_system/query.h" -namespace tachyon::zk { +namespace tachyon::zk::plonk { + +template +class ExpressionFactory; template class AdviceExpression : public Expression { public: static std::unique_ptr CreateForTesting( - const plonk::AdviceQuery& query) { + const AdviceQuery& query) { return absl::WrapUnique(new AdviceExpression(query)); } - const plonk::AdviceQuery& query() const { return query_; } + const AdviceQuery& query() const { return query_; } // Expression methods size_t Degree() const override { return 1; } @@ -51,12 +54,12 @@ class AdviceExpression : public Expression { private: friend class ExpressionFactory; - explicit AdviceExpression(const plonk::AdviceQuery& query) + explicit AdviceExpression(const AdviceQuery& query) : Expression(ExpressionType::kAdvice), query_(query) {} - plonk::AdviceQuery query_; + AdviceQuery query_; }; -} // namespace tachyon::zk +} // namespace tachyon::zk::plonk -#endif // TACHYON_ZK_EXPRESSIONS_ADVICE_EXPRESSION_H_ +#endif // TACHYON_ZK_PLONK_EXPRESSIONS_ADVICE_EXPRESSION_H_ diff --git a/tachyon/zk/expressions/advice_expression_unittest.cc b/tachyon/zk/plonk/expressions/advice_expression_unittest.cc similarity index 62% rename from tachyon/zk/expressions/advice_expression_unittest.cc rename to tachyon/zk/plonk/expressions/advice_expression_unittest.cc index 3b8228a6d..a2b690293 100644 --- a/tachyon/zk/expressions/advice_expression_unittest.cc +++ b/tachyon/zk/plonk/expressions/advice_expression_unittest.cc @@ -1,11 +1,11 @@ -#include "tachyon/zk/expressions/advice_expression.h" +#include "tachyon/zk/plonk/expressions/advice_expression.h" #include "gtest/gtest.h" #include "tachyon/math/finite_fields/test/finite_field_test.h" #include "tachyon/math/finite_fields/test/gf7.h" -namespace tachyon::zk { +namespace tachyon::zk::plonk { using F = math::GF7; @@ -13,10 +13,10 @@ class AdviceExpressionTest : public math::FiniteFieldTest {}; TEST_F(AdviceExpressionTest, DegreeComplexity) { std::unique_ptr> expr = - AdviceExpression::CreateForTesting(plonk::AdviceQuery( - 1, Rotation(1), plonk::AdviceColumnKey(1, plonk::Phase(0)))); + AdviceExpression::CreateForTesting( + AdviceQuery(1, Rotation(1), AdviceColumnKey(1, plonk::Phase(0)))); EXPECT_EQ(expr->Degree(), size_t{1}); EXPECT_EQ(expr->Complexity(), uint64_t{1}); } -} // namespace tachyon::zk +} // namespace tachyon::zk::plonk diff --git a/tachyon/zk/expressions/challenge_expression.h b/tachyon/zk/plonk/expressions/challenge_expression.h similarity index 77% rename from tachyon/zk/expressions/challenge_expression.h rename to tachyon/zk/plonk/expressions/challenge_expression.h index 230ae9b6d..b99a3b0cc 100644 --- a/tachyon/zk/expressions/challenge_expression.h +++ b/tachyon/zk/plonk/expressions/challenge_expression.h @@ -4,8 +4,8 @@ // can be found in the LICENSE-MIT.halo2 and the LICENCE-APACHE.halo2 // file. -#ifndef TACHYON_ZK_EXPRESSIONS_CHALLENGE_EXPRESSION_H_ -#define TACHYON_ZK_EXPRESSIONS_CHALLENGE_EXPRESSION_H_ +#ifndef TACHYON_ZK_PLONK_EXPRESSIONS_CHALLENGE_EXPRESSION_H_ +#define TACHYON_ZK_PLONK_EXPRESSIONS_CHALLENGE_EXPRESSION_H_ #include #include @@ -17,17 +17,20 @@ #include "tachyon/zk/expressions/expression.h" #include "tachyon/zk/plonk/constraint_system/challenge.h" -namespace tachyon::zk { +namespace tachyon::zk::plonk { + +template +class ExpressionFactory; template class ChallengeExpression : public Expression { public: static std::unique_ptr CreateForTesting( - plonk::Challenge challenge) { + Challenge challenge) { return absl::WrapUnique(new ChallengeExpression(challenge)); } - plonk::Challenge challenge() const { return challenge_; } + Challenge challenge() const { return challenge_; } // Expression methods size_t Degree() const override { return 0; } @@ -53,12 +56,12 @@ class ChallengeExpression : public Expression { private: friend class ExpressionFactory; - explicit ChallengeExpression(plonk::Challenge challenge) + explicit ChallengeExpression(Challenge challenge) : Expression(ExpressionType::kChallenge), challenge_(challenge) {} - plonk::Challenge challenge_; + Challenge challenge_; }; -} // namespace tachyon::zk +} // namespace tachyon::zk::plonk -#endif // TACHYON_ZK_EXPRESSIONS_CHALLENGE_EXPRESSION_H_ +#endif // TACHYON_ZK_PLONK_EXPRESSIONS_CHALLENGE_EXPRESSION_H_ diff --git a/tachyon/zk/expressions/challenge_expression_unittest.cc b/tachyon/zk/plonk/expressions/challenge_expression_unittest.cc similarity index 67% rename from tachyon/zk/expressions/challenge_expression_unittest.cc rename to tachyon/zk/plonk/expressions/challenge_expression_unittest.cc index 8a79d5a79..f8204e119 100644 --- a/tachyon/zk/expressions/challenge_expression_unittest.cc +++ b/tachyon/zk/plonk/expressions/challenge_expression_unittest.cc @@ -1,11 +1,11 @@ -#include "tachyon/zk/expressions/challenge_expression.h" +#include "tachyon/zk/plonk/expressions/challenge_expression.h" #include "gtest/gtest.h" #include "tachyon/math/finite_fields/test/finite_field_test.h" #include "tachyon/math/finite_fields/test/gf7.h" -namespace tachyon::zk { +namespace tachyon::zk::plonk { using F = math::GF7; @@ -13,10 +13,9 @@ class ChallengeExpressionTest : public math::FiniteFieldTest {}; TEST_F(ChallengeExpressionTest, DegreeComplexity) { std::unique_ptr> expr = - ChallengeExpression::CreateForTesting( - plonk::Challenge(1, plonk::Phase(1))); + ChallengeExpression::CreateForTesting(Challenge(1, Phase(1))); EXPECT_EQ(expr->Degree(), size_t{0}); EXPECT_EQ(expr->Complexity(), uint64_t{0}); } -} // namespace tachyon::zk +} // namespace tachyon::zk::plonk diff --git a/tachyon/zk/expressions/evaluator/BUILD.bazel b/tachyon/zk/plonk/expressions/evaluator/BUILD.bazel similarity index 75% rename from tachyon/zk/expressions/evaluator/BUILD.bazel rename to tachyon/zk/plonk/expressions/evaluator/BUILD.bazel index c6a98f9ef..0f114d375 100644 --- a/tachyon/zk/expressions/evaluator/BUILD.bazel +++ b/tachyon/zk/plonk/expressions/evaluator/BUILD.bazel @@ -7,17 +7,17 @@ tachyon_cc_library( hdrs = ["identifier.h"], deps = [ "//tachyon/base:logging", - "//tachyon/zk/expressions:advice_expression", - "//tachyon/zk/expressions:challenge_expression", "//tachyon/zk/expressions:constant_expression", "//tachyon/zk/expressions:evaluator", - "//tachyon/zk/expressions:fixed_expression", - "//tachyon/zk/expressions:instance_expression", "//tachyon/zk/expressions:negated_expression", "//tachyon/zk/expressions:product_expression", "//tachyon/zk/expressions:scaled_expression", - "//tachyon/zk/expressions:selector_expression", "//tachyon/zk/expressions:sum_expression", + "//tachyon/zk/plonk/expressions:advice_expression", + "//tachyon/zk/plonk/expressions:challenge_expression", + "//tachyon/zk/plonk/expressions:fixed_expression", + "//tachyon/zk/plonk/expressions:instance_expression", + "//tachyon/zk/plonk/expressions:selector_expression", ], ) @@ -31,8 +31,8 @@ tachyon_cc_library( "//tachyon/zk/expressions:negated_expression", "//tachyon/zk/expressions:product_expression", "//tachyon/zk/expressions:scaled_expression", - "//tachyon/zk/expressions:selector_expression", "//tachyon/zk/expressions:sum_expression", + "//tachyon/zk/plonk/expressions:selector_expression", ], ) @@ -41,7 +41,7 @@ tachyon_cc_library( hdrs = ["simple_selector_extractor.h"], deps = [ "//tachyon/zk/expressions:evaluator", - "//tachyon/zk/expressions:selector_expression", + "//tachyon/zk/plonk/expressions:selector_expression", ], ) @@ -53,8 +53,8 @@ tachyon_cc_library( "//tachyon/zk/expressions:negated_expression", "//tachyon/zk/expressions:product_expression", "//tachyon/zk/expressions:scaled_expression", - "//tachyon/zk/expressions:selector_expression", "//tachyon/zk/expressions:sum_expression", + "//tachyon/zk/plonk/expressions:selector_expression", ], ) @@ -69,7 +69,7 @@ tachyon_cc_unittest( ":selector_replacer", ":simple_selector_extractor", ":simple_selector_finder", - "//tachyon/zk/expressions:expression_factory", - "//tachyon/zk/expressions/evaluator/test:evaluator_test", + "//tachyon/zk/plonk/expressions:expression_factory", + "//tachyon/zk/plonk/expressions/evaluator/test:evaluator_test", ], ) diff --git a/tachyon/zk/plonk/expressions/evaluator/identifier.h b/tachyon/zk/plonk/expressions/evaluator/identifier.h new file mode 100644 index 000000000..dc2cd98fb --- /dev/null +++ b/tachyon/zk/plonk/expressions/evaluator/identifier.h @@ -0,0 +1,86 @@ +#ifndef TACHYON_ZK_PLONK_EXPRESSIONS_EVALUATOR_IDENTIFIER_H_ +#define TACHYON_ZK_PLONK_EXPRESSIONS_EVALUATOR_IDENTIFIER_H_ + +#include +#include + +#include "tachyon/base/logging.h" +#include "tachyon/zk/expressions/constant_expression.h" +#include "tachyon/zk/expressions/expression.h" +#include "tachyon/zk/expressions/negated_expression.h" +#include "tachyon/zk/expressions/product_expression.h" +#include "tachyon/zk/expressions/scaled_expression.h" +#include "tachyon/zk/expressions/sum_expression.h" +#include "tachyon/zk/plonk/expressions/advice_expression.h" +#include "tachyon/zk/plonk/expressions/challenge_expression.h" +#include "tachyon/zk/plonk/expressions/fixed_expression.h" +#include "tachyon/zk/plonk/expressions/instance_expression.h" +#include "tachyon/zk/plonk/expressions/selector_expression.h" + +namespace tachyon::zk::plonk { + +template +std::string Identifier(const Expression* input) { + std::ostringstream ss; + switch (input->type()) { + case ExpressionType::kConstant: + ss << input->ToConstant()->value().ToString(); + return ss.str(); + case ExpressionType::kNegated: + ss << "(-"; + ss << Identifier(input->ToNegated()->expr()); + ss << ")"; + return ss.str(); + case ExpressionType::kSum: + ss << "("; + ss << Identifier(input->ToSum()->left()); + ss << "+"; + ss << Identifier(input->ToSum()->right()); + ss << ")"; + return ss.str(); + case ExpressionType::kProduct: + ss << "("; + ss << Identifier(input->ToProduct()->left()); + ss << "*"; + ss << Identifier(input->ToProduct()->right()); + ss << ")"; + return ss.str(); + case ExpressionType::kScaled: + ss << "*" << input->ToScaled()->scale().ToString(); + return ss.str(); + case ExpressionType::kFixed: { + const FixedExpression* fixed = input->ToFixed(); + ss << "fixed[" << fixed->query().column().index() << "][" + << fixed->query().rotation().value() << "]"; + return ss.str(); + } + case ExpressionType::kAdvice: { + const AdviceExpression* advice = input->ToAdvice(); + ss << "advice[" << advice->query().column().index() << "][" + << advice->query().rotation().value() << "]"; + return ss.str(); + } + case ExpressionType::kInstance: { + const InstanceExpression* instance = input->ToInstance(); + ss << "instance[" << instance->query().column().index() << "][" + << instance->query().rotation().value() << "]"; + return ss.str(); + } + case ExpressionType::kChallenge: { + const ChallengeExpression* challenge = input->ToChallenge(); + ss << "challenge[" << challenge->challenge().index() << "]"; + return ss.str(); + } + case ExpressionType::kSelector: { + const SelectorExpression* selector = input->ToSelector(); + ss << "selector[" << selector->selector().index() << "]"; + return ss.str(); + } + } + NOTREACHED(); + return ""; +} + +} // namespace tachyon::zk::plonk + +#endif // TACHYON_ZK_PLONK_EXPRESSIONS_EVALUATOR_IDENTIFIER_H_ diff --git a/tachyon/zk/expressions/evaluator/selector_replacer.h b/tachyon/zk/plonk/expressions/evaluator/selector_replacer.h similarity index 86% rename from tachyon/zk/expressions/evaluator/selector_replacer.h rename to tachyon/zk/plonk/expressions/evaluator/selector_replacer.h index b6a55e9e9..0194654de 100644 --- a/tachyon/zk/expressions/evaluator/selector_replacer.h +++ b/tachyon/zk/plonk/expressions/evaluator/selector_replacer.h @@ -4,8 +4,8 @@ // can be found in the LICENSE-MIT.halo2 and the LICENCE-APACHE.halo2 // file. -#ifndef TACHYON_ZK_EXPRESSIONS_EVALUATOR_SELECTOR_REPLACER_H_ -#define TACHYON_ZK_EXPRESSIONS_EVALUATOR_SELECTOR_REPLACER_H_ +#ifndef TACHYON_ZK_PLONK_EXPRESSIONS_EVALUATOR_SELECTOR_REPLACER_H_ +#define TACHYON_ZK_PLONK_EXPRESSIONS_EVALUATOR_SELECTOR_REPLACER_H_ #include #include @@ -16,10 +16,10 @@ #include "tachyon/zk/expressions/negated_expression.h" #include "tachyon/zk/expressions/product_expression.h" #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/expressions/selector_expression.h" -namespace tachyon::zk { +namespace tachyon::zk::plonk { template std::unique_ptr> ReplaceSelectors( @@ -34,7 +34,7 @@ std::unique_ptr> ReplaceSelectors( case ExpressionType::kChallenge: return input->Clone(); case ExpressionType::kSelector: { - plonk::Selector selector = input->ToSelector()->selector(); + Selector selector = input->ToSelector()->selector(); if (must_be_non_simple) { // Simple selectors are prohibited from appearing in // expressions in the lookup argument by |ConstraintSystem|. @@ -70,6 +70,6 @@ std::unique_ptr> ReplaceSelectors( return nullptr; } -} // namespace tachyon::zk +} // namespace tachyon::zk::plonk -#endif // TACHYON_ZK_EXPRESSIONS_EVALUATOR_SELECTOR_REPLACER_H_ +#endif // TACHYON_ZK_PLONK_EXPRESSIONS_EVALUATOR_SELECTOR_REPLACER_H_ diff --git a/tachyon/zk/expressions/evaluator/selector_replacer_unittest.cc b/tachyon/zk/plonk/expressions/evaluator/selector_replacer_unittest.cc similarity index 73% rename from tachyon/zk/expressions/evaluator/selector_replacer_unittest.cc rename to tachyon/zk/plonk/expressions/evaluator/selector_replacer_unittest.cc index 4f0a85c60..4998966de 100644 --- a/tachyon/zk/expressions/evaluator/selector_replacer_unittest.cc +++ b/tachyon/zk/plonk/expressions/evaluator/selector_replacer_unittest.cc @@ -1,12 +1,12 @@ -#include "tachyon/zk/expressions/evaluator/selector_replacer.h" +#include "tachyon/zk/plonk/expressions/evaluator/selector_replacer.h" #include -#include "tachyon/zk/expressions/evaluator/simple_selector_finder.h" -#include "tachyon/zk/expressions/evaluator/test/evaluator_test.h" -#include "tachyon/zk/expressions/expression_factory.h" +#include "tachyon/zk/plonk/expressions/evaluator/simple_selector_finder.h" +#include "tachyon/zk/plonk/expressions/evaluator/test/evaluator_test.h" +#include "tachyon/zk/plonk/expressions/expression_factory.h" -namespace tachyon::zk { +namespace tachyon::zk::plonk { using Expr = std::unique_ptr>; @@ -19,12 +19,12 @@ TEST_F(SelectorReplacerTest, Constant) { } TEST_F(SelectorReplacerTest, Selector) { - Expr expr = ExpressionFactory::Selector(plonk::Selector::Simple(1)); + Expr expr = ExpressionFactory::Selector(Selector::Simple(1)); std::vector>> owned_replacements; owned_replacements.push_back( - ExpressionFactory::Selector(plonk::Selector::Simple(2))); + ExpressionFactory::Selector(Selector::Simple(2))); owned_replacements.push_back( - ExpressionFactory::Selector(plonk::Selector::Complex(3))); + ExpressionFactory::Selector(Selector::Complex(3))); std::vector>> replacements; replacements.emplace_back(owned_replacements[0].get()); replacements.emplace_back(owned_replacements[1].get()); @@ -33,7 +33,7 @@ TEST_F(SelectorReplacerTest, Selector) { EXPECT_EQ(*ReplaceSelectors(expr.get(), replacements, false), *replacements[1]); - expr = ExpressionFactory::Selector(plonk::Selector::Complex(1)); + expr = ExpressionFactory::Selector(Selector::Complex(1)); EXPECT_EQ(*ReplaceSelectors(expr.get(), replacements, false), *replacements[1]); } @@ -48,8 +48,8 @@ TEST_F(SelectorReplacerTest, Fixed) { }; for (const auto& test : tests) { - plonk::FixedQuery query(1, Rotation(test.rotation), - plonk::FixedColumnKey(test.column_index)); + FixedQuery query(1, Rotation(test.rotation), + FixedColumnKey(test.column_index)); Expr expr = ExpressionFactory::Fixed(query); EXPECT_EQ(*expr, *ReplaceSelectors(expr.get(), {}, false)); } @@ -65,9 +65,8 @@ TEST_F(SelectorReplacerTest, Advice) { }; for (const auto& test : tests) { - plonk::AdviceQuery query( - 1, Rotation(test.rotation), - plonk::AdviceColumnKey(test.column_index, plonk::Phase(0))); + AdviceQuery query(1, Rotation(test.rotation), + AdviceColumnKey(test.column_index, Phase(0))); Expr expr = ExpressionFactory::Advice(query); EXPECT_EQ(*expr, *ReplaceSelectors(expr.get(), {}, false)); } @@ -83,16 +82,15 @@ TEST_F(SelectorReplacerTest, Instance) { }; for (const auto& test : tests) { - plonk::InstanceQuery query(1, Rotation(test.rotation), - plonk::InstanceColumnKey(test.column_index)); + InstanceQuery query(1, Rotation(test.rotation), + InstanceColumnKey(test.column_index)); Expr expr = ExpressionFactory::Instance(query); EXPECT_EQ(*expr, *ReplaceSelectors(expr.get(), {}, false)); } } TEST_F(SelectorReplacerTest, Challenges) { - Expr expr = - ExpressionFactory::Challenge(plonk::Challenge(1, plonk::Phase(0))); + Expr expr = ExpressionFactory::Challenge(Challenge(1, Phase(0))); EXPECT_EQ(*expr, *ReplaceSelectors(expr.get(), {}, false)); } @@ -127,4 +125,4 @@ TEST_F(SelectorReplacerTest, Scaled) { EXPECT_EQ(*expr, *ReplaceSelectors(expr.get(), {}, false)); } -} // namespace tachyon::zk +} // namespace tachyon::zk::plonk diff --git a/tachyon/zk/expressions/evaluator/simple_selector_extractor.h b/tachyon/zk/plonk/expressions/evaluator/simple_selector_extractor.h similarity index 73% rename from tachyon/zk/expressions/evaluator/simple_selector_extractor.h rename to tachyon/zk/plonk/expressions/evaluator/simple_selector_extractor.h index 06b3dc6df..4bce08a70 100644 --- a/tachyon/zk/expressions/evaluator/simple_selector_extractor.h +++ b/tachyon/zk/plonk/expressions/evaluator/simple_selector_extractor.h @@ -4,22 +4,21 @@ // can be found in the LICENSE-MIT.halo2 and the LICENCE-APACHE.halo2 // file. -#ifndef TACHYON_ZK_EXPRESSIONS_EVALUATOR_SIMPLE_SELECTOR_EXTRACTOR_H_ -#define TACHYON_ZK_EXPRESSIONS_EVALUATOR_SIMPLE_SELECTOR_EXTRACTOR_H_ +#ifndef TACHYON_ZK_PLONK_EXPRESSIONS_EVALUATOR_SIMPLE_SELECTOR_EXTRACTOR_H_ +#define TACHYON_ZK_PLONK_EXPRESSIONS_EVALUATOR_SIMPLE_SELECTOR_EXTRACTOR_H_ #include #include "tachyon/zk/expressions/evaluator.h" -#include "tachyon/zk/expressions/selector_expression.h" +#include "tachyon/zk/plonk/expressions/selector_expression.h" -namespace tachyon::zk { +namespace tachyon::zk::plonk { template -std::optional ExtractSimpleSelector( - const Expression* input) { - auto op = [](const std::optional& left, - const std::optional& right) - -> std::optional { +std::optional ExtractSimpleSelector(const Expression* input) { + auto op = + [](const std::optional& left, + const std::optional& right) -> std::optional { CHECK(!(left.has_value() && right.has_value())) << "two simple selectors cannot be in the same expression"; if (left.has_value()) return left; @@ -31,7 +30,7 @@ std::optional ExtractSimpleSelector( case ExpressionType::kConstant: return std::nullopt; case ExpressionType::kSelector: { - plonk::Selector selector = input->ToSelector()->selector(); + Selector selector = input->ToSelector()->selector(); if (selector.is_simple()) { return selector; } @@ -67,6 +66,6 @@ std::optional ExtractSimpleSelector( return std::nullopt; } -} // namespace tachyon::zk +} // namespace tachyon::zk::plonk -#endif // TACHYON_ZK_EXPRESSIONS_EVALUATOR_SIMPLE_SELECTOR_EXTRACTOR_H_ +#endif // TACHYON_ZK_PLONK_EXPRESSIONS_EVALUATOR_SIMPLE_SELECTOR_EXTRACTOR_H_ diff --git a/tachyon/zk/expressions/evaluator/simple_selector_extractor_unittest.cc b/tachyon/zk/plonk/expressions/evaluator/simple_selector_extractor_unittest.cc similarity index 70% rename from tachyon/zk/expressions/evaluator/simple_selector_extractor_unittest.cc rename to tachyon/zk/plonk/expressions/evaluator/simple_selector_extractor_unittest.cc index 626c82af2..85bdb2979 100644 --- a/tachyon/zk/expressions/evaluator/simple_selector_extractor_unittest.cc +++ b/tachyon/zk/plonk/expressions/evaluator/simple_selector_extractor_unittest.cc @@ -1,11 +1,11 @@ -#include "tachyon/zk/expressions/evaluator/simple_selector_extractor.h" +#include "tachyon/zk/plonk/expressions/evaluator/simple_selector_extractor.h" #include -#include "tachyon/zk/expressions/evaluator/test/evaluator_test.h" -#include "tachyon/zk/expressions/expression_factory.h" +#include "tachyon/zk/plonk/expressions/evaluator/test/evaluator_test.h" +#include "tachyon/zk/plonk/expressions/expression_factory.h" -namespace tachyon::zk { +namespace tachyon::zk::plonk { using Expr = std::unique_ptr>; @@ -19,11 +19,11 @@ TEST_F(SimpleSelectorExtractorTest, Constant) { } TEST_F(SimpleSelectorExtractorTest, Selector) { - plonk::Selector expected_selector = plonk::Selector::Simple(1); + Selector expected_selector = Selector::Simple(1); Expr expr = ExpressionFactory::Selector(expected_selector); - std::optional selector = ExtractSimpleSelector(expr.get()); + std::optional selector = ExtractSimpleSelector(expr.get()); EXPECT_EQ(selector.value(), expected_selector); - expr = ExpressionFactory::Selector(plonk::Selector::Complex(1)); + expr = ExpressionFactory::Selector(Selector::Complex(1)); EXPECT_FALSE(ExtractSimpleSelector(expr.get()).has_value()); } @@ -37,8 +37,8 @@ TEST_F(SimpleSelectorExtractorTest, Fixed) { }; for (const auto& test : tests) { - plonk::FixedQuery query(1, Rotation(test.rotation), - plonk::FixedColumnKey(test.column_index)); + FixedQuery query(1, Rotation(test.rotation), + FixedColumnKey(test.column_index)); Expr expr = ExpressionFactory::Fixed(query); EXPECT_FALSE(ExtractSimpleSelector(expr.get()).has_value()); } @@ -54,9 +54,8 @@ TEST_F(SimpleSelectorExtractorTest, Advice) { }; for (const auto& test : tests) { - plonk::AdviceQuery query( - 1, Rotation(test.rotation), - plonk::AdviceColumnKey(test.column_index, plonk::Phase(0))); + AdviceQuery query(1, Rotation(test.rotation), + AdviceColumnKey(test.column_index, Phase(0))); Expr expr = ExpressionFactory::Advice(query); EXPECT_FALSE(ExtractSimpleSelector(expr.get()).has_value()); } @@ -72,16 +71,15 @@ TEST_F(SimpleSelectorExtractorTest, Instance) { }; for (const auto& test : tests) { - plonk::InstanceQuery query(1, Rotation(test.rotation), - plonk::InstanceColumnKey(test.column_index)); + InstanceQuery query(1, Rotation(test.rotation), + InstanceColumnKey(test.column_index)); Expr expr = ExpressionFactory::Instance(query); EXPECT_FALSE(ExtractSimpleSelector(expr.get()).has_value()); } } TEST_F(SimpleSelectorExtractorTest, Challenges) { - Expr expr = - ExpressionFactory::Challenge(plonk::Challenge(1, plonk::Phase(0))); + Expr expr = ExpressionFactory::Challenge(Challenge(1, Phase(0))); EXPECT_FALSE(ExtractSimpleSelector(expr.get()).has_value()); } @@ -98,11 +96,11 @@ TEST_F(SimpleSelectorExtractorTest, Sum) { Expr expr = ExpressionFactory::Sum(ExpressionFactory::Constant(a), ExpressionFactory::Constant(b)); EXPECT_FALSE(ExtractSimpleSelector(expr.get()).has_value()); - plonk::Selector expected_selector = plonk::Selector::Simple(1); + Selector expected_selector = Selector::Simple(1); expr = ExpressionFactory::Sum( ExpressionFactory::Constant(a), ExpressionFactory::Selector(expected_selector)); - std::optional selector = ExtractSimpleSelector(expr.get()); + std::optional selector = ExtractSimpleSelector(expr.get()); EXPECT_EQ(selector.value(), expected_selector); } @@ -112,11 +110,11 @@ TEST_F(SimpleSelectorExtractorTest, Product) { Expr expr = ExpressionFactory::Product( ExpressionFactory::Constant(a), ExpressionFactory::Constant(b)); EXPECT_FALSE(ExtractSimpleSelector(expr.get()).has_value()); - plonk::Selector expected_selector = plonk::Selector::Simple(1); + Selector expected_selector = Selector::Simple(1); expr = ExpressionFactory::Product( ExpressionFactory::Constant(a), ExpressionFactory::Selector(expected_selector)); - std::optional selector = ExtractSimpleSelector(expr.get()); + std::optional selector = ExtractSimpleSelector(expr.get()); EXPECT_EQ(selector.value(), expected_selector); } @@ -126,11 +124,11 @@ TEST_F(SimpleSelectorExtractorTest, Scaled) { Expr expr = ExpressionFactory::Scaled(ExpressionFactory::Constant(a), b); EXPECT_FALSE(ExtractSimpleSelector(expr.get()).has_value()); - plonk::Selector expected_selector = plonk::Selector::Simple(1); + Selector expected_selector = Selector::Simple(1); expr = ExpressionFactory::Scaled( ExpressionFactory::Selector(expected_selector), GF7(3)); - std::optional selector = ExtractSimpleSelector(expr.get()); + std::optional selector = ExtractSimpleSelector(expr.get()); EXPECT_EQ(selector.value(), expected_selector); } -} // namespace tachyon::zk +} // namespace tachyon::zk::plonk diff --git a/tachyon/zk/expressions/evaluator/simple_selector_finder.h b/tachyon/zk/plonk/expressions/evaluator/simple_selector_finder.h similarity index 82% rename from tachyon/zk/expressions/evaluator/simple_selector_finder.h rename to tachyon/zk/plonk/expressions/evaluator/simple_selector_finder.h index 02ec759ea..f7eae2f11 100644 --- a/tachyon/zk/expressions/evaluator/simple_selector_finder.h +++ b/tachyon/zk/plonk/expressions/evaluator/simple_selector_finder.h @@ -4,17 +4,17 @@ // can be found in the LICENSE-MIT.halo2 and the LICENCE-APACHE.halo2 // file. -#ifndef TACHYON_ZK_EXPRESSIONS_EVALUATOR_SIMPLE_SELECTOR_FINDER_H_ -#define TACHYON_ZK_EXPRESSIONS_EVALUATOR_SIMPLE_SELECTOR_FINDER_H_ +#ifndef TACHYON_ZK_PLONK_EXPRESSIONS_EVALUATOR_SIMPLE_SELECTOR_FINDER_H_ +#define TACHYON_ZK_PLONK_EXPRESSIONS_EVALUATOR_SIMPLE_SELECTOR_FINDER_H_ #include "tachyon/zk/expressions/evaluator.h" #include "tachyon/zk/expressions/negated_expression.h" #include "tachyon/zk/expressions/product_expression.h" #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/expressions/selector_expression.h" -namespace tachyon::zk { +namespace tachyon::zk::plonk { template bool ContainsSimpleSelector(const Expression* input) { @@ -52,6 +52,6 @@ bool ContainsSimpleSelector(const Expression* input) { return false; } -} // namespace tachyon::zk +} // namespace tachyon::zk::plonk -#endif // TACHYON_ZK_EXPRESSIONS_EVALUATOR_SIMPLE_SELECTOR_FINDER_H_ +#endif // TACHYON_ZK_PLONK_EXPRESSIONS_EVALUATOR_SIMPLE_SELECTOR_FINDER_H_ diff --git a/tachyon/zk/expressions/evaluator/simple_selector_finder_unittest.cc b/tachyon/zk/plonk/expressions/evaluator/simple_selector_finder_unittest.cc similarity index 70% rename from tachyon/zk/expressions/evaluator/simple_selector_finder_unittest.cc rename to tachyon/zk/plonk/expressions/evaluator/simple_selector_finder_unittest.cc index a465995ad..920dae24c 100644 --- a/tachyon/zk/expressions/evaluator/simple_selector_finder_unittest.cc +++ b/tachyon/zk/plonk/expressions/evaluator/simple_selector_finder_unittest.cc @@ -1,11 +1,11 @@ -#include "tachyon/zk/expressions/evaluator/simple_selector_finder.h" +#include "tachyon/zk/plonk/expressions/evaluator/simple_selector_finder.h" #include -#include "tachyon/zk/expressions/evaluator/test/evaluator_test.h" -#include "tachyon/zk/expressions/expression_factory.h" +#include "tachyon/zk/plonk/expressions/evaluator/test/evaluator_test.h" +#include "tachyon/zk/plonk/expressions/expression_factory.h" -namespace tachyon::zk { +namespace tachyon::zk::plonk { using Expr = std::unique_ptr>; @@ -13,15 +13,14 @@ class SimpleSelectorFinderTest : public EvaluatorTest {}; TEST_F(SimpleSelectorFinderTest, Constant) { GF7 value = GF7::Random(); - std::unique_ptr> expr = - ExpressionFactory::Constant(value); + Expr expr = ExpressionFactory::Constant(value); EXPECT_FALSE(ContainsSimpleSelector(expr.get())); } TEST_F(SimpleSelectorFinderTest, Selector) { - Expr expr = ExpressionFactory::Selector(plonk::Selector::Simple(1)); + Expr expr = ExpressionFactory::Selector(Selector::Simple(1)); EXPECT_TRUE(ContainsSimpleSelector(expr.get())); - expr = ExpressionFactory::Selector(plonk::Selector::Complex(1)); + expr = ExpressionFactory::Selector(Selector::Complex(1)); EXPECT_FALSE(ContainsSimpleSelector(expr.get())); } @@ -35,8 +34,8 @@ TEST_F(SimpleSelectorFinderTest, Fixed) { }; for (const auto& test : tests) { - plonk::FixedQuery query(1, Rotation(test.rotation), - plonk::FixedColumnKey(test.column_index)); + FixedQuery query(1, Rotation(test.rotation), + FixedColumnKey(test.column_index)); Expr expr = ExpressionFactory::Fixed(query); EXPECT_FALSE(ContainsSimpleSelector(expr.get())); } @@ -52,9 +51,8 @@ TEST_F(SimpleSelectorFinderTest, Advice) { }; for (const auto& test : tests) { - plonk::AdviceQuery query( - 1, Rotation(test.rotation), - plonk::AdviceColumnKey(test.column_index, plonk::Phase(0))); + AdviceQuery query(1, Rotation(test.rotation), + AdviceColumnKey(test.column_index, Phase(0))); Expr expr = ExpressionFactory::Advice(query); EXPECT_FALSE(ContainsSimpleSelector(expr.get())); } @@ -70,16 +68,15 @@ TEST_F(SimpleSelectorFinderTest, Instance) { }; for (const auto& test : tests) { - plonk::InstanceQuery query(1, Rotation(test.rotation), - plonk::InstanceColumnKey(test.column_index)); + InstanceQuery query(1, Rotation(test.rotation), + InstanceColumnKey(test.column_index)); Expr expr = ExpressionFactory::Instance(query); EXPECT_FALSE(ContainsSimpleSelector(expr.get())); } } TEST_F(SimpleSelectorFinderTest, Challenges) { - Expr expr = - ExpressionFactory::Challenge(plonk::Challenge(1, plonk::Phase(0))); + Expr expr = ExpressionFactory::Challenge(Challenge(1, Phase(0))); EXPECT_FALSE(ContainsSimpleSelector(expr.get())); } @@ -98,7 +95,7 @@ TEST_F(SimpleSelectorFinderTest, Sum) { EXPECT_FALSE(ContainsSimpleSelector(expr.get())); expr = ExpressionFactory::Sum( ExpressionFactory::Constant(a), - ExpressionFactory::Selector(plonk::Selector::Simple(1))); + ExpressionFactory::Selector(Selector::Simple(1))); EXPECT_TRUE(ContainsSimpleSelector(expr.get())); } @@ -110,7 +107,7 @@ TEST_F(SimpleSelectorFinderTest, Product) { EXPECT_FALSE(ContainsSimpleSelector(expr.get())); expr = ExpressionFactory::Product( ExpressionFactory::Constant(a), - ExpressionFactory::Selector(plonk::Selector::Simple(1))); + ExpressionFactory::Selector(Selector::Simple(1))); EXPECT_TRUE(ContainsSimpleSelector(expr.get())); } @@ -121,8 +118,8 @@ TEST_F(SimpleSelectorFinderTest, Scaled) { ExpressionFactory::Scaled(ExpressionFactory::Constant(a), b); EXPECT_FALSE(ContainsSimpleSelector(expr.get())); expr = ExpressionFactory::Scaled( - ExpressionFactory::Selector(plonk::Selector::Simple(1)), GF7(3)); + ExpressionFactory::Selector(Selector::Simple(1)), GF7(3)); EXPECT_TRUE(ContainsSimpleSelector(expr.get())); } -} // namespace tachyon::zk +} // namespace tachyon::zk::plonk diff --git a/tachyon/zk/expressions/evaluator/test/BUILD.bazel b/tachyon/zk/plonk/expressions/evaluator/test/BUILD.bazel similarity index 89% rename from tachyon/zk/expressions/evaluator/test/BUILD.bazel rename to tachyon/zk/plonk/expressions/evaluator/test/BUILD.bazel index 2f1648a73..7bd012e42 100644 --- a/tachyon/zk/expressions/evaluator/test/BUILD.bazel +++ b/tachyon/zk/plonk/expressions/evaluator/test/BUILD.bazel @@ -5,8 +5,8 @@ tachyon_cc_library( testonly = True, hdrs = ["evaluator_test.h"], visibility = [ - "//tachyon/zk/expressions/evaluator:__pkg__", "//tachyon/zk/lookup:__pkg__", + "//tachyon/zk/plonk/expressions/evaluator:__pkg__", "//tachyon/zk/plonk/vanishing:__pkg__", ], deps = [ diff --git a/tachyon/zk/expressions/evaluator/test/evaluator_test.h b/tachyon/zk/plonk/expressions/evaluator/test/evaluator_test.h similarity index 74% rename from tachyon/zk/expressions/evaluator/test/evaluator_test.h rename to tachyon/zk/plonk/expressions/evaluator/test/evaluator_test.h index 102d34ff3..c30f223d4 100644 --- a/tachyon/zk/expressions/evaluator/test/evaluator_test.h +++ b/tachyon/zk/plonk/expressions/evaluator/test/evaluator_test.h @@ -4,8 +4,8 @@ // can be found in the LICENSE-MIT.halo2 and the LICENCE-APACHE.halo2 // file. -#ifndef TACHYON_ZK_EXPRESSIONS_EVALUATOR_TEST_EVALUATOR_TEST_H_ -#define TACHYON_ZK_EXPRESSIONS_EVALUATOR_TEST_EVALUATOR_TEST_H_ +#ifndef TACHYON_ZK_PLONK_EXPRESSIONS_EVALUATOR_TEST_EVALUATOR_TEST_H_ +#define TACHYON_ZK_PLONK_EXPRESSIONS_EVALUATOR_TEST_EVALUATOR_TEST_H_ #include @@ -15,7 +15,7 @@ #include "tachyon/math/finite_fields/test/gf7.h" #include "tachyon/math/polynomials/univariate/univariate_evaluations.h" -namespace tachyon::zk { +namespace tachyon::zk::plonk { constexpr size_t kMaxDegree = 5; @@ -30,6 +30,6 @@ class EvaluatorTest : public math::FiniteFieldTest { std::vector challenges_; }; -} // namespace tachyon::zk +} // namespace tachyon::zk::plonk -#endif // TACHYON_ZK_EXPRESSIONS_EVALUATOR_TEST_EVALUATOR_TEST_H_ +#endif // TACHYON_ZK_PLONK_EXPRESSIONS_EVALUATOR_TEST_EVALUATOR_TEST_H_ diff --git a/tachyon/zk/plonk/expressions/expression_factory.h b/tachyon/zk/plonk/expressions/expression_factory.h new file mode 100644 index 000000000..489060535 --- /dev/null +++ b/tachyon/zk/plonk/expressions/expression_factory.h @@ -0,0 +1,52 @@ +// 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_EXPRESSIONS_EXPRESSION_FACTORY_H_ +#define TACHYON_ZK_PLONK_EXPRESSIONS_EXPRESSION_FACTORY_H_ + +#include +#include + +#include "absl/memory/memory.h" + +#include "tachyon/zk/expressions/expression_factory.h" +#include "tachyon/zk/plonk/expressions/advice_expression.h" +#include "tachyon/zk/plonk/expressions/challenge_expression.h" +#include "tachyon/zk/plonk/expressions/fixed_expression.h" +#include "tachyon/zk/plonk/expressions/instance_expression.h" +#include "tachyon/zk/plonk/expressions/selector_expression.h" + +namespace tachyon::zk::plonk { + +template +class ExpressionFactory : public tachyon::zk::ExpressionFactory { + public: + using Expr = Expression; + + static std::unique_ptr Advice(const AdviceQuery& query) { + return absl::WrapUnique(new AdviceExpression(query)); + } + + static std::unique_ptr Instance(const InstanceQuery& query) { + return absl::WrapUnique(new InstanceExpression(query)); + } + + static std::unique_ptr Challenge(Challenge challenge) { + return absl::WrapUnique(new ChallengeExpression(challenge)); + } + + static std::unique_ptr Fixed(const FixedQuery& query) { + return absl::WrapUnique(new FixedExpression(query)); + } + + static std::unique_ptr Selector(Selector selector) { + return absl::WrapUnique(new SelectorExpression(selector)); + } +}; + +} // namespace tachyon::zk::plonk + +#endif // TACHYON_ZK_PLONK_EXPRESSIONS_EXPRESSION_FACTORY_H_ diff --git a/tachyon/zk/plonk/expressions/expression_unittest.cc b/tachyon/zk/plonk/expressions/expression_unittest.cc new file mode 100644 index 000000000..53f9f9ed8 --- /dev/null +++ b/tachyon/zk/plonk/expressions/expression_unittest.cc @@ -0,0 +1,145 @@ +#include "gtest/gtest.h" + +#include "tachyon/base/random.h" +#include "tachyon/math/finite_fields/test/finite_field_test.h" +#include "tachyon/math/finite_fields/test/gf7.h" +#include "tachyon/zk/plonk/expressions/evaluator/simple_selector_finder.h" +#include "tachyon/zk/plonk/expressions/expression_factory.h" + +namespace tachyon::zk::plonk { + +using F = math::GF7; + +namespace { + +class ExpressionTest : public math::FiniteFieldTest { + public: + void SetUp() override { + expressions_.push_back(ExpressionFactory::Constant(F(1))); + expressions_.push_back(ExpressionFactory::Selector(Selector::Simple(1))); + expressions_.push_back( + ExpressionFactory::Selector(Selector::Complex(2))); + expressions_.push_back(ExpressionFactory::Fixed( + FixedQuery(1, Rotation(1), FixedColumnKey(1)))); + expressions_.push_back(ExpressionFactory::Advice( + AdviceQuery(1, Rotation(1), AdviceColumnKey(1, Phase(0))))); + expressions_.push_back(ExpressionFactory::Instance( + InstanceQuery(1, Rotation(1), InstanceColumnKey(1)))); + expressions_.push_back( + ExpressionFactory::Challenge(Challenge(1, Phase(0)))); + expressions_.push_back( + ExpressionFactory::Negated(ExpressionFactory::Constant(F(1)))); + expressions_.push_back( + ExpressionFactory::Sum(ExpressionFactory::Constant(F(1)), + ExpressionFactory::Constant(F(2)))); + expressions_.push_back( + ExpressionFactory::Product(ExpressionFactory::Constant(F(3)), + ExpressionFactory::Constant(F(4)))); + expressions_.push_back(ExpressionFactory::Scaled( + ExpressionFactory::Constant(F(5)), F(6))); + } + + protected: + std::vector>> expressions_; +}; + +} // namespace + +TEST_F(ExpressionTest, ArithmeticOperatorWithClone) { + std::unique_ptr> left = + base::UniformElement(expressions_)->Clone(); + std::unique_ptr> right = + base::UniformElement(expressions_)->Clone(); + + if (!ContainsSimpleSelector(left.get()) && + !ContainsSimpleSelector(right.get())) { + std::unique_ptr> add = left + right; + EXPECT_EQ(*add->ToSum()->left(), *left); + EXPECT_EQ(*add->ToSum()->right(), *right); + EXPECT_TRUE(left); + EXPECT_TRUE(right); + + std::unique_ptr> sub = left - right; + EXPECT_EQ(*sub->ToSum()->left(), *left); + EXPECT_EQ(*sub->ToSum()->right()->ToNegated()->expr(), *right); + EXPECT_TRUE(left); + EXPECT_TRUE(right); + + std::unique_ptr> mul = left * right; + EXPECT_EQ(*mul->ToProduct()->left(), *left); + EXPECT_EQ(*mul->ToProduct()->right(), *right); + EXPECT_TRUE(left); + EXPECT_TRUE(right); + } + + F scale = F::Random(); + std::unique_ptr> scaled = left * scale; + EXPECT_EQ(*scaled->ToScaled()->expr(), *left); + EXPECT_EQ(scaled->ToScaled()->scale(), scale); + EXPECT_TRUE(left); + + std::unique_ptr> neg = -left; + EXPECT_EQ(*neg->ToNegated()->expr(), *left); + EXPECT_TRUE(left); +} + +TEST_F(ExpressionTest, ArithmeticOperatorWithMove) { + std::unique_ptr> left = + base::UniformElement(expressions_)->Clone(); + std::unique_ptr> right = + base::UniformElement(expressions_)->Clone(); + + if (!ContainsSimpleSelector(left.get()) && + !ContainsSimpleSelector(right.get())) { + { + std::unique_ptr> left_tmp = left->Clone(); + std::unique_ptr> right_tmp = right->Clone(); + std::unique_ptr> add = + std::move(left_tmp) + std::move(right_tmp); + EXPECT_EQ(*add->ToSum()->left(), *left); + EXPECT_EQ(*add->ToSum()->right(), *right); + EXPECT_FALSE(left_tmp); + EXPECT_FALSE(right_tmp); + } + + { + std::unique_ptr> left_tmp = left->Clone(); + std::unique_ptr> right_tmp = right->Clone(); + std::unique_ptr> sub = + std::move(left_tmp) - std::move(right_tmp); + EXPECT_EQ(*sub->ToSum()->left(), *left); + EXPECT_EQ(*sub->ToSum()->right()->ToNegated()->expr(), *right); + EXPECT_FALSE(left_tmp); + EXPECT_FALSE(right_tmp); + } + + { + std::unique_ptr> left_tmp = left->Clone(); + std::unique_ptr> right_tmp = right->Clone(); + std::unique_ptr> mul = + std::move(left_tmp) * std::move(right_tmp); + EXPECT_EQ(*mul->ToProduct()->left(), *left); + EXPECT_EQ(*mul->ToProduct()->right(), *right); + EXPECT_FALSE(left_tmp); + EXPECT_FALSE(right_tmp); + } + } + + { + std::unique_ptr> left_tmp = left->Clone(); + F scale = F::Random(); + std::unique_ptr> scaled = std::move(left_tmp) * scale; + EXPECT_EQ(*scaled->ToScaled()->expr(), *left); + EXPECT_EQ(scaled->ToScaled()->scale(), scale); + EXPECT_FALSE(left_tmp); + } + + { + std::unique_ptr> left_tmp = left->Clone(); + std::unique_ptr> neg = -std::move(left_tmp); + EXPECT_EQ(*neg->ToNegated()->expr(), *left); + EXPECT_FALSE(left_tmp); + } +} + +} // namespace tachyon::zk::plonk diff --git a/tachyon/zk/expressions/fixed_expression.h b/tachyon/zk/plonk/expressions/fixed_expression.h similarity index 75% rename from tachyon/zk/expressions/fixed_expression.h rename to tachyon/zk/plonk/expressions/fixed_expression.h index eca9f899b..7a7f2b23c 100644 --- a/tachyon/zk/expressions/fixed_expression.h +++ b/tachyon/zk/plonk/expressions/fixed_expression.h @@ -4,8 +4,8 @@ // can be found in the LICENSE-MIT.halo2 and the LICENCE-APACHE.halo2 // file. -#ifndef TACHYON_ZK_EXPRESSIONS_FIXED_EXPRESSION_H_ -#define TACHYON_ZK_EXPRESSIONS_FIXED_EXPRESSION_H_ +#ifndef TACHYON_ZK_PLONK_EXPRESSIONS_FIXED_EXPRESSION_H_ +#define TACHYON_ZK_PLONK_EXPRESSIONS_FIXED_EXPRESSION_H_ #include #include @@ -15,17 +15,20 @@ #include "tachyon/zk/expressions/expression.h" #include "tachyon/zk/plonk/constraint_system/query.h" -namespace tachyon::zk { +namespace tachyon::zk::plonk { + +template +class ExpressionFactory; template class FixedExpression : public Expression { public: static std::unique_ptr CreateForTesting( - const plonk::FixedQuery& query) { + const FixedQuery& query) { return absl::WrapUnique(new FixedExpression(query)); } - const plonk::FixedQuery& query() const { return query_; } + const FixedQuery& query() const { return query_; } // Expression methods size_t Degree() const override { return 1; } @@ -51,12 +54,12 @@ class FixedExpression : public Expression { private: friend class ExpressionFactory; - explicit FixedExpression(const plonk::FixedQuery& query) + explicit FixedExpression(const FixedQuery& query) : Expression(ExpressionType::kFixed), query_(query) {} - plonk::FixedQuery query_; + FixedQuery query_; }; -} // namespace tachyon::zk +} // namespace tachyon::zk::plonk -#endif // TACHYON_ZK_EXPRESSIONS_FIXED_EXPRESSION_H_ +#endif // TACHYON_ZK_PLONK_EXPRESSIONS_FIXED_EXPRESSION_H_ diff --git a/tachyon/zk/expressions/fixed_expression_unittest.cc b/tachyon/zk/plonk/expressions/fixed_expression_unittest.cc similarity index 71% rename from tachyon/zk/expressions/fixed_expression_unittest.cc rename to tachyon/zk/plonk/expressions/fixed_expression_unittest.cc index 30cb9e4eb..b950d2e0a 100644 --- a/tachyon/zk/expressions/fixed_expression_unittest.cc +++ b/tachyon/zk/plonk/expressions/fixed_expression_unittest.cc @@ -1,11 +1,11 @@ -#include "tachyon/zk/expressions/fixed_expression.h" +#include "tachyon/zk/plonk/expressions/fixed_expression.h" #include "gtest/gtest.h" #include "tachyon/math/finite_fields/test/finite_field_test.h" #include "tachyon/math/finite_fields/test/gf7.h" -namespace tachyon::zk { +namespace tachyon::zk::plonk { using F = math::GF7; @@ -14,9 +14,9 @@ class FixedExpressionTest : public math::FiniteFieldTest {}; TEST_F(FixedExpressionTest, DegreeComplexity) { std::unique_ptr> expr = FixedExpression::CreateForTesting( - plonk::FixedQuery(1, Rotation(1), plonk::FixedColumnKey(1))); + FixedQuery(1, Rotation(1), FixedColumnKey(1))); EXPECT_EQ(expr->Degree(), size_t{1}); EXPECT_EQ(expr->Complexity(), uint64_t{1}); } -} // namespace tachyon::zk +} // namespace tachyon::zk::plonk diff --git a/tachyon/zk/expressions/instance_expression.h b/tachyon/zk/plonk/expressions/instance_expression.h similarity index 75% rename from tachyon/zk/expressions/instance_expression.h rename to tachyon/zk/plonk/expressions/instance_expression.h index 2e55b6463..0501226cd 100644 --- a/tachyon/zk/expressions/instance_expression.h +++ b/tachyon/zk/plonk/expressions/instance_expression.h @@ -4,8 +4,8 @@ // can be found in the LICENSE-MIT.halo2 and the LICENCE-APACHE.halo2 // file. -#ifndef TACHYON_ZK_EXPRESSIONS_INSTANCE_EXPRESSION_H_ -#define TACHYON_ZK_EXPRESSIONS_INSTANCE_EXPRESSION_H_ +#ifndef TACHYON_ZK_PLONK_EXPRESSIONS_INSTANCE_EXPRESSION_H_ +#define TACHYON_ZK_PLONK_EXPRESSIONS_INSTANCE_EXPRESSION_H_ #include #include @@ -15,17 +15,20 @@ #include "tachyon/zk/expressions/expression.h" #include "tachyon/zk/plonk/constraint_system/query.h" -namespace tachyon::zk { +namespace tachyon::zk::plonk { + +template +class ExpressionFactory; template class InstanceExpression : public Expression { public: static std::unique_ptr CreateForTesting( - const plonk::InstanceQuery& query) { + const InstanceQuery& query) { return absl::WrapUnique(new InstanceExpression(query)); } - const plonk::InstanceQuery& query() const { return query_; } + const InstanceQuery& query() const { return query_; } // Expression methods size_t Degree() const override { return 1; } @@ -51,12 +54,12 @@ class InstanceExpression : public Expression { private: friend class ExpressionFactory; - explicit InstanceExpression(const plonk::InstanceQuery& query) + explicit InstanceExpression(const InstanceQuery& query) : Expression(ExpressionType::kInstance), query_(query) {} - plonk::InstanceQuery query_; + InstanceQuery query_; }; -} // namespace tachyon::zk +} // namespace tachyon::zk::plonk -#endif // TACHYON_ZK_EXPRESSIONS_INSTANCE_EXPRESSION_H_ +#endif // TACHYON_ZK_PLONK_EXPRESSIONS_INSTANCE_EXPRESSION_H_ diff --git a/tachyon/zk/expressions/instance_expression_unittest.cc b/tachyon/zk/plonk/expressions/instance_expression_unittest.cc similarity index 70% rename from tachyon/zk/expressions/instance_expression_unittest.cc rename to tachyon/zk/plonk/expressions/instance_expression_unittest.cc index 6dc05b076..f7c56cf08 100644 --- a/tachyon/zk/expressions/instance_expression_unittest.cc +++ b/tachyon/zk/plonk/expressions/instance_expression_unittest.cc @@ -1,11 +1,11 @@ -#include "tachyon/zk/expressions/instance_expression.h" +#include "tachyon/zk/plonk/expressions/instance_expression.h" #include "gtest/gtest.h" #include "tachyon/math/finite_fields/test/finite_field_test.h" #include "tachyon/math/finite_fields/test/gf7.h" -namespace tachyon::zk { +namespace tachyon::zk::plonk { using F = math::GF7; @@ -14,9 +14,9 @@ class InstanceExpressionTest : public math::FiniteFieldTest {}; TEST_F(InstanceExpressionTest, DegreeComplexity) { std::unique_ptr> expr = InstanceExpression::CreateForTesting( - plonk::InstanceQuery(1, Rotation(1), plonk::InstanceColumnKey(1))); + InstanceQuery(1, Rotation(1), InstanceColumnKey(1))); EXPECT_EQ(expr->Degree(), size_t{1}); EXPECT_EQ(expr->Complexity(), uint64_t{1}); } -} // namespace tachyon::zk +} // namespace tachyon::zk::plonk diff --git a/tachyon/zk/expressions/selector_expression.h b/tachyon/zk/plonk/expressions/selector_expression.h similarity index 77% rename from tachyon/zk/expressions/selector_expression.h rename to tachyon/zk/plonk/expressions/selector_expression.h index 315af47bb..a4202f2cd 100644 --- a/tachyon/zk/expressions/selector_expression.h +++ b/tachyon/zk/plonk/expressions/selector_expression.h @@ -4,8 +4,8 @@ // can be found in the LICENSE-MIT.halo2 and the LICENCE-APACHE.halo2 // file. -#ifndef TACHYON_ZK_EXPRESSIONS_SELECTOR_EXPRESSION_H_ -#define TACHYON_ZK_EXPRESSIONS_SELECTOR_EXPRESSION_H_ +#ifndef TACHYON_ZK_PLONK_EXPRESSIONS_SELECTOR_EXPRESSION_H_ +#define TACHYON_ZK_PLONK_EXPRESSIONS_SELECTOR_EXPRESSION_H_ #include #include @@ -16,17 +16,20 @@ #include "tachyon/zk/expressions/expression.h" #include "tachyon/zk/plonk/constraint_system/selector.h" -namespace tachyon::zk { +namespace tachyon::zk::plonk { + +template +class ExpressionFactory; template class SelectorExpression : public Expression { public: static std::unique_ptr CreateForTesting( - plonk::Selector selector) { + Selector selector) { return absl::WrapUnique(new SelectorExpression(selector)); } - plonk::Selector selector() const { return selector_; } + Selector selector() const { return selector_; } // Expression methods size_t Degree() const override { return 1; } @@ -52,12 +55,12 @@ class SelectorExpression : public Expression { private: friend class ExpressionFactory; - explicit SelectorExpression(plonk::Selector selector) + explicit SelectorExpression(Selector selector) : Expression(ExpressionType::kSelector), selector_(selector) {} - plonk::Selector selector_; + Selector selector_; }; -} // namespace tachyon::zk +} // namespace tachyon::zk::plonk -#endif // TACHYON_ZK_EXPRESSIONS_SELECTOR_EXPRESSION_H_ +#endif // TACHYON_ZK_PLONK_EXPRESSIONS_SELECTOR_EXPRESSION_H_ diff --git a/tachyon/zk/expressions/selector_expression_unittest.cc b/tachyon/zk/plonk/expressions/selector_expression_unittest.cc similarity index 68% rename from tachyon/zk/expressions/selector_expression_unittest.cc rename to tachyon/zk/plonk/expressions/selector_expression_unittest.cc index d188daf1b..9a81a22e9 100644 --- a/tachyon/zk/expressions/selector_expression_unittest.cc +++ b/tachyon/zk/plonk/expressions/selector_expression_unittest.cc @@ -1,11 +1,11 @@ -#include "tachyon/zk/expressions/selector_expression.h" +#include "tachyon/zk/plonk/expressions/selector_expression.h" #include "gtest/gtest.h" #include "tachyon/math/finite_fields/test/finite_field_test.h" #include "tachyon/math/finite_fields/test/gf7.h" -namespace tachyon::zk { +namespace tachyon::zk::plonk { using F = math::GF7; @@ -13,9 +13,9 @@ class SelectorExpressionTest : public math::FiniteFieldTest {}; TEST_F(SelectorExpressionTest, Degree_Complexity) { std::unique_ptr> expr = - SelectorExpression::CreateForTesting(plonk::Selector::Simple(1)); + SelectorExpression::CreateForTesting(Selector::Simple(1)); EXPECT_EQ(expr->Degree(), size_t{1}); EXPECT_EQ(expr->Complexity(), uint64_t{1}); } -} // namespace tachyon::zk +} // namespace tachyon::zk::plonk diff --git a/tachyon/zk/plonk/halo2/stringifiers/BUILD.bazel b/tachyon/zk/plonk/halo2/stringifiers/BUILD.bazel index 80ee180b5..405cf0960 100644 --- a/tachyon/zk/plonk/halo2/stringifiers/BUILD.bazel +++ b/tachyon/zk/plonk/halo2/stringifiers/BUILD.bazel @@ -38,16 +38,16 @@ tachyon_cc_library( ":phase_stringifier", ":rotation_stringifier", ":selector_stringifier", - "//tachyon/zk/expressions:advice_expression", - "//tachyon/zk/expressions:challenge_expression", "//tachyon/zk/expressions:constant_expression", - "//tachyon/zk/expressions:fixed_expression", - "//tachyon/zk/expressions:instance_expression", "//tachyon/zk/expressions:negated_expression", "//tachyon/zk/expressions:product_expression", "//tachyon/zk/expressions:scaled_expression", - "//tachyon/zk/expressions:selector_expression", "//tachyon/zk/expressions:sum_expression", + "//tachyon/zk/plonk/expressions:advice_expression", + "//tachyon/zk/plonk/expressions:challenge_expression", + "//tachyon/zk/plonk/expressions:fixed_expression", + "//tachyon/zk/plonk/expressions:instance_expression", + "//tachyon/zk/plonk/expressions:selector_expression", ], ) diff --git a/tachyon/zk/plonk/halo2/stringifiers/expression_stringifier.h b/tachyon/zk/plonk/halo2/stringifiers/expression_stringifier.h index 0c0c7fe19..c221bbde3 100644 --- a/tachyon/zk/plonk/halo2/stringifiers/expression_stringifier.h +++ b/tachyon/zk/plonk/halo2/stringifiers/expression_stringifier.h @@ -7,16 +7,16 @@ #include "tachyon/base/logging.h" #include "tachyon/base/strings/rust_stringifier.h" -#include "tachyon/zk/expressions/advice_expression.h" -#include "tachyon/zk/expressions/challenge_expression.h" #include "tachyon/zk/expressions/constant_expression.h" -#include "tachyon/zk/expressions/fixed_expression.h" -#include "tachyon/zk/expressions/instance_expression.h" #include "tachyon/zk/expressions/negated_expression.h" #include "tachyon/zk/expressions/product_expression.h" #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/expressions/advice_expression.h" +#include "tachyon/zk/plonk/expressions/challenge_expression.h" +#include "tachyon/zk/plonk/expressions/fixed_expression.h" +#include "tachyon/zk/plonk/expressions/instance_expression.h" +#include "tachyon/zk/plonk/expressions/selector_expression.h" #include "tachyon/zk/plonk/halo2/stringifiers/challenge_stringifier.h" #include "tachyon/zk/plonk/halo2/stringifiers/field_stringifier.h" #include "tachyon/zk/plonk/halo2/stringifiers/phase_stringifier.h" diff --git a/tachyon/zk/plonk/vanishing/BUILD.bazel b/tachyon/zk/plonk/vanishing/BUILD.bazel index ca16d686a..55c52bfb0 100644 --- a/tachyon/zk/plonk/vanishing/BUILD.bazel +++ b/tachyon/zk/plonk/vanishing/BUILD.bazel @@ -53,17 +53,17 @@ tachyon_cc_library( hdrs = ["graph_evaluator.h"], deps = [ ":calculation", - "//tachyon/zk/expressions:advice_expression", - "//tachyon/zk/expressions:challenge_expression", "//tachyon/zk/expressions:constant_expression", "//tachyon/zk/expressions:evaluator", - "//tachyon/zk/expressions:fixed_expression", - "//tachyon/zk/expressions:instance_expression", "//tachyon/zk/expressions:negated_expression", "//tachyon/zk/expressions:product_expression", "//tachyon/zk/expressions:scaled_expression", - "//tachyon/zk/expressions:selector_expression", "//tachyon/zk/expressions:sum_expression", + "//tachyon/zk/plonk/expressions:advice_expression", + "//tachyon/zk/plonk/expressions:challenge_expression", + "//tachyon/zk/plonk/expressions:fixed_expression", + "//tachyon/zk/plonk/expressions:instance_expression", + "//tachyon/zk/plonk/expressions:selector_expression", ], ) @@ -150,7 +150,7 @@ tachyon_cc_unittest( "//tachyon/base:random", "//tachyon/math/elliptic_curves/bn/bn254:g1", "//tachyon/math/polynomials/univariate:univariate_evaluation_domain_factory", - "//tachyon/zk/expressions:expression_factory", - "//tachyon/zk/expressions/evaluator/test:evaluator_test", + "//tachyon/zk/plonk/expressions:expression_factory", + "//tachyon/zk/plonk/expressions/evaluator/test:evaluator_test", ], ) diff --git a/tachyon/zk/plonk/vanishing/graph_evaluator.h b/tachyon/zk/plonk/vanishing/graph_evaluator.h index 0411a079d..90080efab 100644 --- a/tachyon/zk/plonk/vanishing/graph_evaluator.h +++ b/tachyon/zk/plonk/vanishing/graph_evaluator.h @@ -12,17 +12,17 @@ #include "absl/strings/substitute.h" -#include "tachyon/zk/expressions/advice_expression.h" -#include "tachyon/zk/expressions/challenge_expression.h" #include "tachyon/zk/expressions/constant_expression.h" #include "tachyon/zk/expressions/evaluator.h" -#include "tachyon/zk/expressions/fixed_expression.h" -#include "tachyon/zk/expressions/instance_expression.h" #include "tachyon/zk/expressions/negated_expression.h" #include "tachyon/zk/expressions/product_expression.h" #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/expressions/advice_expression.h" +#include "tachyon/zk/plonk/expressions/challenge_expression.h" +#include "tachyon/zk/plonk/expressions/fixed_expression.h" +#include "tachyon/zk/plonk/expressions/instance_expression.h" +#include "tachyon/zk/plonk/expressions/selector_expression.h" #include "tachyon/zk/plonk/vanishing/calculation.h" namespace tachyon::zk::plonk { @@ -49,7 +49,7 @@ struct TACHYON_EXPORT CalculationInfo { }; template -class GraphEvaluator : public Evaluator { +class GraphEvaluator : public tachyon::zk::Evaluator { public: GraphEvaluator() = default; diff --git a/tachyon/zk/plonk/vanishing/graph_evaluator_unittest.cc b/tachyon/zk/plonk/vanishing/graph_evaluator_unittest.cc index 8242e0d2a..5f79308c0 100644 --- a/tachyon/zk/plonk/vanishing/graph_evaluator_unittest.cc +++ b/tachyon/zk/plonk/vanishing/graph_evaluator_unittest.cc @@ -3,8 +3,8 @@ #include #include "tachyon/base/random.h" -#include "tachyon/zk/expressions/evaluator/test/evaluator_test.h" -#include "tachyon/zk/expressions/expression_factory.h" +#include "tachyon/zk/plonk/expressions/evaluator/test/evaluator_test.h" +#include "tachyon/zk/plonk/expressions/expression_factory.h" namespace tachyon::zk::plonk { From 359b92574ce886c1d1da6373fe2537ec717db3a3 Mon Sep 17 00:00:00 2001 From: batzor <32958247+batzor@users.noreply.github.com> Date: Thu, 27 Jun 2024 10:18:21 +0900 Subject: [PATCH 7/7] chore(zk): remove legacy unused code It became obsolete in https://github.com/kroma-network/tachyon/commit/919f00 --- tachyon/zk/lookup/proving_evaluator.h | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/tachyon/zk/lookup/proving_evaluator.h b/tachyon/zk/lookup/proving_evaluator.h index 192f135b0..1f3fb08ad 100644 --- a/tachyon/zk/lookup/proving_evaluator.h +++ b/tachyon/zk/lookup/proving_evaluator.h @@ -44,16 +44,6 @@ class ProvingEvaluator // Evaluator methods Field Evaluate(const Expression* input) override { - class ScopedIdxIncrement { - public: - explicit ScopedIdxIncrement(ProvingEvaluator* evaluator) - : evaluator(evaluator) {} - ~ScopedIdxIncrement() { ++evaluator->idx_; } - - private: - // not owned - ProvingEvaluator* const evaluator; - }; switch (input->type()) { case ExpressionType::kConstant: return input->ToConstant()->value();