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

Commit

Permalink
Copy .rustfmt.toml from noir repo (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher authored Feb 21, 2023
1 parent c83fc06 commit 311f061
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 336 deletions.
2 changes: 2 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
edition = "2018"
use_small_heuristics = "Max"
74 changes: 26 additions & 48 deletions acir/src/circuit/black_box_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,12 @@ impl BlackBoxFunc {
input_size: InputSize::Variable,
output_size: OutputSize(32),
},
BlackBoxFunc::HashToField128Security => FuncDefinition {
name,
input_size: InputSize::Variable,
output_size: OutputSize(1),
},
BlackBoxFunc::MerkleMembership => FuncDefinition {
name,
input_size: InputSize::Variable,
output_size: OutputSize(1),
},
BlackBoxFunc::HashToField128Security => {
FuncDefinition { name, input_size: InputSize::Variable, output_size: OutputSize(1) }
}
BlackBoxFunc::MerkleMembership => {
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
Expand All @@ -137,36 +133,24 @@ impl BlackBoxFunc {
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::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,
Expand Down Expand Up @@ -217,10 +201,7 @@ mod test {
fn consistent_function_names() {
for bb_func in BlackBoxFunc::iter() {
let resolved_func = BlackBoxFunc::lookup(bb_func.name()).unwrap_or_else(|| {
panic!(
"BlackBoxFunc::lookup couldn't find black box function {}",
bb_func
)
panic!("BlackBoxFunc::lookup couldn't find black box function {}", bb_func)
});
assert_eq!(
resolved_func, bb_func,
Expand All @@ -234,10 +215,7 @@ mod test {
let func_index = bb_func.to_u16();
let got_bb_func =
BlackBoxFunc::from_u16(func_index).expect("blackbox function should have an index");
assert_eq!(
got_bb_func, bb_func,
"BlackBox function index lookup is inconsistent"
)
assert_eq!(got_bb_func, bb_func, "BlackBox function index lookup is inconsistent")
}
}
}
57 changes: 9 additions & 48 deletions acir/src/circuit/directives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,7 @@ impl Directive {
write_u32(&mut writer, x.witness_index())?;
write_u32(&mut writer, result.witness_index())?;
}
Directive::Quotient {
a,
b,
q,
r,
predicate,
} => {
Directive::Quotient { a, b, q, r, predicate } => {
a.write(&mut writer)?;
b.write(&mut writer)?;
write_u32(&mut writer, q.witness_index())?;
Expand All @@ -123,12 +117,7 @@ impl Directive {
write_u32(&mut writer, r.witness_index())?;
write_u32(&mut writer, *bit_size)?;
}
Directive::ToRadix {
a,
b,
radix,
is_little_endian,
} => {
Directive::ToRadix { a, b, radix, is_little_endian } => {
a.write(&mut writer)?;
write_u32(&mut writer, b.len() as u32)?;
for bit in b {
Expand All @@ -137,12 +126,7 @@ impl Directive {
write_u32(&mut writer, *radix)?;
write_u32(&mut writer, *is_little_endian as u32)?;
}
Directive::PermutationSort {
inputs: a,
tuple,
bits,
sort_by,
} => {
Directive::PermutationSort { inputs: a, tuple, bits, sort_by } => {
write_u32(&mut writer, *tuple)?;
write_u32(&mut writer, a.len() as u32)?;
for e in a {
Expand Down Expand Up @@ -197,13 +181,7 @@ impl Directive {
false => None,
};

Ok(Directive::Quotient {
a,
b,
q,
r,
predicate,
})
Ok(Directive::Quotient { a, b, q, r, predicate })
}
2 => {
let a = Expression::read(&mut reader)?;
Expand Down Expand Up @@ -231,12 +209,7 @@ impl Directive {
let radix = read_u32(&mut reader)?;
let is_little_endian = read_u32(&mut reader)?;

Ok(Directive::ToRadix {
a,
b,
radix,
is_little_endian: is_little_endian == 1,
})
Ok(Directive::ToRadix { a, b, radix, is_little_endian: is_little_endian == 1 })
}
6 => {
let tuple = read_u32(&mut reader)?;
Expand All @@ -260,12 +233,7 @@ impl Directive {
for _ in 0..sort_by_len {
sort_by.push(read_u32(&mut reader)?);
}
Ok(Directive::PermutationSort {
inputs: a,
tuple,
bits,
sort_by,
})
Ok(Directive::PermutationSort { inputs: a, tuple, bits, sort_by })
}

_ => Err(std::io::ErrorKind::InvalidData.into()),
Expand Down Expand Up @@ -293,10 +261,7 @@ fn serialization_roundtrip() {
(directive, got_dir)
}
// TODO: Find a way to ensure that we include all of the variants
let invert = Directive::Invert {
x: Witness(10),
result: Witness(10),
};
let invert = Directive::Invert { x: Witness(10), result: Witness(10) };

let quotient_none = Directive::Quotient {
a: Expression::default(),
Expand All @@ -320,12 +285,8 @@ fn serialization_roundtrip() {
bit_size: 123,
};

let odd_range = Directive::OddRange {
a: Witness(1u32),
b: Witness(2u32),
r: Witness(3u32),
bit_size: 32,
};
let odd_range =
Directive::OddRange { a: Witness(1u32), b: Witness(2u32), r: Witness(3u32), bit_size: 32 };

let to_radix_le = Directive::ToRadix {
a: Expression::default(),
Expand Down
26 changes: 5 additions & 21 deletions acir/src/circuit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,7 @@ impl Circuit {
opcodes.push(opcode)
}

Ok(Self {
current_witness_index,
opcodes,
public_inputs,
})
Ok(Self { current_witness_index, opcodes, public_inputs })
}
}

Expand Down Expand Up @@ -133,10 +129,7 @@ pub struct PublicInputs(pub BTreeSet<Witness>);
impl PublicInputs {
/// Returns the witness index of each public input
pub fn indices(&self) -> Vec<u32> {
self.0
.iter()
.map(|witness| witness.witness_index())
.collect()
self.0.iter().map(|witness| witness.witness_index()).collect()
}

pub fn contains(&self, index: usize) -> bool {
Expand All @@ -159,25 +152,16 @@ mod test {
Opcode::BlackBoxFuncCall(BlackBoxFuncCall {
name: crate::BlackBoxFunc::AND,
inputs: vec![
FunctionInput {
witness: Witness(1),
num_bits: 4,
},
FunctionInput {
witness: Witness(2),
num_bits: 4,
},
FunctionInput { witness: Witness(1), num_bits: 4 },
FunctionInput { witness: Witness(2), num_bits: 4 },
],
outputs: vec![Witness(3)],
})
}
fn range_opcode() -> Opcode {
Opcode::BlackBoxFuncCall(BlackBoxFuncCall {
name: crate::BlackBoxFunc::RANGE,
inputs: vec![FunctionInput {
witness: Witness(1),
num_bits: 8,
}],
inputs: vec![FunctionInput { witness: Witness(1), num_bits: 8 }],
outputs: vec![],
})
}
Expand Down
59 changes: 11 additions & 48 deletions acir/src/circuit/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,7 @@ impl std::fmt::Display for Opcode {
Opcode::Arithmetic(expr) => {
write!(f, "EXPR [ ")?;
for i in &expr.mul_terms {
write!(
f,
"({}, _{}, _{}) ",
i.0,
i.1.witness_index(),
i.2.witness_index()
)?;
write!(f, "({}, _{}, _{}) ", i.0, i.1.witness_index(), i.2.witness_index())?;
}
for i in &expr.linear_combinations {
write!(f, "({}, _{}) ", i.0, i.1.witness_index())?;
Expand All @@ -118,13 +112,7 @@ impl std::fmt::Display for Opcode {
bit_size
)
}
Opcode::Directive(Directive::Quotient {
a,
b,
q,
r,
predicate,
}) => {
Opcode::Directive(Directive::Quotient { a, b, q, r, predicate }) => {
write!(f, "DIR::QUOTIENT ")?;
if let Some(pred) = predicate {
writeln!(f, "PREDICATE = {pred}")?;
Expand Down Expand Up @@ -152,12 +140,7 @@ impl std::fmt::Display for Opcode {
)
}
Opcode::BlackBoxFuncCall(g) => write!(f, "{g}"),
Opcode::Directive(Directive::ToRadix {
a,
b,
radix: _,
is_little_endian,
}) => {
Opcode::Directive(Directive::ToRadix { a, b, radix: _, is_little_endian }) => {
write!(f, "DIR::TORADIX ")?;
write!(
f,
Expand All @@ -170,12 +153,7 @@ impl std::fmt::Display for Opcode {
if *is_little_endian { "little" } else { "big" }
)
}
Opcode::Directive(Directive::PermutationSort {
inputs: a,
tuple,
bits,
sort_by,
}) => {
Opcode::Directive(Directive::PermutationSort { inputs: a, tuple, bits, sort_by }) => {
write!(f, "DIR::PERMUTATIONSORT ")?;
write!(
f,
Expand Down Expand Up @@ -263,11 +241,7 @@ impl BlackBoxFuncCall {
outputs.push(witness)
}

Ok(BlackBoxFuncCall {
name,
inputs,
outputs,
})
Ok(BlackBoxFuncCall { name, inputs, outputs })
}
}

Expand All @@ -291,11 +265,8 @@ impl std::fmt::Display for BlackBoxFuncCall {
let inputs_str = if should_abbreviate_inputs {
let mut result = String::new();
for (index, inp) in self.inputs.iter().enumerate() {
result += &format!(
"(_{}, num_bits: {})",
inp.witness.witness_index(),
inp.num_bits
);
result +=
&format!("(_{}, num_bits: {})", inp.witness.witness_index(), inp.num_bits);
// Add a comma, unless it is the last entry
if index != self.inputs.len() - 1 {
result += ", "
Expand Down Expand Up @@ -372,22 +343,14 @@ fn serialization_roundtrip() {
let opcode_black_box_func = Opcode::BlackBoxFuncCall(BlackBoxFuncCall {
name: BlackBoxFunc::AES,
inputs: vec![
FunctionInput {
witness: Witness(1u32),
num_bits: 12,
},
FunctionInput {
witness: Witness(24u32),
num_bits: 32,
},
FunctionInput { witness: Witness(1u32), num_bits: 12 },
FunctionInput { witness: Witness(24u32), num_bits: 32 },
],
outputs: vec![Witness(123u32), Witness(245u32)],
});

let opcode_directive = Opcode::Directive(Directive::Invert {
x: Witness(1234u32),
result: Witness(56789u32),
});
let opcode_directive =
Opcode::Directive(Directive::Invert { x: Witness(1234u32), result: Witness(56789u32) });

let opcodes = vec![opcode_arith, opcode_black_box_func, opcode_directive];

Expand Down
Loading

0 comments on commit 311f061

Please sign in to comment.