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: refactor two adic fri pcs #526

Merged
merged 21 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
2ef8720
fix(crypto): change `Challenger` argument to reference
chokobole Aug 22, 2024
a7a71d9
perf(crypto): add missing `std::move()`
chokobole Aug 23, 2024
7d784ab
chore(crypto): fix typo
chokobole Aug 23, 2024
31c6350
perf(crypto): parallelize the big for loop
chokobole Aug 23, 2024
f2742e4
refac(crypto): change the argument name in google c++ style
chokobole Aug 23, 2024
3edba86
fix(crypto): fix type inference correctly
chokobole Aug 23, 2024
2a51856
refac(crypto): rename `Pcs` to `PCS`
chokobole Aug 23, 2024
33ed7f1
refac(crypto): change template type of `BatchOpening`
chokobole Aug 23, 2024
c7c5f83
refac(crypto): change template type of `CommitPhaseResult`
chokobole Aug 23, 2024
696e5fd
refac(crypto): use `base::CreateVector`
chokobole Aug 23, 2024
cac0aeb
refac(crypto): move logging final eval to closer where it is used
chokobole Aug 23, 2024
b846c54
refac(crypto): change template type of `TwoAdicFriProof`
chokobole Aug 23, 2024
8486867
refac: rename `TwoAdicFriConfig` to `FriConfig`
chokobole Aug 23, 2024
a3a3240
refac: move zk/air/plonky3 files to crypto
chokobole Aug 27, 2024
6731d54
refac: remove `Domain` template from `TwoAdicFriPCS`
chokobole Aug 27, 2024
31ba5d9
refac(crypto): rename `two_adic_fri_verifier.h` to `verify.h`
chokobole Aug 27, 2024
f25c6bf
refac(crypto): rename `two_adic_fri_prover.h` to `prove.h`
chokobole Aug 27, 2024
c2ba345
refac(crypto): rename `FRIXXX` to `SimpleFRIXXX`
chokobole Aug 29, 2024
799cf3f
refac(crypto): rename `TwoAdicFriProof` to `FriProof`
chokobole Aug 29, 2024
cd5b29b
refac!: rename `TwoAdicFriPcs` to `TwoAdicFRI` for consistency
chokobole Aug 29, 2024
a75038c
refac: reanme `Fri` to `FRI` for consistency
chokobole Aug 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tachyon/c/crypto/commitments/fri/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load("//bazel:tachyon_cc.bzl", "tachyon_cc_library")
package(default_visibility = ["//visibility:public"])

tachyon_cc_library(
name = "two_adic_fri_pcs_impl",
hdrs = ["two_adic_fri_pcs_impl.h"],
deps = ["//tachyon/crypto/commitments/fri:two_adic_fri_pcs"],
name = "two_adic_fri_impl",
hdrs = ["two_adic_fri_impl.h"],
deps = ["//tachyon/crypto/commitments/fri:two_adic_fri"],
)
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
#ifndef TACHYON_C_CRYPTO_COMMITMENTS_FRI_TWO_ADIC_FRI_PCS_IMPL_H_
#define TACHYON_C_CRYPTO_COMMITMENTS_FRI_TWO_ADIC_FRI_PCS_IMPL_H_
#ifndef TACHYON_C_CRYPTO_COMMITMENTS_FRI_TWO_ADIC_FRI_IMPL_H_
#define TACHYON_C_CRYPTO_COMMITMENTS_FRI_TWO_ADIC_FRI_IMPL_H_

#include <memory>
#include <utility>
#include <vector>

#include "tachyon/crypto/commitments/fri/two_adic_fri_pcs.h"
#include "tachyon/crypto/commitments/fri/two_adic_fri.h"

