Skip to content

Commit

Permalink
feat!: Use fixed size arrays in black box functions where sizes are k…
Browse files Browse the repository at this point in the history
…nown (AztecProtocol/aztec-packages#5620)

This PR enforces the sizes of inputs/outputs of blackbox functions to
remove some runtime checks and allocations.

---------

Co-authored-by: guipublic <[email protected]>
Co-authored-by: vezenovm <[email protected]>
  • Loading branch information
3 people committed Apr 16, 2024
2 parents 2994743 + 1c8d1bf commit 0b34821
Show file tree
Hide file tree
Showing 18 changed files with 357 additions and 302 deletions.
2 changes: 1 addition & 1 deletion .aztec-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
f84957584ff76c22c069903f7648735a0be91d7f
f50b180379ac90d782aba3472708f8cef122c25b
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions acvm-repo/acir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ thiserror.workspace = true
flate2.workspace = true
bincode.workspace = true
base64.workspace = true
serde-big-array = "0.5.1"

[dev-dependencies]
serde_json = "1.0"
Expand Down
36 changes: 18 additions & 18 deletions acvm-repo/acir/codegen/acir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace Program {

struct SHA256 {
std::vector<Program::FunctionInput> inputs;
std::vector<Program::Witness> outputs;
std::array<Program::Witness, 32> outputs;

friend bool operator==(const SHA256&, const SHA256&);
std::vector<uint8_t> bincodeSerialize() const;
Expand All @@ -63,7 +63,7 @@ namespace Program {

struct Blake2s {
std::vector<Program::FunctionInput> inputs;
std::vector<Program::Witness> outputs;
std::array<Program::Witness, 32> outputs;

friend bool operator==(const Blake2s&, const Blake2s&);
std::vector<uint8_t> bincodeSerialize() const;
Expand All @@ -72,7 +72,7 @@ namespace Program {

struct Blake3 {
std::vector<Program::FunctionInput> inputs;
std::vector<Program::Witness> outputs;
std::array<Program::Witness, 32> outputs;

friend bool operator==(const Blake3&, const Blake3&);
std::vector<uint8_t> bincodeSerialize() const;
Expand All @@ -82,7 +82,7 @@ namespace Program {
struct SchnorrVerify {
Program::FunctionInput public_key_x;
Program::FunctionInput public_key_y;
std::vector<Program::FunctionInput> signature;
std::array<Program::FunctionInput, 64> signature;
std::vector<Program::FunctionInput> message;
Program::Witness output;

Expand Down Expand Up @@ -112,10 +112,10 @@ namespace Program {
};

struct EcdsaSecp256k1 {
std::vector<Program::FunctionInput> public_key_x;
std::vector<Program::FunctionInput> public_key_y;
std::vector<Program::FunctionInput> signature;
std::vector<Program::FunctionInput> hashed_message;
std::array<Program::FunctionInput, 32> public_key_x;
std::array<Program::FunctionInput, 32> public_key_y;
std::array<Program::FunctionInput, 64> signature;
std::array<Program::FunctionInput, 32> hashed_message;
Program::Witness output;

friend bool operator==(const EcdsaSecp256k1&, const EcdsaSecp256k1&);
Expand All @@ -124,10 +124,10 @@ namespace Program {
};

struct EcdsaSecp256r1 {
std::vector<Program::FunctionInput> public_key_x;
std::vector<Program::FunctionInput> public_key_y;
std::vector<Program::FunctionInput> signature;
std::vector<Program::FunctionInput> hashed_message;
std::array<Program::FunctionInput, 32> public_key_x;
std::array<Program::FunctionInput, 32> public_key_y;
std::array<Program::FunctionInput, 64> signature;
std::array<Program::FunctionInput, 32> hashed_message;
Program::Witness output;

friend bool operator==(const EcdsaSecp256r1&, const EcdsaSecp256r1&);
Expand Down Expand Up @@ -160,16 +160,16 @@ namespace Program {
struct Keccak256 {
std::vector<Program::FunctionInput> inputs;
Program::FunctionInput var_message_size;
std::vector<Program::Witness> outputs;
std::array<Program::Witness, 32> outputs;

friend bool operator==(const Keccak256&, const Keccak256&);
std::vector<uint8_t> bincodeSerialize() const;
static Keccak256 bincodeDeserialize(std::vector<uint8_t>);
};

struct Keccakf1600 {
std::vector<Program::FunctionInput> inputs;
std::vector<Program::Witness> outputs;
std::array<Program::FunctionInput, 25> inputs;
std::array<Program::Witness, 25> outputs;

friend bool operator==(const Keccakf1600&, const Keccakf1600&);
std::vector<uint8_t> bincodeSerialize() const;
Expand Down Expand Up @@ -257,9 +257,9 @@ namespace Program {
};

struct Sha256Compression {
std::vector<Program::FunctionInput> inputs;
std::vector<Program::FunctionInput> hash_values;
std::vector<Program::Witness> outputs;
std::array<Program::FunctionInput, 16> inputs;
std::array<Program::FunctionInput, 8> hash_values;
std::array<Program::Witness, 8> outputs;

friend bool operator==(const Sha256Compression&, const Sha256Compression&);
std::vector<uint8_t> bincodeSerialize() const;
Expand Down
83 changes: 27 additions & 56 deletions acvm-repo/acir/src/circuit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,61 +317,31 @@ mod tests {
})
}
fn keccakf1600_opcode() -> Opcode {
Opcode::BlackBoxFuncCall(BlackBoxFuncCall::Keccakf1600 {
inputs: vec![
FunctionInput { witness: Witness(1), num_bits: 64 },
FunctionInput { witness: Witness(2), num_bits: 64 },
FunctionInput { witness: Witness(3), num_bits: 64 },
FunctionInput { witness: Witness(4), num_bits: 64 },
FunctionInput { witness: Witness(5), num_bits: 64 },
FunctionInput { witness: Witness(6), num_bits: 64 },
FunctionInput { witness: Witness(7), num_bits: 64 },
FunctionInput { witness: Witness(8), num_bits: 64 },
FunctionInput { witness: Witness(9), num_bits: 64 },
FunctionInput { witness: Witness(10), num_bits: 64 },
FunctionInput { witness: Witness(11), num_bits: 64 },
FunctionInput { witness: Witness(12), num_bits: 64 },
FunctionInput { witness: Witness(13), num_bits: 64 },
FunctionInput { witness: Witness(14), num_bits: 64 },
FunctionInput { witness: Witness(15), num_bits: 64 },
FunctionInput { witness: Witness(16), num_bits: 64 },
FunctionInput { witness: Witness(17), num_bits: 64 },
FunctionInput { witness: Witness(18), num_bits: 64 },
FunctionInput { witness: Witness(19), num_bits: 64 },
FunctionInput { witness: Witness(20), num_bits: 64 },
FunctionInput { witness: Witness(21), num_bits: 64 },
FunctionInput { witness: Witness(22), num_bits: 64 },
FunctionInput { witness: Witness(23), num_bits: 64 },
FunctionInput { witness: Witness(24), num_bits: 64 },
FunctionInput { witness: Witness(25), num_bits: 64 },
],
outputs: vec![
Witness(26),
Witness(27),
Witness(28),
Witness(29),
Witness(30),
Witness(31),
Witness(32),
Witness(33),
Witness(34),
Witness(35),
Witness(36),
Witness(37),
Witness(38),
Witness(39),
Witness(40),
Witness(41),
Witness(42),
Witness(43),
Witness(44),
Witness(45),
Witness(46),
Witness(47),
Witness(48),
Witness(49),
Witness(50),
],
let inputs: Box<[FunctionInput; 25]> = Box::new(std::array::from_fn(|i| FunctionInput {
witness: Witness(i as u32 + 1),
num_bits: 8,
}));
let outputs: Box<[Witness; 25]> = Box::new(std::array::from_fn(|i| Witness(i as u32 + 26)));

Opcode::BlackBoxFuncCall(BlackBoxFuncCall::Keccakf1600 { inputs, outputs })
}
fn schnorr_verify_opcode() -> Opcode {
let public_key_x =
FunctionInput { witness: Witness(1), num_bits: FieldElement::max_num_bits() };
let public_key_y =
FunctionInput { witness: Witness(2), num_bits: FieldElement::max_num_bits() };
let signature: Box<[FunctionInput; 64]> = Box::new(std::array::from_fn(|i| {
FunctionInput { witness: Witness(i as u32 + 3), num_bits: 8 }
}));
let message: Vec<FunctionInput> = vec![FunctionInput { witness: Witness(67), num_bits: 8 }];
let output = Witness(68);

Opcode::BlackBoxFuncCall(BlackBoxFuncCall::SchnorrVerify {
public_key_x,
public_key_y,
signature,
message,
output,
})
}

Expand All @@ -380,7 +350,7 @@ mod tests {
let circuit = Circuit {
current_witness_index: 5,
expression_width: ExpressionWidth::Unbounded,
opcodes: vec![and_opcode(), range_opcode()],
opcodes: vec![and_opcode(), range_opcode(), schnorr_verify_opcode()],
private_parameters: BTreeSet::new(),
public_parameters: PublicInputs(BTreeSet::from_iter(vec![Witness(2), Witness(12)])),
return_values: PublicInputs(BTreeSet::from_iter(vec![Witness(4), Witness(12)])),
Expand Down Expand Up @@ -413,6 +383,7 @@ mod tests {
range_opcode(),
and_opcode(),
keccakf1600_opcode(),
schnorr_verify_opcode(),
],
private_parameters: BTreeSet::new(),
public_parameters: PublicInputs(BTreeSet::from_iter(vec![Witness(2)])),
Expand Down
Loading

0 comments on commit 0b34821

Please sign in to comment.