Skip to content

Commit

Permalink
feat: allow calling pedersen opcode with non-zero domain separator (#58)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
TomAFrench authored Jun 20, 2023
1 parent 96bfad1 commit 9263984
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/barretenberg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ mod wasm {
}
}

impl From<u32> for WASMValue {
fn from(value: u32) -> Self {
WASMValue(Some(Value::I32(value as i32)))
}
}

impl From<i32> for WASMValue {
fn from(value: i32) -> Self {
WASMValue(Some(Value::I32(value)))
Expand Down Expand Up @@ -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());
Expand Down
17 changes: 14 additions & 3 deletions src/barretenberg/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@ use acvm::FieldElement;
use super::{Assignments, Barretenberg, Error, FIELD_BYTES};

pub(crate) trait Pedersen {
fn encrypt(&self, inputs: Vec<FieldElement>) -> Result<(FieldElement, FieldElement), Error>;
fn encrypt(
&self,
inputs: Vec<FieldElement>,
hash_index: u32,
) -> Result<(FieldElement, FieldElement), Error>;
}

impl Pedersen for Barretenberg {
fn encrypt(&self, inputs: Vec<FieldElement>) -> Result<(FieldElement, FieldElement), Error> {
fn encrypt(
&self,
inputs: Vec<FieldElement>,
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);
Expand Down
13 changes: 8 additions & 5 deletions src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OpcodeResolution, OpcodeResolutionError> {
let scalars: Result<Vec<_>, _> =
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)
Expand Down

0 comments on commit 9263984

Please sign in to comment.