namespace tachyon::c::crypto {

template <typename ExtF, typename InputMMCS, typename ChallengeMMCS,
typename Challenger, typename Coset>
class TwoAdicFriPCSImpl
: public tachyon::crypto::TwoAdicFriPCS<ExtF, InputMMCS, ChallengeMMCS,
Challenger, Coset> {
typename Challenger>
class TwoAdicFRIImpl
: public tachyon::crypto::TwoAdicFRI<ExtF, InputMMCS, ChallengeMMCS,
Challenger> {
public:
using Base = tachyon::crypto::TwoAdicFriPCS<ExtF, InputMMCS, ChallengeMMCS,
Challenger, Coset>;
using Base =
tachyon::crypto::TwoAdicFRI<ExtF, InputMMCS, ChallengeMMCS, Challenger>;
using F = typename Base::F;
using Commitment = typename Base::Commitment;
using ProverData = typename Base::ProverData;
using Domain = typename Base::Domain;

using Base::Base;

void AllocateLDEs(size_t size) { this->ldes_.reserve(size); }

template <typename Derived>
absl::Span<F> CosetLDEBatch(Eigen::MatrixBase<Derived>& matrix, F shift) {
Coset coset = this->GetNaturalDomainForDegree(matrix.rows());
Domain coset = this->GetNaturalDomainForDegree(matrix.rows());
tachyon::math::RowMajorMatrix<F> mat =
coset.domain()->CosetLDEBatch(matrix, this->fri_.log_blowup, shift);
ReverseMatrixIndexBits(mat);
Expand All @@ -52,4 +53,4 @@ class TwoAdicFriPCSImpl

} // namespace tachyon::c::crypto

#endif // TACHYON_C_CRYPTO_COMMITMENTS_FRI_TWO_ADIC_FRI_PCS_IMPL_H_
#endif // TACHYON_C_CRYPTO_COMMITMENTS_FRI_TWO_ADIC_FRI_IMPL_H_
24 changes: 12 additions & 12 deletions tachyon/c/zk/air/sp1/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ filegroup(
srcs = [
"baby_bear_poseidon2_constants.h",
"baby_bear_poseidon2_duplex_challenger.h",
"baby_bear_poseidon2_two_adic_fri_pcs.h",
"baby_bear_poseidon2_two_adic_fri.h",
],
)

Expand All @@ -28,10 +28,10 @@ tachyon_cc_library(
"//tachyon/c:export",
"//tachyon/c/base:type_traits_forward",
"//tachyon/c/math/finite_fields/baby_bear",
"//tachyon/crypto/challenger:duplex_challenger",
"//tachyon/crypto/hashes/sponge/poseidon2",
"//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_plonky3_external_matrix",
"//tachyon/math/finite_fields/baby_bear:poseidon2",
"//tachyon/zk/air/plonky3/challenger:duplex_challenger",
],
)

Expand All @@ -52,21 +52,23 @@ tachyon_cc_library(
)

tachyon_cc_library(
name = "baby_bear_poseidon2_two_adic_fri_pcs",
srcs = ["baby_bear_poseidon2_two_adic_fri_pcs.cc"],
name = "baby_bear_poseidon2_two_adic_fri",
srcs = ["baby_bear_poseidon2_two_adic_fri.cc"],
hdrs = [
"baby_bear_poseidon2_two_adic_fri_pcs.h",
"baby_bear_poseidon2_two_adic_fri_pcs_type_traits.h",
"baby_bear_poseidon2_two_adic_fri.h",
"baby_bear_poseidon2_two_adic_fri_type_traits.h",
],
deps = [
":baby_bear_poseidon2_constants",
":baby_bear_poseidon2_field_merkle_tree",
"//tachyon/c:export",
"//tachyon/c/base:type_traits_forward",
"//tachyon/c/crypto/commitments/fri:two_adic_fri_pcs_impl",
"//tachyon/c/crypto/commitments/fri:two_adic_fri_impl",
"//tachyon/c/math/finite_fields/baby_bear",
"//tachyon/c/math/matrix:baby_bear_row_major_matrix",
"//tachyon/crypto/commitments/fri:two_adic_fri_pcs",
"//tachyon/crypto/challenger:duplex_challenger",
"//tachyon/crypto/commitments/fri:two_adic_fri",
"//tachyon/crypto/commitments/fri:two_adic_multiplicative_coset",
"//tachyon/crypto/commitments/merkle_tree/field_merkle_tree:extension_field_merkle_tree_mmcs",
"//tachyon/crypto/commitments/merkle_tree/field_merkle_tree:field_merkle_tree_mmcs",
"//tachyon/crypto/hashes/sponge:padding_free_sponge",
Expand All @@ -75,24 +77,22 @@ tachyon_cc_library(
"//tachyon/crypto/hashes/sponge/poseidon2:poseidon2_plonky3_external_matrix",
"//tachyon/math/finite_fields/baby_bear:baby_bear4",
"//tachyon/math/finite_fields/baby_bear:poseidon2",
"//tachyon/zk/air/plonky3/base:two_adic_multiplicative_coset",
"//tachyon/zk/air/plonky3/challenger:duplex_challenger",
],
)

tachyon_cc_library(
name = "sp1",
deps = [
":baby_bear_poseidon2_duplex_challenger",
":baby_bear_poseidon2_two_adic_fri_pcs",
":baby_bear_poseidon2_two_adic_fri",
],
)

tachyon_cc_unittest(
name = "sp1_unittests",
srcs = [
"baby_bear_poseidon2_duplex_challenger_unittest.cc",
"baby_bear_poseidon2_two_adic_fri_pcs_unittest.cc",
"baby_bear_poseidon2_two_adic_fri_unittest.cc",
],
deps = [
":sp1",
Expand Down
16 changes: 9 additions & 7 deletions tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@ tachyon_sp1_baby_bear_poseidon2_duplex_challenger_create() {
std::move(ark));
Poseidon2 sponge(std::move(config));
return c::base::c_cast(
new zk::air::plonky3::DuplexChallenger<
Poseidon2, TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_WIDTH,
TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_RATE>(std::move(sponge)));
new crypto::DuplexChallenger<Poseidon2,
TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_WIDTH,
TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_RATE>(
std::move(sponge)));
}

tachyon_sp1_baby_bear_poseidon2_duplex_challenger*
tachyon_sp1_baby_bear_poseidon2_duplex_challenger_clone(
const tachyon_sp1_baby_bear_poseidon2_duplex_challenger* challenger) {
return c::base::c_cast(new zk::air::plonky3::DuplexChallenger<
Poseidon2, TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_WIDTH,
TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_RATE>(
*c::base::native_cast(challenger)));
return c::base::c_cast(
new crypto::DuplexChallenger<Poseidon2,
TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_WIDTH,
TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_RATE>(
*c::base::native_cast(challenger)));
}

void tachyon_sp1_baby_bear_poseidon2_duplex_challenger_destroy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
#include "tachyon/c/base/type_traits_forward.h"
#include "tachyon/c/zk/air/sp1/baby_bear_poseidon2_constants.h"
#include "tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger.h"
#include "tachyon/crypto/challenger/duplex_challenger.h"
#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2.h"
#include "tachyon/crypto/hashes/sponge/poseidon2/poseidon2_plonky3_external_matrix.h"
#include "tachyon/math/finite_fields/baby_bear/baby_bear.h"
#include "tachyon/zk/air/plonky3/challenger/duplex_challenger.h"

namespace tachyon::c::base {

template <>
struct TypeTraits<tachyon::zk::air::plonky3::DuplexChallenger<
struct TypeTraits<tachyon::crypto::DuplexChallenger<
tachyon::crypto::Poseidon2Sponge<tachyon::crypto::Poseidon2ExternalMatrix<
tachyon::crypto::Poseidon2Plonky3ExternalMatrix<
tachyon::math::BabyBear>>>,
Expand All @@ -23,7 +23,7 @@ struct TypeTraits<tachyon::zk::air::plonky3::DuplexChallenger<

template <>
struct TypeTraits<tachyon_sp1_baby_bear_poseidon2_duplex_challenger> {
using NativeType = tachyon::zk::air::plonky3::DuplexChallenger<
using NativeType = tachyon::crypto::DuplexChallenger<
tachyon::crypto::Poseidon2Sponge<tachyon::crypto::Poseidon2ExternalMatrix<
tachyon::crypto::Poseidon2Plonky3ExternalMatrix<
tachyon::math::BabyBear>>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
#include "tachyon/c/zk/air/sp1/baby_bear_poseidon2_duplex_challenger_type_traits.h"
#include "tachyon/math/finite_fields/test/finite_field_test.h"

namespace tachyon::zk::air::plonky3 {
namespace tachyon::crypto {

using F = math::BabyBear;
using Poseidon2 = crypto::Poseidon2Sponge<
crypto::Poseidon2ExternalMatrix<crypto::Poseidon2Plonky3ExternalMatrix<F>>>;
using Poseidon2 =
Poseidon2Sponge<Poseidon2ExternalMatrix<Poseidon2Plonky3ExternalMatrix<F>>>;

class DuplexChallengerTest : public math::FiniteFieldTest<F> {
public:
Expand Down Expand Up @@ -45,4 +45,4 @@ TEST_F(DuplexChallengerTest, APIs) {
}
}

} // namespace tachyon::zk::air::plonky3
} // namespace tachyon::crypto
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "tachyon/c/zk/air/sp1/baby_bear_poseidon2_two_adic_fri_pcs.h"
#include "tachyon/c/zk/air/sp1/baby_bear_poseidon2_two_adic_fri.h"

#include <utility>

#include "tachyon/c/math/finite_fields/baby_bear/baby_bear_type_traits.h"
#include "tachyon/c/math/matrix/baby_bear_row_major_matrix_type_traits.h"
#include "tachyon/c/zk/air/sp1/baby_bear_poseidon2_field_merkle_tree_type_traits.h"
#include "tachyon/c/zk/air/sp1/baby_bear_poseidon2_two_adic_fri_pcs_type_traits.h"
#include "tachyon/c/zk/air/sp1/baby_bear_poseidon2_two_adic_fri_type_traits.h"

using namespace tachyon;

Expand All @@ -25,9 +25,10 @@ using ExtMMCS = c::zk::air::plonky3::baby_bear::ExtMMCS;
using ChallengeMMCS = c::zk::air::plonky3::baby_bear::ChallengeMMCS;
using PCS = c::zk::air::plonky3::baby_bear::PCS;

tachyon_sp1_baby_bear_poseidon2_two_adic_fri_pcs*
tachyon_sp1_baby_bear_poseidon2_two_adic_fri_pcs_create(
uint32_t log_blowup, size_t num_queries, size_t proof_of_work_bits) {
tachyon_sp1_baby_bear_poseidon2_two_adic_fri*
tachyon_sp1_baby_bear_poseidon2_two_adic_fri_create(uint32_t log_blowup,
size_t num_queries,
size_t proof_of_work_bits) {
ExtF::Init();
ExtPackedF::Init();

Expand Down Expand Up @@ -80,25 +81,24 @@ tachyon_sp1_baby_bear_poseidon2_two_adic_fri_pcs_create(
ExtMMCS(std::move(hasher), std::move(packed_hasher),
std::move(compressor), std::move(packed_compressor)));

crypto::TwoAdicFriConfig<ChallengeMMCS> fri_config{
crypto::FRIConfig<ChallengeMMCS> fri_config{
log_blowup, num_queries, proof_of_work_bits, std::move(challenge_mmcs)};

return c::base::c_cast(new PCS(std::move(mmcs), std::move(fri_config)));
}

void tachyon_sp1_baby_bear_poseidon2_two_adic_fri_pcs_destroy(
tachyon_sp1_baby_bear_poseidon2_two_adic_fri_pcs* pcs) {
void tachyon_sp1_baby_bear_poseidon2_two_adic_fri_destroy(
tachyon_sp1_baby_bear_poseidon2_two_adic_fri* pcs) {
delete c::base::native_cast(pcs);
}

void tachyon_sp1_baby_bear_poseidon2_two_adic_fri_pcs_allocate_ldes(
tachyon_sp1_baby_bear_poseidon2_two_adic_fri_pcs* pcs, size_t size) {
void tachyon_sp1_baby_bear_poseidon2_two_adic_fri_allocate_ldes(
tachyon_sp1_baby_bear_poseidon2_two_adic_fri* pcs, size_t size) {
c::base::native_cast(pcs)->AllocateLDEs(size);
}

tachyon_baby_bear*
tachyon_sp1_baby_bear_poseidon2_two_adic_fri_pcs_coset_lde_batch(
tachyon_sp1_baby_bear_poseidon2_two_adic_fri_pcs* pcs,
tachyon_baby_bear* tachyon_sp1_baby_bear_poseidon2_two_adic_fri_coset_lde_batch(
tachyon_sp1_baby_bear_poseidon2_two_adic_fri* pcs,
tachyon_baby_bear* values, size_t rows, size_t cols,
tachyon_baby_bear shift, size_t* new_rows) {
Eigen::Map<math::RowMajorMatrix<F>> matrix(c::base::native_cast(values),
Expand All @@ -110,8 +110,8 @@ tachyon_sp1_baby_bear_poseidon2_two_adic_fri_pcs_coset_lde_batch(
return c::base::c_cast(ret.data());
}

void tachyon_sp1_baby_bear_poseidon2_two_adic_fri_pcs_commit(
tachyon_sp1_baby_bear_poseidon2_two_adic_fri_pcs* pcs,
void tachyon_sp1_baby_bear_poseidon2_two_adic_fri_commit(
tachyon_sp1_baby_bear_poseidon2_two_adic_fri* pcs,
tachyon_baby_bear* commitment,
tachyon_sp1_baby_bear_poseidon2_field_merkle_tree** prover_data) {
using Commitment = MMCS::Commitment;
Expand Down
Loading