Skip to content

Commit

Permalink
standard honk on grumpkin (#1258)
Browse files Browse the repository at this point in the history
# Description

This PR provides an initial solution for having a complete grumpkin
flavor in standard honk and contains the following modifications:
* the `FileCrsFactory` now has functionality to produce CRSes for both
curves (with their key differences) and I addressed any dependent
changes in the codebase
* the PCSes are now curve agnostic, having the curve set by their
parameters and missing tests were added (shlponk\gemini on both grumpkin
and bn254 which surfaced a bug in shplonk; unit testing
gemini+shplonk+ipa as only the kzg-variant was tested)
* continued work on enabling field-agnostic gates from
(#557, i have to
link issues manually)
* to avoid divison by zero caused by inverting the root of unity in
`EvaluationDomain<grumpkin::fr>` we hardcode the roots of unity to 1
given Grumpkin does not have many roots of unity.

Opens #635 
#637
#636, 
#640
for subsequent work.

# Checklist:

- [ x] I have reviewed my diff in github, line by line.
- [ x] Every change is related to the PR description.
- [ x] I have
[linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue)
this pull request to the issue(s) that it resolves.
- [x ] There are no unexpected formatting changes, superfluous debug
logs, or commented-out code.
- [x ] The branch has been merged or rebased against the head of its
merge target.
- [ x] I'm happy for the PR to be merged at the reviewer's next
convenience.
  • Loading branch information
maramihali authored and AztecBot committed Jul 31, 2023
1 parent b22b8d5 commit f2eca7d
Show file tree
Hide file tree
Showing 97 changed files with 1,225 additions and 950 deletions.
7 changes: 7 additions & 0 deletions cpp/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ echo "#################################"
cmake --preset $PRESET -DCMAKE_BUILD_TYPE=RelWithAssert
cmake --build --preset $PRESET ${@/#/--target }

cd ./build
# The Grumpkin SRS is generated manually at the moment, only up to a large enough size for tests
# If tests require more points, the parameter can be increased here.
cmake --build . --parallel --target grumpkin_srs_gen
./bin/grumpkin_srs_gen 8192
echo "Generated Grumpkin SRS successfully"

# Install wasi-sdk.
./scripts/install-wasi-sdk.sh

Expand Down
1 change: 1 addition & 0 deletions cpp/scripts/run_tests
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ docker run --rm -t $IMAGE_URI /bin/sh -c "\
cd /usr/src/barretenberg/cpp/srs_db; \
./download_ignition.sh $NUM_TRANSCRIPTS; \
cd /usr/src/barretenberg/cpp/build; \
./bin/grumpkin_srs_gen 1048576; \
for BIN in $TESTS; do ./bin/\$BIN $@; done"
4 changes: 3 additions & 1 deletion cpp/src/barretenberg/dsl/acir_format/acir_format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ struct acir_format {
// A standard plonk arithmetic constraint, as defined in the poly_triple struct, consists of selector values
// for q_M,q_L,q_R,q_O,q_C and indices of three variables taking the role of left, right and output wire
// This could be a large vector so use slab allocator, we don't expect the blackbox implementations to be so large.
std::vector<poly_triple, ContainerSlabAllocator<poly_triple>> constraints;
std::vector<poly_triple_<curve::BN254::ScalarField>,
ContainerSlabAllocator<poly_triple_<curve::BN254::ScalarField>>>
constraints;
std::vector<BlockConstraint> block_constraints;

// For serialization, update with any new fields
Expand Down
12 changes: 7 additions & 5 deletions cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ void AcirComposer::create_circuit(acir_format::acir_format& constraint_system)
size_hint_ = circuit_subgroup_size_;
}

void AcirComposer::init_proving_key(std::shared_ptr<barretenberg::srs::factories::CrsFactory> const& crs_factory,
acir_format::acir_format& constraint_system)
void AcirComposer::init_proving_key(
std::shared_ptr<barretenberg::srs::factories::CrsFactory<curve::BN254>> const& crs_factory,
acir_format::acir_format& constraint_system)
{
vinfo("building circuit... ", size_hint_);
builder_ = acir_format::Builder(size_hint_);
Expand All @@ -52,7 +53,7 @@ void AcirComposer::init_proving_key(std::shared_ptr<barretenberg::srs::factories
}

std::vector<uint8_t> AcirComposer::create_proof(
std::shared_ptr<barretenberg::srs::factories::CrsFactory> const& crs_factory,
std::shared_ptr<barretenberg::srs::factories::CrsFactory<curve::BN254>> const& crs_factory,
acir_format::acir_format& constraint_system,
acir_format::WitnessVector& witness,
bool is_recursive)
Expand Down Expand Up @@ -107,8 +108,9 @@ std::shared_ptr<proof_system::plonk::verification_key> AcirComposer::init_verifi
return verification_key_;
}

void AcirComposer::load_verification_key(std::shared_ptr<barretenberg::srs::factories::CrsFactory> const& crs_factory,
proof_system::plonk::verification_key_data&& data)
void AcirComposer::load_verification_key(
std::shared_ptr<barretenberg::srs::factories::CrsFactory<curve::BN254>> const& crs_factory,
proof_system::plonk::verification_key_data&& data)
{
verification_key_ =
std::make_shared<proof_system::plonk::verification_key>(std::move(data), crs_factory->get_verifier_crs());
Expand Down
16 changes: 9 additions & 7 deletions cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ class AcirComposer {

void create_circuit(acir_format::acir_format& constraint_system);

void init_proving_key(std::shared_ptr<barretenberg::srs::factories::CrsFactory> const& crs_factory,
void init_proving_key(std::shared_ptr<barretenberg::srs::factories::CrsFactory<curve::BN254>> const& crs_factory,
acir_format::acir_format& constraint_system);

std::vector<uint8_t> create_proof(std::shared_ptr<barretenberg::srs::factories::CrsFactory> const& crs_factory,
acir_format::acir_format& constraint_system,
acir_format::WitnessVector& witness,
bool is_recursive);
std::vector<uint8_t> create_proof(
std::shared_ptr<barretenberg::srs::factories::CrsFactory<curve::BN254>> const& crs_factory,
acir_format::acir_format& constraint_system,
acir_format::WitnessVector& witness,
bool is_recursive);

void load_verification_key(std::shared_ptr<barretenberg::srs::factories::CrsFactory> const& crs_factory,
proof_system::plonk::verification_key_data&& data);
void load_verification_key(
std::shared_ptr<barretenberg::srs::factories::CrsFactory<curve::BN254>> const& crs_factory,
proof_system::plonk::verification_key_data&& data);

std::shared_ptr<proof_system::plonk::verification_key> init_verification_key();

Expand Down
2 changes: 1 addition & 1 deletion cpp/src/barretenberg/ecc/curves/grumpkin/grumpkin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct GrumpkinG1Params {
};
static constexpr barretenberg::fr a{ 0UL, 0UL, 0UL, 0UL };

// generator point = (x, y) = (1, sqrt(-15))
// generator point = (x, y) = (1, sqrt(-16)), sqrt(-16) = 4i
static constexpr barretenberg::fr one_x = barretenberg::fr::one();
static constexpr barretenberg::fr one_y{
0x11b2dff1448c41d8UL, 0x23d3446f21c77dc3UL, 0xaa7b8cf435dfafbbUL, 0x14b34cf69dc25d68UL
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/barretenberg/examples/simple/simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void build_circuit(Builder& builder)
}

BuilderComposerPtrs create_builder_and_composer(
std::shared_ptr<barretenberg::srs::factories::CrsFactory> const& crs_factory)
std::shared_ptr<barretenberg::srs::factories::CrsFactory<curve::BN254>> const& crs_factory)
{
// WARNING: Size hint is essential to perform 512k circuits!
auto builder = std::make_unique<Builder>(CIRCUIT_SIZE);
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/barretenberg/examples/simple/simple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct BuilderComposerPtrs {
};

BuilderComposerPtrs create_builder_and_composer(
std::shared_ptr<barretenberg::srs::factories::CrsFactory> const& crs_factory);
std::shared_ptr<barretenberg::srs::factories::CrsFactory<curve::BN254>> const& crs_factory);

proof create_proof(BuilderComposerPtrs pair);

Expand Down
2 changes: 1 addition & 1 deletion cpp/src/barretenberg/examples/simple/simple.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace examples::simple {
TEST(examples_simple, create_proof)
{
auto srs_path = std::filesystem::absolute("../srs_db/ignition");
auto crs_factory = std::make_shared<barretenberg::srs::factories::FileCrsFactory>(srs_path);
auto crs_factory = std::make_shared<barretenberg::srs::factories::FileCrsFactory<curve::BN254>>(srs_path);
auto ptrs = create_builder_and_composer(crs_factory);
auto proof = create_proof(ptrs);
bool valid = verify_proof(ptrs, proof);
Expand Down
4 changes: 3 additions & 1 deletion cpp/src/barretenberg/grumpkin_srs_gen/grumpkin_srs_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ const std::string protocol_name = "BARRETENBERG_GRUMPKIN_IPA_CRS";
/**
* @brief Generates a monomial basis Grumpkin SRS.
*
* @details We only provide functionality create a single transcript file.
* @details We only provide functionality to create a single transcript file.
* ! Note that, unlike the bn254 SRS, the first element of the Grumpkin SRS will not be equal to point one defined in
* grumpkin.hpp as this function finds Grumpkin points in an arbitrary order.
*
*/
int main(int argc, char** argv)
Expand Down
19 changes: 13 additions & 6 deletions cpp/src/barretenberg/honk/composer/standard_composer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ template <StandardFlavor Flavor> class StandardComposer_ {
std::shared_ptr<VerificationKey> verification_key;

// The crs_factory holds the path to the srs and exposes methods to extract the srs elements
std::shared_ptr<srs::factories::CrsFactory> crs_factory_;
std::shared_ptr<srs::factories::CrsFactory<typename Flavor::Curve>> crs_factory_;

// The commitment key is passed to the prover but also used herein to compute the verfication key commitments
std::shared_ptr<PCSCommitmentKey> commitment_key;
Expand All @@ -37,15 +37,22 @@ template <StandardFlavor Flavor> class StandardComposer_ {

bool computed_witness = false;
// TODO(Luke): use make_shared
// TODO(#637): design the crs factory better
StandardComposer_()
: StandardComposer_(
std::shared_ptr<srs::factories::CrsFactory>(new srs::factories::FileCrsFactory("../srs_db/ignition")))
{}
StandardComposer_(std::shared_ptr<srs::factories::CrsFactory> crs_factory)
{
if constexpr (IsGrumpkinFlavor<Flavor>) {
crs_factory_ = barretenberg::srs::get_grumpkin_crs_factory();

} else {
crs_factory_ = barretenberg::srs::get_crs_factory();
}
}

StandardComposer_(std::shared_ptr<srs::factories::CrsFactory<typename Flavor::Curve>> crs_factory)
: crs_factory_(std::move(crs_factory))
{}

StandardComposer_(std::unique_ptr<srs::factories::CrsFactory>&& crs_factory)
StandardComposer_(std::unique_ptr<srs::factories::CrsFactory<typename Flavor::Curve>>&& crs_factory)
: crs_factory_(std::move(crs_factory))
{}
StandardComposer_(std::shared_ptr<ProvingKey> p_key, std::shared_ptr<VerificationKey> v_key)
Expand Down
Loading

0 comments on commit f2eca7d

Please sign in to comment.