Skip to content

Commit

Permalink
Merge branch 'master' into phated/lsp-wasm
Browse files Browse the repository at this point in the history
* master:
  chore: refactor integration test (#2901)
  feat(acvm_js): export black box solver functions (#2812)
  chore: add standard linting setup to all crates (#2900)
  chore: use Noir number in release PR (#2905)
  chore: clippy fixes (#2897)
  chore: use workspace rust version in `backend-interface` crate (#2896)
  • Loading branch information
TomAFrench committed Sep 29, 2023
2 parents 4901267 + 00ee14f commit 627c390
Show file tree
Hide file tree
Showing 35 changed files with 706 additions and 162 deletions.
2 changes: 1 addition & 1 deletion acvm-repo/acir/src/circuit/black_box_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ mod tests {
assert_eq!(
resolved_func, bb_func,
"BlackBoxFunc::lookup returns unexpected BlackBoxFunc"
)
);
}
}
}
4 changes: 2 additions & 2 deletions acvm-repo/acir/src/circuit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl std::fmt::Display for Circuit {
write_public_inputs(f, &self.return_values)?;

for opcode in &self.opcodes {
writeln!(f, "{opcode}")?
writeln!(f, "{opcode}")?;
}
Ok(())
}
Expand Down Expand Up @@ -236,7 +236,7 @@ mod tests {
}

let (circ, got_circ) = read_write(circuit);
assert_eq!(circ, got_circ)
assert_eq!(circ, got_circ);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions acvm-repo/acir/src/circuit/opcodes/black_box_function_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ fn get_inputs_string(inputs: &[FunctionInput]) -> String {
result += &format!("(_{}, num_bits: {})", inp.witness.witness_index(), inp.num_bits);
// Add a comma, unless it is the last entry
if index != inputs.len() - 1 {
result += ", "
result += ", ";
}
}
result
Expand Down Expand Up @@ -358,7 +358,7 @@ fn get_outputs_string(outputs: &[Witness]) -> String {
result += &format!("_{}", output.witness_index());
// Add a comma, unless it is the last entry
if index != outputs.len() - 1 {
result += ", "
result += ", ";
}
}
result
Expand Down
4 changes: 3 additions & 1 deletion acvm-repo/acir/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![warn(unused_crate_dependencies)]
#![forbid(unsafe_code)]
#![warn(unreachable_pub)]
#![warn(clippy::semicolon_if_nothing_returned)]
#![cfg_attr(not(test), warn(unused_crate_dependencies, unused_extern_crates))]

// Arbitrary Circuit Intermediate Representation

Expand Down
6 changes: 3 additions & 3 deletions acvm-repo/acir/src/native_types/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Expression {

/// Adds a new linear term to the `Expression`.
pub fn push_addition_term(&mut self, coefficient: FieldElement, variable: Witness) {
self.linear_combinations.push((coefficient, variable))
self.linear_combinations.push((coefficient, variable));
}

/// Adds a new quadratic term to the `Expression`.
Expand All @@ -82,7 +82,7 @@ impl Expression {
lhs: Witness,
rhs: Witness,
) {
self.mul_terms.push((coefficient, lhs, rhs))
self.mul_terms.push((coefficient, lhs, rhs));
}

/// Returns `true` if the expression represents a constant polynomial.
Expand Down Expand Up @@ -394,5 +394,5 @@ fn add_mul_smoketest() {
linear_combinations: vec![(FieldElement::from(40u128), Witness(4))],
q_c: FieldElement::from(10u128)
}
)
);
}
10 changes: 5 additions & 5 deletions acvm-repo/acir_field/src/generic_ark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl<F: PrimeField> std::fmt::Debug for FieldElement<F> {

impl<F: PrimeField> std::hash::Hash for FieldElement<F> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
state.write(&self.to_be_bytes())
state.write(&self.to_be_bytes());
}
}

