-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ts): allow passing srs via env functions (#260)
* feat(ts): switch to node-modules linker * feat(ts): add new env for SRS objects * feat(ts): test srs bindings * fix: proper uint8_t include * feat(ts): revert unneeded changes * feat(ts): revert unneeded changes * feat(ts): unify writeMemory arg order * Update barretenberg_wasm.ts * feat(ts): fix srs comments * Update data_store.hpp --------- Co-authored-by: Adam Domurad <adam@aztecprotocol.com>
- Loading branch information
Showing
73 changed files
with
4,026 additions
and
24,865 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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.