Skip to content

Commit

Permalink
feat!: update aes128_encrypt to return an array
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Jan 7, 2025
1 parent 8b6f720 commit 6e7bd66
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion compiler/noirc_evaluator/src/acir/acir_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,7 @@ impl<F: AcirField, B: BlackBoxFunctionSolver<F>> AcirContext<F, B> {
}
}?;
output_count = input_size + (16 - input_size % 16);
(vec![], vec![F::from(output_count as u128)])
(vec![], vec![])
}
BlackBoxFunc::RecursiveAggregation => {
let proof_type_var = match inputs.pop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,14 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString, Registers: Re
BlackBoxFunc::AES128Encrypt => {
if let (
[inputs, BrilligVariable::BrilligArray(iv), BrilligVariable::BrilligArray(key)],
[BrilligVariable::SingleAddr(out_len), BrilligVariable::BrilligVector(outputs)],
[outputs],
) = (function_arguments, function_results)
{
let inputs = convert_array_or_vector(brillig_context, *inputs, bb_func);
let iv = brillig_context.codegen_brillig_array_to_heap_array(*iv);
let key = brillig_context.codegen_brillig_array_to_heap_array(*key);

let outputs_vector =
brillig_context.codegen_brillig_vector_to_heap_vector(*outputs);
let outputs_vector = convert_array_or_vector(brillig_context, *outputs, bb_func);

brillig_context.black_box_op_instruction(BlackBoxOp::AES128Encrypt {
inputs,
Expand All @@ -372,11 +371,6 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString, Registers: Re
outputs: outputs_vector,
});

brillig_context.mov_instruction(out_len.address, outputs_vector.size);
// Returns slice, so we need to allocate memory for it after the fact

brillig_context.initialize_externally_returned_vector(*outputs, outputs_vector);

brillig_context.deallocate_heap_vector(inputs);
brillig_context.deallocate_heap_vector(outputs_vector);
brillig_context.deallocate_heap_array(iv);
Expand Down
6 changes: 5 additions & 1 deletion noir_stdlib/src/aes128.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#[foreign(aes128_encrypt)]
// docs:start:aes128
pub fn aes128_encrypt<let N: u32>(input: [u8; N], iv: [u8; 16], key: [u8; 16]) -> [u8] {}
pub fn aes128_encrypt<let N: u32>(
input: [u8; N],
iv: [u8; 16],
key: [u8; 16],
) -> [u8; N + 16 - N % 16] {}
// docs:end:aes128
4 changes: 2 additions & 2 deletions test_programs/execution_success/aes128_encrypt/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ unconstrained fn decode_hex<let N: u32, let M: u32>(s: str<N>) -> [u8; M] {

unconstrained fn cipher(plaintext: [u8; 12], iv: [u8; 16], key: [u8; 16]) -> [u8; 16] {
let result = std::aes128::aes128_encrypt(plaintext, iv, key);
result.as_array()
result
}

fn main(inputs: str<12>, iv: str<16>, key: str<16>, output: str<32>) {
let result: [u8; 16] =
std::aes128::aes128_encrypt(inputs.as_bytes(), iv.as_bytes(), key.as_bytes()).as_array();
std::aes128::aes128_encrypt(inputs.as_bytes(), iv.as_bytes(), key.as_bytes());

let output_bytes: [u8; 16] = unsafe {
//@safety: testing context
Expand Down

0 comments on commit 6e7bd66

Please sign in to comment.