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: allow calling pedersen opcode with non-zero domain separator #58

Merged
merged 3 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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