Skip to content

Commit

Permalink
Generalize scalar multiplication to Grumpkin
Browse files Browse the repository at this point in the history
Copy and paste scalar_multiplication.

reduce_buckets_simple compiles, fails expectations

Paste-n-go starter code from old Lagrange work

Set up test.

Baby Grumpkin trancript io.

Fix pathing

Quick and dirt SRS (no checksum)

Add notes on transcript format.

Hide srs processor for now.

Progress: executable to generate SRS; test passes

Add error handling to script

Add todo

Progress: match endianness of BN txpt

Yay reduce_buckets_simple also passes

reduce_buckets also passes! try to docker

Add todo

All tests pass with size 2^20 SRS

Add disabled test.

Make bin runnable from build/

Bump SRS size to see green check

Start: move scalar muls up a level

Templatize: everything builds and links

Add TODO(#473)'s

Prep to templatize test

Templatize; test next up.

Template test suite.

Update srs gen scripts.

Fix WASM build (?)

Cleanup
  • Loading branch information
codygunton committed May 26, 2023
1 parent 4d851dd commit df867e4
Show file tree
Hide file tree
Showing 55 changed files with 1,616 additions and 1,079 deletions.
2 changes: 0 additions & 2 deletions cpp/.clangd
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ Diagnostics:
- readability-function-cognitive-complexity
# It is often nicer to not be explicit
- google-explicit-constructor
CheckOptions:
- cppcoreguidelines-special-member-functions.AllowSoleDefaultDtor: True

--- # this divider is necessary
# Disable some checks for Google Test/Bench
Expand Down
1 change: 1 addition & 0 deletions cpp/dockerfiles/Dockerfile.x86_64-linux-clang-assert
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ WORKDIR /usr/src/barretenberg/cpp
COPY . .
# Build everything to ensure everything builds. All tests will be run from the result of this build.
RUN cmake --preset default -DCMAKE_BUILD_TYPE=RelWithAssert -DCI=ON && cmake --build --preset default
RUN cd build && ./bin/grumpkin_srs_gen 1048576 && cd ../

FROM alpine:3.17
RUN apk update && apk add curl openmp
Expand Down
1 change: 1 addition & 0 deletions cpp/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ add_subdirectory(barretenberg/join_split_example)
add_subdirectory(barretenberg/dsl)
add_subdirectory(barretenberg/serialize)
add_subdirectory(barretenberg/solidity_helpers)
add_subdirectory(barretenberg/grumpkin_srs_gen)

if(BENCHMARKS)
add_subdirectory(barretenberg/benchmark)
Expand Down
7 changes: 4 additions & 3 deletions cpp/src/barretenberg/benchmark/pippenger_bench/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include <chrono>
#include "barretenberg/common/assert.hpp"
#include <cstdlib>
#include "barretenberg/ecc/curves/bn254/scalar_multiplication/scalar_multiplication.hpp"
#include "barretenberg/ecc/curves/bn254/bn254.hpp"
#include "barretenberg/ecc/curves/scalar_multiplication/scalar_multiplication.hpp"
#include "barretenberg/srs/reference_string/file_reference_string.hpp"
#include "barretenberg/polynomials/polynomial_arithmetic.hpp"

Expand Down Expand Up @@ -63,9 +64,9 @@ const auto init = []() {

int pippenger()
{
scalar_multiplication::pippenger_runtime_state state(NUM_POINTS);
scalar_multiplication::pippenger_runtime_state<curve::BN254> state(NUM_POINTS);
std::chrono::steady_clock::time_point time_start = std::chrono::steady_clock::now();
g1::element result = scalar_multiplication::pippenger_unsafe(
g1::element result = scalar_multiplication::pippenger_unsafe<curve::BN254>(
&scalars[0], reference_string->get_monomial_points(), NUM_POINTS, state);
std::chrono::steady_clock::time_point time_end = std::chrono::steady_clock::now();
std::chrono::microseconds diff = std::chrono::duration_cast<std::chrono::microseconds>(time_end - time_start);
Expand Down
11 changes: 6 additions & 5 deletions cpp/src/barretenberg/dsl/acir_format/ecdsa_secp256k1.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <gtest/gtest.h>
#include <vector>

using curve = proof_system::plonk::stdlib::secp256k1<acir_format::Composer>;
using curve_ct = proof_system::plonk::stdlib::secp256k1<acir_format::Composer>;

size_t generate_ecdsa_constraint(acir_format::EcdsaSecp256k1Constraint& ecdsa_constraint,
std::vector<fr>& witness_values)
Expand All @@ -20,12 +20,13 @@ size_t generate_ecdsa_constraint(acir_format::EcdsaSecp256k1Constraint& ecdsa_co
std::copy(message_string.begin(), message_string.end(), std::back_inserter(message_buffer));
auto hashed_message = sha256::sha256(message_buffer);

crypto::ecdsa::key_pair<curve::fr, curve::g1> account;
account.private_key = curve::fr::random_element();
account.public_key = curve::g1::one * account.private_key;
crypto::ecdsa::key_pair<curve_ct::fr, curve_ct::g1> account;
account.private_key = curve_ct::fr::random_element();
account.public_key = curve_ct::g1::one * account.private_key;

crypto::ecdsa::signature signature =
crypto::ecdsa::construct_signature<Sha256Hasher, curve::fq, curve::fr, curve::g1>(message_string, account);
crypto::ecdsa::construct_signature<Sha256Hasher, curve_ct::fq, curve_ct::fr, curve_ct::g1>(message_string,
account);

uint256_t pub_x_value = account.public_key.x;
uint256_t pub_y_value = account.public_key.y;
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/barretenberg/dsl/acir_proofs/acir_proofs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ size_t init_verification_key(void* pippenger, uint8_t const* g2x, uint8_t const*
auto proving_key = std::make_shared<plonk::proving_key>(std::move(pk_data), crs);

auto crs_factory = std::make_unique<PippengerReferenceStringFactory>(
reinterpret_cast<scalar_multiplication::Pippenger*>(pippenger), g2x);
reinterpret_cast<scalar_multiplication::Pippenger<curve::BN254>*>(pippenger), g2x);
proving_key->reference_string = crs_factory->get_prover_crs(proving_key->circuit_size);

acir_format::Composer composer(proving_key, nullptr);
Expand Down Expand Up @@ -108,7 +108,7 @@ size_t new_proof(void* pippenger,
auto witness = from_buffer<std::vector<fr>>(witness_buf);

auto crs_factory = std::make_unique<PippengerReferenceStringFactory>(
reinterpret_cast<scalar_multiplication::Pippenger*>(pippenger), g2x);
reinterpret_cast<scalar_multiplication::Pippenger<curve::BN254>*>(pippenger), g2x);
proving_key->reference_string = crs_factory->get_prover_crs(proving_key->circuit_size);

acir_format::Composer composer(proving_key, nullptr);
Expand Down
19 changes: 19 additions & 0 deletions cpp/src/barretenberg/ecc/curves/bn254/bn254.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include "../bn254/fr.hpp"
#include "../bn254/fq.hpp"
#include "../bn254/fq2.hpp"
#include "../bn254/g1.hpp"
#include "../bn254/g2.hpp"

namespace curve {
class BN254 {
public:
using ScalarField = barretenberg::fr;
using BaseField = barretenberg::fq;
using Group = typename barretenberg::g1;
using Element = typename Group::element;
using AffineElement = typename Group::affine_element;
using G2AffineElement = typename barretenberg::g2::affine_element;
using G2BaseField = typename barretenberg::fq2;
};
} // namespace curve
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "scalar_multiplication.hpp"
#include "pippenger.hpp"
#include "../bn254.hpp"
#include "../../scalar_multiplication/scalar_multiplication.hpp"
#include "../../scalar_multiplication/pippenger.hpp"
#include "barretenberg/common/mem.hpp"

using namespace barretenberg;
Expand All @@ -21,19 +22,19 @@ WASM_EXPORT void bbfree(void* ptr)

WASM_EXPORT void* new_pippenger(uint8_t* points, size_t num_points)
{
auto ptr = new scalar_multiplication::Pippenger(points, num_points);
auto ptr = new scalar_multiplication::Pippenger<curve::BN254>(points, num_points);
return ptr;
}

WASM_EXPORT void delete_pippenger(void* pippenger)
{
delete reinterpret_cast<scalar_multiplication::Pippenger*>(pippenger);
delete reinterpret_cast<scalar_multiplication::Pippenger<curve::BN254>*>(pippenger);
}

WASM_EXPORT void pippenger_unsafe(void* pippenger_ptr, void* scalars_ptr, size_t from, size_t range, void* result_ptr)
{
scalar_multiplication::pippenger_runtime_state state(range);
auto pippenger = reinterpret_cast<scalar_multiplication::Pippenger*>(pippenger_ptr);
scalar_multiplication::pippenger_runtime_state<curve::BN254> state(range);
auto pippenger = reinterpret_cast<scalar_multiplication::Pippenger<curve::BN254>*>(pippenger_ptr);
auto scalars = reinterpret_cast<fr*>(scalars_ptr);
auto result = reinterpret_cast<g1::element*>(result_ptr);
*result = pippenger->pippenger_unsafe(scalars, from, range);
Expand Down

This file was deleted.

This file was deleted.

16 changes: 14 additions & 2 deletions cpp/src/barretenberg/ecc/curves/grumpkin/grumpkin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct GrumpkinG1Params {
static constexpr bool can_hash_to_curve = true;
static constexpr bool small_elements = true;
static constexpr bool has_a = false;
// have checked in grumpkin.test_b that b is Montgomery form of -17
// have checked in grumpkin.test_b that b is Montgomery form of -17
static constexpr barretenberg::fr b{
0xdd7056026000005a, 0x223fa97acb319311, 0xcc388229877910c0, 0x34394632b724eaa
};
Expand All @@ -31,4 +31,16 @@ struct GrumpkinG1Params {
typedef barretenberg::group<barretenberg::fr, barretenberg::fq, GrumpkinG1Params> g1;

g1::affine_element get_generator(const size_t generator_index);
} // namespace grumpkin

}; // namespace grumpkin

namespace curve {
class Grumpkin {
public:
using ScalarField = barretenberg::fq;
using BaseField = barretenberg::fr;
using Group = typename grumpkin::g1;
using Element = typename Group::element;
using AffineElement = typename Group::affine_element;
};
} // namespace curve
Loading

0 comments on commit df867e4

Please sign in to comment.