Skip to content

Commit

Permalink
feat(ts): allow passing srs via env functions (#260)
Browse files Browse the repository at this point in the history
* 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
ludamad and ludamad0 authored Mar 23, 2023
1 parent b121963 commit ac78353
Show file tree
Hide file tree
Showing 73 changed files with 4,026 additions and 24,865 deletions.
52 changes: 52 additions & 0 deletions cpp/src/barretenberg/env/crs.cpp
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;
}

}
23 changes: 23 additions & 0 deletions cpp/src/barretenberg/env/crs.hpp
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);
6 changes: 3 additions & 3 deletions cpp/src/barretenberg/env/data_store.hpp
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);
4 changes: 2 additions & 2 deletions cpp/src/barretenberg/env/logstr.hpp
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*);
19 changes: 18 additions & 1 deletion cpp/src/barretenberg/plonk/proof_system/prover/c_bind.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "prover.hpp"
#include "barretenberg/env/data_store.hpp"
#include "barretenberg/env/crs.hpp"

#define WASM_EXPORT __attribute__((visibility("default")))

Expand Down Expand Up @@ -29,7 +30,23 @@ WASM_EXPORT void* test_async_func(size_t size, int val)
return addr;
}
}

/**
* @brief Simple wrapper for env_load_verifier_crs.
* @return The CRS.
*/
WASM_EXPORT void* test_env_load_verifier_crs()
{
return env_load_verifier_crs();
}
/**
* @brief Simple wrapper for env_load_verifier_crs.
* @param The number of points to load of the prover CRS.
* @return The CRS.
*/
WASM_EXPORT void* test_env_load_prover_crs(size_t num_points)
{
return env_load_prover_crs(num_points);
}
typedef std::conditional_t<plonk::SYSTEM_COMPOSER == plonk::TURBO, plonk::TurboProver, plonk::UltraProver> WasmProver;

WASM_EXPORT void prover_process_queue(WasmProver* prover)
Expand Down
53 changes: 53 additions & 0 deletions cpp/src/barretenberg/srs/reference_string/env_reference_string.hpp
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
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]
);
}
2 changes: 2 additions & 0 deletions ts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ node_modules
yarn-error.log
.yarn
dest
.pnp.cjs
.pnp.loader.mjs
Loading

0 comments on commit ac78353

Please sign in to comment.