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

Commit

Permalink
feat!: use struct variants for blackbox function calls (#269)
Browse files Browse the repository at this point in the history
* feat: use enum for bb call

* style: updated format and clippy

* feat(bb): improved structs of bb fns

* fix: fixed optimizer

* style: fix format

* fix: update ecdsa signature

* fix: remove hash_index from pedersen

* feat(bb): remove bb fn definitions

* refactor: extract mapping to u8 vec to a fn

* fix: address PR comments

* refactor: use slice instead of vec

* feat: add default is bb supported fn

* fix: do not panic in ecdsa

* refactor: add a name fn to bb fn call

* refactor: address pr comments
  • Loading branch information
sirasistant authored May 11, 2023
1 parent 3288b4c commit a83333b
Show file tree
Hide file tree
Showing 13 changed files with 635 additions and 388 deletions.
84 changes: 0 additions & 84 deletions acir/src/circuit/black_box_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,90 +105,6 @@ impl BlackBoxFunc {
pub fn is_valid_black_box_func_name(op_name: &str) -> bool {
BlackBoxFunc::lookup(op_name).is_some()
}
pub fn definition(&self) -> FuncDefinition {
let name = self.name();
match self {
BlackBoxFunc::AES => unimplemented!(),
BlackBoxFunc::SHA256 => FuncDefinition {
name,
input_size: InputSize::Variable,
output_size: OutputSize(32),
},
BlackBoxFunc::Blake2s => FuncDefinition {
name,
input_size: InputSize::Variable,
output_size: OutputSize(32),
},
BlackBoxFunc::HashToField128Security => {
FuncDefinition { name, input_size: InputSize::Variable, output_size: OutputSize(1) }
}
BlackBoxFunc::ComputeMerkleRoot => {
FuncDefinition { name, input_size: InputSize::Variable, output_size: OutputSize(1) }
}
BlackBoxFunc::SchnorrVerify => FuncDefinition {
name,
// XXX: input_size can be changed to fixed, once we hash
// the message before passing it to schnorr.
// This is assuming all hashes will be 256 bits. Reasonable?
input_size: InputSize::Variable,
output_size: OutputSize(1),
},
BlackBoxFunc::Pedersen => {
FuncDefinition { name, input_size: InputSize::Variable, output_size: OutputSize(2) }
}
BlackBoxFunc::EcdsaSecp256k1 => {
FuncDefinition { name, input_size: InputSize::Variable, output_size: OutputSize(1) }
}
BlackBoxFunc::FixedBaseScalarMul => {
FuncDefinition { name, input_size: InputSize::Fixed(1), output_size: OutputSize(2) }
}
BlackBoxFunc::AND => {
FuncDefinition { name, input_size: InputSize::Fixed(2), output_size: OutputSize(1) }
}
BlackBoxFunc::XOR => {
FuncDefinition { name, input_size: InputSize::Fixed(2), output_size: OutputSize(1) }
}
BlackBoxFunc::RANGE => {
FuncDefinition { name, input_size: InputSize::Fixed(1), output_size: OutputSize(0) }
}
BlackBoxFunc::Keccak256 => FuncDefinition {
name,
input_size: InputSize::Variable,
output_size: OutputSize(32),
},
}
}
}

// Descriptor as to whether the input/output is fixed or variable
// Example: The input for Sha256 is Variable and the output is fixed at 2 witnesses
// each holding 128 bits of the actual Sha256 function
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum InputSize {
Variable,
Fixed(u128),
}

impl InputSize {
pub fn fixed_size(&self) -> Option<u128> {
match self {
InputSize::Variable => None,
InputSize::Fixed(size) => Some(*size),
}
}
}

// Output size Cannot currently vary, so we use a separate struct
// XXX: In the future, we may be able to allow the output to vary based on the input size, however this implies support for dynamic circuits
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct OutputSize(pub u128);

#[derive(Clone, Debug, Hash)]
// Specs for how many inputs/outputs the method takes.
pub struct FuncDefinition {
pub name: &'static str,
pub input_size: InputSize,
pub output_size: OutputSize,
}

#[cfg(test)]
Expand Down
17 changes: 6 additions & 11 deletions acir/src/circuit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,15 @@ mod test {
use acir_field::FieldElement;

fn and_opcode() -> Opcode {
Opcode::BlackBoxFuncCall(BlackBoxFuncCall {
name: crate::BlackBoxFunc::AND,
inputs: vec![
FunctionInput { witness: Witness(1), num_bits: 4 },
FunctionInput { witness: Witness(2), num_bits: 4 },
],
outputs: vec![Witness(3)],
Opcode::BlackBoxFuncCall(BlackBoxFuncCall::AND {
lhs: FunctionInput { witness: Witness(1), num_bits: 4 },
rhs: FunctionInput { witness: Witness(2), num_bits: 4 },
output: Witness(3),
})
}
fn range_opcode() -> Opcode {
Opcode::BlackBoxFuncCall(BlackBoxFuncCall {
name: crate::BlackBoxFunc::RANGE,
inputs: vec![FunctionInput { witness: Witness(1), num_bits: 8 }],
outputs: vec![],
Opcode::BlackBoxFuncCall(BlackBoxFuncCall::RANGE {
input: FunctionInput { witness: Witness(1), num_bits: 8 },
})
}
fn oracle_opcode() -> Opcode {
Expand Down
6 changes: 2 additions & 4 deletions acir/src/circuit/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Opcode {
match self {
Opcode::Arithmetic(_) => "arithmetic",
Opcode::Directive(directive) => directive.name(),
Opcode::BlackBoxFuncCall(g) => g.name.name(),
Opcode::BlackBoxFuncCall(g) => g.name(),
Opcode::Block(_) => "block",
Opcode::RAM(_) => "ram",
Opcode::ROM(_) => "rom",
Expand Down Expand Up @@ -228,7 +228,6 @@ impl std::fmt::Debug for Opcode {
#[test]
fn serialization_roundtrip() {
use crate::native_types::Witness;
use crate::BlackBoxFunc;

fn read_write(opcode: Opcode) -> (Opcode, Opcode) {
let mut bytes = Vec::new();
Expand All @@ -239,8 +238,7 @@ fn serialization_roundtrip() {

let opcode_arith = Opcode::Arithmetic(Expression::default());

let opcode_black_box_func = Opcode::BlackBoxFuncCall(BlackBoxFuncCall {
name: BlackBoxFunc::AES,
let opcode_black_box_func = Opcode::BlackBoxFuncCall(BlackBoxFuncCall::AES {
inputs: vec![
FunctionInput { witness: Witness(1u32), num_bits: 12 },
FunctionInput { witness: Witness(24u32), num_bits: 32 },
Expand Down
Loading

0 comments on commit a83333b

Please sign in to comment.