-
Notifications
You must be signed in to change notification settings - Fork 129
feat(ts): allow passing srs via env functions #260
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
41cb44b
feat(ts): switch to node-modules linker
ludamad0 396d843
feat(ts): add new env for SRS objects
ludamad0 0ba7134
feat(ts): test srs bindings
ludamad0 862d904
fix: proper uint8_t include
ludamad0 5fa921b
feat(ts): revert unneeded changes
ludamad0 4ecfcd4
feat(ts): revert unneeded changes
ludamad0 6c34c45
feat(ts): unify writeMemory arg order
ludamad0 f942a86
Update barretenberg_wasm.ts
ludamad d4da9ff
feat(ts): fix srs comments
ludamad0 6caa437
Update data_store.hpp
ludamad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #include <fstream> | ||
|
|
||
| #include "crs.hpp" | ||
|
|
||
| #include "barretenberg/srs/reference_string/file_reference_string.hpp" | ||
| #include "barretenberg/ecc/curves/bn254/scalar_multiplication/c_bind.hpp" | ||
|
|
||
| const int NUM_POINTS_IN_TRANSCRIPT = 5040001; | ||
|
|
||
|
|
||
| extern "C" { | ||
| /** | ||
| * @brief In WASM, loads the verifier reference string. | ||
| * Used in native code to quickly create an in-memory reference string. | ||
| */ | ||
| uint8_t* env_load_verifier_crs() { | ||
| std::ifstream transcript; | ||
| transcript.open("../srs_db/ignition/monomial/transcript00.dat", std::ifstream::binary); | ||
| // We need two g2 points, each 64 bytes. | ||
| size_t g2_points_size = 128; | ||
| std::vector<uint8_t> g2_points(g2_points_size); | ||
| transcript.seekg(28 + NUM_POINTS_IN_TRANSCRIPT * 64); | ||
| transcript.read((char*)g2_points.data(), (std::streamsize)g2_points_size); | ||
| transcript.close(); | ||
| auto* g2_points_copy = (uint8_t*)bbmalloc(g2_points_size); | ||
| memcpy(g2_points_copy, g2_points.data(), g2_points_size); | ||
| return g2_points_copy; | ||
| } | ||
|
|
||
| /** | ||
| * @brief In WASM, loads the prover reference string. | ||
| * Provided as a utility for c-binds to implement a ReferenceStringFactory. | ||
| * In native code, not intended to be used. | ||
| * @param num_points The number of points to load. | ||
| */ | ||
| uint8_t* env_load_prover_crs(size_t num_points) { | ||
| // Note: This implementation is only meant to be instructive. | ||
| // This should only be used in c-binds to implement the C++ abstractions. | ||
| std::ifstream transcript; | ||
| transcript.open("../srs_db/ignition/monomial/transcript00.dat", std::ifstream::binary); | ||
| // Each g1 point is 64 bytes. | ||
| size_t g1_points_size = (num_points) * 64; | ||
| std::vector<uint8_t> g1_points(g1_points_size); | ||
| transcript.seekg(28); | ||
| transcript.read((char*)g1_points.data(), (std::streamsize)g1_points_size); | ||
| transcript.close(); | ||
| auto* g1_points_copy = (uint8_t*)bbmalloc(g1_points_size); | ||
| memcpy(g1_points_copy, g1_points.data(), g1_points_size); | ||
| return g1_points_copy; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #include <stddef.h> | ||
| #include <stdint.h> | ||
|
|
||
| // To be provided by the environment. | ||
| // Outputs from a trusted setup "Common reference string" model: https://en.wikipedia.org/wiki/Common_reference_string_model | ||
| // For a WASM build, this is provided by the JavaScript environment. | ||
| // For a native build, this is provided in this module. | ||
|
|
||
| /** | ||
| * @brief In WASM, loads the verifier reference string. | ||
| * Used in native code to quickly create an in-memory reference string. | ||
| * @returns An array of two g2 points. | ||
| */ | ||
| extern "C" uint8_t* env_load_verifier_crs(); | ||
|
|
||
| /** | ||
| * @brief In WASM, loads the prover reference string. | ||
| * Provided as a utility for c-binds to implement a ReferenceStringFactory. | ||
| * In native code, not intended to be used. | ||
| * @param num_points The number of g1 points to load. | ||
| * @returns An array of g1 points. | ||
| */ | ||
| extern "C" uint8_t* env_load_prover_crs(size_t num_points); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| #include <stddef.h> | ||
|
|
||
| // To be provided by the environment. | ||
| // For barretenberg.wasm, this is provided by the JavaScript environment. | ||
| // For anything other than barretenberg.wasm, this is provided in this module. | ||
| // For a WASM build, this is provided by the JavaScript environment. | ||
| // For a native build, this is provided in this module. | ||
| extern "C" void* get_data(char const* key, size_t* length_out); | ||
| extern "C" void set_data(char const* key, void* addr, size_t length); | ||
| extern "C" void set_data(char const* key, void* addr, size_t length); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| // To be provided by the environment. | ||
| // For barretenberg.wasm, this is provided by the JavaScript environment. | ||
| // For anything other than barretenberg.wasm, this is provided in this module. | ||
| // For a WASM build, this is provided by the JavaScript environment. | ||
| // For a native build, this is provided in this module. | ||
| extern "C" void logstr(char const*); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
cpp/src/barretenberg/srs/reference_string/env_reference_string.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /** | ||
| * Create reference strings given an environment that implements env/crs.hpp. | ||
| * Usable in both native and WASM code, but particularly useful for accessing | ||
| * reference strings from a WASM context. | ||
| * In a native context, the implementations assume a hard-coded string path. | ||
| * For that reason, ideally this is only used in c-bind functions to maintain flexibility. | ||
| */ | ||
| #pragma once | ||
| #include "barretenberg/srs/reference_string/mem_reference_string.hpp" | ||
| #include "reference_string.hpp" | ||
|
|
||
| #include "barretenberg/ecc/curves/bn254/g1.hpp" | ||
| #include "barretenberg/ecc/curves/bn254/g2.hpp" | ||
| #include "barretenberg/ecc/curves/bn254/scalar_multiplication/pippenger.hpp" | ||
|
|
||
| #include "barretenberg/env/crs.hpp" | ||
|
|
||
| #include <utility> | ||
| #include <cstddef> | ||
| namespace bonk { | ||
|
|
||
| using namespace barretenberg; | ||
|
|
||
| class EnvReferenceString : public ProverReferenceString { | ||
| public: | ||
| EnvReferenceString(const size_t num_points) | ||
| : num_points(num_points) | ||
| , pippenger_(env_load_prover_crs(num_points), num_points) | ||
| {} | ||
|
|
||
| g1::affine_element* get_monomial_points() override { return pippenger_.get_point_table(); } | ||
|
|
||
| size_t get_monomial_size() const override { return num_points; } | ||
|
|
||
| private: | ||
| size_t num_points; | ||
| scalar_multiplication::Pippenger pippenger_; | ||
| }; | ||
|
|
||
| class EnvReferenceStringFactory : public ReferenceStringFactory { | ||
| public: | ||
| std::shared_ptr<ProverReferenceString> get_prover_crs(size_t degree) override | ||
| { | ||
| return std::make_shared<EnvReferenceString>(degree); | ||
| } | ||
|
|
||
| std::shared_ptr<VerifierReferenceString> get_verifier_crs() override | ||
| { | ||
| return std::make_shared<VerifierMemReferenceString>(env_load_verifier_crs()); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace bonk | ||
26 changes: 26 additions & 0 deletions
26
cpp/src/barretenberg/srs/reference_string/env_reference_string.test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #include "file_reference_string.hpp" | ||
| #include "env_reference_string.hpp" | ||
|
|
||
| #include "barretenberg/ecc/curves/bn254/pairing.hpp" | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <fstream> | ||
|
|
||
| TEST(reference_string, env_file_consistency) | ||
| { | ||
| auto env_crs = std::make_unique<bonk::EnvReferenceStringFactory>(); | ||
|
|
||
| auto file_crs = std::make_unique<bonk::FileReferenceStringFactory>("../srs_db/ignition"); | ||
| auto file_verifier = file_crs->get_verifier_crs(); | ||
|
|
||
| EXPECT_EQ(env_crs->get_verifier_crs()->get_g2x(), file_verifier->get_g2x()); | ||
| EXPECT_EQ(memcmp(env_crs->get_verifier_crs()->get_precomputed_g2_lines(), | ||
| file_verifier->get_precomputed_g2_lines(), | ||
| sizeof(barretenberg::pairing::miller_lines) * 2), | ||
| 0); | ||
| EXPECT_EQ( | ||
| env_crs->get_prover_crs(1)->get_monomial_points()[0], | ||
| file_crs->get_prover_crs(1)->get_monomial_points()[0] | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,5 @@ node_modules | |
| yarn-error.log | ||
| .yarn | ||
| dest | ||
| .pnp.cjs | ||
| .pnp.loader.mjs | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.