Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
chore(acvm)!: Make internals of ACVM private (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Jun 9, 2023
1 parent bc9c0bc commit c902a01
Show file tree
Hide file tree
Showing 19 changed files with 83 additions and 79 deletions.
19 changes: 10 additions & 9 deletions acvm/src/compiler.rs → acvm/src/compiler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
// The various passes that we can use over ACIR
pub mod optimizers;
pub mod transformers;

use crate::Language;
use acir::{
circuit::{Circuit, Opcode},
native_types::{Expression, Witness},
BlackBoxFunc, FieldElement,
};
use indexmap::IndexMap;
use optimizers::GeneralOptimizer;
use thiserror::Error;

use crate::Language;

// The various passes that we can use over ACIR
mod optimizers;
mod transformers;

use optimizers::{GeneralOptimizer, RangeOptimizer};
use transformers::{CSatTransformer, FallbackTransformer, R1CSTransformer};

use self::optimizers::RangeOptimizer;
use self::optimizers::Simplifier;
pub use optimizers::{CircuitSimplifier, SimplifyResult};

#[derive(PartialEq, Eq, Debug, Error)]
pub enum CompileError {
Expand All @@ -27,7 +28,7 @@ pub fn compile(
acir: Circuit,
np_language: Language,
is_opcode_supported: impl Fn(&Opcode) -> bool,
simplifier: &Simplifier,
simplifier: &CircuitSimplifier,
) -> Result<Circuit, CompileError> {
// Instantiate the optimizer.
// Currently the optimizer and reducer are one in the same
Expand Down
5 changes: 3 additions & 2 deletions acvm/src/compiler/optimizers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
mod general;
mod redundant_range;
pub mod simplify;
mod simplify;

pub(crate) use general::GeneralOptimizer;
pub(crate) use redundant_range::RangeOptimizer;
pub(crate) use simplify::CircuitSimplifier as Simplifier;
// Public as these need to be passed to `acvm::compiler::compile()`
pub use simplify::{CircuitSimplifier, SimplifyResult};
4 changes: 2 additions & 2 deletions acvm/src/compiler/optimizers/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ mod tests {
FieldElement,
};

use crate::compiler::{optimizers::Simplifier, transformers::FallbackTransformer};
use crate::compiler::{optimizers::CircuitSimplifier, transformers::FallbackTransformer};

#[test]
fn simplify_test() {
Expand Down Expand Up @@ -465,7 +465,7 @@ mod tests {
linear_combinations: vec![(one, a)],
q_c: FieldElement::zero(),
};
let mut simplifier = Simplifier::new(1);
let mut simplifier = CircuitSimplifier::new(1);
let mut circuit = vec![
Opcode::Arithmetic(gate_a),
Opcode::Arithmetic(gate_b),
Expand Down
6 changes: 3 additions & 3 deletions acvm/src/compiler/transformers/csat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ use indexmap::IndexMap;
/// to calculate the original expression.
// Should we give it all of the gates?
// Have a single transformer that you instantiate with a width, then pass many gates through
pub struct CSatTransformer {
pub(crate) struct CSatTransformer {
width: usize,
}

impl CSatTransformer {
// Configure the width for the optimizer
pub fn new(width: usize) -> CSatTransformer {
pub(crate) fn new(width: usize) -> CSatTransformer {
assert!(width > 2);

CSatTransformer { width }
Expand All @@ -30,7 +30,7 @@ impl CSatTransformer {
// Still missing dead witness optimization.
// To do this, we will need the whole set of arithmetic gates
// I think it can also be done before the local optimization seen here, as dead variables will come from the user
pub fn transform(
pub(crate) fn transform(
&self,
gate: Expression,
intermediate_variables: &mut IndexMap<Expression, (FieldElement, Witness)>,
Expand Down
8 changes: 4 additions & 4 deletions acvm/src/compiler/transformers/fallback.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::compiler::optimizers::Simplifier;
use crate::compiler::optimizers::CircuitSimplifier;

use super::super::CompileError;
use acir::{
Expand All @@ -8,14 +8,14 @@ use acir::{

/// The initial transformer to act on a [`Circuit`]. This replaces any unsupported opcodes with
/// fallback implementations consisting of well supported opcodes.
pub struct FallbackTransformer;
pub(crate) struct FallbackTransformer;

impl FallbackTransformer {
//ACIR pass which replace unsupported opcodes using arithmetic fallback
pub fn transform(
pub(crate) fn transform(
acir: Circuit,
is_supported: impl Fn(&Opcode) -> bool,
simplifier: &Simplifier,
simplifier: &CircuitSimplifier,
) -> Result<Circuit, CompileError> {
let mut acir_supported_opcodes = Vec::with_capacity(acir.opcodes.len());

Expand Down
6 changes: 3 additions & 3 deletions acvm/src/compiler/transformers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ mod csat;
mod fallback;
mod r1cs;

pub use csat::CSatTransformer;
pub use fallback::FallbackTransformer;
pub use r1cs::R1CSTransformer;
pub(crate) use csat::CSatTransformer;
pub(crate) use fallback::FallbackTransformer;
pub(crate) use r1cs::R1CSTransformer;
6 changes: 3 additions & 3 deletions acvm/src/compiler/transformers/r1cs.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use acir::circuit::Circuit;

/// Currently a "noop" transformer.
pub struct R1CSTransformer {
pub(crate) struct R1CSTransformer {
acir: Circuit,
}

impl R1CSTransformer {
pub fn new(acir: Circuit) -> Self {
pub(crate) fn new(acir: Circuit) -> Self {
Self { acir }
}
// TODO: We could possibly make sure that all polynomials are at most degree-2
pub fn transform(self) -> Circuit {
pub(crate) fn transform(self) -> Circuit {
self.acir
}
}
14 changes: 7 additions & 7 deletions acvm/src/pwg/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ use super::{OpcodeNotSolvable, OpcodeResolution, OpcodeResolutionError};

/// An Arithmetic solver will take a Circuit's arithmetic gates with witness assignments
/// and create the other witness variables
pub struct ArithmeticSolver;
pub(super) struct ArithmeticSolver;

#[allow(clippy::enum_variant_names)]
pub enum GateStatus {
pub(super) enum GateStatus {
GateSatisfied(FieldElement),
GateSolvable(FieldElement, (FieldElement, Witness)),
GateUnsolvable,
}

pub enum MulTerm {
pub(crate) enum MulTerm {
OneUnknown(FieldElement, Witness), // (qM * known_witness, unknown_witness)
TooManyUnknowns,
Solved(FieldElement),
}

impl ArithmeticSolver {
/// Derives the rest of the witness based on the initial low level variables
pub fn solve(
pub(super) fn solve(
initial_witness: &mut WitnessMap,
gate: &Expression,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
Expand Down Expand Up @@ -162,7 +162,7 @@ impl ArithmeticSolver {
/// Returns the summation of all of the variables, plus the unknown variable
/// Returns None, if there is more than one unknown variable
/// We cannot assign
pub fn solve_fan_in_term(
pub(super) fn solve_fan_in_term(
arith_gate: &Expression,
witness_assignments: &WitnessMap,
) -> GateStatus {
Expand Down Expand Up @@ -198,7 +198,7 @@ impl ArithmeticSolver {
}

// Partially evaluate the gate using the known witnesses
pub fn evaluate(expr: &Expression, initial_witness: &WitnessMap) -> Expression {
pub(super) fn evaluate(expr: &Expression, initial_witness: &WitnessMap) -> Expression {
let mut result = Expression::default();
for &(c, w1, w2) in &expr.mul_terms {
let mul_result = ArithmeticSolver::solve_mul_term_helper(&(c, w1, w2), initial_witness);
Expand Down Expand Up @@ -230,7 +230,7 @@ impl ArithmeticSolver {
// Returns one witness belonging to an expression, in no relevant order
// Returns None if the expression is const
// The function is used during partial witness generation to report unsolved witness
pub fn any_witness_from_expression(expr: &Expression) -> Option<Witness> {
pub(super) fn any_witness_from_expression(expr: &Expression) -> Option<Witness> {
if expr.linear_combinations.is_empty() {
if expr.mul_terms.is_empty() {
None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn to_u8_vec(
Ok(result)
}

pub fn secp256k1_prehashed(
pub(crate) fn secp256k1_prehashed(
initial_witness: &mut WitnessMap,
public_key_x_inputs: &[FunctionInput],
public_key_y_inputs: &[FunctionInput],
Expand Down
13 changes: 6 additions & 7 deletions acvm/src/pwg/hash.rs → acvm/src/pwg/blackbox/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ use blake2::{Blake2s256, Digest};
use sha2::Sha256;
use sha3::Keccak256;

use crate::pwg::{insert_value, witness_to_value};
use crate::{pwg::OpcodeResolution, OpcodeResolutionError};

use super::{insert_value, witness_to_value};

pub fn blake2s256(
pub(crate) fn blake2s256(
initial_witness: &mut WitnessMap,
inputs: &[FunctionInput],
outputs: &[Witness],
Expand All @@ -29,7 +28,7 @@ pub fn blake2s256(
Ok(OpcodeResolution::Solved)
}

pub fn sha256(
pub(crate) fn sha256(
initial_witness: &mut WitnessMap,
inputs: &[FunctionInput],
outputs: &[Witness],
Expand All @@ -47,7 +46,7 @@ pub fn sha256(
Ok(OpcodeResolution::Solved)
}

pub fn keccak256(
pub(crate) fn keccak256(
initial_witness: &mut WitnessMap,
inputs: &[FunctionInput],
outputs: &[Witness],
Expand All @@ -65,7 +64,7 @@ pub fn keccak256(
Ok(OpcodeResolution::Solved)
}

pub fn keccak256_variable_length(
pub(crate) fn keccak256_variable_length(
initial_witness: &mut WitnessMap,
inputs: &[FunctionInput],
var_message_size: FunctionInput,
Expand All @@ -84,7 +83,7 @@ pub fn keccak256_variable_length(
Ok(OpcodeResolution::Solved)
}

pub fn hash_to_field_128_security(
pub(crate) fn hash_to_field_128_security(
initial_witness: &mut WitnessMap,
inputs: &[FunctionInput],
output: &Witness,
Expand Down
6 changes: 3 additions & 3 deletions acvm/src/pwg/logic.rs → acvm/src/pwg/blackbox/logic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{insert_value, witness_to_value};
use crate::pwg::{insert_value, witness_to_value};
use crate::{pwg::OpcodeResolution, OpcodeResolutionError};
use acir::{
circuit::opcodes::FunctionInput,
Expand All @@ -8,7 +8,7 @@ use acir::{

/// Solves a [`BlackBoxFunc::And`][acir::circuit::black_box_functions::BlackBoxFunc::AND] opcode and inserts
/// the result into the supplied witness map
pub fn and(
pub(super) fn and(
initial_witness: &mut WitnessMap,
lhs: &FunctionInput,
rhs: &FunctionInput,
Expand All @@ -25,7 +25,7 @@ pub fn and(

/// Solves a [`BlackBoxFunc::XOR`][acir::circuit::black_box_functions::BlackBoxFunc::XOR] opcode and inserts
/// the result into the supplied witness map
pub fn xor(
pub(super) fn xor(
initial_witness: &mut WitnessMap,
lhs: &FunctionInput,
rhs: &FunctionInput,
Expand Down
18 changes: 11 additions & 7 deletions acvm/src/pwg/blackbox.rs → acvm/src/pwg/blackbox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ use acir::{
native_types::{Witness, WitnessMap},
};

use super::{
hash::{blake2s256, hash_to_field_128_security, keccak256, keccak256_variable_length, sha256},
logic::{and, xor},
range::solve_range_opcode,
signature::ecdsa::secp256k1_prehashed,
OpcodeResolution, OpcodeResolutionError,
};
use super::{OpcodeResolution, OpcodeResolutionError};
use crate::pwg::OpcodeNotSolvable;
use crate::PartialWitnessGenerator;

mod ecdsa;
mod hash;
mod logic;
mod range;

use ecdsa::secp256k1_prehashed;
use hash::{blake2s256, hash_to_field_128_security, keccak256, keccak256_variable_length, sha256};
use logic::{and, xor};
use range::solve_range_opcode;

/// Check if all of the inputs to the function have assignments
///
/// Returns the first missing assignment if any are missing
Expand Down
2 changes: 1 addition & 1 deletion acvm/src/pwg/range.rs → acvm/src/pwg/blackbox/range.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{pwg::witness_to_value, pwg::OpcodeResolution, OpcodeResolutionError};
use acir::{circuit::opcodes::FunctionInput, native_types::WitnessMap};

pub fn solve_range_opcode(
pub(super) fn solve_range_opcode(
initial_witness: &mut WitnessMap,
input: &FunctionInput,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
Expand Down
4 changes: 2 additions & 2 deletions acvm/src/pwg/brillig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use crate::{pwg::OpcodeNotSolvable, OpcodeResolution, OpcodeResolutionError};

use super::{get_value, insert_value};

pub struct BrilligSolver;
pub(super) struct BrilligSolver;

impl BrilligSolver {
pub fn solve(
pub(super) fn solve(
initial_witness: &mut WitnessMap,
brillig: &mut Brillig,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
Expand Down
8 changes: 5 additions & 3 deletions acvm/src/pwg/directives.rs → acvm/src/pwg/directives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ use num_traits::Zero;

use crate::{pwg::OpcodeResolution, OpcodeResolutionError};

use super::{get_value, insert_value, sorting::route, witness_to_value};
use super::{get_value, insert_value, witness_to_value};

mod sorting;

/// Attempts to solve the [`Directive`] opcode `directive`.
/// If successful, `initial_witness` will be mutated to contain the new witness assignment.
///
/// Returns `Ok(OpcodeResolution)` to signal whether the directive was successful solved.
///
/// Returns `Err(OpcodeResolutionError)` if a circuit constraint is unsatisfied.
pub fn solve_directives(
pub(super) fn solve_directives(
initial_witness: &mut WitnessMap,
directive: &Directive,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
Expand Down Expand Up @@ -126,7 +128,7 @@ fn solve_directives_internal(
Ordering::Equal
});
let b = val_a.iter().map(|a| *a.last().unwrap()).collect();
let control = route(base, b);
let control = sorting::route(base, b);
for (w, value) in bits.iter().zip(control) {
let value = if value { FieldElement::one() } else { FieldElement::zero() };
insert_value(w, value, initial_witness)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl SortingNetwork {

// Computes the control bits of the sorting network which transform inputs into outputs
// implementation is based on https://www.mdpi.com/2227-7080/10/1/16
pub fn route(inputs: Vec<FieldElement>, outputs: Vec<FieldElement>) -> Vec<bool> {
pub(super) fn route(inputs: Vec<FieldElement>, outputs: Vec<FieldElement>) -> Vec<bool> {
assert_eq!(inputs.len(), outputs.len());
match inputs.len() {
0 => Vec::new(),
Expand Down Expand Up @@ -245,7 +245,7 @@ pub fn route(inputs: Vec<FieldElement>, outputs: Vec<FieldElement>) -> Vec<bool>

#[cfg(test)]
mod tests {
use crate::pwg::sorting::route;
use super::route;
use acir::FieldElement;
use rand::prelude::*;

Expand Down
Loading

0 comments on commit c902a01

Please sign in to comment.