From 5035024ca66830c904ebb25bedb5ff177ced2d7c Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 12:31:05 +0000 Subject: [PATCH 01/43] temporary: add methods to split the proof and take in public inputs separately --- .../dsl/acir_proofs/acir_composer.cpp | 58 +++++++++++++++++++ .../dsl/acir_proofs/acir_composer.hpp | 9 +++ 2 files changed, 67 insertions(+) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp index 29463a06db7..2845fcb63ef 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp @@ -100,6 +100,56 @@ std::vector AcirComposer::create_proof( return proof; } +/** + * @brief Splits a vector into two vectors, + * the first containing the first 32 * k elements, and the second containing + * the rest. + * + * @param original - The original vector to split + * @param k - The number of 32 bytes to remove + * @return std::pair, std::vector> + */ +std::pair, std::vector> splitVector(std::vector& original, uint32_t k) +{ + uint32_t elementsToRemove = 32 * k; + + if (original.size() < elementsToRemove) { + throw_or_abort("Not enough elements in the original vector"); + } + auto elementsToRemoveLong = static_cast(elementsToRemove); + std::vector removed(original.begin(), original.begin() + elementsToRemoveLong); + original = std::vector(original.begin() + elementsToRemoveLong, original.end()); + + return { original, removed }; +} + +std::vector concatenateVectors(const std::vector& firstVector, + const std::vector& secondVector) +{ + std::vector concatenatedVector; + + concatenatedVector.reserve(firstVector.size() + secondVector.size()); + + concatenatedVector.insert(concatenatedVector.end(), firstVector.begin(), firstVector.end()); + concatenatedVector.insert(concatenatedVector.end(), secondVector.begin(), secondVector.end()); + + return concatenatedVector; +} + +// This splits the proof and public inputs into two vectors. +std::pair, std::vector> AcirComposer::create_proof_public_splitted( + std::shared_ptr> const& crs_factory, + acir_format::acir_format& constraint_system, + acir_format::WitnessVector& witness, + bool is_recursive) +{ + auto proof = create_proof(crs_factory, constraint_system, witness, is_recursive); + auto num_public_inputs = static_cast(constraint_system.public_inputs.size()); + + auto [proof_without_public_inputs, public_inputs] = splitVector(proof, num_public_inputs); + return { proof_without_public_inputs, public_inputs }; +} + std::shared_ptr AcirComposer::init_verification_key() { vinfo("computing verification key..."); @@ -137,6 +187,14 @@ bool AcirComposer::verify_proof(std::vector const& proof, bool is_recur } } +bool AcirComposer::verify_proof_splitted(std::vector const& public_inputs, + std::vector const& proof, + bool is_recursive) +{ + auto proof_with_public_inputs = concatenateVectors(public_inputs, proof); + return verify_proof(proof_with_public_inputs, is_recursive); +} + std::string AcirComposer::get_solidity_verifier() { std::ostringstream stream; diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp index 25814e78d91..c6ad634a199 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp @@ -23,6 +23,12 @@ class AcirComposer { acir_format::WitnessVector& witness, bool is_recursive); + std::pair, std::vector> create_proof_public_splitted( + std::shared_ptr> const& crs_factory, + acir_format::acir_format& constraint_system, + acir_format::WitnessVector& witness, + bool is_recursive); + void load_verification_key( std::shared_ptr> const& crs_factory, proof_system::plonk::verification_key_data&& data); @@ -30,6 +36,9 @@ class AcirComposer { std::shared_ptr init_verification_key(); bool verify_proof(std::vector const& proof, bool is_recursive); + bool verify_proof_splitted(std::vector const& public_inputs, + std::vector const& proof, + bool is_recursive); std::string get_solidity_verifier(); size_t get_exact_circuit_size() { return exact_circuit_size_; }; From 2b438db268b9b02d372f8bac4aad522c2b70a6f0 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 12:31:25 +0000 Subject: [PATCH 02/43] modify c-binds to use those methods --- .../src/barretenberg/dsl/acir_proofs/c_bind.cpp | 16 ++++++++++------ .../src/barretenberg/dsl/acir_proofs/c_bind.hpp | 8 +++++--- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp index 1af145e2978..cba03f0c82d 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp @@ -42,15 +42,17 @@ WASM_EXPORT void acir_create_proof(in_ptr acir_composer_ptr, uint8_t const* acir_vec, uint8_t const* witness_vec, bool const* is_recursive, - uint8_t** out) + uint8_t** out_public_inputs, + uint8_t** out_proof_without_public_inputs) { auto acir_composer = reinterpret_cast(*acir_composer_ptr); auto constraint_system = acir_format::circuit_buf_to_acir_format(from_buffer>(acir_vec)); auto witness = acir_format::witness_buf_to_witness_data(from_buffer>(witness_vec)); - auto proof_data = - acir_composer->create_proof(barretenberg::srs::get_crs_factory(), constraint_system, witness, *is_recursive); - *out = to_heap_buffer(proof_data); + auto [public_inputs, proof_without_public_inputs] = acir_composer->create_proof_public_splitted( + barretenberg::srs::get_crs_factory(), constraint_system, witness, *is_recursive); + *out_public_inputs = to_heap_buffer(public_inputs); + *out_proof_without_public_inputs = to_heap_buffer(proof_without_public_inputs); } WASM_EXPORT void acir_load_verification_key(in_ptr acir_composer_ptr, uint8_t const* vk_buf) @@ -75,13 +77,15 @@ WASM_EXPORT void acir_get_verification_key(in_ptr acir_composer_ptr, uint8_t** o } WASM_EXPORT void acir_verify_proof(in_ptr acir_composer_ptr, + uint8_t const* public_inputs_buf, uint8_t const* proof_buf, bool const* is_recursive, bool* result) { auto acir_composer = reinterpret_cast(*acir_composer_ptr); - auto proof = from_buffer>(proof_buf); - *result = acir_composer->verify_proof(proof, *is_recursive); + auto public_inputs = from_buffer>(public_inputs_buf); + auto proof_without_public_inputs = from_buffer>(proof_buf); + *result = acir_composer->verify_proof_splitted(public_inputs, proof_without_public_inputs, *is_recursive); } WASM_EXPORT void acir_get_solidity_verifier(in_ptr acir_composer_ptr, out_str_buf out) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.hpp index e17af7a260d..6392994f68f 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.hpp @@ -27,10 +27,11 @@ WASM_EXPORT void acir_init_proving_key(in_ptr acir_composer_ptr, uint8_t const* * to pass it in everytime. */ WASM_EXPORT void acir_create_proof(in_ptr acir_composer_ptr, - uint8_t const* constraint_system_buf, - uint8_t const* witness_buf, + uint8_t const* acir_vec, + uint8_t const* witness_vec, bool const* is_recursive, - uint8_t** out); + uint8_t** out_public_inputs, + uint8_t** out_proof_without_public_inputs); WASM_EXPORT void acir_load_verification_key(in_ptr acir_composer_ptr, uint8_t const* vk_buf); @@ -39,6 +40,7 @@ WASM_EXPORT void acir_init_verification_key(in_ptr acir_composer_ptr); WASM_EXPORT void acir_get_verification_key(in_ptr acir_composer_ptr, uint8_t** out); WASM_EXPORT void acir_verify_proof(in_ptr acir_composer_ptr, + uint8_t const* public_inputs_buf, uint8_t const* proof_buf, bool const* is_recursive, bool* result); From 934a5782c7fb4e514a02daf4d34160982579ef67 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 12:33:23 +0000 Subject: [PATCH 03/43] modify browser test app to account for separate public inputs --- barretenberg/acir_tests/browser-test-app/src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/barretenberg/acir_tests/browser-test-app/src/index.ts b/barretenberg/acir_tests/browser-test-app/src/index.ts index 46cc925c31c..cc67d3305a6 100644 --- a/barretenberg/acir_tests/browser-test-app/src/index.ts +++ b/barretenberg/acir_tests/browser-test-app/src/index.ts @@ -27,14 +27,14 @@ async function runTest( ); const acirComposer = await api.acirNewAcirComposer(CIRCUIT_SIZE); - const proof = await api.acirCreateProof( + const [publicInputs, proofWithOutPublicInputs] = await api.acirCreateProof( acirComposer, bytecode, witness, true ); debug(`verifying...`); - const verified = await api.acirVerifyProof(acirComposer, proof, true); + const verified = await api.acirVerifyProof(acirComposer, publicInputs, proofWithOutPublicInputs, true); debug(`verified: ${verified}`); await api.destroy(); From 75b9dc719c1660313a5e5163a57a787a98b97fdc Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 12:33:44 +0000 Subject: [PATCH 04/43] modify bb binary to account for separate public inputs --- barretenberg/cpp/src/barretenberg/bb/main.cpp | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/bb/main.cpp b/barretenberg/cpp/src/barretenberg/bb/main.cpp index 81c4a2bfb49..ebbab50368b 100644 --- a/barretenberg/cpp/src/barretenberg/bb/main.cpp +++ b/barretenberg/cpp/src/barretenberg/bb/main.cpp @@ -78,19 +78,22 @@ bool proveAndVerify(const std::string& bytecodePath, const std::string& witnessP void prove(const std::string& bytecodePath, const std::string& witnessPath, bool recursive, - const std::string& outputPath) + const std::string& outputProofPath) { auto acir_composer = new acir_proofs::AcirComposer(MAX_CIRCUIT_SIZE, verbose); auto constraint_system = get_constraint_system(bytecodePath); auto witness = get_witness(witnessPath); - auto proof = acir_composer->create_proof(srs::get_crs_factory(), constraint_system, witness, recursive); + auto [proof_without_public_inputs, public_inputs] = + acir_composer->create_proof_public_splitted(srs::get_crs_factory(), constraint_system, witness, recursive); - if (outputPath == "-") { - writeRawBytesToStdout(proof); - vinfo("proof written to stdout"); + if (outputProofPath == "-") { + writeRawBytesToStdout(proof_without_public_inputs); + writeRawBytesToStdout(public_inputs); + vinfo("proof and public inputs written to stdout"); } else { - write_file(outputPath, proof); - vinfo("proof written to: ", outputPath); + write_file(outputProofPath, proof_without_public_inputs); + write_file(outputProofPath + "-public_inputs", public_inputs); + vinfo("proof and public inputs written to: ", outputProofPath); } } @@ -134,7 +137,10 @@ bool verify(const std::string& proof_path, bool recursive, const std::string& vk auto acir_composer = new acir_proofs::AcirComposer(MAX_CIRCUIT_SIZE, verbose); auto vk_data = from_buffer(read_file(vk_path)); acir_composer->load_verification_key(barretenberg::srs::get_crs_factory(), std::move(vk_data)); - auto verified = acir_composer->verify_proof(read_file(proof_path), recursive); + + auto public_inputs_path = proof_path + "-public_inputs"; + auto verified = + acir_composer->verify_proof_splitted(read_file(public_inputs_path), read_file(proof_path), recursive); vinfo("verified: ", verified); From 65acca838ad33b7890cc8daa3c4cc7ffacc2d16d Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 12:34:05 +0000 Subject: [PATCH 05/43] modify bb.js node binary to account for separate public inputs --- barretenberg/ts/src/main.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/barretenberg/ts/src/main.ts b/barretenberg/ts/src/main.ts index 91cefe16406..2e956147b50 100755 --- a/barretenberg/ts/src/main.ts +++ b/barretenberg/ts/src/main.ts @@ -85,10 +85,10 @@ export async function proveAndVerify(bytecodePath: string, witnessPath: string, debug(`creating proof...`); const bytecode = getBytecode(bytecodePath); const witness = getWitness(witnessPath); - const proof = await api.acirCreateProof(acirComposer, bytecode, witness, isRecursive); + const [publicInputs, proofWithOutPublicInputs] = await api.acirCreateProof(acirComposer, bytecode, witness, isRecursive); debug(`verifying...`); - const verified = await api.acirVerifyProof(acirComposer, proof, isRecursive); + const verified = await api.acirVerifyProof(acirComposer, publicInputs, proofWithOutPublicInputs,isRecursive); debug(`verified: ${verified}`); return verified; } finally { @@ -108,14 +108,16 @@ export async function prove( debug(`creating proof...`); const bytecode = getBytecode(bytecodePath); const witness = getWitness(witnessPath); - const proof = await api.acirCreateProof(acirComposer, bytecode, witness, isRecursive); + const [publicInputs, proofWithOutPublicInputs] = await api.acirCreateProof(acirComposer, bytecode, witness, isRecursive); debug(`done.`); if (outputPath === '-') { - process.stdout.write(proof); + process.stdout.write(publicInputs); + process.stdout.write(proofWithOutPublicInputs); debug(`proof written to stdout`); } else { - writeFileSync(outputPath, proof); + writeFileSync(outputPath+"-public_inputs", proofWithOutPublicInputs); + writeFileSync(outputPath, proofWithOutPublicInputs); debug(`proof written to: ${outputPath}`); } } finally { @@ -147,7 +149,7 @@ export async function verify(proofPath: string, isRecursive: boolean, vkPath: st const { api, acirComposer } = await initLite(); try { await api.acirLoadVerificationKey(acirComposer, new RawBuffer(readFileSync(vkPath))); - const verified = await api.acirVerifyProof(acirComposer, readFileSync(proofPath), isRecursive); + const verified = await api.acirVerifyProof(acirComposer, readFileSync(proofPath+"-public_inputs"),readFileSync(proofPath), isRecursive); debug(`verified: ${verified}`); return verified; } finally { From 902520a4ec48e590822f8c5a8428cac84e26c221 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 12:34:26 +0000 Subject: [PATCH 06/43] exports.json file was regenerated --- barretenberg/exports.json | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/barretenberg/exports.json b/barretenberg/exports.json index c786bb2cabd..10cb90e2b5a 100644 --- a/barretenberg/exports.json +++ b/barretenberg/exports.json @@ -532,18 +532,6 @@ ], "isAsync": false }, - { - "functionName": "test_thread_abort", - "inArgs": [], - "outArgs": [], - "isAsync": false - }, - { - "functionName": "test_abort", - "inArgs": [], - "outArgs": [], - "isAsync": false - }, { "functionName": "common_init_slab_allocator", "inArgs": [ @@ -648,11 +636,11 @@ "type": "in_ptr" }, { - "name": "constraint_system_buf", + "name": "acir_vec", "type": "const uint8_t *" }, { - "name": "witness_buf", + "name": "witness_vec", "type": "const uint8_t *" }, { @@ -662,7 +650,11 @@ ], "outArgs": [ { - "name": "out", + "name": "out_public_inputs", + "type": "uint8_t **" + }, + { + "name": "out_proof_without_public_inputs", "type": "uint8_t **" } ], @@ -717,6 +709,10 @@ "name": "acir_composer_ptr", "type": "in_ptr" }, + { + "name": "public_inputs_buf", + "type": "const uint8_t *" + }, { "name": "proof_buf", "type": "const uint8_t *" From 8dadcfabf8422416794745103507c5ef9d1dbdf7 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 12:35:16 +0000 Subject: [PATCH 07/43] regenerate api file -- unfortunately my linter changed lines in the file too --- barretenberg/ts/src/barretenberg_api/index.ts | 170 +++--------------- 1 file changed, 27 insertions(+), 143 deletions(-) diff --git a/barretenberg/ts/src/barretenberg_api/index.ts b/barretenberg/ts/src/barretenberg_api/index.ts index 2f377aba9b1..eedf4b041e7 100644 --- a/barretenberg/ts/src/barretenberg_api/index.ts +++ b/barretenberg/ts/src/barretenberg_api/index.ts @@ -1,13 +1,7 @@ // WARNING: FILE CODE GENERATED BY BINDGEN UTILITY. DO NOT EDIT! /* eslint-disable @typescript-eslint/no-unused-vars */ import { BarretenbergBinder } from '../barretenberg_binder/index.js'; -import { - BufferDeserializer, - NumberDeserializer, - VectorDeserializer, - BoolDeserializer, - StringDeserializer, -} from '../serialize/index.js'; +import { BufferDeserializer, NumberDeserializer, VectorDeserializer, BoolDeserializer, StringDeserializer } from '../serialize/index.js'; import { Fr, Fq, Point, Buffer32, Buffer128, Ptr } from '../types/index.js'; export class BarretenbergApi { @@ -43,11 +37,7 @@ export class BarretenbergApi { } async pedersenCompressWithHashIndex(inputsBuffer: Fr[], hashIndex: number): Promise { - const result = await this.binder.callWasmExport( - 'pedersen___compress_with_hash_index', - [inputsBuffer, hashIndex], - [Fr], - ); + const result = await this.binder.callWasmExport('pedersen___compress_with_hash_index', [inputsBuffer, hashIndex], [Fr]); return result[0]; } @@ -62,11 +52,7 @@ export class BarretenbergApi { } async pedersenPlookupCommitWithHashIndex(inputsBuffer: Fr[], hashIndex: number): Promise { - const result = await this.binder.callWasmExport( - 'pedersen___plookup_commit_with_hash_index', - [inputsBuffer, hashIndex], - [Fr], - ); + const result = await this.binder.callWasmExport('pedersen___plookup_commit_with_hash_index', [inputsBuffer, hashIndex], [Fr]); return result[0]; } @@ -91,11 +77,7 @@ export class BarretenbergApi { } async pedersenHashMultipleWithHashIndex(inputsBuffer: Fr[], hashIndex: number): Promise { - const result = await this.binder.callWasmExport( - 'pedersen_hash_multiple_with_hash_index', - [inputsBuffer, hashIndex], - [Fr], - ); + const result = await this.binder.callWasmExport('pedersen_hash_multiple_with_hash_index', [inputsBuffer, hashIndex], [Fr]); return result[0]; } @@ -125,76 +107,37 @@ export class BarretenbergApi { } async schnorrConstructSignature(message: Uint8Array, privateKey: Fr): Promise<[Buffer32, Buffer32]> { - const result = await this.binder.callWasmExport( - 'schnorr_construct_signature', - [message, privateKey], - [Buffer32, Buffer32], - ); + const result = await this.binder.callWasmExport('schnorr_construct_signature', [message, privateKey], [Buffer32, Buffer32]); return result as any; } async schnorrVerifySignature(message: Uint8Array, pubKey: Point, sigS: Buffer32, sigE: Buffer32): Promise { - const result = await this.binder.callWasmExport( - 'schnorr_verify_signature', - [message, pubKey, sigS, sigE], - [BoolDeserializer()], - ); + const result = await this.binder.callWasmExport('schnorr_verify_signature', [message, pubKey, sigS, sigE], [BoolDeserializer()]); return result[0]; } async schnorrMultisigCreateMultisigPublicKey(privateKey: Fq): Promise { - const result = await this.binder.callWasmExport( - 'schnorr_multisig_create_multisig_public_key', - [privateKey], - [Buffer128], - ); + const result = await this.binder.callWasmExport('schnorr_multisig_create_multisig_public_key', [privateKey], [Buffer128]); return result[0]; } async schnorrMultisigValidateAndCombineSignerPubkeys(signerPubkeyBuf: Buffer128[]): Promise<[Point, boolean]> { - const result = await this.binder.callWasmExport( - 'schnorr_multisig_validate_and_combine_signer_pubkeys', - [signerPubkeyBuf], - [Point, BoolDeserializer()], - ); + const result = await this.binder.callWasmExport('schnorr_multisig_validate_and_combine_signer_pubkeys', [signerPubkeyBuf], [Point, BoolDeserializer()]); return result as any; } async schnorrMultisigConstructSignatureRound1(): Promise<[Buffer128, Buffer128]> { - const result = await this.binder.callWasmExport( - 'schnorr_multisig_construct_signature_round_1', - [], - [Buffer128, Buffer128], - ); + const result = await this.binder.callWasmExport('schnorr_multisig_construct_signature_round_1', [], [Buffer128, Buffer128]); return result as any; } - async schnorrMultisigConstructSignatureRound2( - message: Uint8Array, - privateKey: Fq, - signerRoundOnePrivateBuf: Buffer128, - signerPubkeysBuf: Buffer128[], - roundOnePublicBuf: Buffer128[], - ): Promise<[Fq, boolean]> { - const result = await this.binder.callWasmExport( - 'schnorr_multisig_construct_signature_round_2', - [message, privateKey, signerRoundOnePrivateBuf, signerPubkeysBuf, roundOnePublicBuf], - [Fq, BoolDeserializer()], - ); + async schnorrMultisigConstructSignatureRound2(message: Uint8Array, privateKey: Fq, signerRoundOnePrivateBuf: Buffer128, signerPubkeysBuf: Buffer128[], roundOnePublicBuf: Buffer128[]): Promise<[Fq, boolean]> { + const result = await this.binder.callWasmExport('schnorr_multisig_construct_signature_round_2', [message, privateKey, signerRoundOnePrivateBuf, signerPubkeysBuf, roundOnePublicBuf], [Fq, BoolDeserializer()]); return result as any; } - async schnorrMultisigCombineSignatures( - message: Uint8Array, - signerPubkeysBuf: Buffer128[], - roundOneBuf: Buffer128[], - roundTwoBuf: Fr[], - ): Promise<[Buffer32, Buffer32, boolean]> { - const result = await this.binder.callWasmExport( - 'schnorr_multisig_combine_signatures', - [message, signerPubkeysBuf, roundOneBuf, roundTwoBuf], - [Buffer32, Buffer32, BoolDeserializer()], - ); + async schnorrMultisigCombineSignatures(message: Uint8Array, signerPubkeysBuf: Buffer128[], roundOneBuf: Buffer128[], roundTwoBuf: Fr[]): Promise<[Buffer32, Buffer32, boolean]> { + const result = await this.binder.callWasmExport('schnorr_multisig_combine_signatures', [message, signerPubkeysBuf, roundOneBuf, roundTwoBuf], [Buffer32, Buffer32, BoolDeserializer()]); return result as any; } @@ -204,11 +147,7 @@ export class BarretenbergApi { } async examplesSimpleCreateAndVerifyProof(): Promise { - const result = await this.binder.callWasmExport( - 'examples_simple_create_and_verify_proof', - [], - [BoolDeserializer()], - ); + const result = await this.binder.callWasmExport('examples_simple_create_and_verify_proof', [], [BoolDeserializer()]); return result[0]; } @@ -217,27 +156,13 @@ export class BarretenbergApi { return result[0]; } - async testThreadAbort(): Promise { - const result = await this.binder.callWasmExport('test_thread_abort', [], []); - return; - } - - async testAbort(): Promise { - const result = await this.binder.callWasmExport('test_abort', [], []); - return; - } - async commonInitSlabAllocator(circuitSize: number): Promise { const result = await this.binder.callWasmExport('common_init_slab_allocator', [circuitSize], []); return; } async acirGetCircuitSizes(constraintSystemBuf: Uint8Array): Promise<[number, number, number]> { - const result = await this.binder.callWasmExport( - 'acir_get_circuit_sizes', - [constraintSystemBuf], - [NumberDeserializer(), NumberDeserializer(), NumberDeserializer()], - ); + const result = await this.binder.callWasmExport('acir_get_circuit_sizes', [constraintSystemBuf], [NumberDeserializer(), NumberDeserializer(), NumberDeserializer()]); return result as any; } @@ -252,35 +177,18 @@ export class BarretenbergApi { } async acirCreateCircuit(acirComposerPtr: Ptr, constraintSystemBuf: Uint8Array, sizeHint: number): Promise { - const result = await this.binder.callWasmExport( - 'acir_create_circuit', - [acirComposerPtr, constraintSystemBuf, sizeHint], - [], - ); + const result = await this.binder.callWasmExport('acir_create_circuit', [acirComposerPtr, constraintSystemBuf, sizeHint], []); return; } async acirInitProvingKey(acirComposerPtr: Ptr, constraintSystemBuf: Uint8Array): Promise { - const result = await this.binder.callWasmExport( - 'acir_init_proving_key', - [acirComposerPtr, constraintSystemBuf], - [], - ); + const result = await this.binder.callWasmExport('acir_init_proving_key', [acirComposerPtr, constraintSystemBuf], []); return; } - async acirCreateProof( - acirComposerPtr: Ptr, - constraintSystemBuf: Uint8Array, - witnessBuf: Uint8Array, - isRecursive: boolean, - ): Promise { - const result = await this.binder.callWasmExport( - 'acir_create_proof', - [acirComposerPtr, constraintSystemBuf, witnessBuf, isRecursive], - [BufferDeserializer()], - ); - return result[0]; + async acirCreateProof(acirComposerPtr: Ptr, acirVec: Uint8Array, witnessVec: Uint8Array, isRecursive: boolean): Promise<[Uint8Array, Uint8Array]> { + const result = await this.binder.callWasmExport('acir_create_proof', [acirComposerPtr, acirVec, witnessVec, isRecursive], [BufferDeserializer(), BufferDeserializer()]); + return result as any; } async acirLoadVerificationKey(acirComposerPtr: Ptr, vkBuf: Uint8Array): Promise { @@ -294,51 +202,27 @@ export class BarretenbergApi { } async acirGetVerificationKey(acirComposerPtr: Ptr): Promise { - const result = await this.binder.callWasmExport( - 'acir_get_verification_key', - [acirComposerPtr], - [BufferDeserializer()], - ); + const result = await this.binder.callWasmExport('acir_get_verification_key', [acirComposerPtr], [BufferDeserializer()]); return result[0]; } - async acirVerifyProof(acirComposerPtr: Ptr, proofBuf: Uint8Array, isRecursive: boolean): Promise { - const result = await this.binder.callWasmExport( - 'acir_verify_proof', - [acirComposerPtr, proofBuf, isRecursive], - [BoolDeserializer()], - ); + async acirVerifyProof(acirComposerPtr: Ptr, publicInputsBuf: Uint8Array, proofBuf: Uint8Array, isRecursive: boolean): Promise { + const result = await this.binder.callWasmExport('acir_verify_proof', [acirComposerPtr, publicInputsBuf, proofBuf, isRecursive], [BoolDeserializer()]); return result[0]; } async acirGetSolidityVerifier(acirComposerPtr: Ptr): Promise { - const result = await this.binder.callWasmExport( - 'acir_get_solidity_verifier', - [acirComposerPtr], - [StringDeserializer()], - ); + const result = await this.binder.callWasmExport('acir_get_solidity_verifier', [acirComposerPtr], [StringDeserializer()]); return result[0]; } - async acirSerializeProofIntoFields( - acirComposerPtr: Ptr, - proofBuf: Uint8Array, - numInnerPublicInputs: number, - ): Promise { - const result = await this.binder.callWasmExport( - 'acir_serialize_proof_into_fields', - [acirComposerPtr, proofBuf, numInnerPublicInputs], - [VectorDeserializer(Fr)], - ); + async acirSerializeProofIntoFields(acirComposerPtr: Ptr, proofBuf: Uint8Array, numInnerPublicInputs: number): Promise { + const result = await this.binder.callWasmExport('acir_serialize_proof_into_fields', [acirComposerPtr, proofBuf, numInnerPublicInputs], [VectorDeserializer(Fr)]); return result[0]; } async acirSerializeVerificationKeyIntoFields(acirComposerPtr: Ptr): Promise<[Fr[], Fr]> { - const result = await this.binder.callWasmExport( - 'acir_serialize_verification_key_into_fields', - [acirComposerPtr], - [VectorDeserializer(Fr), Fr], - ); + const result = await this.binder.callWasmExport('acir_serialize_verification_key_into_fields', [acirComposerPtr], [VectorDeserializer(Fr), Fr]); return result as any; } } From 6376452ce461c1457bbf0ee3a0958fcf584b713f Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 18:17:55 +0000 Subject: [PATCH 08/43] formatter --- barretenberg/ts/src/barretenberg_api/index.ts | 163 +++++++++++++++--- barretenberg/ts/src/main.ts | 25 ++- 2 files changed, 157 insertions(+), 31 deletions(-) diff --git a/barretenberg/ts/src/barretenberg_api/index.ts b/barretenberg/ts/src/barretenberg_api/index.ts index eedf4b041e7..0697151c866 100644 --- a/barretenberg/ts/src/barretenberg_api/index.ts +++ b/barretenberg/ts/src/barretenberg_api/index.ts @@ -1,7 +1,13 @@ // WARNING: FILE CODE GENERATED BY BINDGEN UTILITY. DO NOT EDIT! /* eslint-disable @typescript-eslint/no-unused-vars */ import { BarretenbergBinder } from '../barretenberg_binder/index.js'; -import { BufferDeserializer, NumberDeserializer, VectorDeserializer, BoolDeserializer, StringDeserializer } from '../serialize/index.js'; +import { + BufferDeserializer, + NumberDeserializer, + VectorDeserializer, + BoolDeserializer, + StringDeserializer, +} from '../serialize/index.js'; import { Fr, Fq, Point, Buffer32, Buffer128, Ptr } from '../types/index.js'; export class BarretenbergApi { @@ -37,7 +43,11 @@ export class BarretenbergApi { } async pedersenCompressWithHashIndex(inputsBuffer: Fr[], hashIndex: number): Promise { - const result = await this.binder.callWasmExport('pedersen___compress_with_hash_index', [inputsBuffer, hashIndex], [Fr]); + const result = await this.binder.callWasmExport( + 'pedersen___compress_with_hash_index', + [inputsBuffer, hashIndex], + [Fr], + ); return result[0]; } @@ -52,7 +62,11 @@ export class BarretenbergApi { } async pedersenPlookupCommitWithHashIndex(inputsBuffer: Fr[], hashIndex: number): Promise { - const result = await this.binder.callWasmExport('pedersen___plookup_commit_with_hash_index', [inputsBuffer, hashIndex], [Fr]); + const result = await this.binder.callWasmExport( + 'pedersen___plookup_commit_with_hash_index', + [inputsBuffer, hashIndex], + [Fr], + ); return result[0]; } @@ -77,7 +91,11 @@ export class BarretenbergApi { } async pedersenHashMultipleWithHashIndex(inputsBuffer: Fr[], hashIndex: number): Promise { - const result = await this.binder.callWasmExport('pedersen_hash_multiple_with_hash_index', [inputsBuffer, hashIndex], [Fr]); + const result = await this.binder.callWasmExport( + 'pedersen_hash_multiple_with_hash_index', + [inputsBuffer, hashIndex], + [Fr], + ); return result[0]; } @@ -107,37 +125,76 @@ export class BarretenbergApi { } async schnorrConstructSignature(message: Uint8Array, privateKey: Fr): Promise<[Buffer32, Buffer32]> { - const result = await this.binder.callWasmExport('schnorr_construct_signature', [message, privateKey], [Buffer32, Buffer32]); + const result = await this.binder.callWasmExport( + 'schnorr_construct_signature', + [message, privateKey], + [Buffer32, Buffer32], + ); return result as any; } async schnorrVerifySignature(message: Uint8Array, pubKey: Point, sigS: Buffer32, sigE: Buffer32): Promise { - const result = await this.binder.callWasmExport('schnorr_verify_signature', [message, pubKey, sigS, sigE], [BoolDeserializer()]); + const result = await this.binder.callWasmExport( + 'schnorr_verify_signature', + [message, pubKey, sigS, sigE], + [BoolDeserializer()], + ); return result[0]; } async schnorrMultisigCreateMultisigPublicKey(privateKey: Fq): Promise { - const result = await this.binder.callWasmExport('schnorr_multisig_create_multisig_public_key', [privateKey], [Buffer128]); + const result = await this.binder.callWasmExport( + 'schnorr_multisig_create_multisig_public_key', + [privateKey], + [Buffer128], + ); return result[0]; } async schnorrMultisigValidateAndCombineSignerPubkeys(signerPubkeyBuf: Buffer128[]): Promise<[Point, boolean]> { - const result = await this.binder.callWasmExport('schnorr_multisig_validate_and_combine_signer_pubkeys', [signerPubkeyBuf], [Point, BoolDeserializer()]); + const result = await this.binder.callWasmExport( + 'schnorr_multisig_validate_and_combine_signer_pubkeys', + [signerPubkeyBuf], + [Point, BoolDeserializer()], + ); return result as any; } async schnorrMultisigConstructSignatureRound1(): Promise<[Buffer128, Buffer128]> { - const result = await this.binder.callWasmExport('schnorr_multisig_construct_signature_round_1', [], [Buffer128, Buffer128]); + const result = await this.binder.callWasmExport( + 'schnorr_multisig_construct_signature_round_1', + [], + [Buffer128, Buffer128], + ); return result as any; } - async schnorrMultisigConstructSignatureRound2(message: Uint8Array, privateKey: Fq, signerRoundOnePrivateBuf: Buffer128, signerPubkeysBuf: Buffer128[], roundOnePublicBuf: Buffer128[]): Promise<[Fq, boolean]> { - const result = await this.binder.callWasmExport('schnorr_multisig_construct_signature_round_2', [message, privateKey, signerRoundOnePrivateBuf, signerPubkeysBuf, roundOnePublicBuf], [Fq, BoolDeserializer()]); + async schnorrMultisigConstructSignatureRound2( + message: Uint8Array, + privateKey: Fq, + signerRoundOnePrivateBuf: Buffer128, + signerPubkeysBuf: Buffer128[], + roundOnePublicBuf: Buffer128[], + ): Promise<[Fq, boolean]> { + const result = await this.binder.callWasmExport( + 'schnorr_multisig_construct_signature_round_2', + [message, privateKey, signerRoundOnePrivateBuf, signerPubkeysBuf, roundOnePublicBuf], + [Fq, BoolDeserializer()], + ); return result as any; } - async schnorrMultisigCombineSignatures(message: Uint8Array, signerPubkeysBuf: Buffer128[], roundOneBuf: Buffer128[], roundTwoBuf: Fr[]): Promise<[Buffer32, Buffer32, boolean]> { - const result = await this.binder.callWasmExport('schnorr_multisig_combine_signatures', [message, signerPubkeysBuf, roundOneBuf, roundTwoBuf], [Buffer32, Buffer32, BoolDeserializer()]); + async schnorrMultisigCombineSignatures( + message: Uint8Array, + signerPubkeysBuf: Buffer128[], + roundOneBuf: Buffer128[], + roundTwoBuf: Fr[], + ): Promise<[Buffer32, Buffer32, boolean]> { + const result = await this.binder.callWasmExport( + 'schnorr_multisig_combine_signatures', + [message, signerPubkeysBuf, roundOneBuf, roundTwoBuf], + [Buffer32, Buffer32, BoolDeserializer()], + ); return result as any; } @@ -147,7 +204,11 @@ export class BarretenbergApi { } async examplesSimpleCreateAndVerifyProof(): Promise { - const result = await this.binder.callWasmExport('examples_simple_create_and_verify_proof', [], [BoolDeserializer()]); + const result = await this.binder.callWasmExport( + 'examples_simple_create_and_verify_proof', + [], + [BoolDeserializer()], + ); return result[0]; } @@ -162,7 +223,11 @@ export class BarretenbergApi { } async acirGetCircuitSizes(constraintSystemBuf: Uint8Array): Promise<[number, number, number]> { - const result = await this.binder.callWasmExport('acir_get_circuit_sizes', [constraintSystemBuf], [NumberDeserializer(), NumberDeserializer(), NumberDeserializer()]); + const result = await this.binder.callWasmExport( + 'acir_get_circuit_sizes', + [constraintSystemBuf], + [NumberDeserializer(), NumberDeserializer(), NumberDeserializer()], + ); return result as any; } @@ -177,17 +242,34 @@ export class BarretenbergApi { } async acirCreateCircuit(acirComposerPtr: Ptr, constraintSystemBuf: Uint8Array, sizeHint: number): Promise { - const result = await this.binder.callWasmExport('acir_create_circuit', [acirComposerPtr, constraintSystemBuf, sizeHint], []); + const result = await this.binder.callWasmExport( + 'acir_create_circuit', + [acirComposerPtr, constraintSystemBuf, sizeHint], + [], + ); return; } async acirInitProvingKey(acirComposerPtr: Ptr, constraintSystemBuf: Uint8Array): Promise { - const result = await this.binder.callWasmExport('acir_init_proving_key', [acirComposerPtr, constraintSystemBuf], []); + const result = await this.binder.callWasmExport( + 'acir_init_proving_key', + [acirComposerPtr, constraintSystemBuf], + [], + ); return; } - async acirCreateProof(acirComposerPtr: Ptr, acirVec: Uint8Array, witnessVec: Uint8Array, isRecursive: boolean): Promise<[Uint8Array, Uint8Array]> { - const result = await this.binder.callWasmExport('acir_create_proof', [acirComposerPtr, acirVec, witnessVec, isRecursive], [BufferDeserializer(), BufferDeserializer()]); + async acirCreateProof( + acirComposerPtr: Ptr, + acirVec: Uint8Array, + witnessVec: Uint8Array, + isRecursive: boolean, + ): Promise<[Uint8Array, Uint8Array]> { + const result = await this.binder.callWasmExport( + 'acir_create_proof', + [acirComposerPtr, acirVec, witnessVec, isRecursive], + [BufferDeserializer(), BufferDeserializer()], + ); return result as any; } @@ -202,27 +284,56 @@ export class BarretenbergApi { } async acirGetVerificationKey(acirComposerPtr: Ptr): Promise { - const result = await this.binder.callWasmExport('acir_get_verification_key', [acirComposerPtr], [BufferDeserializer()]); + const result = await this.binder.callWasmExport( + 'acir_get_verification_key', + [acirComposerPtr], + [BufferDeserializer()], + ); return result[0]; } - async acirVerifyProof(acirComposerPtr: Ptr, publicInputsBuf: Uint8Array, proofBuf: Uint8Array, isRecursive: boolean): Promise { - const result = await this.binder.callWasmExport('acir_verify_proof', [acirComposerPtr, publicInputsBuf, proofBuf, isRecursive], [BoolDeserializer()]); + async acirVerifyProof( + acirComposerPtr: Ptr, + publicInputsBuf: Uint8Array, + proofBuf: Uint8Array, + isRecursive: boolean, + ): Promise { + const result = await this.binder.callWasmExport( + 'acir_verify_proof', + [acirComposerPtr, publicInputsBuf, proofBuf, isRecursive], + [BoolDeserializer()], + ); return result[0]; } async acirGetSolidityVerifier(acirComposerPtr: Ptr): Promise { - const result = await this.binder.callWasmExport('acir_get_solidity_verifier', [acirComposerPtr], [StringDeserializer()]); + const result = await this.binder.callWasmExport( + 'acir_get_solidity_verifier', + [acirComposerPtr], + [StringDeserializer()], + ); return result[0]; } - async acirSerializeProofIntoFields(acirComposerPtr: Ptr, proofBuf: Uint8Array, numInnerPublicInputs: number): Promise { - const result = await this.binder.callWasmExport('acir_serialize_proof_into_fields', [acirComposerPtr, proofBuf, numInnerPublicInputs], [VectorDeserializer(Fr)]); + async acirSerializeProofIntoFields( + acirComposerPtr: Ptr, + proofBuf: Uint8Array, + numInnerPublicInputs: number, + ): Promise { + const result = await this.binder.callWasmExport( + 'acir_serialize_proof_into_fields', + [acirComposerPtr, proofBuf, numInnerPublicInputs], + [VectorDeserializer(Fr)], + ); return result[0]; } async acirSerializeVerificationKeyIntoFields(acirComposerPtr: Ptr): Promise<[Fr[], Fr]> { - const result = await this.binder.callWasmExport('acir_serialize_verification_key_into_fields', [acirComposerPtr], [VectorDeserializer(Fr), Fr]); + const result = await this.binder.callWasmExport( + 'acir_serialize_verification_key_into_fields', + [acirComposerPtr], + [VectorDeserializer(Fr), Fr], + ); return result as any; } } diff --git a/barretenberg/ts/src/main.ts b/barretenberg/ts/src/main.ts index 2e956147b50..ab27b4e693a 100755 --- a/barretenberg/ts/src/main.ts +++ b/barretenberg/ts/src/main.ts @@ -85,10 +85,15 @@ export async function proveAndVerify(bytecodePath: string, witnessPath: string, debug(`creating proof...`); const bytecode = getBytecode(bytecodePath); const witness = getWitness(witnessPath); - const [publicInputs, proofWithOutPublicInputs] = await api.acirCreateProof(acirComposer, bytecode, witness, isRecursive); + const [publicInputs, proofWithOutPublicInputs] = await api.acirCreateProof( + acirComposer, + bytecode, + witness, + isRecursive, + ); debug(`verifying...`); - const verified = await api.acirVerifyProof(acirComposer, publicInputs, proofWithOutPublicInputs,isRecursive); + const verified = await api.acirVerifyProof(acirComposer, publicInputs, proofWithOutPublicInputs, isRecursive); debug(`verified: ${verified}`); return verified; } finally { @@ -108,7 +113,12 @@ export async function prove( debug(`creating proof...`); const bytecode = getBytecode(bytecodePath); const witness = getWitness(witnessPath); - const [publicInputs, proofWithOutPublicInputs] = await api.acirCreateProof(acirComposer, bytecode, witness, isRecursive); + const [publicInputs, proofWithOutPublicInputs] = await api.acirCreateProof( + acirComposer, + bytecode, + witness, + isRecursive, + ); debug(`done.`); if (outputPath === '-') { @@ -116,7 +126,7 @@ export async function prove( process.stdout.write(proofWithOutPublicInputs); debug(`proof written to stdout`); } else { - writeFileSync(outputPath+"-public_inputs", proofWithOutPublicInputs); + writeFileSync(outputPath + '-public_inputs', proofWithOutPublicInputs); writeFileSync(outputPath, proofWithOutPublicInputs); debug(`proof written to: ${outputPath}`); } @@ -149,7 +159,12 @@ export async function verify(proofPath: string, isRecursive: boolean, vkPath: st const { api, acirComposer } = await initLite(); try { await api.acirLoadVerificationKey(acirComposer, new RawBuffer(readFileSync(vkPath))); - const verified = await api.acirVerifyProof(acirComposer, readFileSync(proofPath+"-public_inputs"),readFileSync(proofPath), isRecursive); + const verified = await api.acirVerifyProof( + acirComposer, + readFileSync(proofPath + '-public_inputs'), + readFileSync(proofPath), + isRecursive, + ); debug(`verified: ${verified}`); return verified; } finally { From 06e2ad7f28d8bddaceff655f530c0a9cc9d11cb7 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 18:41:29 +0000 Subject: [PATCH 09/43] add code to not read the public inputs file if there are no public inputs --- barretenberg/cpp/src/barretenberg/bb/main.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/bb/main.cpp b/barretenberg/cpp/src/barretenberg/bb/main.cpp index ebbab50368b..be273a3b7dd 100644 --- a/barretenberg/cpp/src/barretenberg/bb/main.cpp +++ b/barretenberg/cpp/src/barretenberg/bb/main.cpp @@ -138,9 +138,15 @@ bool verify(const std::string& proof_path, bool recursive, const std::string& vk auto vk_data = from_buffer(read_file(vk_path)); acir_composer->load_verification_key(barretenberg::srs::get_crs_factory(), std::move(vk_data)); + // If the number of public inputs is 0, then read_file will trigger a failure + // because the file will be empty. auto public_inputs_path = proof_path + "-public_inputs"; - auto verified = - acir_composer->verify_proof_splitted(read_file(public_inputs_path), read_file(proof_path), recursive); + std::vector pub_inputs_file; + if (vk_data.num_public_inputs != 0) { + pub_inputs_file = read_file(public_inputs_path); + } + auto proof_path_file = read_file(proof_path); + auto verified = acir_composer->verify_proof_splitted(pub_inputs_file, proof_path_file, recursive); vinfo("verified: ", verified); From ab683a147198f311445805886b3a5557a826c9ab Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 20:55:37 +0000 Subject: [PATCH 10/43] temporarily switch args --- barretenberg/ts/src/main.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/barretenberg/ts/src/main.ts b/barretenberg/ts/src/main.ts index ab27b4e693a..b1a51ff2990 100755 --- a/barretenberg/ts/src/main.ts +++ b/barretenberg/ts/src/main.ts @@ -93,7 +93,7 @@ export async function proveAndVerify(bytecodePath: string, witnessPath: string, ); debug(`verifying...`); - const verified = await api.acirVerifyProof(acirComposer, publicInputs, proofWithOutPublicInputs, isRecursive); + const verified = await api.acirVerifyProof(acirComposer, proofWithOutPublicInputs, publicInputs, isRecursive); debug(`verified: ${verified}`); return verified; } finally { @@ -216,6 +216,7 @@ export async function proofAsFields(proofPath: string, vkPath: string, outputPat const { api, acirComposer } = await initLite(); try { + // This should get the public inputs too debug('serializing proof byte array into field elements'); const numPublicInputs = readFileSync(vkPath).readUint32BE(8); const proofAsFields = await api.acirSerializeProofIntoFields( From adc2357000259398a428a83a03af83ea0bb12a0d Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 21:19:41 +0000 Subject: [PATCH 11/43] put publicInputs in serializeProofIntoFields --- barretenberg/ts/src/barretenberg_api/index.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/barretenberg/ts/src/barretenberg_api/index.ts b/barretenberg/ts/src/barretenberg_api/index.ts index 0697151c866..167beb37cf4 100644 --- a/barretenberg/ts/src/barretenberg_api/index.ts +++ b/barretenberg/ts/src/barretenberg_api/index.ts @@ -295,12 +295,12 @@ export class BarretenbergApi { async acirVerifyProof( acirComposerPtr: Ptr, publicInputsBuf: Uint8Array, - proofBuf: Uint8Array, + proofWithoutPublicInputsBuf: Uint8Array, isRecursive: boolean, ): Promise { const result = await this.binder.callWasmExport( 'acir_verify_proof', - [acirComposerPtr, publicInputsBuf, proofBuf, isRecursive], + [acirComposerPtr, publicInputsBuf, proofWithoutPublicInputsBuf, isRecursive], [BoolDeserializer()], ); return result[0]; @@ -317,12 +317,13 @@ export class BarretenbergApi { async acirSerializeProofIntoFields( acirComposerPtr: Ptr, + publicInputsBuf: Uint8Array, proofBuf: Uint8Array, numInnerPublicInputs: number, ): Promise { const result = await this.binder.callWasmExport( 'acir_serialize_proof_into_fields', - [acirComposerPtr, proofBuf, numInnerPublicInputs], + [acirComposerPtr, publicInputsBuf, proofBuf, numInnerPublicInputs], [VectorDeserializer(Fr)], ); return result[0]; From 4a026aad2bc14143cc63e7c6d4a556b9f8fc515b Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 21:20:08 +0000 Subject: [PATCH 12/43] modify binary to account for new cbind api --- barretenberg/ts/src/main.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/barretenberg/ts/src/main.ts b/barretenberg/ts/src/main.ts index b1a51ff2990..2ba2349c892 100755 --- a/barretenberg/ts/src/main.ts +++ b/barretenberg/ts/src/main.ts @@ -93,7 +93,7 @@ export async function proveAndVerify(bytecodePath: string, witnessPath: string, ); debug(`verifying...`); - const verified = await api.acirVerifyProof(acirComposer, proofWithOutPublicInputs, publicInputs, isRecursive); + const verified = await api.acirVerifyProof(acirComposer, publicInputs, proofWithOutPublicInputs, isRecursive); debug(`verified: ${verified}`); return verified; } finally { @@ -216,11 +216,11 @@ export async function proofAsFields(proofPath: string, vkPath: string, outputPat const { api, acirComposer } = await initLite(); try { - // This should get the public inputs too debug('serializing proof byte array into field elements'); const numPublicInputs = readFileSync(vkPath).readUint32BE(8); const proofAsFields = await api.acirSerializeProofIntoFields( acirComposer, + readFileSync(proofPath + '-public_inputs'), readFileSync(proofPath), numPublicInputs, ); From f6af120671fe803ec419e598ca3b73b01fa70403 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 21:21:33 +0000 Subject: [PATCH 13/43] always put public inputs vector first --- barretenberg/cpp/src/barretenberg/bb/main.cpp | 9 +++++++-- .../barretenberg/dsl/acir_proofs/acir_composer.cpp | 9 ++++++--- .../barretenberg/dsl/acir_proofs/acir_composer.hpp | 3 ++- .../cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp | 11 +++++++---- .../cpp/src/barretenberg/dsl/acir_proofs/c_bind.hpp | 3 ++- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/bb/main.cpp b/barretenberg/cpp/src/barretenberg/bb/main.cpp index be273a3b7dd..20b0ae3a383 100644 --- a/barretenberg/cpp/src/barretenberg/bb/main.cpp +++ b/barretenberg/cpp/src/barretenberg/bb/main.cpp @@ -83,7 +83,7 @@ void prove(const std::string& bytecodePath, auto acir_composer = new acir_proofs::AcirComposer(MAX_CIRCUIT_SIZE, verbose); auto constraint_system = get_constraint_system(bytecodePath); auto witness = get_witness(witnessPath); - auto [proof_without_public_inputs, public_inputs] = + auto [public_inputs, proof_without_public_inputs] = acir_composer->create_proof_public_splitted(srs::get_crs_factory(), constraint_system, witness, recursive); if (outputProofPath == "-") { @@ -235,9 +235,14 @@ void contract(const std::string& output_path, const std::string& vk_path) */ void proofAsFields(const std::string& proof_path, std::string const& vk_path, const std::string& output_path) { + + auto public_inputs_file = proof_path + "-public_inputs"; + auto proof = read_file(proof_path); + auto public_inputs = read_file(public_inputs_file); + auto acir_composer = new acir_proofs::AcirComposer(MAX_CIRCUIT_SIZE, verbose); auto vk_data = from_buffer(read_file(vk_path)); - auto data = acir_composer->serialize_proof_into_fields(read_file(proof_path), vk_data.num_public_inputs); + auto data = acir_composer->serialize_proof_into_fields(public_inputs, proof, vk_data.num_public_inputs); auto json = format("[", join(map(data, [](auto fr) { return format("\"", fr, "\""); })), "]"); if (output_path == "-") { diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp index 2845fcb63ef..1573b7bc72e 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp @@ -147,7 +147,7 @@ std::pair, std::vector> AcirComposer::create_proof auto num_public_inputs = static_cast(constraint_system.public_inputs.size()); auto [proof_without_public_inputs, public_inputs] = splitVector(proof, num_public_inputs); - return { proof_without_public_inputs, public_inputs }; + return { public_inputs, proof_without_public_inputs }; } std::shared_ptr AcirComposer::init_verification_key() @@ -210,9 +210,12 @@ std::string AcirComposer::get_solidity_verifier() * @param proof * @param num_inner_public_inputs - number of public inputs on the proof being serialized */ -std::vector AcirComposer::serialize_proof_into_fields(std::vector const& proof, - size_t num_inner_public_inputs) +std::vector AcirComposer::serialize_proof_into_fields( + std::vector const& public_inputs, + std::vector const& proof_without_public_inputs, + size_t num_inner_public_inputs) // TODO: remove this, can be derived from public_inputs { + auto proof = concatenateVectors(public_inputs, proof_without_public_inputs); transcript::StandardTranscript transcript(proof, acir_format::Composer::create_manifest(num_inner_public_inputs), transcript::HashType::PlookupPedersenBlake3s, diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp index c6ad634a199..5789fb57aba 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp @@ -44,7 +44,8 @@ class AcirComposer { size_t get_exact_circuit_size() { return exact_circuit_size_; }; size_t get_total_circuit_size() { return total_circuit_size_; }; - std::vector serialize_proof_into_fields(std::vector const& proof, + std::vector serialize_proof_into_fields(std::vector const& public_inputs, + std::vector const& proof_without_public_inputs, size_t num_inner_public_inputs); std::vector serialize_verification_key_into_fields(); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp index cba03f0c82d..45a6eae60e7 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp @@ -78,13 +78,13 @@ WASM_EXPORT void acir_get_verification_key(in_ptr acir_composer_ptr, uint8_t** o WASM_EXPORT void acir_verify_proof(in_ptr acir_composer_ptr, uint8_t const* public_inputs_buf, - uint8_t const* proof_buf, + uint8_t const* proof_without_public_inputs_buf, bool const* is_recursive, bool* result) { auto acir_composer = reinterpret_cast(*acir_composer_ptr); auto public_inputs = from_buffer>(public_inputs_buf); - auto proof_without_public_inputs = from_buffer>(proof_buf); + auto proof_without_public_inputs = from_buffer>(proof_without_public_inputs_buf); *result = acir_composer->verify_proof_splitted(public_inputs, proof_without_public_inputs, *is_recursive); } @@ -96,13 +96,16 @@ WASM_EXPORT void acir_get_solidity_verifier(in_ptr acir_composer_ptr, out_str_bu } WASM_EXPORT void acir_serialize_proof_into_fields(in_ptr acir_composer_ptr, + uint8_t const* public_inputs_buf, uint8_t const* proof_buf, uint32_t const* num_inner_public_inputs, fr::vec_out_buf out) { auto acir_composer = reinterpret_cast(*acir_composer_ptr); - auto proof = from_buffer>(proof_buf); - auto proof_as_fields = acir_composer->serialize_proof_into_fields(proof, ntohl(*num_inner_public_inputs)); + auto public_inputs = from_buffer>(proof_buf); + auto proof = from_buffer>(public_inputs_buf); + auto proof_as_fields = + acir_composer->serialize_proof_into_fields(public_inputs, proof, ntohl(*num_inner_public_inputs)); *out = to_heap_buffer(proof_as_fields); } diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.hpp index 6392994f68f..e01476b9998 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.hpp @@ -41,13 +41,14 @@ WASM_EXPORT void acir_get_verification_key(in_ptr acir_composer_ptr, uint8_t** o WASM_EXPORT void acir_verify_proof(in_ptr acir_composer_ptr, uint8_t const* public_inputs_buf, - uint8_t const* proof_buf, + uint8_t const* proof_without_public_inputs_buf, bool const* is_recursive, bool* result); WASM_EXPORT void acir_get_solidity_verifier(in_ptr acir_composer_ptr, out_str_buf out); WASM_EXPORT void acir_serialize_proof_into_fields(in_ptr acir_composer_ptr, + uint8_t const* public_inputs_buf, uint8_t const* proof_buf, uint32_t const* num_inner_public_inputs, fr::vec_out_buf out); From 06ea909836c614981989c2abc7bd073dd110895c Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 21:21:52 +0000 Subject: [PATCH 14/43] regenerate exports.json --- barretenberg/exports.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/barretenberg/exports.json b/barretenberg/exports.json index 10cb90e2b5a..7954d069ef4 100644 --- a/barretenberg/exports.json +++ b/barretenberg/exports.json @@ -714,7 +714,7 @@ "type": "const uint8_t *" }, { - "name": "proof_buf", + "name": "proof_without_public_inputs_buf", "type": "const uint8_t *" }, { @@ -753,6 +753,10 @@ "name": "acir_composer_ptr", "type": "in_ptr" }, + { + "name": "public_inputs_buf", + "type": "const uint8_t *" + }, { "name": "proof_buf", "type": "const uint8_t *" From 93218e7a82551a2ffdc7be05e3acb1ab17ce9ef9 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 21:52:17 +0000 Subject: [PATCH 15/43] conditionally read the public inputs --- barretenberg/cpp/src/barretenberg/bb/main.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/bb/main.cpp b/barretenberg/cpp/src/barretenberg/bb/main.cpp index 20b0ae3a383..13b23e906d4 100644 --- a/barretenberg/cpp/src/barretenberg/bb/main.cpp +++ b/barretenberg/cpp/src/barretenberg/bb/main.cpp @@ -236,12 +236,16 @@ void contract(const std::string& output_path, const std::string& vk_path) void proofAsFields(const std::string& proof_path, std::string const& vk_path, const std::string& output_path) { - auto public_inputs_file = proof_path + "-public_inputs"; + auto vk_data = from_buffer(read_file(vk_path)); + + auto public_inputs_path = proof_path + "-public_inputs"; auto proof = read_file(proof_path); - auto public_inputs = read_file(public_inputs_file); + std::vector public_inputs; + if (vk_data.num_public_inputs != 0) { + public_inputs = read_file(public_inputs_path); + } auto acir_composer = new acir_proofs::AcirComposer(MAX_CIRCUIT_SIZE, verbose); - auto vk_data = from_buffer(read_file(vk_path)); auto data = acir_composer->serialize_proof_into_fields(public_inputs, proof, vk_data.num_public_inputs); auto json = format("[", join(map(data, [](auto fr) { return format("\"", fr, "\""); })), "]"); From 7aa38556b4ba9851734c212fc0efff5e4079904a Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 21:53:13 +0000 Subject: [PATCH 16/43] typo --- barretenberg/ts/src/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/barretenberg/ts/src/main.ts b/barretenberg/ts/src/main.ts index 2ba2349c892..5250fa53a00 100755 --- a/barretenberg/ts/src/main.ts +++ b/barretenberg/ts/src/main.ts @@ -126,7 +126,7 @@ export async function prove( process.stdout.write(proofWithOutPublicInputs); debug(`proof written to stdout`); } else { - writeFileSync(outputPath + '-public_inputs', proofWithOutPublicInputs); + writeFileSync(outputPath + '-public_inputs', publicInputs); writeFileSync(outputPath, proofWithOutPublicInputs); debug(`proof written to: ${outputPath}`); } From 0c6ca7f8f9e2a1804385576fb3c1bb21e14c5b23 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 21:53:41 +0000 Subject: [PATCH 17/43] yarn --- barretenberg/acir_tests/run_acir_tests_browser.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/barretenberg/acir_tests/run_acir_tests_browser.sh b/barretenberg/acir_tests/run_acir_tests_browser.sh index 22830656250..a1781c2de9b 100755 --- a/barretenberg/acir_tests/run_acir_tests_browser.sh +++ b/barretenberg/acir_tests/run_acir_tests_browser.sh @@ -16,6 +16,8 @@ THREAD_MODEL=${THREAD_MODEL:-mt} # TODO: Currently webkit doesn't seem to have shared memory so is a single threaded test regardless of THREAD_MODEL! echo "Testing thread model: $THREAD_MODEL" + +cd headless-test && yarn install && cd .. (cd browser-test-app && yarn serve:dest:$THREAD_MODEL) > /dev/null 2>&1 & sleep 1 VERBOSE=1 BIN=./headless-test/bb.js.browser ./run_acir_tests.sh $@ From 0df922cedba66b19c2f6a3fdbd80dcff141716aa Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 21:54:35 +0000 Subject: [PATCH 18/43] put in separate PR --- barretenberg/acir_tests/run_acir_tests_browser.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/barretenberg/acir_tests/run_acir_tests_browser.sh b/barretenberg/acir_tests/run_acir_tests_browser.sh index a1781c2de9b..b462d9a6122 100755 --- a/barretenberg/acir_tests/run_acir_tests_browser.sh +++ b/barretenberg/acir_tests/run_acir_tests_browser.sh @@ -17,7 +17,6 @@ THREAD_MODEL=${THREAD_MODEL:-mt} # TODO: Currently webkit doesn't seem to have shared memory so is a single threaded test regardless of THREAD_MODEL! echo "Testing thread model: $THREAD_MODEL" -cd headless-test && yarn install && cd .. (cd browser-test-app && yarn serve:dest:$THREAD_MODEL) > /dev/null 2>&1 & sleep 1 VERBOSE=1 BIN=./headless-test/bb.js.browser ./run_acir_tests.sh $@ From 0e3bfa7577fc92721f6c0b3b757e89b1c4abe91f Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 22:36:33 +0000 Subject: [PATCH 19/43] cleanup bb --- barretenberg/cpp/src/barretenberg/bb/main.cpp | 52 ++++++++++++------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/bb/main.cpp b/barretenberg/cpp/src/barretenberg/bb/main.cpp index 13b23e906d4..1d73bd59787 100644 --- a/barretenberg/cpp/src/barretenberg/bb/main.cpp +++ b/barretenberg/cpp/src/barretenberg/bb/main.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -38,6 +39,28 @@ acir_format::acir_format get_constraint_system(std::string const& bytecode_path) return acir_format::circuit_buf_to_acir_format(bytecode); } +std::vector read_public_inputs(std::string const& public_inputs_path, size_t num_public_inputs) +{ + // If the number of public inputs is 0, then read_file will trigger a failure + // because the file will be empty. + if (num_public_inputs == 0) { + return {}; + } + return read_file(public_inputs_path); +} + +// When given a proof path, we simply append "-public_inputs" to it. +// to derive a file path for the public inputs. +// +// The alternative is to have the user pass in a path for their proof +// and a path for their public inputs. This is more verbose and +// its likely that the user will want their public inputs to be +// in the same directory as their proof. +std::string public_inputs_path_from_proof_path(std::string const& proof_path) +{ + return proof_path + "-public_inputs"; +} + /** * @brief Proves and Verifies an ACIR circuit * @@ -87,13 +110,15 @@ void prove(const std::string& bytecodePath, acir_composer->create_proof_public_splitted(srs::get_crs_factory(), constraint_system, witness, recursive); if (outputProofPath == "-") { - writeRawBytesToStdout(proof_without_public_inputs); writeRawBytesToStdout(public_inputs); + writeRawBytesToStdout(proof_without_public_inputs); vinfo("proof and public inputs written to stdout"); } else { + auto outputPublicInputsPath = public_inputs_path_from_proof_path(outputProofPath); + write_file(outputPublicInputsPath, public_inputs); write_file(outputProofPath, proof_without_public_inputs); - write_file(outputProofPath + "-public_inputs", public_inputs); - vinfo("proof and public inputs written to: ", outputProofPath); + vinfo("proof written to: ", outputProofPath); + vinfo("public inputs written to: ", outputPublicInputsPath); } } @@ -138,15 +163,10 @@ bool verify(const std::string& proof_path, bool recursive, const std::string& vk auto vk_data = from_buffer(read_file(vk_path)); acir_composer->load_verification_key(barretenberg::srs::get_crs_factory(), std::move(vk_data)); - // If the number of public inputs is 0, then read_file will trigger a failure - // because the file will be empty. - auto public_inputs_path = proof_path + "-public_inputs"; - std::vector pub_inputs_file; - if (vk_data.num_public_inputs != 0) { - pub_inputs_file = read_file(public_inputs_path); - } - auto proof_path_file = read_file(proof_path); - auto verified = acir_composer->verify_proof_splitted(pub_inputs_file, proof_path_file, recursive); + auto public_inputs_path = public_inputs_path_from_proof_path(proof_path); + std::vector public_inputs = read_public_inputs(public_inputs_path, vk_data.num_public_inputs); + auto proof_without_public_inputs = read_file(proof_path); + auto verified = acir_composer->verify_proof_splitted(public_inputs, proof_without_public_inputs, recursive); vinfo("verified: ", verified); @@ -235,15 +255,11 @@ void contract(const std::string& output_path, const std::string& vk_path) */ void proofAsFields(const std::string& proof_path, std::string const& vk_path, const std::string& output_path) { - auto vk_data = from_buffer(read_file(vk_path)); - auto public_inputs_path = proof_path + "-public_inputs"; + auto public_inputs_path = public_inputs_path_from_proof_path(proof_path); auto proof = read_file(proof_path); - std::vector public_inputs; - if (vk_data.num_public_inputs != 0) { - public_inputs = read_file(public_inputs_path); - } + std::vector public_inputs = read_public_inputs(public_inputs_path, vk_data.num_public_inputs); auto acir_composer = new acir_proofs::AcirComposer(MAX_CIRCUIT_SIZE, verbose); auto data = acir_composer->serialize_proof_into_fields(public_inputs, proof, vk_data.num_public_inputs); From b463a8a6434627bdbc883d24f325c582026908fa Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 22:44:02 +0000 Subject: [PATCH 20/43] cleanup bb.js binary --- barretenberg/ts/src/main.ts | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/barretenberg/ts/src/main.ts b/barretenberg/ts/src/main.ts index 5250fa53a00..ecb51b2ac83 100755 --- a/barretenberg/ts/src/main.ts +++ b/barretenberg/ts/src/main.ts @@ -33,6 +33,10 @@ function getWitness(witnessPath: string) { return decompressed; } +function publicInputsPathFromProofPath(proofPath: string) { + return proofPath + '-public_inputs'; +} + async function computeCircuitSize(bytecodePath: string, api: Barretenberg) { debug(`computing circuit size...`); const bytecode = getBytecode(bytecodePath); @@ -106,7 +110,7 @@ export async function prove( witnessPath: string, crsPath: string, isRecursive: boolean, - outputPath: string, + outputProofPath: string, ) { const { api, acirComposer } = await init(bytecodePath, crsPath); try { @@ -121,14 +125,18 @@ export async function prove( ); debug(`done.`); - if (outputPath === '-') { + if (outputProofPath === '-') { process.stdout.write(publicInputs); process.stdout.write(proofWithOutPublicInputs); debug(`proof written to stdout`); } else { - writeFileSync(outputPath + '-public_inputs', publicInputs); - writeFileSync(outputPath, proofWithOutPublicInputs); - debug(`proof written to: ${outputPath}`); + const publicInputsPath = publicInputsPathFromProofPath(outputProofPath); + + writeFileSync(publicInputsPath, publicInputs); + writeFileSync(outputProofPath, proofWithOutPublicInputs); + + debug(`proof written to: ${outputProofPath}`); + debug(`public inputs written to: ${publicInputsPath}`); } } finally { await api.destroy(); @@ -159,9 +167,10 @@ export async function verify(proofPath: string, isRecursive: boolean, vkPath: st const { api, acirComposer } = await initLite(); try { await api.acirLoadVerificationKey(acirComposer, new RawBuffer(readFileSync(vkPath))); + const publicInputsPath = publicInputsPathFromProofPath(proofPath); const verified = await api.acirVerifyProof( acirComposer, - readFileSync(proofPath + '-public_inputs'), + readFileSync(publicInputsPath), readFileSync(proofPath), isRecursive, ); @@ -218,9 +227,10 @@ export async function proofAsFields(proofPath: string, vkPath: string, outputPat try { debug('serializing proof byte array into field elements'); const numPublicInputs = readFileSync(vkPath).readUint32BE(8); + const publicInputsPath = publicInputsPathFromProofPath(proofPath); const proofAsFields = await api.acirSerializeProofIntoFields( acirComposer, - readFileSync(proofPath + '-public_inputs'), + readFileSync(publicInputsPath), readFileSync(proofPath), numPublicInputs, ); From 23d206742ee215c4ce62463edddff619dcc0ab8e Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 23:01:52 +0000 Subject: [PATCH 21/43] remove _splitted methods --- .../dsl/acir_proofs/acir_composer.cpp | 121 +++++++++--------- .../dsl/acir_proofs/acir_composer.hpp | 13 +- .../barretenberg/dsl/acir_proofs/c_bind.cpp | 6 +- 3 files changed, 67 insertions(+), 73 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp index 1573b7bc72e..d31e3201a02 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp @@ -17,6 +17,60 @@ AcirComposer::AcirComposer(size_t size_hint, bool verbose) , verbose_(verbose) {} +/** + * @brief Splits a vector into two vectors, + * the first containing the first 32 * k elements, and the second containing + * the rest. + * + * @param original - The original vector to split + * @param k - The number of 32 bytes to remove + * @return std::pair, std::vector> + */ +std::pair, std::vector> splitVector(std::vector& original, uint32_t k) +{ + uint32_t elementsToRemove = 32 * k; + + if (original.size() < elementsToRemove) { + throw_or_abort("Not enough elements in the original vector"); + } + auto elementsToRemoveLong = static_cast(elementsToRemove); + std::vector removed(original.begin(), original.begin() + elementsToRemoveLong); + original = std::vector(original.begin() + elementsToRemoveLong, original.end()); + + return { original, removed }; +} + +/** + * @brief Splits the proof into two vectors. + * + * Barretenberg returns a proof that is concatenated with the public inputs. + * This function splits the proof into two vectors, one containing the public inputs, + * and the other containing the proof without the public inputs. + * + * @param proof - proof with the public inputs preprended + * @param num_public_inputs - the number of public inputs prepended to the proof + * @return std::pair, std::vector> + */ +std::pair, std::vector> split_proof(std::vector& proof, + uint32_t num_public_inputs) +{ + auto [proof_without_public_inputs, public_inputs] = splitVector(proof, num_public_inputs); + return { public_inputs, proof_without_public_inputs }; +} + +std::vector concatenateVectors(const std::vector& firstVector, + const std::vector& secondVector) +{ + std::vector concatenatedVector; + + concatenatedVector.reserve(firstVector.size() + secondVector.size()); + + concatenatedVector.insert(concatenatedVector.end(), firstVector.begin(), firstVector.end()); + concatenatedVector.insert(concatenatedVector.end(), secondVector.begin(), secondVector.end()); + + return concatenatedVector; +} + void AcirComposer::create_circuit(acir_format::acir_format& constraint_system) { builder_ = acir_format::create_circuit(constraint_system, size_hint_); @@ -52,7 +106,7 @@ void AcirComposer::init_proving_key( proving_key_ = composer_.compute_proving_key(builder_); } -std::vector AcirComposer::create_proof( +std::pair, std::vector> AcirComposer::create_proof( std::shared_ptr> const& crs_factory, acir_format::acir_format& constraint_system, acir_format::WitnessVector& witness, @@ -97,57 +151,9 @@ std::vector AcirComposer::create_proof( proof = prover.construct_proof().proof_data; } vinfo("done."); - return proof; -} -/** - * @brief Splits a vector into two vectors, - * the first containing the first 32 * k elements, and the second containing - * the rest. - * - * @param original - The original vector to split - * @param k - The number of 32 bytes to remove - * @return std::pair, std::vector> - */ -std::pair, std::vector> splitVector(std::vector& original, uint32_t k) -{ - uint32_t elementsToRemove = 32 * k; - - if (original.size() < elementsToRemove) { - throw_or_abort("Not enough elements in the original vector"); - } - auto elementsToRemoveLong = static_cast(elementsToRemove); - std::vector removed(original.begin(), original.begin() + elementsToRemoveLong); - original = std::vector(original.begin() + elementsToRemoveLong, original.end()); - - return { original, removed }; -} - -std::vector concatenateVectors(const std::vector& firstVector, - const std::vector& secondVector) -{ - std::vector concatenatedVector; - - concatenatedVector.reserve(firstVector.size() + secondVector.size()); - - concatenatedVector.insert(concatenatedVector.end(), firstVector.begin(), firstVector.end()); - concatenatedVector.insert(concatenatedVector.end(), secondVector.begin(), secondVector.end()); - - return concatenatedVector; -} - -// This splits the proof and public inputs into two vectors. -std::pair, std::vector> AcirComposer::create_proof_public_splitted( - std::shared_ptr> const& crs_factory, - acir_format::acir_format& constraint_system, - acir_format::WitnessVector& witness, - bool is_recursive) -{ - auto proof = create_proof(crs_factory, constraint_system, witness, is_recursive); auto num_public_inputs = static_cast(constraint_system.public_inputs.size()); - - auto [proof_without_public_inputs, public_inputs] = splitVector(proof, num_public_inputs); - return { public_inputs, proof_without_public_inputs }; + return split_proof(proof, num_public_inputs); } std::shared_ptr AcirComposer::init_verification_key() @@ -167,8 +173,13 @@ void AcirComposer::load_verification_key( composer_ = acir_format::Composer(proving_key_, verification_key_); } -bool AcirComposer::verify_proof(std::vector const& proof, bool is_recursive) +bool AcirComposer::verify_proof(std::vector const& public_inputs, + std::vector const& proof_without_public_inputs, + bool is_recursive) { + + auto proof = concatenateVectors(public_inputs, proof_without_public_inputs); + if (!verification_key_) { vinfo("computing verification key..."); verification_key_ = composer_.compute_verification_key(builder_); @@ -187,14 +198,6 @@ bool AcirComposer::verify_proof(std::vector const& proof, bool is_recur } } -bool AcirComposer::verify_proof_splitted(std::vector const& public_inputs, - std::vector const& proof, - bool is_recursive) -{ - auto proof_with_public_inputs = concatenateVectors(public_inputs, proof); - return verify_proof(proof_with_public_inputs, is_recursive); -} - std::string AcirComposer::get_solidity_verifier() { std::ostringstream stream; diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp index 5789fb57aba..28b334adeeb 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp @@ -17,13 +17,7 @@ class AcirComposer { void init_proving_key(std::shared_ptr> const& crs_factory, acir_format::acir_format& constraint_system); - std::vector create_proof( - std::shared_ptr> const& crs_factory, - acir_format::acir_format& constraint_system, - acir_format::WitnessVector& witness, - bool is_recursive); - - std::pair, std::vector> create_proof_public_splitted( + std::pair, std::vector> create_proof( std::shared_ptr> const& crs_factory, acir_format::acir_format& constraint_system, acir_format::WitnessVector& witness, @@ -35,10 +29,7 @@ class AcirComposer { std::shared_ptr init_verification_key(); - bool verify_proof(std::vector const& proof, bool is_recursive); - bool verify_proof_splitted(std::vector const& public_inputs, - std::vector const& proof, - bool is_recursive); + bool verify_proof(std::vector const& public_inputs, std::vector const& proof, bool is_recursive); std::string get_solidity_verifier(); size_t get_exact_circuit_size() { return exact_circuit_size_; }; diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp index 45a6eae60e7..f28f0e220de 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp @@ -49,8 +49,8 @@ WASM_EXPORT void acir_create_proof(in_ptr acir_composer_ptr, auto constraint_system = acir_format::circuit_buf_to_acir_format(from_buffer>(acir_vec)); auto witness = acir_format::witness_buf_to_witness_data(from_buffer>(witness_vec)); - auto [public_inputs, proof_without_public_inputs] = acir_composer->create_proof_public_splitted( - barretenberg::srs::get_crs_factory(), constraint_system, witness, *is_recursive); + auto [public_inputs, proof_without_public_inputs] = + acir_composer->create_proof(barretenberg::srs::get_crs_factory(), constraint_system, witness, *is_recursive); *out_public_inputs = to_heap_buffer(public_inputs); *out_proof_without_public_inputs = to_heap_buffer(proof_without_public_inputs); } @@ -85,7 +85,7 @@ WASM_EXPORT void acir_verify_proof(in_ptr acir_composer_ptr, auto acir_composer = reinterpret_cast(*acir_composer_ptr); auto public_inputs = from_buffer>(public_inputs_buf); auto proof_without_public_inputs = from_buffer>(proof_without_public_inputs_buf); - *result = acir_composer->verify_proof_splitted(public_inputs, proof_without_public_inputs, *is_recursive); + *result = acir_composer->verify_proof(public_inputs, proof_without_public_inputs, *is_recursive); } WASM_EXPORT void acir_get_solidity_verifier(in_ptr acir_composer_ptr, out_str_buf out) From 446113968ca10cdb0d5741d87658c6ec023de018 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 23:02:28 +0000 Subject: [PATCH 22/43] modify bb binary; since we removed _splitted methods --- barretenberg/cpp/src/barretenberg/bb/main.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/bb/main.cpp b/barretenberg/cpp/src/barretenberg/bb/main.cpp index 1d73bd59787..4939294cfac 100644 --- a/barretenberg/cpp/src/barretenberg/bb/main.cpp +++ b/barretenberg/cpp/src/barretenberg/bb/main.cpp @@ -79,8 +79,10 @@ bool proveAndVerify(const std::string& bytecodePath, const std::string& witnessP auto acir_composer = new acir_proofs::AcirComposer(MAX_CIRCUIT_SIZE, verbose); auto constraint_system = get_constraint_system(bytecodePath); auto witness = get_witness(witnessPath); - auto proof = acir_composer->create_proof(srs::get_crs_factory(), constraint_system, witness, recursive); - auto verified = acir_composer->verify_proof(proof, recursive); + auto [public_inputs, proof_without_public_inputs] = + acir_composer->create_proof(srs::get_crs_factory(), constraint_system, witness, recursive); + + auto verified = acir_composer->verify_proof(public_inputs, proof_without_public_inputs, recursive); vinfo("verified: ", verified); return verified; @@ -96,7 +98,7 @@ bool proveAndVerify(const std::string& bytecodePath, const std::string& witnessP * @param bytecodePath Path to the file containing the serialized circuit * @param witnessPath Path to the file containing the serialized witness * @param recursive Whether to use recursive proof generation of non-recursive - * @param outputPath Path to write the proof to + * @param outputProofPath Path to write the proof to - The proof does not contain public inputs */ void prove(const std::string& bytecodePath, const std::string& witnessPath, @@ -107,7 +109,7 @@ void prove(const std::string& bytecodePath, auto constraint_system = get_constraint_system(bytecodePath); auto witness = get_witness(witnessPath); auto [public_inputs, proof_without_public_inputs] = - acir_composer->create_proof_public_splitted(srs::get_crs_factory(), constraint_system, witness, recursive); + acir_composer->create_proof(srs::get_crs_factory(), constraint_system, witness, recursive); if (outputProofPath == "-") { writeRawBytesToStdout(public_inputs); @@ -151,7 +153,7 @@ void gateCount(const std::string& bytecodePath) * - proc_exit: A boolean value is returned indicating whether the proof is valid. * an exit code of 0 will be returned for success and 1 for failure. * - * @param proof_path Path to the file containing the serialized proof + * @param proof_path Path to the file containing the serialized proof without public inputs preprended * @param recursive Whether to use recursive proof generation of non-recursive * @param vk_path Path to the file containing the serialized verification key * @return true If the proof is valid @@ -166,7 +168,7 @@ bool verify(const std::string& proof_path, bool recursive, const std::string& vk auto public_inputs_path = public_inputs_path_from_proof_path(proof_path); std::vector public_inputs = read_public_inputs(public_inputs_path, vk_data.num_public_inputs); auto proof_without_public_inputs = read_file(proof_path); - auto verified = acir_composer->verify_proof_splitted(public_inputs, proof_without_public_inputs, recursive); + auto verified = acir_composer->verify_proof(public_inputs, proof_without_public_inputs, recursive); vinfo("verified: ", verified); From c2a67733b36e82148a1ad798460911a966f63fa0 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Tue, 3 Oct 2023 00:04:01 +0100 Subject: [PATCH 23/43] Update barretenberg/acir_tests/run_acir_tests_browser.sh --- barretenberg/acir_tests/run_acir_tests_browser.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/barretenberg/acir_tests/run_acir_tests_browser.sh b/barretenberg/acir_tests/run_acir_tests_browser.sh index b462d9a6122..22830656250 100755 --- a/barretenberg/acir_tests/run_acir_tests_browser.sh +++ b/barretenberg/acir_tests/run_acir_tests_browser.sh @@ -16,7 +16,6 @@ THREAD_MODEL=${THREAD_MODEL:-mt} # TODO: Currently webkit doesn't seem to have shared memory so is a single threaded test regardless of THREAD_MODEL! echo "Testing thread model: $THREAD_MODEL" - (cd browser-test-app && yarn serve:dest:$THREAD_MODEL) > /dev/null 2>&1 & sleep 1 VERBOSE=1 BIN=./headless-test/bb.js.browser ./run_acir_tests.sh $@ From b4233da331772fe554a6051a157d1667ecf01f18 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 2 Oct 2023 23:14:06 +0000 Subject: [PATCH 24/43] formatting fix --- barretenberg/ts/src/main.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/barretenberg/ts/src/main.ts b/barretenberg/ts/src/main.ts index ecb51b2ac83..cdf99723929 100755 --- a/barretenberg/ts/src/main.ts +++ b/barretenberg/ts/src/main.ts @@ -33,7 +33,7 @@ function getWitness(witnessPath: string) { return decompressed; } -function publicInputsPathFromProofPath(proofPath: string) { +function publicInputsPathFromProofPath(proofPath: string) { return proofPath + '-public_inputs'; } @@ -134,7 +134,7 @@ export async function prove( writeFileSync(publicInputsPath, publicInputs); writeFileSync(outputProofPath, proofWithOutPublicInputs); - + debug(`proof written to: ${outputProofPath}`); debug(`public inputs written to: ${publicInputsPath}`); } From 362552a27bb2e2405acf82c017692e47a2846696 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Tue, 3 Oct 2023 15:34:51 +0000 Subject: [PATCH 25/43] change outwards facing API to not mention proofWithoutPublicInputs --- .../acir_tests/browser-test-app/src/index.ts | 4 ++-- barretenberg/cpp/src/barretenberg/bb/main.cpp | 14 +++++++------- barretenberg/ts/src/main.ts | 10 +++++----- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/barretenberg/acir_tests/browser-test-app/src/index.ts b/barretenberg/acir_tests/browser-test-app/src/index.ts index cc67d3305a6..0e4fd41e174 100644 --- a/barretenberg/acir_tests/browser-test-app/src/index.ts +++ b/barretenberg/acir_tests/browser-test-app/src/index.ts @@ -27,14 +27,14 @@ async function runTest( ); const acirComposer = await api.acirNewAcirComposer(CIRCUIT_SIZE); - const [publicInputs, proofWithOutPublicInputs] = await api.acirCreateProof( + const [publicInputs, proof] = await api.acirCreateProof( acirComposer, bytecode, witness, true ); debug(`verifying...`); - const verified = await api.acirVerifyProof(acirComposer, publicInputs, proofWithOutPublicInputs, true); + const verified = await api.acirVerifyProof(acirComposer, publicInputs, proof, true); debug(`verified: ${verified}`); await api.destroy(); diff --git a/barretenberg/cpp/src/barretenberg/bb/main.cpp b/barretenberg/cpp/src/barretenberg/bb/main.cpp index 4939294cfac..118e77ef1a9 100644 --- a/barretenberg/cpp/src/barretenberg/bb/main.cpp +++ b/barretenberg/cpp/src/barretenberg/bb/main.cpp @@ -79,10 +79,10 @@ bool proveAndVerify(const std::string& bytecodePath, const std::string& witnessP auto acir_composer = new acir_proofs::AcirComposer(MAX_CIRCUIT_SIZE, verbose); auto constraint_system = get_constraint_system(bytecodePath); auto witness = get_witness(witnessPath); - auto [public_inputs, proof_without_public_inputs] = + auto [public_inputs, proof] = acir_composer->create_proof(srs::get_crs_factory(), constraint_system, witness, recursive); - auto verified = acir_composer->verify_proof(public_inputs, proof_without_public_inputs, recursive); + auto verified = acir_composer->verify_proof(public_inputs, proof, recursive); vinfo("verified: ", verified); return verified; @@ -108,17 +108,17 @@ void prove(const std::string& bytecodePath, auto acir_composer = new acir_proofs::AcirComposer(MAX_CIRCUIT_SIZE, verbose); auto constraint_system = get_constraint_system(bytecodePath); auto witness = get_witness(witnessPath); - auto [public_inputs, proof_without_public_inputs] = + auto [public_inputs, proof] = acir_composer->create_proof(srs::get_crs_factory(), constraint_system, witness, recursive); if (outputProofPath == "-") { writeRawBytesToStdout(public_inputs); - writeRawBytesToStdout(proof_without_public_inputs); + writeRawBytesToStdout(proof); vinfo("proof and public inputs written to stdout"); } else { auto outputPublicInputsPath = public_inputs_path_from_proof_path(outputProofPath); write_file(outputPublicInputsPath, public_inputs); - write_file(outputProofPath, proof_without_public_inputs); + write_file(outputProofPath, proof); vinfo("proof written to: ", outputProofPath); vinfo("public inputs written to: ", outputPublicInputsPath); } @@ -167,8 +167,8 @@ bool verify(const std::string& proof_path, bool recursive, const std::string& vk auto public_inputs_path = public_inputs_path_from_proof_path(proof_path); std::vector public_inputs = read_public_inputs(public_inputs_path, vk_data.num_public_inputs); - auto proof_without_public_inputs = read_file(proof_path); - auto verified = acir_composer->verify_proof(public_inputs, proof_without_public_inputs, recursive); + auto proof = read_file(proof_path); + auto verified = acir_composer->verify_proof(public_inputs, proof, recursive); vinfo("verified: ", verified); diff --git a/barretenberg/ts/src/main.ts b/barretenberg/ts/src/main.ts index cdf99723929..4bc364b15d7 100755 --- a/barretenberg/ts/src/main.ts +++ b/barretenberg/ts/src/main.ts @@ -89,7 +89,7 @@ export async function proveAndVerify(bytecodePath: string, witnessPath: string, debug(`creating proof...`); const bytecode = getBytecode(bytecodePath); const witness = getWitness(witnessPath); - const [publicInputs, proofWithOutPublicInputs] = await api.acirCreateProof( + const [publicInputs, proof] = await api.acirCreateProof( acirComposer, bytecode, witness, @@ -97,7 +97,7 @@ export async function proveAndVerify(bytecodePath: string, witnessPath: string, ); debug(`verifying...`); - const verified = await api.acirVerifyProof(acirComposer, publicInputs, proofWithOutPublicInputs, isRecursive); + const verified = await api.acirVerifyProof(acirComposer, publicInputs, proof, isRecursive); debug(`verified: ${verified}`); return verified; } finally { @@ -117,7 +117,7 @@ export async function prove( debug(`creating proof...`); const bytecode = getBytecode(bytecodePath); const witness = getWitness(witnessPath); - const [publicInputs, proofWithOutPublicInputs] = await api.acirCreateProof( + const [publicInputs, proof] = await api.acirCreateProof( acirComposer, bytecode, witness, @@ -127,13 +127,13 @@ export async function prove( if (outputProofPath === '-') { process.stdout.write(publicInputs); - process.stdout.write(proofWithOutPublicInputs); + process.stdout.write(proof); debug(`proof written to stdout`); } else { const publicInputsPath = publicInputsPathFromProofPath(outputProofPath); writeFileSync(publicInputsPath, publicInputs); - writeFileSync(outputProofPath, proofWithOutPublicInputs); + writeFileSync(outputProofPath, proof); debug(`proof written to: ${outputProofPath}`); debug(`public inputs written to: ${publicInputsPath}`); From 857616492a3c8d73db099ef7e704d1d6b0f28229 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Tue, 3 Oct 2023 15:46:32 +0000 Subject: [PATCH 26/43] using proof instead of proof_without_public_inputs in cbinds --- .../dsl/acir_proofs/acir_composer.cpp | 12 +++++------- .../dsl/acir_proofs/acir_composer.hpp | 3 +-- .../src/barretenberg/dsl/acir_proofs/c_bind.cpp | 16 +++++++--------- .../src/barretenberg/dsl/acir_proofs/c_bind.hpp | 5 ++--- 4 files changed, 15 insertions(+), 21 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp index d31e3201a02..dc89782008a 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp @@ -211,15 +211,13 @@ std::string AcirComposer::get_solidity_verifier() * Use this method to get the witness values! * * @param proof - * @param num_inner_public_inputs - number of public inputs on the proof being serialized + * @param public_inputs - number of public inputs associated with the proof */ -std::vector AcirComposer::serialize_proof_into_fields( - std::vector const& public_inputs, - std::vector const& proof_without_public_inputs, - size_t num_inner_public_inputs) // TODO: remove this, can be derived from public_inputs +std::vector AcirComposer::serialize_proof_into_fields(std::vector const& public_inputs, + std::vector const& proof) { - auto proof = concatenateVectors(public_inputs, proof_without_public_inputs); - transcript::StandardTranscript transcript(proof, + auto num_inner_public_inputs = public_inputs.size() / 32; + transcript::StandardTranscript transcript(concatenateVectors(public_inputs, proof), acir_format::Composer::create_manifest(num_inner_public_inputs), transcript::HashType::PlookupPedersenBlake3s, 16); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp index 28b334adeeb..2aa9dbb028e 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp @@ -36,8 +36,7 @@ class AcirComposer { size_t get_total_circuit_size() { return total_circuit_size_; }; std::vector serialize_proof_into_fields(std::vector const& public_inputs, - std::vector const& proof_without_public_inputs, - size_t num_inner_public_inputs); + std::vector const& proof); std::vector serialize_verification_key_into_fields(); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp index f28f0e220de..32774970437 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp @@ -43,16 +43,16 @@ WASM_EXPORT void acir_create_proof(in_ptr acir_composer_ptr, uint8_t const* witness_vec, bool const* is_recursive, uint8_t** out_public_inputs, - uint8_t** out_proof_without_public_inputs) + uint8_t** out_proof) { auto acir_composer = reinterpret_cast(*acir_composer_ptr); auto constraint_system = acir_format::circuit_buf_to_acir_format(from_buffer>(acir_vec)); auto witness = acir_format::witness_buf_to_witness_data(from_buffer>(witness_vec)); - auto [public_inputs, proof_without_public_inputs] = + auto [public_inputs, proof] = acir_composer->create_proof(barretenberg::srs::get_crs_factory(), constraint_system, witness, *is_recursive); *out_public_inputs = to_heap_buffer(public_inputs); - *out_proof_without_public_inputs = to_heap_buffer(proof_without_public_inputs); + *out_proof = to_heap_buffer(proof); } WASM_EXPORT void acir_load_verification_key(in_ptr acir_composer_ptr, uint8_t const* vk_buf) @@ -78,14 +78,14 @@ WASM_EXPORT void acir_get_verification_key(in_ptr acir_composer_ptr, uint8_t** o WASM_EXPORT void acir_verify_proof(in_ptr acir_composer_ptr, uint8_t const* public_inputs_buf, - uint8_t const* proof_without_public_inputs_buf, + uint8_t const* proof_buf, bool const* is_recursive, bool* result) { auto acir_composer = reinterpret_cast(*acir_composer_ptr); auto public_inputs = from_buffer>(public_inputs_buf); - auto proof_without_public_inputs = from_buffer>(proof_without_public_inputs_buf); - *result = acir_composer->verify_proof(public_inputs, proof_without_public_inputs, *is_recursive); + auto proof = from_buffer>(proof_buf); + *result = acir_composer->verify_proof(public_inputs, proof, *is_recursive); } WASM_EXPORT void acir_get_solidity_verifier(in_ptr acir_composer_ptr, out_str_buf out) @@ -98,14 +98,12 @@ WASM_EXPORT void acir_get_solidity_verifier(in_ptr acir_composer_ptr, out_str_bu WASM_EXPORT void acir_serialize_proof_into_fields(in_ptr acir_composer_ptr, uint8_t const* public_inputs_buf, uint8_t const* proof_buf, - uint32_t const* num_inner_public_inputs, fr::vec_out_buf out) { auto acir_composer = reinterpret_cast(*acir_composer_ptr); auto public_inputs = from_buffer>(proof_buf); auto proof = from_buffer>(public_inputs_buf); - auto proof_as_fields = - acir_composer->serialize_proof_into_fields(public_inputs, proof, ntohl(*num_inner_public_inputs)); + auto proof_as_fields = acir_composer->serialize_proof_into_fields(public_inputs, proof); *out = to_heap_buffer(proof_as_fields); } diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.hpp index e01476b9998..9f07e531618 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.hpp @@ -31,7 +31,7 @@ WASM_EXPORT void acir_create_proof(in_ptr acir_composer_ptr, uint8_t const* witness_vec, bool const* is_recursive, uint8_t** out_public_inputs, - uint8_t** out_proof_without_public_inputs); + uint8_t** out_proof); WASM_EXPORT void acir_load_verification_key(in_ptr acir_composer_ptr, uint8_t const* vk_buf); @@ -41,7 +41,7 @@ WASM_EXPORT void acir_get_verification_key(in_ptr acir_composer_ptr, uint8_t** o WASM_EXPORT void acir_verify_proof(in_ptr acir_composer_ptr, uint8_t const* public_inputs_buf, - uint8_t const* proof_without_public_inputs_buf, + uint8_t const* proof_buf, bool const* is_recursive, bool* result); @@ -50,7 +50,6 @@ WASM_EXPORT void acir_get_solidity_verifier(in_ptr acir_composer_ptr, out_str_bu WASM_EXPORT void acir_serialize_proof_into_fields(in_ptr acir_composer_ptr, uint8_t const* public_inputs_buf, uint8_t const* proof_buf, - uint32_t const* num_inner_public_inputs, fr::vec_out_buf out); WASM_EXPORT void acir_serialize_verification_key_into_fields(in_ptr acir_composer_ptr, From 51714c83edebe7de8af0ff8eb0c6a7174b466bdf Mon Sep 17 00:00:00 2001 From: kevaundray Date: Tue, 3 Oct 2023 15:47:01 +0000 Subject: [PATCH 27/43] redo bindings --- barretenberg/exports.json | 8 +- barretenberg/ts/src/barretenberg_api/index.ts | 164 +++--------------- 2 files changed, 28 insertions(+), 144 deletions(-) diff --git a/barretenberg/exports.json b/barretenberg/exports.json index 7954d069ef4..2114c83aa99 100644 --- a/barretenberg/exports.json +++ b/barretenberg/exports.json @@ -654,7 +654,7 @@ "type": "uint8_t **" }, { - "name": "out_proof_without_public_inputs", + "name": "out_proof", "type": "uint8_t **" } ], @@ -714,7 +714,7 @@ "type": "const uint8_t *" }, { - "name": "proof_without_public_inputs_buf", + "name": "proof_buf", "type": "const uint8_t *" }, { @@ -760,10 +760,6 @@ { "name": "proof_buf", "type": "const uint8_t *" - }, - { - "name": "num_inner_public_inputs", - "type": "const uint32_t *" } ], "outArgs": [ diff --git a/barretenberg/ts/src/barretenberg_api/index.ts b/barretenberg/ts/src/barretenberg_api/index.ts index 167beb37cf4..c8bffe622a1 100644 --- a/barretenberg/ts/src/barretenberg_api/index.ts +++ b/barretenberg/ts/src/barretenberg_api/index.ts @@ -1,13 +1,7 @@ // WARNING: FILE CODE GENERATED BY BINDGEN UTILITY. DO NOT EDIT! /* eslint-disable @typescript-eslint/no-unused-vars */ import { BarretenbergBinder } from '../barretenberg_binder/index.js'; -import { - BufferDeserializer, - NumberDeserializer, - VectorDeserializer, - BoolDeserializer, - StringDeserializer, -} from '../serialize/index.js'; +import { BufferDeserializer, NumberDeserializer, VectorDeserializer, BoolDeserializer, StringDeserializer } from '../serialize/index.js'; import { Fr, Fq, Point, Buffer32, Buffer128, Ptr } from '../types/index.js'; export class BarretenbergApi { @@ -43,11 +37,7 @@ export class BarretenbergApi { } async pedersenCompressWithHashIndex(inputsBuffer: Fr[], hashIndex: number): Promise { - const result = await this.binder.callWasmExport( - 'pedersen___compress_with_hash_index', - [inputsBuffer, hashIndex], - [Fr], - ); + const result = await this.binder.callWasmExport('pedersen___compress_with_hash_index', [inputsBuffer, hashIndex], [Fr]); return result[0]; } @@ -62,11 +52,7 @@ export class BarretenbergApi { } async pedersenPlookupCommitWithHashIndex(inputsBuffer: Fr[], hashIndex: number): Promise { - const result = await this.binder.callWasmExport( - 'pedersen___plookup_commit_with_hash_index', - [inputsBuffer, hashIndex], - [Fr], - ); + const result = await this.binder.callWasmExport('pedersen___plookup_commit_with_hash_index', [inputsBuffer, hashIndex], [Fr]); return result[0]; } @@ -91,11 +77,7 @@ export class BarretenbergApi { } async pedersenHashMultipleWithHashIndex(inputsBuffer: Fr[], hashIndex: number): Promise { - const result = await this.binder.callWasmExport( - 'pedersen_hash_multiple_with_hash_index', - [inputsBuffer, hashIndex], - [Fr], - ); + const result = await this.binder.callWasmExport('pedersen_hash_multiple_with_hash_index', [inputsBuffer, hashIndex], [Fr]); return result[0]; } @@ -125,76 +107,37 @@ export class BarretenbergApi { } async schnorrConstructSignature(message: Uint8Array, privateKey: Fr): Promise<[Buffer32, Buffer32]> { - const result = await this.binder.callWasmExport( - 'schnorr_construct_signature', - [message, privateKey], - [Buffer32, Buffer32], - ); + const result = await this.binder.callWasmExport('schnorr_construct_signature', [message, privateKey], [Buffer32, Buffer32]); return result as any; } async schnorrVerifySignature(message: Uint8Array, pubKey: Point, sigS: Buffer32, sigE: Buffer32): Promise { - const result = await this.binder.callWasmExport( - 'schnorr_verify_signature', - [message, pubKey, sigS, sigE], - [BoolDeserializer()], - ); + const result = await this.binder.callWasmExport('schnorr_verify_signature', [message, pubKey, sigS, sigE], [BoolDeserializer()]); return result[0]; } async schnorrMultisigCreateMultisigPublicKey(privateKey: Fq): Promise { - const result = await this.binder.callWasmExport( - 'schnorr_multisig_create_multisig_public_key', - [privateKey], - [Buffer128], - ); + const result = await this.binder.callWasmExport('schnorr_multisig_create_multisig_public_key', [privateKey], [Buffer128]); return result[0]; } async schnorrMultisigValidateAndCombineSignerPubkeys(signerPubkeyBuf: Buffer128[]): Promise<[Point, boolean]> { - const result = await this.binder.callWasmExport( - 'schnorr_multisig_validate_and_combine_signer_pubkeys', - [signerPubkeyBuf], - [Point, BoolDeserializer()], - ); + const result = await this.binder.callWasmExport('schnorr_multisig_validate_and_combine_signer_pubkeys', [signerPubkeyBuf], [Point, BoolDeserializer()]); return result as any; } async schnorrMultisigConstructSignatureRound1(): Promise<[Buffer128, Buffer128]> { - const result = await this.binder.callWasmExport( - 'schnorr_multisig_construct_signature_round_1', - [], - [Buffer128, Buffer128], - ); + const result = await this.binder.callWasmExport('schnorr_multisig_construct_signature_round_1', [], [Buffer128, Buffer128]); return result as any; } - async schnorrMultisigConstructSignatureRound2( - message: Uint8Array, - privateKey: Fq, - signerRoundOnePrivateBuf: Buffer128, - signerPubkeysBuf: Buffer128[], - roundOnePublicBuf: Buffer128[], - ): Promise<[Fq, boolean]> { - const result = await this.binder.callWasmExport( - 'schnorr_multisig_construct_signature_round_2', - [message, privateKey, signerRoundOnePrivateBuf, signerPubkeysBuf, roundOnePublicBuf], - [Fq, BoolDeserializer()], - ); + async schnorrMultisigConstructSignatureRound2(message: Uint8Array, privateKey: Fq, signerRoundOnePrivateBuf: Buffer128, signerPubkeysBuf: Buffer128[], roundOnePublicBuf: Buffer128[]): Promise<[Fq, boolean]> { + const result = await this.binder.callWasmExport('schnorr_multisig_construct_signature_round_2', [message, privateKey, signerRoundOnePrivateBuf, signerPubkeysBuf, roundOnePublicBuf], [Fq, BoolDeserializer()]); return result as any; } - async schnorrMultisigCombineSignatures( - message: Uint8Array, - signerPubkeysBuf: Buffer128[], - roundOneBuf: Buffer128[], - roundTwoBuf: Fr[], - ): Promise<[Buffer32, Buffer32, boolean]> { - const result = await this.binder.callWasmExport( - 'schnorr_multisig_combine_signatures', - [message, signerPubkeysBuf, roundOneBuf, roundTwoBuf], - [Buffer32, Buffer32, BoolDeserializer()], - ); + async schnorrMultisigCombineSignatures(message: Uint8Array, signerPubkeysBuf: Buffer128[], roundOneBuf: Buffer128[], roundTwoBuf: Fr[]): Promise<[Buffer32, Buffer32, boolean]> { + const result = await this.binder.callWasmExport('schnorr_multisig_combine_signatures', [message, signerPubkeysBuf, roundOneBuf, roundTwoBuf], [Buffer32, Buffer32, BoolDeserializer()]); return result as any; } @@ -204,11 +147,7 @@ export class BarretenbergApi { } async examplesSimpleCreateAndVerifyProof(): Promise { - const result = await this.binder.callWasmExport( - 'examples_simple_create_and_verify_proof', - [], - [BoolDeserializer()], - ); + const result = await this.binder.callWasmExport('examples_simple_create_and_verify_proof', [], [BoolDeserializer()]); return result[0]; } @@ -223,11 +162,7 @@ export class BarretenbergApi { } async acirGetCircuitSizes(constraintSystemBuf: Uint8Array): Promise<[number, number, number]> { - const result = await this.binder.callWasmExport( - 'acir_get_circuit_sizes', - [constraintSystemBuf], - [NumberDeserializer(), NumberDeserializer(), NumberDeserializer()], - ); + const result = await this.binder.callWasmExport('acir_get_circuit_sizes', [constraintSystemBuf], [NumberDeserializer(), NumberDeserializer(), NumberDeserializer()]); return result as any; } @@ -242,34 +177,17 @@ export class BarretenbergApi { } async acirCreateCircuit(acirComposerPtr: Ptr, constraintSystemBuf: Uint8Array, sizeHint: number): Promise { - const result = await this.binder.callWasmExport( - 'acir_create_circuit', - [acirComposerPtr, constraintSystemBuf, sizeHint], - [], - ); + const result = await this.binder.callWasmExport('acir_create_circuit', [acirComposerPtr, constraintSystemBuf, sizeHint], []); return; } async acirInitProvingKey(acirComposerPtr: Ptr, constraintSystemBuf: Uint8Array): Promise { - const result = await this.binder.callWasmExport( - 'acir_init_proving_key', - [acirComposerPtr, constraintSystemBuf], - [], - ); + const result = await this.binder.callWasmExport('acir_init_proving_key', [acirComposerPtr, constraintSystemBuf], []); return; } - async acirCreateProof( - acirComposerPtr: Ptr, - acirVec: Uint8Array, - witnessVec: Uint8Array, - isRecursive: boolean, - ): Promise<[Uint8Array, Uint8Array]> { - const result = await this.binder.callWasmExport( - 'acir_create_proof', - [acirComposerPtr, acirVec, witnessVec, isRecursive], - [BufferDeserializer(), BufferDeserializer()], - ); + async acirCreateProof(acirComposerPtr: Ptr, acirVec: Uint8Array, witnessVec: Uint8Array, isRecursive: boolean): Promise<[Uint8Array, Uint8Array]> { + const result = await this.binder.callWasmExport('acir_create_proof', [acirComposerPtr, acirVec, witnessVec, isRecursive], [BufferDeserializer(), BufferDeserializer()]); return result as any; } @@ -284,57 +202,27 @@ export class BarretenbergApi { } async acirGetVerificationKey(acirComposerPtr: Ptr): Promise { - const result = await this.binder.callWasmExport( - 'acir_get_verification_key', - [acirComposerPtr], - [BufferDeserializer()], - ); + const result = await this.binder.callWasmExport('acir_get_verification_key', [acirComposerPtr], [BufferDeserializer()]); return result[0]; } - async acirVerifyProof( - acirComposerPtr: Ptr, - publicInputsBuf: Uint8Array, - proofWithoutPublicInputsBuf: Uint8Array, - isRecursive: boolean, - ): Promise { - const result = await this.binder.callWasmExport( - 'acir_verify_proof', - [acirComposerPtr, publicInputsBuf, proofWithoutPublicInputsBuf, isRecursive], - [BoolDeserializer()], - ); + async acirVerifyProof(acirComposerPtr: Ptr, publicInputsBuf: Uint8Array, proofBuf: Uint8Array, isRecursive: boolean): Promise { + const result = await this.binder.callWasmExport('acir_verify_proof', [acirComposerPtr, publicInputsBuf, proofBuf, isRecursive], [BoolDeserializer()]); return result[0]; } async acirGetSolidityVerifier(acirComposerPtr: Ptr): Promise { - const result = await this.binder.callWasmExport( - 'acir_get_solidity_verifier', - [acirComposerPtr], - [StringDeserializer()], - ); + const result = await this.binder.callWasmExport('acir_get_solidity_verifier', [acirComposerPtr], [StringDeserializer()]); return result[0]; } - async acirSerializeProofIntoFields( - acirComposerPtr: Ptr, - publicInputsBuf: Uint8Array, - proofBuf: Uint8Array, - numInnerPublicInputs: number, - ): Promise { - const result = await this.binder.callWasmExport( - 'acir_serialize_proof_into_fields', - [acirComposerPtr, publicInputsBuf, proofBuf, numInnerPublicInputs], - [VectorDeserializer(Fr)], - ); + async acirSerializeProofIntoFields(acirComposerPtr: Ptr, publicInputsBuf: Uint8Array, proofBuf: Uint8Array): Promise { + const result = await this.binder.callWasmExport('acir_serialize_proof_into_fields', [acirComposerPtr, publicInputsBuf, proofBuf], [VectorDeserializer(Fr)]); return result[0]; } async acirSerializeVerificationKeyIntoFields(acirComposerPtr: Ptr): Promise<[Fr[], Fr]> { - const result = await this.binder.callWasmExport( - 'acir_serialize_verification_key_into_fields', - [acirComposerPtr], - [VectorDeserializer(Fr), Fr], - ); + const result = await this.binder.callWasmExport('acir_serialize_verification_key_into_fields', [acirComposerPtr], [VectorDeserializer(Fr), Fr]); return result as any; } } From a811d366c0380d4d86932a5d7d5c5ca28698b292 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Tue, 3 Oct 2023 15:47:19 +0000 Subject: [PATCH 28/43] modify binaries --- barretenberg/cpp/src/barretenberg/bb/main.cpp | 2 +- barretenberg/ts/src/main.ts | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/bb/main.cpp b/barretenberg/cpp/src/barretenberg/bb/main.cpp index 118e77ef1a9..8233e3902ab 100644 --- a/barretenberg/cpp/src/barretenberg/bb/main.cpp +++ b/barretenberg/cpp/src/barretenberg/bb/main.cpp @@ -264,7 +264,7 @@ void proofAsFields(const std::string& proof_path, std::string const& vk_path, co std::vector public_inputs = read_public_inputs(public_inputs_path, vk_data.num_public_inputs); auto acir_composer = new acir_proofs::AcirComposer(MAX_CIRCUIT_SIZE, verbose); - auto data = acir_composer->serialize_proof_into_fields(public_inputs, proof, vk_data.num_public_inputs); + auto data = acir_composer->serialize_proof_into_fields(public_inputs, proof); auto json = format("[", join(map(data, [](auto fr) { return format("\"", fr, "\""); })), "]"); if (output_path == "-") { diff --git a/barretenberg/ts/src/main.ts b/barretenberg/ts/src/main.ts index 4bc364b15d7..f9b7e412f2d 100755 --- a/barretenberg/ts/src/main.ts +++ b/barretenberg/ts/src/main.ts @@ -221,18 +221,16 @@ export async function writeVk(bytecodePath: string, crsPath: string, outputPath: } } -export async function proofAsFields(proofPath: string, vkPath: string, outputPath: string) { +export async function proofAsFields(proofPath: string, outputPath: string) { const { api, acirComposer } = await initLite(); try { debug('serializing proof byte array into field elements'); - const numPublicInputs = readFileSync(vkPath).readUint32BE(8); const publicInputsPath = publicInputsPathFromProofPath(proofPath); const proofAsFields = await api.acirSerializeProofIntoFields( acirComposer, readFileSync(publicInputsPath), readFileSync(proofPath), - numPublicInputs, ); const jsonProofAsFields = JSON.stringify(proofAsFields.map(f => f.toString())); From 2d075502a34331d6e94cc58095a6e25ffc4c52a6 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Tue, 3 Oct 2023 15:47:38 +0000 Subject: [PATCH 29/43] linter --- barretenberg/ts/src/barretenberg_api/index.ts | 163 +++++++++++++++--- barretenberg/ts/src/main.ts | 14 +- 2 files changed, 139 insertions(+), 38 deletions(-) diff --git a/barretenberg/ts/src/barretenberg_api/index.ts b/barretenberg/ts/src/barretenberg_api/index.ts index c8bffe622a1..1b784020f8f 100644 --- a/barretenberg/ts/src/barretenberg_api/index.ts +++ b/barretenberg/ts/src/barretenberg_api/index.ts @@ -1,7 +1,13 @@ // WARNING: FILE CODE GENERATED BY BINDGEN UTILITY. DO NOT EDIT! /* eslint-disable @typescript-eslint/no-unused-vars */ import { BarretenbergBinder } from '../barretenberg_binder/index.js'; -import { BufferDeserializer, NumberDeserializer, VectorDeserializer, BoolDeserializer, StringDeserializer } from '../serialize/index.js'; +import { + BufferDeserializer, + NumberDeserializer, + VectorDeserializer, + BoolDeserializer, + StringDeserializer, +} from '../serialize/index.js'; import { Fr, Fq, Point, Buffer32, Buffer128, Ptr } from '../types/index.js'; export class BarretenbergApi { @@ -37,7 +43,11 @@ export class BarretenbergApi { } async pedersenCompressWithHashIndex(inputsBuffer: Fr[], hashIndex: number): Promise { - const result = await this.binder.callWasmExport('pedersen___compress_with_hash_index', [inputsBuffer, hashIndex], [Fr]); + const result = await this.binder.callWasmExport( + 'pedersen___compress_with_hash_index', + [inputsBuffer, hashIndex], + [Fr], + ); return result[0]; } @@ -52,7 +62,11 @@ export class BarretenbergApi { } async pedersenPlookupCommitWithHashIndex(inputsBuffer: Fr[], hashIndex: number): Promise { - const result = await this.binder.callWasmExport('pedersen___plookup_commit_with_hash_index', [inputsBuffer, hashIndex], [Fr]); + const result = await this.binder.callWasmExport( + 'pedersen___plookup_commit_with_hash_index', + [inputsBuffer, hashIndex], + [Fr], + ); return result[0]; } @@ -77,7 +91,11 @@ export class BarretenbergApi { } async pedersenHashMultipleWithHashIndex(inputsBuffer: Fr[], hashIndex: number): Promise { - const result = await this.binder.callWasmExport('pedersen_hash_multiple_with_hash_index', [inputsBuffer, hashIndex], [Fr]); + const result = await this.binder.callWasmExport( + 'pedersen_hash_multiple_with_hash_index', + [inputsBuffer, hashIndex], + [Fr], + ); return result[0]; } @@ -107,37 +125,76 @@ export class BarretenbergApi { } async schnorrConstructSignature(message: Uint8Array, privateKey: Fr): Promise<[Buffer32, Buffer32]> { - const result = await this.binder.callWasmExport('schnorr_construct_signature', [message, privateKey], [Buffer32, Buffer32]); + const result = await this.binder.callWasmExport( + 'schnorr_construct_signature', + [message, privateKey], + [Buffer32, Buffer32], + ); return result as any; } async schnorrVerifySignature(message: Uint8Array, pubKey: Point, sigS: Buffer32, sigE: Buffer32): Promise { - const result = await this.binder.callWasmExport('schnorr_verify_signature', [message, pubKey, sigS, sigE], [BoolDeserializer()]); + const result = await this.binder.callWasmExport( + 'schnorr_verify_signature', + [message, pubKey, sigS, sigE], + [BoolDeserializer()], + ); return result[0]; } async schnorrMultisigCreateMultisigPublicKey(privateKey: Fq): Promise { - const result = await this.binder.callWasmExport('schnorr_multisig_create_multisig_public_key', [privateKey], [Buffer128]); + const result = await this.binder.callWasmExport( + 'schnorr_multisig_create_multisig_public_key', + [privateKey], + [Buffer128], + ); return result[0]; } async schnorrMultisigValidateAndCombineSignerPubkeys(signerPubkeyBuf: Buffer128[]): Promise<[Point, boolean]> { - const result = await this.binder.callWasmExport('schnorr_multisig_validate_and_combine_signer_pubkeys', [signerPubkeyBuf], [Point, BoolDeserializer()]); + const result = await this.binder.callWasmExport( + 'schnorr_multisig_validate_and_combine_signer_pubkeys', + [signerPubkeyBuf], + [Point, BoolDeserializer()], + ); return result as any; } async schnorrMultisigConstructSignatureRound1(): Promise<[Buffer128, Buffer128]> { - const result = await this.binder.callWasmExport('schnorr_multisig_construct_signature_round_1', [], [Buffer128, Buffer128]); + const result = await this.binder.callWasmExport( + 'schnorr_multisig_construct_signature_round_1', + [], + [Buffer128, Buffer128], + ); return result as any; } - async schnorrMultisigConstructSignatureRound2(message: Uint8Array, privateKey: Fq, signerRoundOnePrivateBuf: Buffer128, signerPubkeysBuf: Buffer128[], roundOnePublicBuf: Buffer128[]): Promise<[Fq, boolean]> { - const result = await this.binder.callWasmExport('schnorr_multisig_construct_signature_round_2', [message, privateKey, signerRoundOnePrivateBuf, signerPubkeysBuf, roundOnePublicBuf], [Fq, BoolDeserializer()]); + async schnorrMultisigConstructSignatureRound2( + message: Uint8Array, + privateKey: Fq, + signerRoundOnePrivateBuf: Buffer128, + signerPubkeysBuf: Buffer128[], + roundOnePublicBuf: Buffer128[], + ): Promise<[Fq, boolean]> { + const result = await this.binder.callWasmExport( + 'schnorr_multisig_construct_signature_round_2', + [message, privateKey, signerRoundOnePrivateBuf, signerPubkeysBuf, roundOnePublicBuf], + [Fq, BoolDeserializer()], + ); return result as any; } - async schnorrMultisigCombineSignatures(message: Uint8Array, signerPubkeysBuf: Buffer128[], roundOneBuf: Buffer128[], roundTwoBuf: Fr[]): Promise<[Buffer32, Buffer32, boolean]> { - const result = await this.binder.callWasmExport('schnorr_multisig_combine_signatures', [message, signerPubkeysBuf, roundOneBuf, roundTwoBuf], [Buffer32, Buffer32, BoolDeserializer()]); + async schnorrMultisigCombineSignatures( + message: Uint8Array, + signerPubkeysBuf: Buffer128[], + roundOneBuf: Buffer128[], + roundTwoBuf: Fr[], + ): Promise<[Buffer32, Buffer32, boolean]> { + const result = await this.binder.callWasmExport( + 'schnorr_multisig_combine_signatures', + [message, signerPubkeysBuf, roundOneBuf, roundTwoBuf], + [Buffer32, Buffer32, BoolDeserializer()], + ); return result as any; } @@ -147,7 +204,11 @@ export class BarretenbergApi { } async examplesSimpleCreateAndVerifyProof(): Promise { - const result = await this.binder.callWasmExport('examples_simple_create_and_verify_proof', [], [BoolDeserializer()]); + const result = await this.binder.callWasmExport( + 'examples_simple_create_and_verify_proof', + [], + [BoolDeserializer()], + ); return result[0]; } @@ -162,7 +223,11 @@ export class BarretenbergApi { } async acirGetCircuitSizes(constraintSystemBuf: Uint8Array): Promise<[number, number, number]> { - const result = await this.binder.callWasmExport('acir_get_circuit_sizes', [constraintSystemBuf], [NumberDeserializer(), NumberDeserializer(), NumberDeserializer()]); + const result = await this.binder.callWasmExport( + 'acir_get_circuit_sizes', + [constraintSystemBuf], + [NumberDeserializer(), NumberDeserializer(), NumberDeserializer()], + ); return result as any; } @@ -177,17 +242,34 @@ export class BarretenbergApi { } async acirCreateCircuit(acirComposerPtr: Ptr, constraintSystemBuf: Uint8Array, sizeHint: number): Promise { - const result = await this.binder.callWasmExport('acir_create_circuit', [acirComposerPtr, constraintSystemBuf, sizeHint], []); + const result = await this.binder.callWasmExport( + 'acir_create_circuit', + [acirComposerPtr, constraintSystemBuf, sizeHint], + [], + ); return; } async acirInitProvingKey(acirComposerPtr: Ptr, constraintSystemBuf: Uint8Array): Promise { - const result = await this.binder.callWasmExport('acir_init_proving_key', [acirComposerPtr, constraintSystemBuf], []); + const result = await this.binder.callWasmExport( + 'acir_init_proving_key', + [acirComposerPtr, constraintSystemBuf], + [], + ); return; } - async acirCreateProof(acirComposerPtr: Ptr, acirVec: Uint8Array, witnessVec: Uint8Array, isRecursive: boolean): Promise<[Uint8Array, Uint8Array]> { - const result = await this.binder.callWasmExport('acir_create_proof', [acirComposerPtr, acirVec, witnessVec, isRecursive], [BufferDeserializer(), BufferDeserializer()]); + async acirCreateProof( + acirComposerPtr: Ptr, + acirVec: Uint8Array, + witnessVec: Uint8Array, + isRecursive: boolean, + ): Promise<[Uint8Array, Uint8Array]> { + const result = await this.binder.callWasmExport( + 'acir_create_proof', + [acirComposerPtr, acirVec, witnessVec, isRecursive], + [BufferDeserializer(), BufferDeserializer()], + ); return result as any; } @@ -202,27 +284,56 @@ export class BarretenbergApi { } async acirGetVerificationKey(acirComposerPtr: Ptr): Promise { - const result = await this.binder.callWasmExport('acir_get_verification_key', [acirComposerPtr], [BufferDeserializer()]); + const result = await this.binder.callWasmExport( + 'acir_get_verification_key', + [acirComposerPtr], + [BufferDeserializer()], + ); return result[0]; } - async acirVerifyProof(acirComposerPtr: Ptr, publicInputsBuf: Uint8Array, proofBuf: Uint8Array, isRecursive: boolean): Promise { - const result = await this.binder.callWasmExport('acir_verify_proof', [acirComposerPtr, publicInputsBuf, proofBuf, isRecursive], [BoolDeserializer()]); + async acirVerifyProof( + acirComposerPtr: Ptr, + publicInputsBuf: Uint8Array, + proofBuf: Uint8Array, + isRecursive: boolean, + ): Promise { + const result = await this.binder.callWasmExport( + 'acir_verify_proof', + [acirComposerPtr, publicInputsBuf, proofBuf, isRecursive], + [BoolDeserializer()], + ); return result[0]; } async acirGetSolidityVerifier(acirComposerPtr: Ptr): Promise { - const result = await this.binder.callWasmExport('acir_get_solidity_verifier', [acirComposerPtr], [StringDeserializer()]); + const result = await this.binder.callWasmExport( + 'acir_get_solidity_verifier', + [acirComposerPtr], + [StringDeserializer()], + ); return result[0]; } - async acirSerializeProofIntoFields(acirComposerPtr: Ptr, publicInputsBuf: Uint8Array, proofBuf: Uint8Array): Promise { - const result = await this.binder.callWasmExport('acir_serialize_proof_into_fields', [acirComposerPtr, publicInputsBuf, proofBuf], [VectorDeserializer(Fr)]); + async acirSerializeProofIntoFields( + acirComposerPtr: Ptr, + publicInputsBuf: Uint8Array, + proofBuf: Uint8Array, + ): Promise { + const result = await this.binder.callWasmExport( + 'acir_serialize_proof_into_fields', + [acirComposerPtr, publicInputsBuf, proofBuf], + [VectorDeserializer(Fr)], + ); return result[0]; } async acirSerializeVerificationKeyIntoFields(acirComposerPtr: Ptr): Promise<[Fr[], Fr]> { - const result = await this.binder.callWasmExport('acir_serialize_verification_key_into_fields', [acirComposerPtr], [VectorDeserializer(Fr), Fr]); + const result = await this.binder.callWasmExport( + 'acir_serialize_verification_key_into_fields', + [acirComposerPtr], + [VectorDeserializer(Fr), Fr], + ); return result as any; } } diff --git a/barretenberg/ts/src/main.ts b/barretenberg/ts/src/main.ts index f9b7e412f2d..8d84a2b0521 100755 --- a/barretenberg/ts/src/main.ts +++ b/barretenberg/ts/src/main.ts @@ -89,12 +89,7 @@ export async function proveAndVerify(bytecodePath: string, witnessPath: string, debug(`creating proof...`); const bytecode = getBytecode(bytecodePath); const witness = getWitness(witnessPath); - const [publicInputs, proof] = await api.acirCreateProof( - acirComposer, - bytecode, - witness, - isRecursive, - ); + const [publicInputs, proof] = await api.acirCreateProof(acirComposer, bytecode, witness, isRecursive); debug(`verifying...`); const verified = await api.acirVerifyProof(acirComposer, publicInputs, proof, isRecursive); @@ -117,12 +112,7 @@ export async function prove( debug(`creating proof...`); const bytecode = getBytecode(bytecodePath); const witness = getWitness(witnessPath); - const [publicInputs, proof] = await api.acirCreateProof( - acirComposer, - bytecode, - witness, - isRecursive, - ); + const [publicInputs, proof] = await api.acirCreateProof(acirComposer, bytecode, witness, isRecursive); debug(`done.`); if (outputProofPath === '-') { From 0b49345ef0357436b26c76ddc20c5479f98a64d4 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Tue, 3 Oct 2023 15:49:07 +0000 Subject: [PATCH 30/43] multi: - no longer need vkpath for proofasfields - _public_inputs --- barretenberg/ts/src/main.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/barretenberg/ts/src/main.ts b/barretenberg/ts/src/main.ts index 8d84a2b0521..6869fec4c4b 100755 --- a/barretenberg/ts/src/main.ts +++ b/barretenberg/ts/src/main.ts @@ -34,7 +34,7 @@ function getWitness(witnessPath: string) { } function publicInputsPathFromProofPath(proofPath: string) { - return proofPath + '-public_inputs'; + return proofPath + '_public_inputs'; } async function computeCircuitSize(bytecodePath: string, api: Barretenberg) { @@ -343,11 +343,10 @@ program .command('proof_as_fields') .description('Return the proof as fields elements') .requiredOption('-p, --proof-path ', 'Specify the proof path') - .requiredOption('-k, --vk-path ', 'Path to verification key.') .requiredOption('-o, --output-path ', 'Specify the JSON path to write the proof fields') - .action(async ({ proofPath, vkPath, outputPath }) => { + .action(async ({ proofPath, outputPath }) => { handleGlobalOptions(); - await proofAsFields(proofPath, vkPath, outputPath); + await proofAsFields(proofPath, outputPath); }); program From b669013110d5333e5f399f979f8b31cf80a2ce0c Mon Sep 17 00:00:00 2001 From: kevaundray Date: Tue, 3 Oct 2023 15:49:47 +0000 Subject: [PATCH 31/43] bb: -public_inputs -> _public_inputs --- barretenberg/cpp/src/barretenberg/bb/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/barretenberg/cpp/src/barretenberg/bb/main.cpp b/barretenberg/cpp/src/barretenberg/bb/main.cpp index 8233e3902ab..30f9f8331bc 100644 --- a/barretenberg/cpp/src/barretenberg/bb/main.cpp +++ b/barretenberg/cpp/src/barretenberg/bb/main.cpp @@ -58,7 +58,7 @@ std::vector read_public_inputs(std::string const& public_inputs_path, s // in the same directory as their proof. std::string public_inputs_path_from_proof_path(std::string const& proof_path) { - return proof_path + "-public_inputs"; + return proof_path + "_public_inputs"; } /** From aeea25efb59cd880dfacd05e502c9d4d2ca1a0d7 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Tue, 3 Oct 2023 15:50:05 +0000 Subject: [PATCH 32/43] lint --- barretenberg/ts/src/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/barretenberg/ts/src/main.ts b/barretenberg/ts/src/main.ts index 6869fec4c4b..52d132620e6 100755 --- a/barretenberg/ts/src/main.ts +++ b/barretenberg/ts/src/main.ts @@ -344,7 +344,7 @@ program .description('Return the proof as fields elements') .requiredOption('-p, --proof-path ', 'Specify the proof path') .requiredOption('-o, --output-path ', 'Specify the JSON path to write the proof fields') - .action(async ({ proofPath, outputPath }) => { + .action(async ({ proofPath, outputPath }) => { handleGlobalOptions(); await proofAsFields(proofPath, outputPath); }); From dab3ec3ab18c27450a5b6173bf69a5d7302e307e Mon Sep 17 00:00:00 2001 From: kevaundray Date: Tue, 3 Oct 2023 16:08:53 +0000 Subject: [PATCH 33/43] add get_file_size method --- .../cpp/src/barretenberg/bb/file_io.hpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/bb/file_io.hpp b/barretenberg/cpp/src/barretenberg/bb/file_io.hpp index 09009ebbd2b..06699968d8e 100644 --- a/barretenberg/cpp/src/barretenberg/bb/file_io.hpp +++ b/barretenberg/cpp/src/barretenberg/bb/file_io.hpp @@ -1,27 +1,32 @@ #pragma once #include +#include #include #include -inline std::vector read_file(const std::string& filename) +inline std::streamsize get_file_size(const std::string& filename) { - // Open the file in binary mode and move to the end. std::ifstream file(filename, std::ios::binary | std::ios::ate); if (!file) { throw std::runtime_error("Unable to open file: " + filename); } - // Get the file size. - std::streamsize size = file.tellg(); + return file.tellg(); +} + +inline std::vector read_file(const std::string& filename) +{ + std::streamsize size = get_file_size(filename); + if (size <= 0) { throw std::runtime_error("File is empty or there's an error reading it: " + filename); } - // Create a vector with enough space for the file data. std::vector fileData((size_t)size); - // Go back to the start of the file and read all its contents. - file.seekg(0, std::ios::beg); + // Since the file was closed after getting its size, + // we need to open it again. + std::ifstream file(filename, std::ios::binary); file.read(reinterpret_cast(fileData.data()), size); return fileData; From 2b60a6cab865f64ed76c27d9b3cd22eae8017983 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Tue, 3 Oct 2023 16:10:29 +0000 Subject: [PATCH 34/43] use get_file_size instead of passing number of public inputs --- barretenberg/cpp/src/barretenberg/bb/main.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/bb/main.cpp b/barretenberg/cpp/src/barretenberg/bb/main.cpp index 30f9f8331bc..06b15c70923 100644 --- a/barretenberg/cpp/src/barretenberg/bb/main.cpp +++ b/barretenberg/cpp/src/barretenberg/bb/main.cpp @@ -39,11 +39,12 @@ acir_format::acir_format get_constraint_system(std::string const& bytecode_path) return acir_format::circuit_buf_to_acir_format(bytecode); } -std::vector read_public_inputs(std::string const& public_inputs_path, size_t num_public_inputs) +std::vector read_public_inputs(std::string const& public_inputs_path) { // If the number of public inputs is 0, then read_file will trigger a failure - // because the file will be empty. - if (num_public_inputs == 0) { + // because the file will be empty. We therefore check if the file is empty + // before trying to read it. + if (get_file_size(public_inputs_path) <= 0) { return {}; } return read_file(public_inputs_path); @@ -166,7 +167,7 @@ bool verify(const std::string& proof_path, bool recursive, const std::string& vk acir_composer->load_verification_key(barretenberg::srs::get_crs_factory(), std::move(vk_data)); auto public_inputs_path = public_inputs_path_from_proof_path(proof_path); - std::vector public_inputs = read_public_inputs(public_inputs_path, vk_data.num_public_inputs); + std::vector public_inputs = read_public_inputs(public_inputs_path); auto proof = read_file(proof_path); auto verified = acir_composer->verify_proof(public_inputs, proof, recursive); @@ -252,16 +253,14 @@ void contract(const std::string& output_path, const std::string& vk_path) * * * @param proof_path Path to the file containing the serialized proof - * @param vk_path Path to the file containing the serialized verification key * @param output_path Path to write the proof to */ -void proofAsFields(const std::string& proof_path, std::string const& vk_path, const std::string& output_path) +void proofAsFields(const std::string& proof_path, const std::string& output_path) { - auto vk_data = from_buffer(read_file(vk_path)); auto public_inputs_path = public_inputs_path_from_proof_path(proof_path); auto proof = read_file(proof_path); - std::vector public_inputs = read_public_inputs(public_inputs_path, vk_data.num_public_inputs); + std::vector public_inputs = read_public_inputs(public_inputs_path); auto acir_composer = new acir_proofs::AcirComposer(MAX_CIRCUIT_SIZE, verbose); auto data = acir_composer->serialize_proof_into_fields(public_inputs, proof); @@ -402,7 +401,7 @@ int main(int argc, char* argv[]) writeVk(bytecode_path, output_path); } else if (command == "proof_as_fields") { std::string output_path = getOption(args, "-o", proof_path + "_fields.json"); - proofAsFields(proof_path, vk_path, output_path); + proofAsFields(proof_path, output_path); } else if (command == "vk_as_fields") { std::string output_path = getOption(args, "-o", vk_path + "_fields.json"); vkAsFields(vk_path, output_path); From 24d0bc6880f06c24bab0c5036051d45c06567662 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Tue, 3 Oct 2023 16:10:49 +0000 Subject: [PATCH 35/43] do not pass vk to proofAsFields --- barretenberg/acir_tests/flows/all_cmds.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/barretenberg/acir_tests/flows/all_cmds.sh b/barretenberg/acir_tests/flows/all_cmds.sh index dda4353fb6f..8f815ef883f 100755 --- a/barretenberg/acir_tests/flows/all_cmds.sh +++ b/barretenberg/acir_tests/flows/all_cmds.sh @@ -20,5 +20,5 @@ $BIN verify -k vk -p proof $FLAGS # Grep to determine success. $BIN contract -k vk $BFLAG -o - | grep "Verification Key Hash" > /dev/null # Use jq to determine success. -$BIN proof_as_fields -k vk -p proof -o - | jq . > /dev/null +$BIN proof_as_fields -p proof -o - | jq . > /dev/null $BIN vk_as_fields -k vk -o - > vk_as_fields | jq . > /dev/null \ No newline at end of file From 7f539fe6b6147ea95375b932efe3a0052a9d3ab4 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Tue, 3 Oct 2023 16:19:10 +0000 Subject: [PATCH 36/43] use methods in container to refactor --- .../dsl/acir_proofs/acir_composer.cpp | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp index dc89782008a..91d2b4360bc 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp @@ -1,4 +1,5 @@ #include "acir_composer.hpp" +#include "barretenberg/common/container.hpp" #include "barretenberg/common/serialize.hpp" #include "barretenberg/common/throw_or_abort.hpp" #include "barretenberg/dsl/acir_format/acir_format.hpp" @@ -26,18 +27,21 @@ AcirComposer::AcirComposer(size_t size_hint, bool verbose) * @param k - The number of 32 bytes to remove * @return std::pair, std::vector> */ -std::pair, std::vector> splitVector(std::vector& original, uint32_t k) +std::pair, std::vector> splitVector(const std::vector& original, uint32_t k) { uint32_t elementsToRemove = 32 * k; if (original.size() < elementsToRemove) { throw_or_abort("Not enough elements in the original vector"); } - auto elementsToRemoveLong = static_cast(elementsToRemove); - std::vector removed(original.begin(), original.begin() + elementsToRemoveLong); - original = std::vector(original.begin() + elementsToRemoveLong, original.end()); - return { original, removed }; + std::vector removed = slice(original, 0, elementsToRemove); + std::vector rest = slice(original, elementsToRemove); + + return { + rest, + removed, + }; } /** @@ -61,14 +65,7 @@ std::pair, std::vector> split_proof(std::vector concatenateVectors(const std::vector& firstVector, const std::vector& secondVector) { - std::vector concatenatedVector; - - concatenatedVector.reserve(firstVector.size() + secondVector.size()); - - concatenatedVector.insert(concatenatedVector.end(), firstVector.begin(), firstVector.end()); - concatenatedVector.insert(concatenatedVector.end(), secondVector.begin(), secondVector.end()); - - return concatenatedVector; + return join({ firstVector, secondVector }); } void AcirComposer::create_circuit(acir_format::acir_format& constraint_system) From c62a6cdb85ecac24e9de4f70385387ec35d08623 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Tue, 3 Oct 2023 16:21:58 +0000 Subject: [PATCH 37/43] change c-style cast and add back comment --- barretenberg/cpp/src/barretenberg/bb/file_io.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/barretenberg/cpp/src/barretenberg/bb/file_io.hpp b/barretenberg/cpp/src/barretenberg/bb/file_io.hpp index 06699968d8e..ed3eac37ac1 100644 --- a/barretenberg/cpp/src/barretenberg/bb/file_io.hpp +++ b/barretenberg/cpp/src/barretenberg/bb/file_io.hpp @@ -6,6 +6,7 @@ inline std::streamsize get_file_size(const std::string& filename) { + // Open the file in binary mode and move to the end. std::ifstream file(filename, std::ios::binary | std::ios::ate); if (!file) { throw std::runtime_error("Unable to open file: " + filename); @@ -22,7 +23,7 @@ inline std::vector read_file(const std::string& filename) throw std::runtime_error("File is empty or there's an error reading it: " + filename); } - std::vector fileData((size_t)size); + std::vector fileData(static_cast(size)); // Since the file was closed after getting its size, // we need to open it again. From 21e67bd7937e21e3631c3063350003549ae4f50d Mon Sep 17 00:00:00 2001 From: kevaundray Date: Tue, 3 Oct 2023 16:24:28 +0000 Subject: [PATCH 38/43] tellg returns 0 for empty file, if there was an error reading it, read_file will throw --- barretenberg/cpp/src/barretenberg/bb/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/barretenberg/cpp/src/barretenberg/bb/main.cpp b/barretenberg/cpp/src/barretenberg/bb/main.cpp index 06b15c70923..c416000022f 100644 --- a/barretenberg/cpp/src/barretenberg/bb/main.cpp +++ b/barretenberg/cpp/src/barretenberg/bb/main.cpp @@ -44,7 +44,7 @@ std::vector read_public_inputs(std::string const& public_inputs_path) // If the number of public inputs is 0, then read_file will trigger a failure // because the file will be empty. We therefore check if the file is empty // before trying to read it. - if (get_file_size(public_inputs_path) <= 0) { + if (get_file_size(public_inputs_path) == 0) { return {}; } return read_file(public_inputs_path); From 4a369abbc2c4a6907a4f16d15e1f62d983ebec67 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Tue, 3 Oct 2023 16:31:36 +0000 Subject: [PATCH 39/43] inline splitVector and remove concatenate vector --- .../dsl/acir_proofs/acir_composer.cpp | 48 +++++-------------- 1 file changed, 12 insertions(+), 36 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp index 91d2b4360bc..67f94d22ae0 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp @@ -18,32 +18,6 @@ AcirComposer::AcirComposer(size_t size_hint, bool verbose) , verbose_(verbose) {} -/** - * @brief Splits a vector into two vectors, - * the first containing the first 32 * k elements, and the second containing - * the rest. - * - * @param original - The original vector to split - * @param k - The number of 32 bytes to remove - * @return std::pair, std::vector> - */ -std::pair, std::vector> splitVector(const std::vector& original, uint32_t k) -{ - uint32_t elementsToRemove = 32 * k; - - if (original.size() < elementsToRemove) { - throw_or_abort("Not enough elements in the original vector"); - } - - std::vector removed = slice(original, 0, elementsToRemove); - std::vector rest = slice(original, elementsToRemove); - - return { - rest, - removed, - }; -} - /** * @brief Splits the proof into two vectors. * @@ -58,14 +32,17 @@ std::pair, std::vector> splitVector(const std::vec std::pair, std::vector> split_proof(std::vector& proof, uint32_t num_public_inputs) { - auto [proof_without_public_inputs, public_inputs] = splitVector(proof, num_public_inputs); - return { public_inputs, proof_without_public_inputs }; -} -std::vector concatenateVectors(const std::vector& firstVector, - const std::vector& secondVector) -{ - return join({ firstVector, secondVector }); + uint32_t elementsToRemove = 32 * num_public_inputs; + + if (proof.size() < elementsToRemove) { + throw_or_abort("Not enough elements in the original vector"); + } + + std::vector public_inputs = slice(proof, 0, elementsToRemove); + std::vector proof_without_public_inputs = slice(proof, elementsToRemove); + + return { public_inputs, proof_without_public_inputs }; } void AcirComposer::create_circuit(acir_format::acir_format& constraint_system) @@ -174,8 +151,7 @@ bool AcirComposer::verify_proof(std::vector const& public_inputs, std::vector const& proof_without_public_inputs, bool is_recursive) { - - auto proof = concatenateVectors(public_inputs, proof_without_public_inputs); + auto proof = join({ public_inputs, proof_without_public_inputs }); if (!verification_key_) { vinfo("computing verification key..."); @@ -214,7 +190,7 @@ std::vector AcirComposer::serialize_proof_into_fields(std::vec std::vector const& proof) { auto num_inner_public_inputs = public_inputs.size() / 32; - transcript::StandardTranscript transcript(concatenateVectors(public_inputs, proof), + transcript::StandardTranscript transcript(join({ public_inputs, proof }), acir_format::Composer::create_manifest(num_inner_public_inputs), transcript::HashType::PlookupPedersenBlake3s, 16); From c7b510295a2f9e6fbc40f7068581288f70f90a26 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Tue, 3 Oct 2023 16:38:26 +0000 Subject: [PATCH 40/43] reduce diff --- .../src/barretenberg/dsl/acir_proofs/c_bind.cpp | 17 +++++++++++------ .../src/barretenberg/dsl/acir_proofs/c_bind.hpp | 4 ++-- barretenberg/exports.json | 4 ++-- barretenberg/ts/src/barretenberg_api/index.ts | 6 +++--- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp index 32774970437..019654e7980 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp @@ -11,9 +11,13 @@ #include #include -WASM_EXPORT void acir_get_circuit_sizes(uint8_t const* acir_vec, uint32_t* exact, uint32_t* total, uint32_t* subgroup) +WASM_EXPORT void acir_get_circuit_sizes(uint8_t const* constraint_system_buf, + uint32_t* exact, + uint32_t* total, + uint32_t* subgroup) { - auto constraint_system = acir_format::circuit_buf_to_acir_format(from_buffer>(acir_vec)); + auto constraint_system = + acir_format::circuit_buf_to_acir_format(from_buffer>(constraint_system_buf)); auto composer = acir_format::create_circuit(constraint_system, 1 << 19); *exact = htonl((uint32_t)composer.get_num_gates()); *total = htonl((uint32_t)composer.get_total_circuit_size()); @@ -39,15 +43,16 @@ WASM_EXPORT void acir_init_proving_key(in_ptr acir_composer_ptr, uint8_t const* } WASM_EXPORT void acir_create_proof(in_ptr acir_composer_ptr, - uint8_t const* acir_vec, - uint8_t const* witness_vec, + uint8_t const* constraint_system_buf, + uint8_t const* witness_buf, bool const* is_recursive, uint8_t** out_public_inputs, uint8_t** out_proof) { auto acir_composer = reinterpret_cast(*acir_composer_ptr); - auto constraint_system = acir_format::circuit_buf_to_acir_format(from_buffer>(acir_vec)); - auto witness = acir_format::witness_buf_to_witness_data(from_buffer>(witness_vec)); + auto constraint_system = + acir_format::circuit_buf_to_acir_format(from_buffer>(constraint_system_buf)); + auto witness = acir_format::witness_buf_to_witness_data(from_buffer>(witness_buf)); auto [public_inputs, proof] = acir_composer->create_proof(barretenberg::srs::get_crs_factory(), constraint_system, witness, *is_recursive); diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.hpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.hpp index 9f07e531618..1e35c2469a9 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.hpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.hpp @@ -27,8 +27,8 @@ WASM_EXPORT void acir_init_proving_key(in_ptr acir_composer_ptr, uint8_t const* * to pass it in everytime. */ WASM_EXPORT void acir_create_proof(in_ptr acir_composer_ptr, - uint8_t const* acir_vec, - uint8_t const* witness_vec, + uint8_t const* constraint_system_buf, + uint8_t const* witness_buf, bool const* is_recursive, uint8_t** out_public_inputs, uint8_t** out_proof); diff --git a/barretenberg/exports.json b/barretenberg/exports.json index 2114c83aa99..3e8198d90b3 100644 --- a/barretenberg/exports.json +++ b/barretenberg/exports.json @@ -636,11 +636,11 @@ "type": "in_ptr" }, { - "name": "acir_vec", + "name": "constraint_system_buf", "type": "const uint8_t *" }, { - "name": "witness_vec", + "name": "witness_buf", "type": "const uint8_t *" }, { diff --git a/barretenberg/ts/src/barretenberg_api/index.ts b/barretenberg/ts/src/barretenberg_api/index.ts index 1b784020f8f..d03afe99227 100644 --- a/barretenberg/ts/src/barretenberg_api/index.ts +++ b/barretenberg/ts/src/barretenberg_api/index.ts @@ -261,13 +261,13 @@ export class BarretenbergApi { async acirCreateProof( acirComposerPtr: Ptr, - acirVec: Uint8Array, - witnessVec: Uint8Array, + constraintSystemBuf: Uint8Array, + witnessBuf: Uint8Array, isRecursive: boolean, ): Promise<[Uint8Array, Uint8Array]> { const result = await this.binder.callWasmExport( 'acir_create_proof', - [acirComposerPtr, acirVec, witnessVec, isRecursive], + [acirComposerPtr, constraintSystemBuf, witnessBuf, isRecursive], [BufferDeserializer(), BufferDeserializer()], ); return result as any; From fc6aefd0da0c18179d1bd0ace26c53e9643584c8 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Tue, 3 Oct 2023 16:43:18 +0000 Subject: [PATCH 41/43] better variable name --- .../src/barretenberg/dsl/acir_proofs/acir_composer.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp index 67f94d22ae0..4cb26dba7cc 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp @@ -33,14 +33,14 @@ std::pair, std::vector> split_proof(std::vector public_inputs = slice(proof, 0, elementsToRemove); - std::vector proof_without_public_inputs = slice(proof, elementsToRemove); + std::vector public_inputs = slice(proof, 0, numBytesToRemove); + std::vector proof_without_public_inputs = slice(proof, numBytesToRemove); return { public_inputs, proof_without_public_inputs }; } From 036d1f3e1d7b9f6d156d38aa670814a63a2ed24c Mon Sep 17 00:00:00 2001 From: kevaundray Date: Thu, 12 Oct 2023 09:25:45 +0000 Subject: [PATCH 42/43] fix merge --- barretenberg/cpp/src/barretenberg/bb/file_io.hpp | 9 +++++++-- barretenberg/cpp/src/barretenberg/bb/main.cpp | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/bb/file_io.hpp b/barretenberg/cpp/src/barretenberg/bb/file_io.hpp index 9df86e8742c..b9c85ea93ce 100644 --- a/barretenberg/cpp/src/barretenberg/bb/file_io.hpp +++ b/barretenberg/cpp/src/barretenberg/bb/file_io.hpp @@ -16,9 +16,14 @@ inline std::streamsize get_file_size(const std::string& filename) return file.tellg(); } -inline std::vector read_file(const std::string& filename) +inline std::vector read_file(const std::string& filename, size_t bytes = 0) { - std::streamsize size = get_file_size(filename); + std::streamsize size; + if (bytes == 0) { + size = get_file_size(filename); + } else { + size = (std::streamsize)bytes; + } if (size <= 0) { throw std::runtime_error("File is empty or there's an error reading it: " + filename); diff --git a/barretenberg/cpp/src/barretenberg/bb/main.cpp b/barretenberg/cpp/src/barretenberg/bb/main.cpp index 6035f6e8a7d..313f479eec0 100644 --- a/barretenberg/cpp/src/barretenberg/bb/main.cpp +++ b/barretenberg/cpp/src/barretenberg/bb/main.cpp @@ -178,7 +178,7 @@ bool verify(const std::string& proof_path, bool recursive, const std::string& vk auto public_inputs_path = public_inputs_path_from_proof_path(proof_path); std::vector public_inputs = read_public_inputs(public_inputs_path); auto proof = read_file(proof_path); - auto verified = acir_composer->verify_proof(public_inputs, proof, recursive); + auto verified = acir_composer.verify_proof(public_inputs, proof, recursive); vinfo("verified: ", verified); @@ -272,7 +272,7 @@ void proofAsFields(const std::string& proof_path, const std::string& output_path std::vector public_inputs = read_public_inputs(public_inputs_path); auto acir_composer = init(); - auto data = acir_composer->serialize_proof_into_fields(public_inputs, proof); + auto data = acir_composer.serialize_proof_into_fields(public_inputs, proof); auto json = format("[", join(map(data, [](auto fr) { return format("\"", fr, "\""); })), "]"); if (output_path == "-") { From fe507335a05b5530386aad174364c4a06dfe7afd Mon Sep 17 00:00:00 2001 From: kevaundray Date: Thu, 12 Oct 2023 12:33:32 +0000 Subject: [PATCH 43/43] deduplicate public witness indices --- barretenberg/cpp/src/barretenberg/bb/main.cpp | 21 ++++++++++++++++++- .../dsl/acir_proofs/acir_composer.cpp | 4 ++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/bb/main.cpp b/barretenberg/cpp/src/barretenberg/bb/main.cpp index 313f479eec0..c982004b2f6 100644 --- a/barretenberg/cpp/src/barretenberg/bb/main.cpp +++ b/barretenberg/cpp/src/barretenberg/bb/main.cpp @@ -48,7 +48,26 @@ acir_format::WitnessVector get_witness(std::string const& witness_path) acir_format::acir_format get_constraint_system(std::string const& bytecode_path) { auto bytecode = get_bytecode(bytecode_path); - return acir_format::circuit_buf_to_acir_format(bytecode); + auto constraint_system = acir_format::circuit_buf_to_acir_format(bytecode); + + auto remove_duplicates = [](std::vector& vec) { + std::sort(vec.begin(), vec.end()); + auto lastUnique = std::unique(vec.begin(), vec.end()); + vec.erase(lastUnique, vec.end()); + }; + + // The deserialized acir can have duplicate public inputs. + // + // This can happen if for example, the input to a function is a public input and + // then this input is returned as a public output. + // + // One solution for this, is to have ACIR introduce the concept of a public output + // which would be different to the public input. A public input would be the public values + // which are inputted to the program and the public output would be the public values + // which are returned when the program terminates. + remove_duplicates(constraint_system.public_inputs); + + return constraint_system; } std::vector read_public_inputs(std::string const& public_inputs_path) diff --git a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp index f0195d5943a..b59e5e186d8 100644 --- a/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp +++ b/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp @@ -132,8 +132,8 @@ bool AcirComposer::verify_proof(std::vector const& public_inputs, vinfo("done."); } - // Hack. Shouldn't need to do this. - builder_.public_inputs.resize(public_inputs.size() / 32); + auto numPublicInputs = public_inputs.size() / 32; + builder_.public_inputs.resize(numPublicInputs); if (is_recursive) { auto verifier = composer.create_verifier(builder_);