From 9263984cd1ab4e21ec1a43010890e09c446b91a0 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Tue, 20 Jun 2023 21:11:32 +0700 Subject: [PATCH] feat: allow calling pedersen opcode with non-zero domain separator (#58) * chore: bump bberg commit to match `noir-lang/noir` * fix: support usage of pedersen with non-zero `domain_separator` * chore: fix minimum amount of memory for bberg wasm --- flake.lock | 6 +++--- src/barretenberg/mod.rs | 8 +++++++- src/barretenberg/pedersen.rs | 17 ++++++++++++++--- src/execute.rs | 13 ++++++++----- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/flake.lock b/flake.lock index 1e64250c9..5a611c88d 100644 --- a/flake.lock +++ b/flake.lock @@ -10,11 +10,11 @@ ] }, "locked": { - "lastModified": 1683909802, - "narHash": "sha256-2CL8NYKLYwwy6n0RyldvH86ULgrSvfzHrgq2Qf0ZUkE=", + "lastModified": 1686952051, + "narHash": "sha256-mpsCXzHMaqSveQcD/SA9k3NH4pF167KqR5/oYJJjKE8=", "owner": "AztecProtocol", "repo": "barretenberg", - "rev": "97c9bc72aebab850b4a647d6e6cc50085226eafb", + "rev": "65e651d04c6092cb5ca079cd9e12ed9b5846fa3a", "type": "github" }, "original": { diff --git a/src/barretenberg/mod.rs b/src/barretenberg/mod.rs index efae7ad06..0d707ba81 100644 --- a/src/barretenberg/mod.rs +++ b/src/barretenberg/mod.rs @@ -102,6 +102,12 @@ mod wasm { } } + impl From for WASMValue { + fn from(value: u32) -> Self { + WASMValue(Some(Value::I32(value as i32))) + } + } + impl From for WASMValue { fn from(value: i32) -> Self { WASMValue(Some(Value::I32(value))) @@ -241,7 +247,7 @@ mod wasm { debug!("> Will Load black box functions vendor binary"); let mut store = Store::default(); - let mem_type = MemoryType::new(22, None, false); + let mem_type = MemoryType::new(23, None, false); let memory = Memory::new(&mut store, mem_type).unwrap(); let function_env = FunctionEnv::new(&mut store, memory.clone()); diff --git a/src/barretenberg/pedersen.rs b/src/barretenberg/pedersen.rs index 47eeb4b85..41e29b25d 100644 --- a/src/barretenberg/pedersen.rs +++ b/src/barretenberg/pedersen.rs @@ -3,16 +3,27 @@ use acvm::FieldElement; use super::{Assignments, Barretenberg, Error, FIELD_BYTES}; pub(crate) trait Pedersen { - fn encrypt(&self, inputs: Vec) -> Result<(FieldElement, FieldElement), Error>; + fn encrypt( + &self, + inputs: Vec, + hash_index: u32, + ) -> Result<(FieldElement, FieldElement), Error>; } impl Pedersen for Barretenberg { - fn encrypt(&self, inputs: Vec) -> Result<(FieldElement, FieldElement), Error> { + fn encrypt( + &self, + inputs: Vec, + hash_index: u32, + ) -> Result<(FieldElement, FieldElement), Error> { let input_buf = Assignments::from(inputs).to_bytes(); let input_ptr = self.allocate(&input_buf)?; let result_ptr: usize = 0; - self.call_multiple("pedersen_plookup_commit", vec![&input_ptr, &result_ptr.into()])?; + self.call_multiple( + "pedersen_plookup_commit_with_hash_index", + vec![&input_ptr, &result_ptr.into(), &hash_index.into()], + )?; let result_bytes: [u8; 2 * FIELD_BYTES] = self.read_memory(result_ptr); let (point_x_bytes, point_y_bytes) = result_bytes.split_at(FIELD_BYTES); diff --git a/src/execute.rs b/src/execute.rs index b7efee91d..7e1ae27f2 100644 --- a/src/execute.rs +++ b/src/execute.rs @@ -105,17 +105,20 @@ impl PartialWitnessGenerator for SimulatedBackend { &self, initial_witness: &mut WitnessMap, inputs: &[FunctionInput], - // Assumed to be `0` - _domain_separator: u32, + domain_separator: u32, outputs: &[Witness], ) -> Result { let scalars: Result, _> = inputs.iter().map(|input| witness_to_value(initial_witness, input.witness)).collect(); let scalars: Vec<_> = scalars?.into_iter().cloned().collect(); - let (res_x, res_y) = self.blackbox_vendor.encrypt(scalars).map_err(|err| { - OpcodeResolutionError::BlackBoxFunctionFailed(BlackBoxFunc::Pedersen, err.to_string()) - })?; + let (res_x, res_y) = + self.blackbox_vendor.encrypt(scalars, domain_separator).map_err(|err| { + OpcodeResolutionError::BlackBoxFunctionFailed( + BlackBoxFunc::Pedersen, + err.to_string(), + ) + })?; insert_value(&outputs[0], res_x, initial_witness)?; insert_value(&outputs[1], res_y, initial_witness)?; Ok(OpcodeResolution::Solved)