Skip to content

Commit

Permalink
feat(sp1): add rust binding for sp1 variant TwoAdicFriPCS initially
Browse files Browse the repository at this point in the history
  • Loading branch information
chokobole committed Aug 27, 2024
1 parent 25d2308 commit cd66023
Show file tree
Hide file tree
Showing 12 changed files with 571 additions and 134 deletions.
15 changes: 14 additions & 1 deletion Cargo.Bazel.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "f18b47b24d322559ba405f9cb8683e71d3b1c15dedbda6554ce1275594c7e08f",
"checksum": "493a31da6b3ac2a5605ba241c05d083f25487a1fdd2efcb09d582c8b3be8cba9",
"crates": {
"addchain 0.2.0": {
"name": "addchain",
Expand Down Expand Up @@ -18158,6 +18158,19 @@
],
"selects": {}
},
"deps_dev": {
"common": [
{
"id": "rand 0.8.5",
"target": "rand"
},
{
"id": "rand_chacha 0.3.1",
"target": "rand_chacha"
}
],
"selects": {}
},
"edition": "2021",
"version": "0.0.1"
},
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 26 additions & 1 deletion vendors/sp1/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ tachyon_rust_library(
deps = all_crate_deps(normal = True) + [
":baby_bear_poseidon2_cxx_bridge",
":baby_bear_poseidon2_duplex_challenger",
":baby_bear_poseidon2_prover_data",
":baby_bear_poseidon2_two_adic_fri_pcs",
"//tachyon/rs:tachyon_rs",
],
)
Expand Down Expand Up @@ -59,9 +61,14 @@ rust_cxx_bridge(

tachyon_cc_library(
name = "baby_bear_poseidon2_api_hdrs",
hdrs = ["include/baby_bear_poseidon2_duplex_challenger.h"],
hdrs = [
"include/baby_bear_poseidon2_duplex_challenger.h",
"include/baby_bear_poseidon2_prover_data.h",
"include/baby_bear_poseidon2_two_adic_fri_pcs.h",
],
deps = [
"//tachyon/c/zk/air/sp1:baby_bear_poseidon2_duplex_challenger",
"//tachyon/c/zk/air/sp1:baby_bear_poseidon2_two_adic_fri_pcs",
"@cxx.rs//:core",
],
)
Expand All @@ -74,3 +81,21 @@ tachyon_cc_library(
":baby_bear_poseidon2_cxx_bridge/include",
],
)

tachyon_cc_library(
name = "baby_bear_poseidon2_prover_data",
srcs = ["src/baby_bear_poseidon2_prover_data.cc"],
deps = [
":baby_bear_poseidon2_api_hdrs",
":baby_bear_poseidon2_cxx_bridge/include",
],
)

tachyon_cc_library(
name = "baby_bear_poseidon2_two_adic_fri_pcs",
srcs = ["src/baby_bear_poseidon2_two_adic_fri_pcs.cc"],
deps = [
":baby_bear_poseidon2_api_hdrs",
":baby_bear_poseidon2_cxx_bridge/include",
],
)
2 changes: 2 additions & 0 deletions vendors/sp1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ sp1-primitives = { git = "https://github.com/kroma-network/sp1.git", rev = "dd03
tachyon_rs = { path = "../../tachyon/rs" }

[dev-dependencies]
rand = "0.8.5"
rand_chacha = "0.3.1"

[features]
default = []
Expand Down
42 changes: 42 additions & 0 deletions vendors/sp1/include/baby_bear_poseidon2_prover_data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef VENDORS_SP1_INCLUDE_BABY_BEAR_POSEIDON2_PROVER_DATA_H_
#define VENDORS_SP1_INCLUDE_BABY_BEAR_POSEIDON2_PROVER_DATA_H_

#include <memory>

#include "rust/cxx.h"

#include "tachyon/c/math/finite_fields/baby_bear/baby_bear.h"
#include "tachyon/c/zk/air/sp1/baby_bear_poseidon2_constants.h"
#include "tachyon/c/zk/air/sp1/baby_bear_poseidon2_field_merkle_tree.h"

namespace tachyon::sp1_api::baby_bear_poseidon2 {

struct TachyonBabyBear;

class ProverData {
public:
ProverData() = default;
ProverData(tachyon_sp1_baby_bear_poseidon2_field_merkle_tree* tree,
bool owned_tree)
: tree_(tree), owned_tree_(owned_tree) {}
ProverData(const ProverData& other) = delete;
ProverData& operator=(const ProverData& other) = delete;
~ProverData();

tachyon_baby_bear* commitment() { return commitment_; }
tachyon_sp1_baby_bear_poseidon2_field_merkle_tree** tree_ptr() {
return &tree_;
}

void write_commit(rust::Slice<TachyonBabyBear> values) const;
std::unique_ptr<ProverData> clone() const;

private:
tachyon_baby_bear commitment_[TACHYON_PLONKY3_BABY_BEAR_POSEIDON2_CHUNK];
tachyon_sp1_baby_bear_poseidon2_field_merkle_tree* tree_ = nullptr;
bool owned_tree_ = false;
};

} // namespace tachyon::sp1_api::baby_bear_poseidon2

#endif // VENDORS_SP1_INCLUDE_BABY_BEAR_POSEIDON2_PROVER_DATA_H_
43 changes: 43 additions & 0 deletions vendors/sp1/include/baby_bear_poseidon2_two_adic_fri_pcs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef VENDORS_SP1_INCLUDE_BABY_BEAR_POSEIDON2_TWO_ADIC_FRI_PCS_H_
#define VENDORS_SP1_INCLUDE_BABY_BEAR_POSEIDON2_TWO_ADIC_FRI_PCS_H_

#include <stddef.h>
#include <stdint.h>

#include <memory>

#include "rust/cxx.h"

#include "tachyon/c/zk/air/sp1/baby_bear_poseidon2_two_adic_fri_pcs.h"

namespace tachyon::sp1_api::baby_bear_poseidon2 {

class CommitResult;
class ProverData;
struct TachyonBabyBear;

class TwoAdicFriPcs {
public:
TwoAdicFriPcs(size_t log_blowup, size_t num_queries,
size_t proof_of_work_bits);
TwoAdicFriPcs(const TwoAdicFriPcs& other) = delete;
TwoAdicFriPcs& operator=(const TwoAdicFriPcs& other) = delete;
~TwoAdicFriPcs();

void allocate_ldes(size_t size) const;
rust::Slice<TachyonBabyBear> coset_lde_batch(
rust::Slice<TachyonBabyBear> values, size_t cols,
const TachyonBabyBear& shift) const;
std::unique_ptr<ProverData> commit() const;

private:
tachyon_sp1_baby_bear_poseidon2_two_adic_fri_pcs* pcs_;
};

std::unique_ptr<TwoAdicFriPcs> new_two_adic_fri_pcs(size_t log_blowup,
size_t num_queries,
size_t proof_of_work_bits);

} // namespace tachyon::sp1_api::baby_bear_poseidon2

#endif // VENDORS_SP1_INCLUDE_BABY_BEAR_POSEIDON2_TWO_ADIC_FRI_PCS_H_
Loading

0 comments on commit cd66023

Please sign in to comment.