Skip to content
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
d137366
Initial work at replacing wasm hashing with pure TS.
charlielye Oct 28, 2023
26ac28a
Plumbing.
charlielye Oct 29, 2023
13569e6
wip
charlielye Oct 29, 2023
78c3933
Add abis tests.
charlielye Oct 30, 2023
e90614d
Merge branch 'cl/more_abis_tests' into cl/ts_use_new_pedersen
charlielye Oct 30, 2023
5e92b49
wip
charlielye Nov 1, 2023
017ad81
Merge remote-tracking branch 'origin/master' into cl/ts_use_new_pedersen
charlielye Nov 1, 2023
7a5b7dc
Wip
charlielye Nov 1, 2023
3357fb8
wip
charlielye Nov 1, 2023
42cad25
Merge remote-tracking branch 'origin/master' into cl/ts_use_new_pedersen
charlielye Nov 1, 2023
db10fbd
wip
charlielye Nov 1, 2023
a35beff
Fix
charlielye Nov 1, 2023
6861c58
We have a serious circular dependency problem.
charlielye Nov 2, 2023
39c1c67
one less circular.
charlielye Nov 2, 2023
9607656
fix
charlielye Nov 2, 2023
fdc1145
Remove awaits.
charlielye Nov 2, 2023
8b3289a
Try to colorise output.
charlielye Nov 2, 2023
92d3260
fix
charlielye Nov 2, 2023
55be313
remove test.
charlielye Nov 2, 2023
0f1e439
fix
charlielye Nov 2, 2023
6eb96b8
Merge remote-tracking branch 'origin/master' into cl/ts_use_new_pedersen
charlielye Nov 2, 2023
42c1d84
Cleanup
charlielye Nov 2, 2023
36d226e
Fixes.
charlielye Nov 2, 2023
880acb7
Fixes.
charlielye Nov 2, 2023
e83961a
timestamps on compose runs [ci rebuild end-to-end]
charlielye Nov 3, 2023
34b82fb
Update cond_spot_run_compose [ci rebuild end-to-end]
charlielye Nov 3, 2023
96fb209
Update cond_spot_run_compose [ci rebuild end-to-end]
charlielye Nov 3, 2023
ccdd2be
use printf in add_timestamps as I got paranoid about performance. [ci…
charlielye Nov 3, 2023
fe50556
Merge branch 'cl/ts_use_new_pedersen' of github.com:aztecprotocol/azt…
charlielye Nov 3, 2023
4bad573
[ci rebuild end-to-end]
charlielye Nov 3, 2023
d5ee8cc
pedersenCommit shouldn't have hashIndex.
charlielye Nov 3, 2023
6bbb4c0
fix
charlielye Nov 4, 2023
e061ce0
Merge branch 'master' into cl/ts_use_new_pedersen
charlielye Nov 6, 2023
14186a4
Merge branch 'master' into cl/ts_use_new_pedersen
charlielye Nov 6, 2023
1714d46
Fix field change oops. Remove c_bind_new for schnorr.
charlielye Nov 7, 2023
fdfb0ef
Merge branch 'master' into cl/ts_use_new_pedersen
charlielye Nov 7, 2023
d3d42bf
Formatting.
charlielye Nov 7, 2023
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
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,7 @@ workflows:
requires:
- circuits-wasm-linux-clang
- l1-contracts
- bb-js
<<: *defaults
- yarn-project:
requires:
Expand Down
1 change: 1 addition & 0 deletions barretenberg/cpp/src/barretenberg/barretenberg.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

// External Barretenberg C++ API
#include "common/bbmalloc.hpp"
#include "common/container.hpp"
#include "common/map.hpp"
#include "common/mem.hpp"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "./mem.hpp"
#include "./bbmalloc.hpp"
#include "./slab_allocator.hpp"
#include "./wasm_export.hpp"

WASM_EXPORT void* bbmalloc(size_t size)
{
Expand Down
7 changes: 7 additions & 0 deletions barretenberg/cpp/src/barretenberg/common/bbmalloc.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once
#include "./wasm_export.hpp"
#include <cstddef>

WASM_EXPORT void* bbmalloc(size_t size);

WASM_EXPORT void bbfree(void* ptr);
5 changes: 1 addition & 4 deletions barretenberg/cpp/src/barretenberg/common/mem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,4 @@ inline void aligned_free(void* mem)
// info("Total allocated space (uordblks): ", minfo.uordblks);
// info("Total free space (fordblks): ", minfo.fordblks);
// info("Top-most, releasable space (keepcost): ", minfo.keepcost);
// }

WASM_EXPORT void* bbmalloc(size_t size);
WASM_EXPORT void bbfree(void* ptr);
// }
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
#include "barretenberg/common/serialize.hpp"
#include "pedersen.hpp"

WASM_EXPORT void pedersen__commit(uint8_t const* inputs_buffer, uint8_t* output)
extern "C" {

using namespace barretenberg;

WASM_EXPORT void pedersen_commit(fr::vec_in_buf inputs_buffer, affine_element::out_buf output)
{
std::vector<grumpkin::fq> to_commit;
read(inputs_buffer, to_commit);
grumpkin::g1::affine_element pedersen_commitment = crypto::pedersen_commitment::commit_native(to_commit);

serialize::write(output, pedersen_commitment);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#pragma once
#include "barretenberg/common/mem.hpp"
#include "barretenberg/common/serialize.hpp"
#include "barretenberg/common/streams.hpp"
#include "barretenberg/common/timer.hpp"
#include "barretenberg/common/wasm_export.hpp"
#include "barretenberg/ecc/curves/bn254/fr.hpp"
#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp"

WASM_EXPORT void pedersen__commit(uint8_t const* inputs_buffer, uint8_t* output);
extern "C" {

using namespace barretenberg;
using affine_element = grumpkin::g1::affine_element;

WASM_EXPORT void pedersen_commit(fr::vec_in_buf inputs_buffer, affine_element::out_buf output);
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "pedersen.hpp"
#include "barretenberg/common/timer.hpp"
#include "barretenberg/crypto/generators/generator_data.hpp"
#include <gtest/gtest.h>

Expand All @@ -16,4 +17,38 @@ TEST(Pedersen, Commitment)
EXPECT_EQ(r, expected);
}

} // namespace crypto
TEST(Pedersen, CommitmentWithZero)
{
auto x = pedersen_commitment::Fq::zero();
auto y = pedersen_commitment::Fq::one();
auto r = pedersen_commitment::commit_native({ x, y });
auto expected =
grumpkin::g1::affine_element(fr(uint256_t("054aa86a73cb8a34525e5bbed6e43ba1198e860f5f3950268f71df4591bde402")),
fr(uint256_t("209dcfbf2cfb57f9f6046f44d71ac6faf87254afc7407c04eb621a6287cac126")));
EXPECT_EQ(r, expected);
}

TEST(Pedersen, CommitmentProf)
{
GTEST_SKIP() << "Skipping mini profiler.";
auto x = fr::random_element();
auto y = fr::random_element();
Timer t;
for (int i = 0; i < 10000; ++i) {
pedersen_commitment::commit_native({ x, y });
}
info(t.nanoseconds() / 1000 / 10000);
}

// Useful for pasting into ts version of pedersen.
TEST(Pedersen, GeneratorPrinter)
{
GTEST_SKIP() << "Skipping generator-for-ts printer.";
pedersen_commitment::GeneratorContext ctx;
auto generators = ctx.generators->get_default_generators()->get(128);
for (auto g : generators) {
info("[", g.x, "n, ", g.y, "n],");
}
}

}; // namespace crypto
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

extern "C" {

WASM_EXPORT void pedersen__hash_with_hash_index(uint8_t const* inputs_buffer, uint32_t hash_index, uint8_t* output)
WASM_EXPORT void pedersen_hash(uint8_t const* inputs_buffer, uint32_t const* hash_index, uint8_t* output)
{
std::vector<grumpkin::fq> to_hash;
read(inputs_buffer, to_hash);
crypto::GeneratorContext<curve::Grumpkin> ctx;
ctx.offset = static_cast<size_t>(hash_index);
ctx.offset = static_cast<size_t>(ntohl(*hash_index));
auto r = crypto::pedersen_hash::hash(to_hash, ctx);
barretenberg::fr::serialize_to_buffer(r, output);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ extern "C" {

using namespace barretenberg;

WASM_EXPORT void pedersen_hash_with_hash_index(fr::vec_in_buf inputs_buffer,
uint32_t const* hash_index,
fr::out_buf output);
WASM_EXPORT void pedersen_hash(fr::vec_in_buf inputs_buffer, uint32_t const* hash_index, fr::out_buf output);
}

This file was deleted.

6 changes: 3 additions & 3 deletions barretenberg/cpp/src/barretenberg/crypto/schnorr/c_bind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ WASM_EXPORT void schnorr_construct_signature(uint8_t const* message, fr::in_buf
WASM_EXPORT void schnorr_verify_signature(
uint8_t const* message, affine_element::in_buf pub_key, in_buf32 sig_s, in_buf32 sig_e, bool* result);

WASM_EXPORT void schnorr_multisig_create_multisig_public_key(fq::in_buf private_key,
WASM_EXPORT void schnorr_multisig_create_multisig_public_key(fr::in_buf private_key,
multisig::MultiSigPublicKey::out_buf multisig_pubkey_buf);

WASM_EXPORT void schnorr_multisig_validate_and_combine_signer_pubkeys(
Expand All @@ -29,11 +29,11 @@ WASM_EXPORT void schnorr_multisig_construct_signature_round_1(

WASM_EXPORT void schnorr_multisig_construct_signature_round_2(
uint8_t const* message,
fq::in_buf private_key,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mind tl;dr'ing why we're switching field types?

@charlielye charlielye Nov 7, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was an oopsie. Fixed. It was actually another api call that was meant to become Fq.
Typescript didnt raise error due to Fq and Fr being structurally the same, although this PR changes that as I optimised Fr slightly (made it Uint8Array under the hood rather than bigint as the conversion seriously impacts performance when doing lots of hashing).

fr::in_buf private_key,
multisig::RoundOnePrivateOutput::in_buf signer_round_one_private_buf,
multisig::MultiSigPublicKey::vec_in_buf signer_pubkeys_buf,
multisig::RoundOnePublicOutput::vec_in_buf round_one_public_buf,
fq::out_buf round_two_buf,
fr::out_buf round_two_buf,
bool* success);

WASM_EXPORT void schnorr_multisig_combine_signatures(uint8_t const* message,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "barretenberg/crypto/pedersen_commitment/pedersen.hpp"
#include "barretenberg/common/test.hpp"
#include "barretenberg/crypto/pedersen_commitment/c_bind_new.hpp"
#include "barretenberg/crypto/pedersen_commitment/c_bind.hpp"
#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp"
#include "barretenberg/numeric/random/engine.hpp"
#include "barretenberg/stdlib/primitives/curves/bn254.hpp"
Expand Down
10 changes: 5 additions & 5 deletions barretenberg/exports.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"functionName": "pedersen___commit",
"functionName": "pedersen_commit",
"inArgs": [
{
"name": "inputs_buffer",
Expand All @@ -16,7 +16,7 @@
"isAsync": false
},
{
"functionName": "pedersen_hash_with_hash_index",
"functionName": "pedersen_hash",
"inArgs": [
{
"name": "inputs_buffer",
Expand Down Expand Up @@ -156,7 +156,7 @@
"inArgs": [
{
"name": "private_key",
"type": "fq::in_buf"
"type": "fr::in_buf"
}
],
"outArgs": [
Expand Down Expand Up @@ -211,7 +211,7 @@
},
{
"name": "private_key",
"type": "fq::in_buf"
"type": "fr::in_buf"
},
{
"name": "signer_round_one_private_buf",
Expand All @@ -229,7 +229,7 @@
"outArgs": [
{
"name": "round_two_buf",
"type": "fq::out_buf"
"type": "fr::out_buf"
},
{
"name": "success",
Expand Down
2 changes: 1 addition & 1 deletion barretenberg/scripts/c_bind_files.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
./cpp/src/barretenberg/crypto/pedersen_commitment/c_bind_new.hpp
./cpp/src/barretenberg/crypto/pedersen_commitment/c_bind.hpp
./cpp/src/barretenberg/crypto/pedersen_hash/c_bind.hpp
./cpp/src/barretenberg/crypto/blake2s/c_bind.hpp
./cpp/src/barretenberg/crypto/schnorr/c_bind.hpp
Expand Down
7 changes: 0 additions & 7 deletions barretenberg/scripts/decls_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ def process_files(files: List[str]) -> List[dict]:
idx = clang.cindex.Index.create()
for path in files:
tu = idx.parse(path, args=[
'-isystem', '/usr/include/c++/10',
'-isystem', '/usr/include/x86_64-linux-gnu/c++/10',
'-isystem', '/usr/include/c++/10/backward',
'-isystem', '/usr/lib/llvm-15/lib/clang/15.0.7/include',
'-isystem', '/usr/local/include',
'-isystem', '/usr/include/x86_64-linux-gnu',
'-isystem', '/usr/include',
"-I./cpp/src",
'-std=gnu++20', '-Wall', '-Wextra'])
for diag in tu.diagnostics:
Expand Down
File renamed without changes.
14 changes: 7 additions & 7 deletions barretenberg/ts/src/barretenberg_api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ export class BarretenbergApi {
}

async pedersenCommit(inputsBuffer: Fr[]): Promise<Point> {
const result = await this.binder.callWasmExport('pedersen___commit', [inputsBuffer], [Point]);
const result = await this.binder.callWasmExport('pedersen_commit', [inputsBuffer], [Point]);
return result[0];
}

async pedersenHashWithHashIndex(inputsBuffer: Fr[], hashIndex: number): Promise<Fr> {
const result = await this.binder.callWasmExport('pedersen_hash_with_hash_index', [inputsBuffer, hashIndex], [Fr]);
async pedersenHash(inputsBuffer: Fr[], hashIndex: number): Promise<Fr> {
const result = await this.binder.callWasmExport('pedersen_hash', [inputsBuffer, hashIndex], [Fr]);
return result[0];
}

Expand Down Expand Up @@ -65,7 +65,7 @@ export class BarretenbergApi {
return result[0];
}

async schnorrMultisigCreateMultisigPublicKey(privateKey: Fq): Promise<Buffer128> {
async schnorrMultisigCreateMultisigPublicKey(privateKey: Fr): Promise<Buffer128> {
const result = await this.binder.callWasmExport(
'schnorr_multisig_create_multisig_public_key',
[privateKey],
Expand Down Expand Up @@ -94,15 +94,15 @@ export class BarretenbergApi {

async schnorrMultisigConstructSignatureRound2(
message: Uint8Array,
privateKey: Fq,
privateKey: Fr,
signerRoundOnePrivateBuf: Buffer128,
signerPubkeysBuf: Buffer128[],
roundOnePublicBuf: Buffer128[],
): Promise<[Fq, boolean]> {
): Promise<[Fr, boolean]> {
const result = await this.binder.callWasmExport(
'schnorr_multisig_construct_signature_round_2',
[message, privateKey, signerRoundOnePrivateBuf, signerPubkeysBuf, roundOnePublicBuf],
[Fq, BoolDeserializer()],
[Fr, BoolDeserializer()],
);
return result as any;
}
Expand Down
29 changes: 0 additions & 29 deletions barretenberg/ts/src/barretenberg_api/pedersen.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { killSelf } from '../helpers/index.js';

const debug = createDebug('bb.js:wasm');

/**
* Base implementation of BarretenbergWasm.
* Contains code that is common to the "main thread" implementation and the "child thread" implementation.
*/
export class BarretenbergWasmBase {
protected memStore: { [key: string]: Uint8Array } = {};
protected memory!: WebAssembly.Memory;
Expand Down
Loading