From a20e3b5256d18130854ba3c3910a5dd11ffef07f Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Mon, 24 Apr 2023 20:05:37 -0400 Subject: [PATCH 1/9] serialization bindings for recursion and is_recursive flag on new_proof and verify_proof --- build.rs | 2 ++ src/composer.rs | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/build.rs b/build.rs index b73cfcd..f23f93a 100644 --- a/build.rs +++ b/build.rs @@ -80,6 +80,8 @@ fn main() -> Result<()> { .allowlist_function("acir_proofs_get_total_circuit_size") .allowlist_function("acir_proofs_init_proving_key") .allowlist_function("acir_proofs_init_verification_key") + .allowlist_function("acir_serialize_verification_key_into_field_elements") + .allowlist_function("acir_serialize_proof_into_field_elements") .allowlist_function("acir_proofs_new_proof") .allowlist_function("acir_proofs_verify_proof") .allowlist_function("pedersen_plookup_compress_fields") diff --git a/src/composer.rs b/src/composer.rs index 3906744..f2678f6 100644 --- a/src/composer.rs +++ b/src/composer.rs @@ -48,6 +48,32 @@ pub unsafe fn init_verification_key( ) } +pub unsafe fn serialize_verification_key_into_field_elements( + g2_ptr: &[u8], + vk_buf: &[u8], + serialized_vk_buf: *mut *mut u8, + serialized_vk_hash_buf: *mut *mut u8, +) -> usize { + acir_serialize_verification_key_into_field_elements( + g2_ptr.as_ptr() as *const u8, + vk_buf.as_ptr() as *const u8, + serialized_vk_buf as *const *mut u8 as *mut *mut u8, + serialized_vk_hash_buf as *const *mut u8 as *mut *mut u8, + ) +} + +pub unsafe fn serialize_proof_into_field_elements( + proof: &[u8], + serialized_proof_data_buf: *mut *mut u8, + proof_data_length: usize, +) -> usize { + acir_serialize_proof_into_field_elements( + proof.as_ptr() as *const u8, + serialized_proof_data_buf, + proof_data_length, + ) +} + /// # Safety /// pippenger must point to a valid Pippenger object pub unsafe fn create_proof_with_pk( @@ -57,6 +83,7 @@ pub unsafe fn create_proof_with_pk( cs_ptr: &[u8], witness_ptr: &[u8], proof_data_ptr: *mut *mut u8, + is_recursive: bool, ) -> usize { let cs_ptr = cs_ptr.as_ptr() as *const u8; let pk_ptr = pk_ptr.as_ptr() as *const u8; @@ -67,12 +94,19 @@ pub unsafe fn create_proof_with_pk( cs_ptr, witness_ptr.as_ptr(), proof_data_ptr as *const *mut u8 as *mut *mut u8, + is_recursive, ) } /// # Safety /// cs_prt must point to a valid constraints system structure of type standard_format -pub unsafe fn verify_with_vk(g2_ptr: &[u8], vk_ptr: &[u8], cs_ptr: &[u8], proof: &[u8]) -> bool { +pub unsafe fn verify_with_vk( + g2_ptr: &[u8], + vk_ptr: &[u8], + cs_ptr: &[u8], + proof: &[u8], + is_recursive: bool, +) -> bool { let proof_ptr = proof.as_ptr() as *const u8; acir_proofs_verify_proof( @@ -81,5 +115,6 @@ pub unsafe fn verify_with_vk(g2_ptr: &[u8], vk_ptr: &[u8], cs_ptr: &[u8], proof: cs_ptr.as_ptr() as *const u8, proof_ptr as *mut u8, proof.len() as u32, + is_recursive, ) } From 78ce8a7ecc2689fbd91788ee0cbc6f2258835f14 Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Thu, 27 Apr 2023 23:30:45 -0400 Subject: [PATCH 2/9] new recursion method verify_proof --- build.rs | 1 + src/lib.rs | 1 + src/recursion.rs | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+) create mode 100644 src/recursion.rs diff --git a/build.rs b/build.rs index f23f93a..cf859d5 100644 --- a/build.rs +++ b/build.rs @@ -84,6 +84,7 @@ fn main() -> Result<()> { .allowlist_function("acir_serialize_proof_into_field_elements") .allowlist_function("acir_proofs_new_proof") .allowlist_function("acir_proofs_verify_proof") + .allowlist_function("acir_proofs_verify_recursive_proof") .allowlist_function("pedersen_plookup_compress_fields") .allowlist_function("pedersen_plookup_compress") .allowlist_function("pedersen_plookup_commit") diff --git a/src/lib.rs b/src/lib.rs index e671b2a..278fce7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,7 @@ pub mod composer; pub mod pedersen; pub mod pippenger; pub mod schnorr; +pub mod recursion; #[cfg(test)] mod tests { diff --git a/src/recursion.rs b/src/recursion.rs new file mode 100644 index 0000000..14e1963 --- /dev/null +++ b/src/recursion.rs @@ -0,0 +1,20 @@ +use crate::*; + +pub unsafe fn verify_proof( + vk_fields_ptr: &[u8], + proof_fields: &[u8], + public_inputs: &[u8], + input_aggregation_obj_ptr: &[u8], + output_aggregation_obj_ptr: *mut *mut u8, +) -> usize { + + acir_proofs_verify_recursive_proof( + proof_fields.as_ptr() as *const u8, + proof_fields.len() as u32, + vk_fields_ptr.as_ptr() as *const u8, + vk_fields_ptr.len() as u32, + public_inputs.as_ptr() as *const u8, + input_aggregation_obj_ptr.as_ptr() as *const u8, + output_aggregation_obj_ptr, + ) +} \ No newline at end of file From 42aa36a7af5bd3a52da6f043d27e16c3b96dd4b7 Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Mon, 8 May 2023 13:07:20 -0400 Subject: [PATCH 3/9] update bindings for arbitrary recursion --- src/composer.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/composer.rs b/src/composer.rs index f2678f6..19311ac 100644 --- a/src/composer.rs +++ b/src/composer.rs @@ -66,11 +66,13 @@ pub unsafe fn serialize_proof_into_field_elements( proof: &[u8], serialized_proof_data_buf: *mut *mut u8, proof_data_length: usize, + num_public_inputs: usize, ) -> usize { acir_serialize_proof_into_field_elements( proof.as_ptr() as *const u8, serialized_proof_data_buf, proof_data_length, + num_public_inputs, ) } From 49907db86cfe1268b88116297d6dcd249851c3ce Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Mon, 15 May 2023 13:24:30 -0400 Subject: [PATCH 4/9] update recursion verify_proof to take num_public_inputs --- src/recursion.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/recursion.rs b/src/recursion.rs index 14e1963..0baa4d6 100644 --- a/src/recursion.rs +++ b/src/recursion.rs @@ -3,17 +3,16 @@ use crate::*; pub unsafe fn verify_proof( vk_fields_ptr: &[u8], proof_fields: &[u8], - public_inputs: &[u8], + num_public_inputs: u32, input_aggregation_obj_ptr: &[u8], output_aggregation_obj_ptr: *mut *mut u8, ) -> usize { - acir_proofs_verify_recursive_proof( proof_fields.as_ptr() as *const u8, proof_fields.len() as u32, vk_fields_ptr.as_ptr() as *const u8, vk_fields_ptr.len() as u32, - public_inputs.as_ptr() as *const u8, + num_public_inputs, input_aggregation_obj_ptr.as_ptr() as *const u8, output_aggregation_obj_ptr, ) From 5788fd0ac5125fedb1ab00e486bc98b098be1d6f Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Wed, 24 May 2023 10:54:36 -0400 Subject: [PATCH 5/9] verify_recursive_proof --- src/lib.rs | 2 +- src/recursion.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 278fce7..5298ca2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,8 +12,8 @@ pub mod blake2s; pub mod composer; pub mod pedersen; pub mod pippenger; -pub mod schnorr; pub mod recursion; +pub mod schnorr; #[cfg(test)] mod tests { diff --git a/src/recursion.rs b/src/recursion.rs index 0baa4d6..263ae3e 100644 --- a/src/recursion.rs +++ b/src/recursion.rs @@ -16,4 +16,4 @@ pub unsafe fn verify_proof( input_aggregation_obj_ptr.as_ptr() as *const u8, output_aggregation_obj_ptr, ) -} \ No newline at end of file +} From 6826700593db2fee91b62cb98206a330a5c64fa2 Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Wed, 24 May 2023 11:32:46 -0400 Subject: [PATCH 6/9] update reference to test CI --- .github/workflows/test.yml | 2 +- README.md | 2 +- flake.lock | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b7a5f6b..a1f6ea7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -54,7 +54,7 @@ jobs: with: repository: AztecProtocol/barretenberg path: barretenberg - ref: ecb61292c96c3b1fc673bcd96920cd2f00fe28b9 + ref: 209667624f706be9106acab2cc0f7bfbdc7fa793 - name: Setup Linux environment if: matrix.os == 'ubuntu-latest' diff --git a/README.md b/README.md index 7d5fa35..4b49cbe 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ to install everything globally, you'll need: Linker provided by Clang, but might need to be installed via `apt install lld`. -4. `barretenberg` (preferably at commit `ecb61292c96c3b1fc673bcd96920cd2f00fe28b9`) +4. `barretenberg` (preferably at commit `209667624f706be9106acab2cc0f7bfbdc7fa793`) Needs to be built and installed following the instructions [in the README](https://github.com/AztecProtocol/barretenberg#getting-started). Note that barretenberg has its own [dependencies](https://github.com/AztecProtocol/barretenberg#dependencies) that will need to be installed, such as `cmake` and `ninja`. diff --git a/flake.lock b/flake.lock index 9a83e84..5fffd69 100644 --- a/flake.lock +++ b/flake.lock @@ -10,11 +10,11 @@ ] }, "locked": { - "lastModified": 1682094239, - "narHash": "sha256-dJ+Ww1IxdI37XnWMDOJQbzOonzR/AQWJQfL2xkAc1Js=", + "lastModified": 1684859506, + "narHash": "sha256-OqI5pg6yZLz9YRkRNky3Ezh2kWpwvCdKWPH6ad+DWFU=", "owner": "AztecProtocol", "repo": "barretenberg", - "rev": "ecb61292c96c3b1fc673bcd96920cd2f00fe28b9", + "rev": "209667624f706be9106acab2cc0f7bfbdc7fa793", "type": "github" }, "original": { From c4e1ce5117985268a1921a3d2478687e76b1832f Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Wed, 24 May 2023 12:02:24 -0400 Subject: [PATCH 7/9] Safety section for recursion methods --- src/composer.rs | 61 ++++++++++++++++++++++++++---------------------- src/recursion.rs | 5 ++++ 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/src/composer.rs b/src/composer.rs index 19311ac..8c9b7a4 100644 --- a/src/composer.rs +++ b/src/composer.rs @@ -48,34 +48,6 @@ pub unsafe fn init_verification_key( ) } -pub unsafe fn serialize_verification_key_into_field_elements( - g2_ptr: &[u8], - vk_buf: &[u8], - serialized_vk_buf: *mut *mut u8, - serialized_vk_hash_buf: *mut *mut u8, -) -> usize { - acir_serialize_verification_key_into_field_elements( - g2_ptr.as_ptr() as *const u8, - vk_buf.as_ptr() as *const u8, - serialized_vk_buf as *const *mut u8 as *mut *mut u8, - serialized_vk_hash_buf as *const *mut u8 as *mut *mut u8, - ) -} - -pub unsafe fn serialize_proof_into_field_elements( - proof: &[u8], - serialized_proof_data_buf: *mut *mut u8, - proof_data_length: usize, - num_public_inputs: usize, -) -> usize { - acir_serialize_proof_into_field_elements( - proof.as_ptr() as *const u8, - serialized_proof_data_buf, - proof_data_length, - num_public_inputs, - ) -} - /// # Safety /// pippenger must point to a valid Pippenger object pub unsafe fn create_proof_with_pk( @@ -120,3 +92,36 @@ pub unsafe fn verify_with_vk( is_recursive, ) } + +/// # Safety +/// vk_buf must point to a valid verification key previously exported by this composer +pub unsafe fn serialize_verification_key_into_field_elements( + g2_ptr: &[u8], + vk_buf: &[u8], + serialized_vk_buf: *mut *mut u8, + serialized_vk_hash_buf: *mut *mut u8, +) -> usize { + acir_serialize_verification_key_into_field_elements( + g2_ptr.as_ptr() as *const u8, + vk_buf.as_ptr() as *const u8, + serialized_vk_buf as *const *mut u8 as *mut *mut u8, + serialized_vk_hash_buf as *const *mut u8 as *mut *mut u8, + ) +} + +/// # Safety +/// proof must point to a valid proof previously generated by this composer +/// The proof must also have its public inputs prepended in order for the call to be valid +pub unsafe fn serialize_proof_into_field_elements( + proof: &[u8], + serialized_proof_data_buf: *mut *mut u8, + proof_data_length: usize, + num_public_inputs: usize, +) -> usize { + acir_serialize_proof_into_field_elements( + proof.as_ptr() as *const u8, + serialized_proof_data_buf, + proof_data_length, + num_public_inputs, + ) +} diff --git a/src/recursion.rs b/src/recursion.rs index 263ae3e..649a5fd 100644 --- a/src/recursion.rs +++ b/src/recursion.rs @@ -1,5 +1,10 @@ use crate::*; +/// # Safety +/// vk_fields_ptr and proof_fields must point to a valid recursion format structure +/// laid out in the acir format recursion constraint +/// input_aggregation_obj_ptr must point to a valid aggregation object whose +/// structure is also laid out in the acir format recursion constraint pub unsafe fn verify_proof( vk_fields_ptr: &[u8], proof_fields: &[u8], From 1105ea80e25e9922934566a0843f9c99aa579026 Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Wed, 24 May 2023 12:59:11 -0400 Subject: [PATCH 8/9] update pedersen tests to reflect changes to pedersen in bberg --- src/pedersen.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/pedersen.rs b/src/pedersen.rs index 2981bee..a839daf 100644 --- a/src/pedersen.rs +++ b/src/pedersen.rs @@ -69,17 +69,17 @@ mod tests { Test { input_left: f_zero, input_right: f_one, - expected_hex: "11831f49876c313f2a9ec6d8d521c7ce0b6311c852117e340bfe27fd1ac096ef", + expected_hex: "0c5e1ddecd49de44ed5e5798d3f6fb7c71fe3d37f5bee8664cf88a445b5ba0af", }, Test { input_left: f_one, input_right: f_one, - expected_hex: "1044a769e185fcdf077c8289a6bf87c5c77ff9561cab69d39fadd90a07ee4af4", + expected_hex: "0e1793a0c122887bcb53c84776f4704c26bc093b25eaa9c7847a672c65e314ae", }, Test { input_left: f_one, input_right: f_zero, - expected_hex: "17d213c8fe83e89a2f3190933d437a3e231124e0383e6dc6a7b6e6358833e427", + expected_hex: "0c93b3f27730b2e331e634af15bc9d5a769688921f30b36ca926b35a96b3306c", }, ]; @@ -100,8 +100,8 @@ mod tests { let inputs: Vec<[u8; 32]> = vec![f_zero, f_one]; let (x, y) = encrypt(&inputs); - let expected_x = "11831f49876c313f2a9ec6d8d521c7ce0b6311c852117e340bfe27fd1ac096ef"; - let expected_y = "0ecf9d98be4597a88c46a7e0fa8836b57a7dcb41ee30f8d8787b11cc259c83fa"; + let expected_x = "0c5e1ddecd49de44ed5e5798d3f6fb7c71fe3d37f5bee8664cf88a445b5ba0af"; + let expected_y = "230294a041e26fe80b827c2ef5cb8784642bbaa83842da2714d62b1f3c4f9752"; assert_eq!(expected_x, hex::encode(x)); assert_eq!(expected_y, hex::encode(y)); } From d51e03a9e40e8a620659accd5f5b262f8330edfb Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Wed, 24 May 2023 13:54:33 -0400 Subject: [PATCH 9/9] missed tests::pedersen update --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 5298ca2..71c7ba0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,7 +29,7 @@ mod tests { f_one[31] = 1; let got = pedersen::compress_native(&f_zero, &f_one); assert_eq!( - "11831f49876c313f2a9ec6d8d521c7ce0b6311c852117e340bfe27fd1ac096ef", + "0c5e1ddecd49de44ed5e5798d3f6fb7c71fe3d37f5bee8664cf88a445b5ba0af", hex::encode(got) ); }