Skip to content

Commit

Permalink
Merge pull request #493 from kroma-network/refac/prepare-circle-stark
Browse files Browse the repository at this point in the history
refac: prepare circle stark
  • Loading branch information
chokobole authored Jul 30, 2024
2 parents 180e6a4 + 8095d3b commit 2e76ad9
Show file tree
Hide file tree
Showing 73 changed files with 409 additions and 421 deletions.
2 changes: 1 addition & 1 deletion benchmark/ec/ec_double_benchmark_gpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
#include "tachyon/base/time/time_interval.h"
#include "tachyon/device/gpu/gpu_memory.h"
#include "tachyon/math/elliptic_curves/bn/bn254/g1_gpu.h"
#include "tachyon/math/elliptic_curves/point_conversions.h"
#include "tachyon/math/elliptic_curves/short_weierstrass/kernels/elliptic_curve_ops.cu.h"
#include "tachyon/math/elliptic_curves/test/random.h"
#include "tachyon/math/geometry/point_conversions.h"

namespace tachyon {

Expand Down
4 changes: 2 additions & 2 deletions tachyon/c/math/elliptic_curves/msm/msm_gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#include "tachyon/c/math/elliptic_curves/msm/msm_input_provider.h"
#include "tachyon/device/gpu/scoped_mem_pool.h"
#include "tachyon/device/gpu/scoped_stream.h"
#include "tachyon/math/elliptic_curves/affine_point.h"
#include "tachyon/math/elliptic_curves/msm/variable_base_msm_gpu.h"
#include "tachyon/math/elliptic_curves/point_conversions.h"
#include "tachyon/math/geometry/affine_point.h"
#include "tachyon/math/geometry/point_conversions.h"

namespace tachyon::c::math {

Expand Down
2 changes: 1 addition & 1 deletion tachyon/c/zk/plonk/halo2/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ tachyon_cc_library(
"//tachyon/base/buffer:endian_auto_reset",
"//tachyon/base/buffer:read_only_buffer",
"//tachyon/base/containers:container_util",
"//tachyon/math/elliptic_curves:points",
"//tachyon/math/finite_fields:cubic_extension_field",
"//tachyon/math/finite_fields:prime_field_base",
"//tachyon/math/finite_fields:quadratic_extension_field",
"//tachyon/math/geometry:affine_point",
"//tachyon/math/polynomials/univariate:univariate_evaluations",
"//tachyon/math/polynomials/univariate:univariate_polynomial",
"//tachyon/zk/expressions:expression_factory",
Expand Down
2 changes: 1 addition & 1 deletion tachyon/c/zk/plonk/halo2/buffer_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
#include "tachyon/base/logging.h"
#include "tachyon/c/zk/plonk/halo2/bn254_gwc_pcs.h"
#include "tachyon/c/zk/plonk/halo2/bn254_shplonk_pcs.h"
#include "tachyon/math/elliptic_curves/affine_point.h"
#include "tachyon/math/finite_fields/cubic_extension_field.h"
#include "tachyon/math/finite_fields/prime_field_base.h"
#include "tachyon/math/finite_fields/quadratic_extension_field.h"
#include "tachyon/math/geometry/affine_point.h"
#include "tachyon/math/polynomials/univariate/univariate_evaluations.h"
#include "tachyon/math/polynomials/univariate/univariate_polynomial.h"
#include "tachyon/zk/expressions/expression_factory.h"
Expand Down
4 changes: 2 additions & 2 deletions tachyon/crypto/commitments/fri/fri.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class FRI final
// Pᵢ₊₁(X) = Pᵢ_even(X²) + β * Pᵢ_odd(X²)
beta = writer->SqueezeChallenge();
VLOG(2) << "FRI(beta[" << i - 1 << "]): " << beta.ToHexString(true);
folded_poly = cur_poly->template Fold<false>(beta);
folded_poly = cur_poly->Fold(beta);
BinaryMerkleTree<F, F, MaxDegree + 1> tree(storage_->GetLayer(i),
hasher_);
evals = sub_domains_[i - 1]->FFT(folded_poly);
Expand All @@ -82,7 +82,7 @@ class FRI final
beta = writer->SqueezeChallenge();
VLOG(2) << "FRI(beta[" << num_layers - 1
<< "]): " << beta.ToHexString(true);
folded_poly = cur_poly->template Fold<false>(beta);
folded_poly = cur_poly->Fold(beta);
return writer->WriteToProof(folded_poly[0]);
}

Expand Down
2 changes: 1 addition & 1 deletion tachyon/crypto/commitments/kzg/kzg.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "tachyon/base/logging.h"
#include "tachyon/crypto/commitments/batch_commitment_state.h"
#include "tachyon/math/elliptic_curves/msm/variable_base_msm.h"
#include "tachyon/math/elliptic_curves/point_conversions.h"
#include "tachyon/math/geometry/point_conversions.h"
#include "tachyon/math/polynomials/univariate/univariate_evaluation_domain.h"

#if TACHYON_CUDA
Expand Down
2 changes: 1 addition & 1 deletion tachyon/crypto/commitments/pedersen/pedersen.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "tachyon/base/strings/string_util.h"
#include "tachyon/crypto/commitments/vector_commitment_scheme.h"
#include "tachyon/math/elliptic_curves/msm/variable_base_msm.h"
#include "tachyon/math/elliptic_curves/point_conversions.h"
#include "tachyon/math/geometry/point_conversions.h"

#if TACHYON_CUDA
#include "tachyon/device/gpu/scoped_mem_pool.h"
Expand Down
2 changes: 1 addition & 1 deletion tachyon/crypto/transcripts/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ tachyon_cc_library(
name = "transcript_traits",
hdrs = ["transcript_traits.h"],
deps = [
"//tachyon/math/elliptic_curves:points",
"//tachyon/math/finite_fields:prime_field_base",
"//tachyon/math/geometry:affine_point",
],
)
2 changes: 1 addition & 1 deletion tachyon/crypto/transcripts/transcript_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include <type_traits>

#include "tachyon/math/elliptic_curves/affine_point.h"
#include "tachyon/math/finite_fields/prime_field_base.h"
#include "tachyon/math/geometry/affine_point.h"

namespace tachyon::crypto {

Expand Down
10 changes: 5 additions & 5 deletions tachyon/math/base/semigroups.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct MultiplicativeSemigroupTraits {
using ReturnTy = G;
};

template <typename G>
template <typename G, typename SFINAE = void>
struct AdditiveSemigroupTraits {
using ReturnTy = G;
};
Expand Down Expand Up @@ -320,25 +320,25 @@ class AdditiveSemigroup {
case 2:
return Double();
case 3:
return Double() * g;
return Double() + g;
case 4:
return Double().Double();
case 5: {
AddResult g4 = Double();
g4.DoubleInPlace();
return g4 * g;
return g4 + g;
}
case 6: {
AddResult g2 = Double();
AddResult g4 = g2;
g4.DoubleInPlace();
return g4 * g2;
return g4 + g2;
}
case 7: {
AddResult g2 = Double();
AddResult g4 = g2;
g4.DoubleInPlace();
return g4 * g2 * g;
return g4 + g2 + g;
}
default:
return DoScalarMul(BigInt<1>(scalar));
Expand Down
27 changes: 13 additions & 14 deletions tachyon/math/circle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@ load("//bazel:tachyon_cc.bzl", "tachyon_cc_library", "tachyon_cc_unittest")
package(default_visibility = ["//visibility:public"])

tachyon_cc_library(
name = "circle",
hdrs = ["circle.h"],
deps = [":circle_traits_forward"],
)

tachyon_cc_library(
name = "circle_point",
hdrs = ["circle_point.h"],
name = "affine_point",
hdrs = ["affine_point.h"],
deps = [
":circle_point_forward",
"//tachyon/base:logging",
"//tachyon/math/base:groups",
"//tachyon/math/geometry:curve_type",
"//tachyon/math/geometry:point2",
"@com_google_absl//absl/strings",
],
)

tachyon_cc_library(
name = "circle_point_forward",
hdrs = ["circle_point_forward.h"],
name = "circle",
hdrs = ["circle.h"],
deps = [
":circle_traits_forward",
"//tachyon/math/geometry:affine_point",
"//tachyon/math/geometry:curve_type",
],
)

tachyon_cc_library(
Expand All @@ -32,9 +31,9 @@ tachyon_cc_library(

tachyon_cc_unittest(
name = "circle_unittests",
srcs = ["circle_point_unittest.cc"],
srcs = ["affine_point_unittest.cc"],
deps = [
"//tachyon/math/circle/stark:g1",
"//tachyon/math/circle/stark:g4",
"//tachyon/math/circle/m31:g1",
"//tachyon/math/circle/m31:g4",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a Apache-2.0 style license that
// can be found in the LICENSE-APACHE.stwo

#ifndef TACHYON_MATH_CIRCLE_CIRCLE_POINT_H_
#define TACHYON_MATH_CIRCLE_CIRCLE_POINT_H_
#ifndef TACHYON_MATH_CIRCLE_AFFINE_POINT_H_
#define TACHYON_MATH_CIRCLE_AFFINE_POINT_H_

#include <string>
#include <utility>
Expand All @@ -12,75 +12,77 @@

#include "tachyon/base/logging.h"
#include "tachyon/math/base/groups.h"
#include "tachyon/math/circle/circle_point_forward.h"
#include "tachyon/math/geometry/affine_point.h"
#include "tachyon/math/geometry/curve_type.h"
#include "tachyon/math/geometry/point2.h"

namespace tachyon::math {

// TODO(chokobole): Unify this into |AffinePoint|.
template <typename _Circle>
class CirclePoint : public AdditiveGroup<CirclePoint<_Circle>> {
template <typename _Curve>
class AffinePoint<_Curve, std::enable_if_t<_Curve::kType == CurveType::kCircle>>
final : public AdditiveGroup<AffinePoint<_Curve>> {
public:
using Circle = _Circle;
using Curve = _Curve;
using Circle = _Curve;
using BaseField = typename Circle::BaseField;
using ScalarField = typename Circle::ScalarField;

constexpr CirclePoint() : CirclePoint(BaseField::One(), BaseField::Zero()) {}
explicit constexpr CirclePoint(const Point2<BaseField>& point)
: CirclePoint(point.x, point.y) {}
explicit constexpr CirclePoint(Point2<BaseField>&& point)
: CirclePoint(std::move(point.x), std::move(point.y)) {}
constexpr CirclePoint(const BaseField& x, const BaseField& y)
constexpr AffinePoint() : AffinePoint(BaseField::One(), BaseField::Zero()) {}
explicit constexpr AffinePoint(const Point2<BaseField>& point)
: AffinePoint(point.x, point.y) {}
explicit constexpr AffinePoint(Point2<BaseField>&& point)
: AffinePoint(std::move(point.x), std::move(point.y)) {}
constexpr AffinePoint(const BaseField& x, const BaseField& y)
: x_(x), y_(y) {}
constexpr CirclePoint(BaseField&& x, BaseField&& y)
constexpr AffinePoint(BaseField&& x, BaseField&& y)
: x_(std::move(x)), y_(std::move(y)) {}

constexpr static CirclePoint CreateChecked(const BaseField& x,
constexpr static AffinePoint CreateChecked(const BaseField& x,
const BaseField& y) {
CirclePoint ret = {x, y};
AffinePoint ret = {x, y};
CHECK(ret.IsOnCircle());
return ret;
}

constexpr static CirclePoint CreateChecked(BaseField&& x, BaseField&& y) {
CirclePoint ret = {std::move(x), std::move(y)};
constexpr static AffinePoint CreateChecked(BaseField&& x, BaseField&& y) {
AffinePoint ret = {std::move(x), std::move(y)};
CHECK(ret.IsOnCircle());
return ret;
}

constexpr static CirclePoint Zero() { return CirclePoint(); }
constexpr static AffinePoint Zero() { return AffinePoint(); }

constexpr static CirclePoint One() { return Generator(); }
constexpr static AffinePoint One() { return Generator(); }

constexpr static CirclePoint Generator() {
constexpr static AffinePoint Generator() {
return {Circle::Config::kGenerator.x, Circle::Config::kGenerator.y};
}

constexpr static CirclePoint Random() {
constexpr static AffinePoint Random() {
return ScalarField::Random() * Generator();
}

constexpr const BaseField& x() const { return x_; }
constexpr const BaseField& y() const { return y_; }

constexpr bool operator==(const CirclePoint& other) const {
constexpr bool operator==(const AffinePoint& other) const {
return x_ == other.x_ && y_ == other.y_;
}
constexpr bool operator!=(const CirclePoint& other) const {
constexpr bool operator!=(const AffinePoint& other) const {
return !operator==(other);
}

constexpr bool IsZero() const { return x_.IsOne() && y_.IsZero(); }

constexpr bool IsOnCircle() { return Circle::IsOnCircle(*this); }

constexpr CirclePoint Conjugate() const { return {x_, -y_}; }
constexpr AffinePoint Conjugate() const { return {x_, -y_}; }

constexpr CirclePoint ComplexConjugate() const {
constexpr AffinePoint ComplexConjugate() const {
return {x_.ComplexConjugate(), y_.ComplexConjugate()};
}

constexpr CirclePoint Antipode() const { return {-x_, -y_}; }
constexpr AffinePoint Antipode() const { return {-x_, -y_}; }

std::string ToString() const {
return absl::Substitute("($0, $1)", x_.ToString(), y_.ToString());
Expand All @@ -92,33 +94,43 @@ class CirclePoint : public AdditiveGroup<CirclePoint<_Circle>> {
}

// AdditiveSemigroup methods
constexpr CirclePoint Add(const CirclePoint& other) const {
constexpr AffinePoint Add(const AffinePoint& other) const {
return {x_ * other.x_ - y_ * other.y_, x_ * other.y_ + y_ * other.x_};
}

constexpr CirclePoint& AddInPlace(const CirclePoint& other) {
constexpr AffinePoint& AddInPlace(const AffinePoint& other) {
return *this = {x_ * other.x_ - y_ * other.y_,
x_ * other.y_ + y_ * other.x_};
}

constexpr AffinePoint DoubleImpl() const {
return {x_.Square().Double() - BaseField::One(), x_.Double() * y_};
}

constexpr AffinePoint& DoubleImplInPlace() {
y_ *= x_.Double();
x_.SquareInPlace().DoubleInPlace() -= BaseField::One();
return *this;
}

// AdditiveGroup methods
constexpr CirclePoint Sub(const CirclePoint& other) const {
constexpr AffinePoint Sub(const AffinePoint& other) const {
return {x_ * other.x_ + y_ * other.y_, -x_ * other.y_ + y_ * other.x_};
}

constexpr CirclePoint& SubInPlace(const CirclePoint& other) {
constexpr AffinePoint& SubInPlace(const AffinePoint& other) {
return *this = {x_ * other.x_ + y_ * other.y_,
-x_ * other.y_ + y_ * other.x_};
}

constexpr CirclePoint Negate() const { return {x_, -y_}; }
constexpr AffinePoint Negate() const { return {x_, -y_}; }

constexpr CirclePoint& NegateInPlace() {
constexpr AffinePoint& NegateInPlace() {
y_.NegateInPlace();
return *this;
}

constexpr CirclePoint operator*(const ScalarField& idx) const {
constexpr AffinePoint operator*(const ScalarField& idx) const {
return this->ScalarMul(idx.value());
}

Expand All @@ -129,4 +141,4 @@ class CirclePoint : public AdditiveGroup<CirclePoint<_Circle>> {

} // namespace tachyon::math

#endif // TACHYON_MATH_CIRCLE_CIRCLE_POINT_H_
#endif // TACHYON_MATH_CIRCLE_AFFINE_POINT_H_
Loading

0 comments on commit 2e76ad9

Please sign in to comment.