Skip to content

Commit

Permalink
Merge pull request #446 from kroma-network/refac/modularize-expressions
Browse files Browse the repository at this point in the history
refac(zk): modularize expressions
  • Loading branch information
chokobole authored Jun 27, 2024
2 parents fa4d68b + 359b925 commit 5de7501
Show file tree
Hide file tree
Showing 54 changed files with 1,055 additions and 798 deletions.
13 changes: 8 additions & 5 deletions tachyon/c/zk/plonk/halo2/buffer_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,24 +295,27 @@ class BufferReader<std::unique_ptr<tachyon::zk::Expression<F>>> {
static std::unique_ptr<tachyon::zk::Expression<F>> Read(
const tachyon::base::ReadOnlyBuffer& buffer) {
uint8_t kind = BufferReader<uint8_t>::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<F>::Constant(
BufferReader<F>::Read(buffer));
case 1:
return tachyon::zk::ExpressionFactory<F>::Selector(
return tachyon::zk::plonk::ExpressionFactory<F>::Selector(
BufferReader<tachyon::zk::plonk::Selector>::Read(buffer));
case 2:
return tachyon::zk::ExpressionFactory<F>::Fixed(
return tachyon::zk::plonk::ExpressionFactory<F>::Fixed(
BufferReader<tachyon::zk::plonk::FixedQuery>::Read(buffer));
case 3:
return tachyon::zk::ExpressionFactory<F>::Advice(
return tachyon::zk::plonk::ExpressionFactory<F>::Advice(
BufferReader<tachyon::zk::plonk::AdviceQuery>::Read(buffer));
case 4:
return tachyon::zk::ExpressionFactory<F>::Instance(
return tachyon::zk::plonk::ExpressionFactory<F>::Instance(
BufferReader<tachyon::zk::plonk::InstanceQuery>::Read(buffer));
case 5:
return tachyon::zk::ExpressionFactory<F>::Challenge(
return tachyon::zk::plonk::ExpressionFactory<F>::Challenge(
BufferReader<tachyon::zk::plonk::Challenge>::Read(buffer));
case 6:
return tachyon::zk::ExpressionFactory<F>::Negated(
Expand Down
66 changes: 0 additions & 66 deletions tachyon/zk/expressions/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -44,29 +23,19 @@ tachyon_cc_library(
hdrs = ["expression.h"],
deps = [
":expression_type",
"//tachyon/base:ref",
"//tachyon/base/containers:container_util",
"//tachyon/zk/plonk/constraint_system:selector",
],
)

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",
"//tachyon/zk/expressions/evaluator:selector_replacer",
"//tachyon/zk/expressions/evaluator:simple_selector_extractor",
"//tachyon/zk/expressions/evaluator:simple_selector_finder",
],
)

Expand All @@ -77,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"],
Expand Down Expand Up @@ -127,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"],
Expand All @@ -150,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 = [
Expand Down
4 changes: 0 additions & 4 deletions tachyon/zk/expressions/constant_expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ class ConstantExpression : public Expression<F> {
value_.ToString());
}

void WriteIdentifier(std::ostream& out) const override {
out << value_.ToString();
}

bool operator==(const Expression<F>& other) const override {
if (!Expression<F>::operator==(other)) return false;
const ConstantExpression* constant = other.ToConstant();
Expand Down
77 changes: 77 additions & 0 deletions tachyon/zk/expressions/evaluator/identifier.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#ifndef TACHYON_ZK_EXPRESSIONS_EVALUATOR_IDENTIFIER_H_
#define TACHYON_ZK_EXPRESSIONS_EVALUATOR_IDENTIFIER_H_

#include <sstream>
#include <string>

#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 <typename F>
std::string Identifier(const Expression<F>* 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_
89 changes: 0 additions & 89 deletions tachyon/zk/expressions/evaluator/selector_replacer.h

This file was deleted.

Loading

0 comments on commit 5de7501

Please sign in to comment.