From 4c04a79e6d2b0115f3b4526c60f9f7dae8b464ae Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Mon, 22 May 2023 15:53:35 +0100 Subject: [PATCH 1/3] feat: Add serde to `ConstraintSystem` types (#196) chore: Add serde to ConstraintSystem types --- Cargo.lock | 10 +++++++++ Cargo.toml | 1 + src/barretenberg_structures.rs | 38 ++++++++++++++++++++-------------- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index de104a3a..53c00ebd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -62,6 +62,7 @@ dependencies = [ "reqwest", "rust-embed", "serde", + "serde-big-array", "thiserror", "tokio", "wasmer", @@ -1967,6 +1968,15 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde-big-array" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11fc7cc2c76d73e0f27ee52abbd64eec84d46f370c88371120433196934e4b7f" +dependencies = [ + "serde", +] + [[package]] name = "serde_bytes" version = "0.11.9" diff --git a/Cargo.toml b/Cargo.toml index 4ff06804..daae9fb3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ bincode = "1.3.3" bytesize = "1.2" reqwest = { version = "0.11.16", default-features = false, features = ["rustls-tls"] } serde = { version = "1.0.136", features = ["derive"] } +serde-big-array = "0.5.1" thiserror = "1.0.21" # Native diff --git a/src/barretenberg_structures.rs b/src/barretenberg_structures.rs index 394f6822..55ca562a 100644 --- a/src/barretenberg_structures.rs +++ b/src/barretenberg_structures.rs @@ -3,10 +3,12 @@ use acvm::acir::circuit::{Circuit, Opcode}; use acvm::acir::native_types::Expression; use acvm::acir::BlackBoxFunc; use acvm::FieldElement; +use serde::{Deserialize, Serialize}; +use serde_big_array::BigArray; use crate::Error; -#[derive(Debug, Default, Clone)] +#[derive(Debug, Default, Clone, Serialize, Deserialize)] pub(crate) struct Assignments(Vec); // This is a separate impl so the constructor can get the wasm_bindgen macro in the future @@ -51,7 +53,7 @@ impl From> for Assignments { } } -#[derive(Clone, Hash, Debug)] +#[derive(Clone, Hash, Debug, Serialize, Deserialize)] pub(crate) struct Constraint { pub(crate) a: i32, pub(crate) b: i32, @@ -112,7 +114,7 @@ impl Constraint { } } -#[derive(Clone, Hash, Debug)] +#[derive(Clone, Hash, Debug, Serialize, Deserialize)] pub(crate) struct RangeConstraint { pub(crate) a: i32, pub(crate) num_bits: i32, @@ -128,9 +130,11 @@ impl RangeConstraint { buffer } } -#[derive(Clone, Hash, Debug)] +#[derive(Clone, Hash, Debug, Serialize, Deserialize)] pub(crate) struct EcdsaConstraint { pub(crate) hashed_message: Vec, + // Required until Serde adopts const generics: https://github.com/serde-rs/serde/issues/1937 + #[serde(with = "BigArray")] pub(crate) signature: [i32; 64], pub(crate) public_key_x: [i32; 32], pub(crate) public_key_y: [i32; 32], @@ -170,9 +174,11 @@ impl EcdsaConstraint { buffer } } -#[derive(Clone, Hash, Debug)] +#[derive(Clone, Hash, Debug, Serialize, Deserialize)] pub(crate) struct SchnorrConstraint { pub(crate) message: Vec, + // Required until Serde adopts const generics: https://github.com/serde-rs/serde/issues/1937 + #[serde(with = "BigArray")] pub(crate) signature: [i32; 64], pub(crate) public_key_x: i32, pub(crate) public_key_y: i32, @@ -202,7 +208,7 @@ impl SchnorrConstraint { buffer } } -#[derive(Clone, Hash, Debug)] +#[derive(Clone, Hash, Debug, Serialize, Deserialize)] pub(crate) struct ComputeMerkleRootConstraint { pub(crate) hash_path: Vec, pub(crate) leaf: i32, @@ -229,7 +235,7 @@ impl ComputeMerkleRootConstraint { } } -#[derive(Clone, Hash, Debug)] +#[derive(Clone, Hash, Debug, Serialize, Deserialize)] pub(crate) struct Sha256Constraint { pub(crate) inputs: Vec<(i32, i32)>, pub(crate) result: [i32; 32], @@ -255,7 +261,7 @@ impl Sha256Constraint { buffer } } -#[derive(Clone, Hash, Debug)] +#[derive(Clone, Hash, Debug, Serialize, Deserialize)] pub(crate) struct Blake2sConstraint { pub(crate) inputs: Vec<(i32, i32)>, pub(crate) result: [i32; 32], @@ -281,7 +287,7 @@ impl Blake2sConstraint { buffer } } -#[derive(Clone, Hash, Debug)] +#[derive(Clone, Hash, Debug, Serialize, Deserialize)] pub(crate) struct HashToFieldConstraint { pub(crate) inputs: Vec<(i32, i32)>, pub(crate) result: i32, @@ -304,7 +310,7 @@ impl HashToFieldConstraint { } } -#[derive(Clone, Hash, Debug)] +#[derive(Clone, Hash, Debug, Serialize, Deserialize)] pub(crate) struct Keccak256Constraint { pub(crate) inputs: Vec<(i32, i32)>, pub(crate) result: [i32; 32], @@ -331,7 +337,7 @@ impl Keccak256Constraint { } } -#[derive(Clone, Hash, Debug)] +#[derive(Clone, Hash, Debug, Serialize, Deserialize)] pub(crate) struct PedersenConstraint { pub(crate) inputs: Vec, pub(crate) result_x: i32, @@ -354,7 +360,7 @@ impl PedersenConstraint { buffer } } -#[derive(Clone, Hash, Debug)] +#[derive(Clone, Hash, Debug, Serialize, Deserialize)] pub(crate) struct FixedBaseScalarMulConstraint { pub(crate) scalar: i32, pub(crate) pubkey_x: i32, @@ -373,7 +379,7 @@ impl FixedBaseScalarMulConstraint { } } -#[derive(Clone, Hash, Debug)] +#[derive(Clone, Hash, Debug, Serialize, Deserialize)] pub(crate) struct LogicConstraint { pub(crate) a: i32, pub(crate) b: i32, @@ -415,7 +421,7 @@ impl LogicConstraint { } } -#[derive(Clone, Hash, Debug, Default)] +#[derive(Clone, Hash, Debug, Default, Serialize, Deserialize)] pub(crate) struct ConstraintSystem { var_num: u32, public_inputs: Vec, @@ -659,7 +665,7 @@ impl ConstraintSystem { } } -#[derive(Clone, Hash, Debug)] +#[derive(Clone, Hash, Debug, Serialize, Deserialize)] pub(crate) struct MemOpBarretenberg { pub(crate) is_store: i8, pub(crate) index: Constraint, @@ -677,7 +683,7 @@ impl MemOpBarretenberg { } } -#[derive(Clone, Hash, Debug)] +#[derive(Clone, Hash, Debug, Serialize, Deserialize)] pub(crate) struct BlockConstraint { pub(crate) init: Vec, pub(crate) trace: Vec, From 0f978af9c64276ceb1a4d243e86efe32b2c177e2 Mon Sep 17 00:00:00 2001 From: Koby Hall <102518238+kobyhallx@users.noreply.github.com> Date: Mon, 22 May 2023 18:20:49 +0200 Subject: [PATCH 2/3] chore: Release 0.2.0 (#186) --- CHANGELOG.md | 14 ++++++++++++++ Cargo.lock | 2 +- Cargo.toml | 2 +- flake.nix | 2 +- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08127c0f..9a78e3bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,19 @@ # Changelog +## [0.2.0](https://github.com/noir-lang/acvm-backend-barretenberg/compare/v0.1.2...v0.2.0) (2023-05-22) + + +### ⚠ BREAKING CHANGES + +* Update to acvm 0.12.0 ([#165](https://github.com/noir-lang/acvm-backend-barretenberg/issues/165)) +* Add serialization logic for RAM and ROM opcodes ([#153](https://github.com/noir-lang/acvm-backend-barretenberg/issues/153)) + +### Features + +* Add serde to `ConstraintSystem` types ([#196](https://github.com/noir-lang/acvm-backend-barretenberg/issues/196)) ([4c04a79](https://github.com/noir-lang/acvm-backend-barretenberg/commit/4c04a79e6d2b0115f3b4526c60f9f7dae8b464ae)) +* Add serialization logic for RAM and ROM opcodes ([#153](https://github.com/noir-lang/acvm-backend-barretenberg/issues/153)) ([3d3847d](https://github.com/noir-lang/acvm-backend-barretenberg/commit/3d3847de70e74a8f65c64e165ad15ae3d31f5350)) +* Update to acvm 0.12.0 ([#165](https://github.com/noir-lang/acvm-backend-barretenberg/issues/165)) ([d613c79](https://github.com/noir-lang/acvm-backend-barretenberg/commit/d613c79584a599f4adbd11d2ce3b61403c185b73)) + ## [0.1.2](https://github.com/noir-lang/acvm-backend-barretenberg/compare/v0.1.1...v0.1.2) (2023-05-11) diff --git a/Cargo.lock b/Cargo.lock index 53c00ebd..3dc28d62 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,7 +50,7 @@ dependencies = [ [[package]] name = "acvm-backend-barretenberg" -version = "0.1.2" +version = "0.2.0" dependencies = [ "acvm", "barretenberg-sys", diff --git a/Cargo.toml b/Cargo.toml index daae9fb3..141577b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "acvm-backend-barretenberg" description = "An ACVM backend which allows proving/verifying ACIR circuits against Aztec Lab's Barretenberg library." -version = "0.1.2" +version = "0.2.0" authors = ["The Noir Team "] edition = "2021" rust-version = "1.66" diff --git a/flake.nix b/flake.nix index a477a70b..59c2415a 100644 --- a/flake.nix +++ b/flake.nix @@ -110,7 +110,7 @@ commonArgs = { pname = "acvm-backend-barretenberg"; # x-release-please-start-version - version = "0.1.2"; + version = "0.2.0"; # x-release-please-end src = pkgs.lib.cleanSourceWith { From f6134b7b502cb74882300b0046ab91ab000daf3c Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Mon, 22 May 2023 17:30:57 +0100 Subject: [PATCH 3/3] fix: Add or cleanup implementations for JS target (#199) --- src/lib.rs | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6690b437..b4f4bbaf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -165,7 +165,6 @@ mod native { #[cfg(not(feature = "native"))] mod wasm { - use std::cell::Cell; use wasmer::{imports, Function, Instance, Memory, MemoryType, Module, Store, Value}; use super::{Barretenberg, Error, FeatureError}; @@ -281,11 +280,10 @@ mod wasm { #[cfg(feature = "js")] { - let view: js_sys::Uint8Array = memory.uint8view(); + let view = memory.uint8view(); for (byte_id, cell_id) in (offset..(offset + arr.len())).enumerate() { view.set_index(cell_id as u32, arr[byte_id]) } - return; } #[cfg(not(feature = "js"))] @@ -311,12 +309,15 @@ mod wasm { let end = start + length; #[cfg(feature = "js")] - return memory.uint8view().to_vec()[start..end].to_vec(); + return memory + .uint8view() + .subarray(start as u32, end as u32) + .to_vec(); #[cfg(not(feature = "js"))] return memory.view()[start..end] .iter() - .map(|cell: &Cell| cell.get()) + .map(|cell| cell.get()) .collect(); } @@ -432,14 +433,29 @@ mod wasm { let mut ptr_end = 0; let byte_view = env.memory.uint8view(); + #[cfg(feature = "js")] + for (i, cell) in byte_view.to_vec()[ptr as usize..].iter().enumerate() { + if cell != &0_u8 { + ptr_end = i; + } else { + break; + } + } + + #[cfg(not(feature = "js"))] for (i, cell) in byte_view[ptr as usize..].iter().enumerate() { - if cell != &Cell::new(0) { + if cell.get() != 0 { ptr_end = i; } else { break; } } + #[cfg(feature = "js")] + let str_vec: Vec<_> = + byte_view.to_vec()[ptr as usize..=(ptr + ptr_end as i32) as usize].to_vec(); + + #[cfg(not(feature = "js"))] let str_vec: Vec<_> = byte_view[ptr as usize..=(ptr + ptr_end as i32) as usize] .iter() .cloned()