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

feat(ts): allow passing srs via env functions #260

Merged
merged 10 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
53 changes: 53 additions & 0 deletions cpp/src/barretenberg/env/crs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#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.
* @param circuit_size The size of the circuit.
ludamad marked this conversation as resolved.
Show resolved Hide resolved
*/
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 circuit_size The size of the circuit.
*/
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;
}

}
24 changes: 24 additions & 0 deletions cpp/src/barretenberg/env/crs.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#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.
* @param circuit_size The size of the circuit.
* @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*);
18 changes: 17 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,22 @@ 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.
* @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.
ludamad marked this conversation as resolved.
Show resolved Hide resolved
*/
#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