Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Separate public inputs from proof in acir composer #2618

Closed
wants to merge 45 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
5035024
temporary: add methods to split the proof and take in public inputs s…
kevaundray Oct 2, 2023
2b438db
modify c-binds to use those methods
kevaundray Oct 2, 2023
934a578
modify browser test app to account for separate public inputs
kevaundray Oct 2, 2023
75b9dc7
modify bb binary to account for separate public inputs
kevaundray Oct 2, 2023
65acca8
modify bb.js node binary to account for separate public inputs
kevaundray Oct 2, 2023
902520a
exports.json file was regenerated
kevaundray Oct 2, 2023
8dadcfa
regenerate api file -- unfortunately my linter changed lines in the f…
kevaundray Oct 2, 2023
6376452
formatter
kevaundray Oct 2, 2023
06e2ad7
add code to not read the public inputs file if there are no public in…
kevaundray Oct 2, 2023
ab683a1
temporarily switch args
kevaundray Oct 2, 2023
adc2357
put publicInputs in serializeProofIntoFields
kevaundray Oct 2, 2023
4a026aa
modify binary to account for new cbind api
kevaundray Oct 2, 2023
f6af120
always put public inputs vector first
kevaundray Oct 2, 2023
06ea909
regenerate exports.json
kevaundray Oct 2, 2023
93218e7
conditionally read the public inputs
kevaundray Oct 2, 2023
7aa3855
typo
kevaundray Oct 2, 2023
0c6ca7f
yarn
kevaundray Oct 2, 2023
0df922c
put in separate PR
kevaundray Oct 2, 2023
0e3bfa7
cleanup bb
kevaundray Oct 2, 2023
b463a8a
cleanup bb.js binary
kevaundray Oct 2, 2023
23d2067
remove _splitted methods
kevaundray Oct 2, 2023
4461139
modify bb binary; since we removed _splitted methods
kevaundray Oct 2, 2023
c2a6773
Update barretenberg/acir_tests/run_acir_tests_browser.sh
kevaundray Oct 2, 2023
b4233da
formatting fix
kevaundray Oct 2, 2023
362552a
change outwards facing API to not mention proofWithoutPublicInputs
kevaundray Oct 3, 2023
8576164
using proof instead of proof_without_public_inputs in cbinds
kevaundray Oct 3, 2023
51714c8
redo bindings
kevaundray Oct 3, 2023
a811d36
modify binaries
kevaundray Oct 3, 2023
2d07550
linter
kevaundray Oct 3, 2023
0b49345
multi:
kevaundray Oct 3, 2023
b669013
bb: -public_inputs -> _public_inputs
kevaundray Oct 3, 2023
aeea25e
lint
kevaundray Oct 3, 2023
dab3ec3
add get_file_size method
kevaundray Oct 3, 2023
2b60a6c
use get_file_size instead of passing number of public inputs
kevaundray Oct 3, 2023
24d0bc6
do not pass vk to proofAsFields
kevaundray Oct 3, 2023
7f539fe
use methods in container to refactor
kevaundray Oct 3, 2023
c62a6cd
change c-style cast and add back comment
kevaundray Oct 3, 2023
21e67bd
tellg returns 0 for empty file, if there was an error reading it, rea…
kevaundray Oct 3, 2023
4a369ab
inline splitVector and remove concatenate vector
kevaundray Oct 3, 2023
c7b5102
reduce diff
kevaundray Oct 3, 2023
00a19ba
Merge branch 'master' into kw/separate-public-inputs
kevaundray Oct 3, 2023
fc6aefd
better variable name
kevaundray Oct 3, 2023
0cafaa8
Merge remote-tracking branch 'origin/master' into kw/separate-public-…
kevaundray Oct 12, 2023
036d1f3
fix merge
kevaundray Oct 12, 2023
fe50733
deduplicate public witness indices
kevaundray Oct 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions barretenberg/acir_tests/browser-test-app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
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();
Expand Down
22 changes: 14 additions & 8 deletions barretenberg/cpp/src/barretenberg/bb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
vinfo("proof and public inputs written to: ", outputProofPath);
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -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<plonk::verification_key_data>(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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,56 @@ std::vector<uint8_t> 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<uint8_t>, std::vector<uint8_t>>
*/
std::pair<std::vector<uint8_t>, std::vector<uint8_t>> splitVector(std::vector<uint8_t>& 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<long>(elementsToRemove);
std::vector<uint8_t> removed(original.begin(), original.begin() + elementsToRemoveLong);
original = std::vector<uint8_t>(original.begin() + elementsToRemoveLong, original.end());

return { original, removed };
}

std::vector<uint8_t> concatenateVectors(const std::vector<uint8_t>& firstVector,
const std::vector<uint8_t>& secondVector)
{
std::vector<uint8_t> 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<uint8_t>, std::vector<uint8_t>> AcirComposer::create_proof_public_splitted(
std::shared_ptr<barretenberg::srs::factories::CrsFactory<curve::BN254>> const& crs_factory,
acir_format::acir_format& constraint_system,
acir_format::WitnessVector& witness,
bool is_recursive)
{
auto proof = create_proof(crs_factory, constraint_system, witness, is_recursive);
auto num_public_inputs = static_cast<uint32_t>(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<proof_system::plonk::verification_key> AcirComposer::init_verification_key()
{
vinfo("computing verification key...");
Expand Down Expand Up @@ -137,6 +187,14 @@ bool AcirComposer::verify_proof(std::vector<uint8_t> const& proof, bool is_recur
}
}

bool AcirComposer::verify_proof_splitted(std::vector<uint8_t> const& public_inputs,
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
std::vector<uint8_t> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,22 @@ class AcirComposer {
acir_format::WitnessVector& witness,
bool is_recursive);

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

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

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

bool verify_proof(std::vector<uint8_t> const& proof, bool is_recursive);
bool verify_proof_splitted(std::vector<uint8_t> const& public_inputs,
std::vector<uint8_t> const& proof,
bool is_recursive);

std::string get_solidity_verifier();
size_t get_exact_circuit_size() { return exact_circuit_size_; };
Expand Down
16 changes: 10 additions & 6 deletions barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_proofs::AcirComposer*>(*acir_composer_ptr);
auto constraint_system = acir_format::circuit_buf_to_acir_format(from_buffer<std::vector<uint8_t>>(acir_vec));
auto witness = acir_format::witness_buf_to_witness_data(from_buffer<std::vector<uint8_t>>(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)
Expand All @@ -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_proofs::AcirComposer*>(*acir_composer_ptr);
auto proof = from_buffer<std::vector<uint8_t>>(proof_buf);
*result = acir_composer->verify_proof(proof, *is_recursive);
auto public_inputs = from_buffer<std::vector<uint8_t>>(public_inputs_buf);
auto proof_without_public_inputs = from_buffer<std::vector<uint8_t>>(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)
Expand Down
8 changes: 5 additions & 3 deletions barretenberg/cpp/src/barretenberg/dsl/acir_proofs/c_bind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
26 changes: 11 additions & 15 deletions barretenberg/exports.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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 *"
},
{
Expand All @@ -662,7 +650,11 @@
],
"outArgs": [
{
"name": "out",
"name": "out_public_inputs",
"type": "uint8_t **"
},
{
"name": "out_proof_without_public_inputs",
"type": "uint8_t **"
}
],
Expand Down Expand Up @@ -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 *"
Expand Down
Loading