Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compilation panic while trying to convert Field to fixed length byte array #1023

Closed
mvid opened this issue Mar 21, 2023 · 5 comments
Closed
Assignees
Labels
bug Something isn't working discussion

Comments

@mvid
Copy link

mvid commented Mar 21, 2023

Aim

I wrote a small snippet of code to try to turn a field into a fixed length byte array, but the compiler panics

Expected behavior

Compilation

Bug

➜ aes-noir nargo compile main
The application panicked (crashed).
Message: called Option::unwrap() on a None value
Location: crates/noirc_frontend/src/monomorphization/mod.rs:619

To reproduce

main.nr code:

use dep::std;


fn main(message : pub Field, key : Field) -> pub [u8; 16] {
    let key_bytes = field_to_fixed_byte_array(key);

    std::println(message);
    key_bytes
}

fn field_to_fixed_byte_array(f : Field) -> [u8; 16] {
    let bytes = f.to_le_bytes(8);
    constrain bytes.len() == 16;
    let mut result: [u8; 16] = [0; 16];

    for i in 0..16 {
        result[i] = bytes[i];
    }

    result
}

Installation method

Binary

Nargo version

nargo 0.3.2 (git version hash: 4f3208d, is dirty: false)

@noir-lang/noir_wasm version

No response

@noir-lang/barretenberg version

No response

@noir-lang/aztec_backend version

No response

Additional context

No response

@mvid mvid added the bug Something isn't working label Mar 21, 2023
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Mar 21, 2023
@jfecher
Copy link
Contributor

jfecher commented Mar 23, 2023

Thank you for the report.

Looks like this is a result of the monomorphization pass in the compiler failing to deduce the size of the bytes array returned by to_le_bytes in this case.

Originally, I was going to suggest getting around this problem by changing your program to include a type annotation:

let bytes: [u8; 16] = f.to_le_bytes(8);

Or return the le_bytes directly:

fn field_to_fixed_byte_array(f : Field) -> [u8; 16] {
    f.to_le_bytes(8)
}

But this seems to produce an unintended program to me as you can now change the result type of the function to any array size and have it still prove.

It'll likely be a while before this is fixed completely, unfortunately. These API of these methods on field may have to be changed.

@mvid
Copy link
Author

mvid commented Mar 23, 2023

I got around this issue by iterating with a fixed count of the array, and pulling values into a new, sized array. I don't love it, but it works for now

@kevaundray
Copy link
Contributor

@jfecher what is the status of this issue?

@jfecher
Copy link
Contributor

jfecher commented Jul 24, 2023

@kevaundray still open. Looks like the --experimental-ssa crashes with:

The application panicked (crashed).
Message:  not yet implemented: expected a black box function
Location: crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs:764

This is a bug. We may have already fixed this in newer versions of Nargo so try searching for similar issues at https://github.com/noir-lang/noir/issues/.
If there isn't an open issue for this bug, consider opening one at https://github.com/noir-lang/noir/issues/new?labels=bug&template=bug_report.yml

  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ BACKTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
                                ⋮ 13 frames hidden ⋮                              
  14: noirc_evaluator::ssa_refactor::acir_gen::Context::convert_ssa_intrinsic_call::h4e381019f8ccaf7b
      at /.../noir/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs:764
  15: noirc_evaluator::ssa_refactor::acir_gen::Context::convert_ssa_instruction::h703b8aeac9abe683
      at /.../noir/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs:299
  16: noirc_evaluator::ssa_refactor::acir_gen::Context::convert_acir_main::h0979454e08e8176e
      at /.../noir/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs:149
  17: noirc_evaluator::ssa_refactor::acir_gen::Context::convert_ssa::h0420a3719daa6cc0
      at /.../noir/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs:131
  18: noirc_evaluator::ssa_refactor::acir_gen::<impl noirc_evaluator::ssa_refactor::ssa_gen::program::Ssa>::into_acir::h77893a77e96625eb
      at /.../noir/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs:90
  19: noirc_evaluator::ssa_refactor::optimize_into_acir::h21fe114c8d08ac20
      at /.../noir/crates/noirc_evaluator/src/ssa_refactor.rs:61
  20: noirc_evaluator::ssa_refactor::experimental_create_circuit::h342973076ae03ce0
      at /.../noir/crates/noirc_evaluator/src/ssa_refactor.rs:76
  21: noirc_driver::compile_no_check::h443020e1cf881b60
      at /.../noir/crates/noirc_driver/src/lib.rs:329
  22: noirc_driver::compile_main::hb04fb90f2a0e9161
      at /.../noir/crates/noirc_driver/src/lib.rs:219
  23: nargo_cli::cli::compile_cmd::compile_circuit::hf36a08fddc7b2f69
      at /.../noir/crates/nargo_cli/src/cli/compile_cmd.rs:128
  24: nargo_cli::cli::prove_cmd::prove_with_path::h33305f9d01d25eb6
      at /.../noir/crates/nargo_cli/src/cli/prove_cmd.rs:112
  25: nargo_cli::cli::prove_cmd::run::h8742a2056354ab8b
      at /.../noir/crates/nargo_cli/src/cli/prove_cmd.rs:68
  26: nargo_cli::cli::start_cli::ha3dd0e262f76402b
      at /.../noir/crates/nargo_cli/src/cli/mod.rs:77
  27: nargo::main::h3def67c913670aef
      at /.../noir/crates/nargo_cli/src/main.rs:14
  28: core::ops::function::FnOnce::call_once::hbf89579456324c40
      at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/ops/function.rs:251
                                ⋮ 13 frames hidden ⋮                              
exit 101

@kevaundray
Copy link
Contributor

The new error would be coming from convert_ssa_intrinsic_call -- It seems that one of the following are being expected to be implemented:

            Intrinsic::ArrayLen => todo!(),
            Intrinsic::SlicePushBack => todo!(),
            Intrinsic::SlicePushFront => todo!(),
            Intrinsic::SlicePopBack => todo!(),
            Intrinsic::SlicePopFront => todo!(),
            Intrinsic::SliceInsert => todo!(),
            Intrinsic::SliceRemove => todo!(),

Most likely ArrayLen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working discussion
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants