Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

refac: generalize example circuit tests #415

Merged
merged 12 commits into from
May 17, 2024
5 changes: 5 additions & 0 deletions tachyon/base/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ load("//bazel:tachyon_cc.bzl", "tachyon_cc_library", "tachyon_cc_unittest")

package(default_visibility = ["//visibility:public"])

tachyon_cc_library(
name = "array_to_vector",
hdrs = ["array_to_vector.h"],
)

tachyon_cc_library(
name = "bit_cast",
hdrs = ["bit_cast.h"],
Expand Down
27 changes: 27 additions & 0 deletions tachyon/base/array_to_vector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef TACHYON_BASE_ARRAY_TO_VECTOR_H_
#define TACHYON_BASE_ARRAY_TO_VECTOR_H_

#include <stddef.h>

#include <vector>

namespace tachyon::base {

template <typename T, size_t N>
const std::vector<T> ArrayToVector(const T (&arr)[N]) {
return std::vector<T>(std::begin(arr), std::end(arr));
chokobole marked this conversation as resolved.
Show resolved Hide resolved
}

template <typename T, size_t N, size_t M>
std::vector<std::vector<T>> Array2DToVector2D(const T (&arr)[N][M]) {
std::vector<std::vector<T>> vec;
vec.reserve(N);
for (const auto& inner_array : arr) {
vec.emplace_back(std::begin(inner_array), std::end(inner_array));
}
return vec;
}

} // namespace tachyon::base

#endif // TACHYON_BASE_ARRAY_TO_VECTOR_H_
17 changes: 9 additions & 8 deletions tachyon/zk/plonk/base/column_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ namespace tachyon::zk::plonk {

class TACHYON_EXPORT ColumnKeyBase {
public:
ColumnKeyBase() = default;
ColumnKeyBase(ColumnType type, size_t index) : type_(type), index_(index) {}
constexpr ColumnKeyBase() = default;
constexpr ColumnKeyBase(ColumnType type, size_t index)
: type_(type), index_(index) {}

ColumnType type() const { return type_; }
size_t index() const { return index_; }
Expand Down Expand Up @@ -62,16 +63,16 @@ class ColumnKey : public ColumnKeyBase {
public:
constexpr static ColumnType kDefaultType = C;

ColumnKey() : ColumnKeyBase(C, 0) {}
explicit ColumnKey(size_t index) : ColumnKeyBase(C, index) {}
constexpr ColumnKey() : ColumnKeyBase(C, 0) {}
constexpr explicit ColumnKey(size_t index) : ColumnKeyBase(C, index) {}

// NOTE(chokobole): AdviceColumnKey can be constructed with an additional
// argument |phase|.
//
// AdviceColumnKey column(1, kSecondPhase);
template <ColumnType C2 = C,
std::enable_if_t<C2 == ColumnType::kAdvice>* = nullptr>
ColumnKey(size_t index, Phase phase)
constexpr ColumnKey(size_t index, Phase phase)
: ColumnKeyBase(C, index), phase_(phase) {}

// NOTE(chokobole): in this case, |type_| is changed!
Expand All @@ -84,7 +85,7 @@ class ColumnKey : public ColumnKeyBase {
// CHECK_EQ(c.phase(), kSecondPhase);
template <ColumnType C2,
std::enable_if_t<C == ColumnType::kAny && C != C2>* = nullptr>
ColumnKey(const ColumnKey<C2>& other)
constexpr ColumnKey(const ColumnKey<C2>& other)
: ColumnKeyBase(other.type_, other.index_), phase_(other.phase_) {}

// NOTE(chokobole): in this case, |type_| is not changed!
Expand All @@ -98,11 +99,11 @@ class ColumnKey : public ColumnKeyBase {
// CHECK_EQ(c2.phase(), kSecondPhase);
template <ColumnType C2, std::enable_if_t<C != ColumnType::kAny &&
C2 == ColumnType::kAny>* = nullptr>
ColumnKey(const ColumnKey<C2>& other)
constexpr ColumnKey(const ColumnKey<C2>& other)
: ColumnKeyBase(C, other.index_), phase_(other.phase_) {}

// FixedColumnKey c(FixedColumnKey(1));
ColumnKey(const ColumnKey& other) = default;
constexpr ColumnKey(const ColumnKey& other) = default;

// NOTE(chokobole): in this case, |type_| is changed!
//
Expand Down
149 changes: 86 additions & 63 deletions tachyon/zk/plonk/examples/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,17 +1,72 @@
load("//bazel:tachyon_cc.bzl", "tachyon_cc_library", "tachyon_cc_test")
load("//bazel:tachyon_cc.bzl", "tachyon_cc_library", "tachyon_cc_unittest")

package(default_visibility = ["//visibility:public"])

COMMON_TEST_DATA_DEPS = [
":circuit_test_data",
":circuit_test_type_traits",
":point",
]

COMMON_TEST_DEPS = [
":circuit_test",
"//tachyon/zk/plonk/layout/floor_planner:simple_floor_planner",
"//tachyon/zk/plonk/layout/floor_planner/v1:v1_floor_planner",
]

tachyon_cc_library(
name = "circuit_test_data",
testonly = True,
hdrs = ["circuit_test_data.h"],
deps = [
":point",
"//tachyon/base:range",
],
)

tachyon_cc_library(
name = "circuit_test_type_traits",
hdrs = ["circuit_test_type_traits.h"],
deps = [
"//tachyon/math/elliptic_curves/bn/bn254",
"//tachyon/zk/base/commitments:gwc_extension",
"//tachyon/zk/base/commitments:shplonk_extension",
"//tachyon/zk/lookup/halo2:scheme",
"//tachyon/zk/plonk/layout/floor_planner:simple_floor_planner",
"//tachyon/zk/plonk/layout/floor_planner/v1:v1_floor_planner",
],
)

tachyon_cc_library(
name = "circuit_test",
testonly = True,
srcs = ["circuit_test.cc"],
hdrs = ["circuit_test.h"],
deps = [
":point",
":shuffle_circuit",
":shuffle_circuit_test_data",
":simple_circuit",
":simple_circuit_test_data",
":simple_lookup_circuit",
":simple_lookup_circuit_test_data",
"//tachyon/base:array_to_vector",
"//tachyon/math/elliptic_curves/bn/bn254",
"//tachyon/zk/lookup:lookup_pair",
"//tachyon/zk/plonk/halo2:pinned_constraint_system",
"//tachyon/zk/plonk/halo2:pinned_verifying_key",
"//tachyon/zk/plonk/halo2:prover_test",
"//tachyon/zk/plonk/keys:proving_key",
"//tachyon/zk/plonk/layout/floor_planner:simple_floor_planner",
"//tachyon/zk/plonk/layout/floor_planner/v1:v1_floor_planner",
],
)

tachyon_cc_library(
name = "point",
hdrs = ["point.h"],
)

tachyon_cc_library(
name = "shuffle_circuit",
hdrs = ["shuffle_circuit.h"],
Expand All @@ -21,94 +76,62 @@ tachyon_cc_library(
],
)

tachyon_cc_library(
name = "shuffle_circuit_test_data",
testonly = True,
hdrs = ["shuffle_circuit_test_data.h"],
deps = COMMON_TEST_DATA_DEPS,
)

tachyon_cc_library(
name = "simple_circuit",
hdrs = ["simple_circuit.h"],
deps = ["//tachyon/zk/plonk/constraint_system:circuit"],
)

tachyon_cc_library(
name = "simple_circuit_test_data",
testonly = True,
hdrs = ["simple_circuit_test_data.h"],
deps = COMMON_TEST_DATA_DEPS,
)

tachyon_cc_library(
name = "simple_lookup_circuit",
hdrs = ["simple_lookup_circuit.h"],
deps = ["//tachyon/zk/plonk/constraint_system:circuit"],
)

# TODO(dongchangYoo): This is failed in CI because of timeout, 60 secs.
# Change |tachyon_cc_test| to |tachyon_cc_unittest| once CI can run within the timeout.
tachyon_cc_test(
tachyon_cc_library(
name = "simple_lookup_circuit_test_data",
testonly = True,
hdrs = ["simple_lookup_circuit_test_data.h"],
deps = COMMON_TEST_DATA_DEPS + ["//tachyon/base:range"],
)

tachyon_cc_unittest(
name = "shuffle_circuit_test",
srcs = ["shuffle_circuit_test.cc"],
deps = [
":circuit_test",
deps = COMMON_TEST_DEPS + [
":shuffle_circuit",
"//tachyon/math/elliptic_curves/bn/bn254",
"//tachyon/zk/base/commitments:gwc_extension",
"//tachyon/zk/base/commitments:shplonk_extension",
"//tachyon/zk/lookup/halo2:scheme",
"//tachyon/zk/plonk/halo2:pinned_verifying_key",
"//tachyon/zk/plonk/keys:proving_key",
"//tachyon/zk/plonk/layout/floor_planner:simple_floor_planner",
"//tachyon/zk/plonk/layout/floor_planner/v1:v1_floor_planner",
":shuffle_circuit_test_data",
],
)

tachyon_cc_test(
tachyon_cc_unittest(
name = "simple_circuit_test",
srcs = ["simple_circuit_test.cc"],
deps = [
":circuit_test",
deps = COMMON_TEST_DEPS + [
":simple_circuit",
"//tachyon/math/elliptic_curves/bn/bn254",
"//tachyon/zk/base/commitments:shplonk_extension",
"//tachyon/zk/lookup/halo2:scheme",
"//tachyon/zk/plonk/halo2:pinned_verifying_key",
"//tachyon/zk/plonk/keys:proving_key",
"//tachyon/zk/plonk/layout/floor_planner:simple_floor_planner",
":simple_circuit_test_data",
],
)

tachyon_cc_test(
tachyon_cc_unittest(
name = "simple_lookup_circuit_test",
srcs = ["simple_lookup_circuit_test.cc"],
deps = [
":circuit_test",
":simple_lookup_circuit",
"//tachyon/math/elliptic_curves/bn/bn254",
"//tachyon/zk/base/commitments:shplonk_extension",
"//tachyon/zk/lookup/halo2:scheme",
"//tachyon/zk/plonk/halo2:pinned_verifying_key",
"//tachyon/zk/plonk/keys:proving_key",
"//tachyon/zk/plonk/layout/floor_planner:simple_floor_planner",
],
)

tachyon_cc_test(
name = "simple_lookup_v1_circuit_test",
srcs = ["simple_lookup_v1_circuit_test.cc"],
deps = [
":circuit_test",
deps = COMMON_TEST_DEPS + [
":simple_lookup_circuit",
"//tachyon/math/elliptic_curves/bn/bn254",
"//tachyon/zk/base/commitments:shplonk_extension",
"//tachyon/zk/lookup/halo2:scheme",
"//tachyon/zk/plonk/halo2:pinned_verifying_key",
"//tachyon/zk/plonk/keys:proving_key",
"//tachyon/zk/plonk/layout/floor_planner:simple_floor_planner",
"//tachyon/zk/plonk/layout/floor_planner/v1:v1_floor_planner",
],
)

tachyon_cc_test(
name = "simple_v1_circuit_test",
srcs = ["simple_v1_circuit_test.cc"],
deps = [
":circuit_test",
":simple_circuit",
"//tachyon/math/elliptic_curves/bn/bn254",
"//tachyon/zk/base/commitments:shplonk_extension",
"//tachyon/zk/lookup/halo2:scheme",
"//tachyon/zk/plonk/halo2:pinned_verifying_key",
"//tachyon/zk/plonk/keys:proving_key",
"//tachyon/zk/plonk/layout/floor_planner/v1:v1_floor_planner",
":simple_lookup_circuit_test_data",
],
)
Loading
Loading