Expand Down Expand Up @@ -295,7 +295,7 @@ impl<F: PrimeField> FieldElement<F> {
fn byte_to_bit(byte: u8) -> Vec<bool> {
let mut bits = Vec::with_capacity(8);
for index in (0..=7).rev() {
bits.push((byte & (1 << index)) >> index == 1)
bits.push((byte & (1 << index)) >> index == 1);
}
bits
}
Expand Down Expand Up @@ -341,7 +341,7 @@ impl<F: PrimeField> FieldElement<F> {

let and_byte_arr: Vec<_> = lhs_bytes
.into_iter()
.zip(rhs_bytes.into_iter())
.zip(rhs_bytes)
.map(|(lhs, rhs)| if is_xor { lhs ^ rhs } else { lhs & rhs })
.collect();

Expand Down Expand Up @@ -433,13 +433,13 @@ mod tests {
for (i, string) in hex_strings.into_iter().enumerate() {
let minus_i_field_element =
-crate::generic_ark::FieldElement::<ark_bn254::Fr>::from(i as i128);
assert_eq!(minus_i_field_element.to_hex(), string)
assert_eq!(minus_i_field_element.to_hex(), string);
}
}
#[test]
fn max_num_bits_smoke() {
let max_num_bits_bn254 = crate::generic_ark::FieldElement::<ark_bn254::Fr>::max_num_bits();
assert_eq!(max_num_bits_bn254, 254)
assert_eq!(max_num_bits_bn254, 254);
}
}

Expand Down
4 changes: 3 additions & 1 deletion acvm-repo/acir_field/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![warn(unused_crate_dependencies)]
#![forbid(unsafe_code)]
#![warn(unreachable_pub)]
#![warn(clippy::semicolon_if_nothing_returned)]
#![cfg_attr(not(test), warn(unused_crate_dependencies, unused_extern_crates))]

cfg_if::cfg_if! {
if #[cfg(feature = "bn254")] {
Expand Down
8 changes: 4 additions & 4 deletions acvm-repo/acvm/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub fn compile(
for opcode in acir.opcodes {
match opcode {
Opcode::Arithmetic(arith_expr) => {
opcodes.push(Opcode::Arithmetic(GeneralOptimizer::optimize(arith_expr)))
opcodes.push(Opcode::Arithmetic(GeneralOptimizer::optimize(arith_expr)));
}
other_opcode => opcodes.push(other_opcode),
};
Expand Down Expand Up @@ -168,7 +168,7 @@ pub fn compile(
match func {
acir::circuit::opcodes::BlackBoxFuncCall::AND { output, .. }
| acir::circuit::opcodes::BlackBoxFuncCall::XOR { output, .. } => {
transformer.mark_solvable(*output)
transformer.mark_solvable(*output);
}
acir::circuit::opcodes::BlackBoxFuncCall::RANGE { .. } => (),
acir::circuit::opcodes::BlackBoxFuncCall::SHA256 { outputs, .. }
Expand All @@ -192,7 +192,7 @@ pub fn compile(
}
| acir::circuit::opcodes::BlackBoxFuncCall::Pedersen { outputs, .. } => {
transformer.mark_solvable(outputs.0);
transformer.mark_solvable(outputs.1)
transformer.mark_solvable(outputs.1);
}
acir::circuit::opcodes::BlackBoxFuncCall::HashToField128Security {
output,
Expand All @@ -201,7 +201,7 @@ pub fn compile(
| acir::circuit::opcodes::BlackBoxFuncCall::EcdsaSecp256k1 { output, .. }
| acir::circuit::opcodes::BlackBoxFuncCall::EcdsaSecp256r1 { output, .. }
| acir::circuit::opcodes::BlackBoxFuncCall::SchnorrVerify { output, .. } => {
transformer.mark_solvable(*output)
transformer.mark_solvable(*output);
}
}

Expand Down
2 changes: 1 addition & 1 deletion acvm-repo/acvm/src/compiler/optimizers/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn simplify_mul_terms(mut gate: Expression) -> Expression {

// Canonicalize the ordering of the multiplication, lets just order by variable name
for (scale, w_l, w_r) in gate.mul_terms.clone().into_iter() {
let mut pair = vec![w_l, w_r];
let mut pair = [w_l, w_r];
// Sort using rust sort algorithm
pair.sort();

Expand Down
2 changes: 1 addition & 1 deletion acvm-repo/acvm/src/compiler/optimizers/redundant_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,6 @@ mod tests {
let acir_opcode_positions = circuit.opcodes.iter().enumerate().map(|(i, _)| i).collect();
let optimizer = RangeOptimizer::new(circuit);
let (optimized_circuit, _) = optimizer.replace_redundant_ranges(acir_opcode_positions);
assert_eq!(optimized_circuit.opcodes.len(), 5)
assert_eq!(optimized_circuit.opcodes.len(), 5);
}
}
4 changes: 3 additions & 1 deletion acvm-repo/acvm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![warn(unused_crate_dependencies)]
#![forbid(unsafe_code)]
#![warn(unreachable_pub)]
#![warn(clippy::semicolon_if_nothing_returned)]
#![cfg_attr(not(test), warn(unused_crate_dependencies, unused_extern_crates))]

pub mod compiler;
pub mod pwg;
Expand Down
2 changes: 1 addition & 1 deletion acvm-repo/acvm/src/pwg/blackbox/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
use acir::{circuit::opcodes::FunctionInput, native_types::WitnessMap};

pub(super) fn solve_range_opcode(
initial_witness: &mut WitnessMap,
initial_witness: &WitnessMap,
input: &FunctionInput,
) -> Result<(), OpcodeResolutionError> {
let w_value = witness_to_value(initial_witness, input.witness)?;
Expand Down
4 changes: 2 additions & 2 deletions acvm-repo/acvm/src/pwg/brillig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ impl BrilligSolver {
for output in &brillig.outputs {
match output {
BrilligOutputs::Simple(witness) => {
insert_value(witness, FieldElement::zero(), initial_witness)?
insert_value(witness, FieldElement::zero(), initial_witness)?;
}
BrilligOutputs::Array(witness_arr) => {
for witness in witness_arr {
insert_value(witness, FieldElement::zero(), initial_witness)?
insert_value(witness, FieldElement::zero(), initial_witness)?;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion acvm-repo/acvm/src/pwg/directives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub(super) fn solve_directives(
None => FieldElement::zero(),
};

insert_value(witness, value, initial_witness)?
insert_value(witness, value, initial_witness)?;
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion acvm-repo/acvm/src/pwg/directives/sorting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ mod tests {
result.push(*out1.last().unwrap());
result.push(*out2.last().unwrap());
} else {
result.push(*out2.last().unwrap())
result.push(*out2.last().unwrap());
}
result
}
Expand Down
100 changes: 100 additions & 0 deletions acvm-repo/acvm_js/src/black_box_solvers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use js_sys::JsString;
use wasm_bindgen::prelude::*;

use crate::js_witness_map::{field_element_to_js_string, js_value_to_field_element};
use acvm::FieldElement;

/// Performs a bitwise AND operation between `lhs` and `rhs`
#[wasm_bindgen]
pub fn and(lhs: JsString, rhs: JsString) -> JsString {
let lhs = js_value_to_field_element(lhs.into()).unwrap();
let rhs = js_value_to_field_element(rhs.into()).unwrap();
let result = lhs.and(&rhs, FieldElement::max_num_bits());
field_element_to_js_string(&result)
}

/// Performs a bitwise XOR operation between `lhs` and `rhs`
#[wasm_bindgen]
pub fn xor(lhs: JsString, rhs: JsString) -> JsString {
let lhs = js_value_to_field_element(lhs.into()).unwrap();
let rhs = js_value_to_field_element(rhs.into()).unwrap();
let result = lhs.xor(&rhs, FieldElement::max_num_bits());
field_element_to_js_string(&result)
}

/// Calculates the SHA256 hash of the input bytes
#[wasm_bindgen]
pub fn sha256(inputs: &[u8]) -> Vec<u8> {
acvm::blackbox_solver::sha256(inputs).unwrap().into()
}

/// Calculates the Blake2s256 hash of the input bytes
#[wasm_bindgen]
pub fn blake2s256(inputs: &[u8]) -> Vec<u8> {
acvm::blackbox_solver::blake2s(inputs).unwrap().into()
}

/// Calculates the Keccak256 hash of the input bytes
#[wasm_bindgen]
pub fn keccak256(inputs: &[u8]) -> Vec<u8> {
acvm::blackbox_solver::keccak256(inputs).unwrap().into()
}

/// Calculates the Blake2s256 hash of the input bytes and represents these as a single field element.
// #[wasm_bindgen]
// pub fn hash_to_field_128_security(inputs: Vec<JsString>) -> JsString {
// let input_bytes: Vec<u8> = inputs
// .into_iter()
// .flat_map(|field_string| {
// let field_element = js_value_to_field_element(field_string.into()).unwrap();
// witness_assignment.fetch_nearest_bytes(FieldElement::max_num_bits());
// })
// .collect();
// field_element_to_js_string(
// &acvm::blackbox_solver::hash_to_field_128_security(&input_bytes).unwrap(),
// )
// }

/// Verifies a ECDSA signature over the secp256k1 curve.
#[wasm_bindgen]
pub fn ecdsa_secp256k1_verify(
hashed_msg: &[u8],
public_key_x_bytes: &[u8],
public_key_y_bytes: &[u8],
signature: &[u8],
) -> bool {
let public_key_x_bytes: &[u8; 32] = public_key_x_bytes.try_into().unwrap();
let public_key_y_bytes: &[u8; 32] = public_key_y_bytes.try_into().unwrap();
let signature: &[u8; 64] = signature.try_into().unwrap();

acvm::blackbox_solver::ecdsa_secp256k1_verify(
hashed_msg,
public_key_x_bytes,
public_key_y_bytes,
signature,
)
.unwrap()
.into()
}

/// Verifies a ECDSA signature over the secp256r1 curve.
#[wasm_bindgen]
pub fn ecdsa_secp256r1_verify(
hashed_msg: &[u8],
public_key_x_bytes: &[u8],
public_key_y_bytes: &[u8],
signature: &[u8],
) -> bool {
let public_key_x_bytes: &[u8; 32] = public_key_x_bytes.try_into().unwrap();
let public_key_y_bytes: &[u8; 32] = public_key_y_bytes.try_into().unwrap();
let signature: &[u8; 64] = signature.try_into().unwrap();

acvm::blackbox_solver::ecdsa_secp256r1_verify(
hashed_msg,
public_key_x_bytes,
public_key_y_bytes,
signature,
)
.unwrap()
.into()
}
6 changes: 5 additions & 1 deletion acvm-repo/acvm_js/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// #![warn(unused_crate_dependencies, unused_extern_crates)]
#![forbid(unsafe_code)]
#![warn(unreachable_pub)]
#![warn(clippy::semicolon_if_nothing_returned)]
#![cfg_attr(not(test), warn(unused_crate_dependencies, unused_extern_crates))]

// TODO: Absence of per package targets
// https://doc.rust-lang.org/cargo/reference/unstable.html#per-package-target
Expand All @@ -15,7 +17,9 @@ cfg_if::cfg_if! {
mod logging;
mod public_witness;
mod js_execution_error;
mod black_box_solvers;

pub use black_box_solvers::{and, xor, sha256, blake2s256, keccak256, ecdsa_secp256k1_verify, ecdsa_secp256r1_verify};
pub use build_info::build_info;
pub use compression::{compress_witness, decompress_witness};
pub use execute::{execute_circuit, execute_circuit_with_black_box_solver, create_black_box_solver};
Expand Down
Loading

0 comments on commit 627c390

Please sign in to comment.