noir-lang / noir

Noir is a domain specific language for zero knowledge proofs
https://noir-lang.org
Apache License 2.0
878 stars 190 forks source link

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

Closed mvid closed 1 year ago

mvid commented 1 year ago

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: 4f3208dbad8ca69eec80674ddb1cb26593cd5d63, 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

jfecher commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@jfecher what is the status of this issue?

jfecher commented 1 year ago

@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 commented 1 year ago

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