noir-lang / noir

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

Cannot print slices #1344

Open shuklaayush opened 1 year ago

shuklaayush commented 1 year ago

Aim

Running the following tests causes in a panic in test1 while test2 passes

use dep::std;

fn inner(x: Field, b: u32) -> [u1] {
    x.to_le_bits(b)
}

#[test]
fn test1() {
    let x = 11;
    let y = x.to_le_bits(4);

    std::println(y);
}

#[test]
fn test2() {
    let x = 11;
    let y = inner(x, 4);

    std::println(y);
}

Expected behavior

std::println should work for all arrays

Bug

This is the error message

The application panicked (crashed).
Message:  array element being logged does not exist in memory
Location: crates/noirc_evaluator/src/ssa/acir_gen/operations/intrinsics.rs:25

To reproduce

  1. Copy the code above
  2. Run nargo test --show-output --allow-warnings test1

Installation method

Compiled from source

Nargo version

nargo 0.5.1 (git version hash: da473685524fc6e5e17f9c3eb95116378ac41fb8, 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

Submission Checklist

kevaundray commented 1 year ago

@vezenovm can you check if this is an issue with the new ssa?

vezenovm commented 1 year ago

This is still a bug but for a new reason. As in the new println we use the Abi to encode/decode types, and because slices are not supported in the Abi we cannot print slices.

vezenovm commented 1 year ago

Although not ideal, for now the workaround to print slices is to convert the slice into an array:

use dep::std;

fn inner(x: Field, b: u32) -> [u1; 254] {
    let mut arr = [0; 254];
    let bits = x.to_le_bits(b);
    for i in 0..254 {
        arr[i] = bits[i];
    }
    arr
}

#[test]
fn test() {
    let x = 11;
    let y = inner(x, 4);

    std::println(y);
}
jfecher commented 1 year ago

Adding onto this, std::println causes a compiler panic whenever slices are attempted to be printed. This is a result of it using as_abi_type internally which is normally expected to only be used on arguments to main, leading to this panic message:

The application panicked (crashed).
Message:  Cannot have variable sized arrays as a parameter to main
Location: crates/noirc_frontend/src/hir_def/types.rs:1366

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: core::panicking::panic_display::h4aab9670ede8cc38
      at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/panicking.rs:139
  15: core::panicking::panic_str::h8692426b1216e163
      at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/panicking.rs:123
  16: core::option::expect_failed::he7581704548985ec
      at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/option.rs:1879
  17: core::option::Option<T>::expect::h13ff5d76950af1ed
      at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/option.rs:741
  18: noirc_frontend::hir_def::types::Type::as_abi_type::hae0654d4e2a23ddb
      at /.../noir/crates/noirc_frontend/src/hir_def/types.rs:1364
  19: noirc_frontend::monomorphization::Monomorphizer::append_abi_arg::h0e7889cd04075832
      at /.../noir/crates/noirc_frontend/src/monomorphization/mod.rs:714
  20: noirc_frontend::monomorphization::Monomorphizer::function_call::h5541d1ff09982e0c
      at /.../noir/crates/noirc_frontend/src/monomorphization/mod.rs:684
  21: noirc_frontend::monomorphization::Monomorphizer::expr::h418be6fed07f1970
      at /.../noir/crates/noirc_frontend/src/monomorphization/mod.rs:300
  22: noirc_frontend::monomorphization::Monomorphizer::statement::hd53b1bb90a1f7a61
      at /.../noir/crates/noirc_frontend/src/monomorphization/mod.rs:398
  23: noirc_frontend::monomorphization::Monomorphizer::block::{{closure}}::hda3756246c4040fe
      at /.../noir/crates/noirc_frontend/src/monomorphization/mod.rs:459
  24: core::iter::adapters::map::map_fold::{{closure}}::h8d72301fe86ff5c2
      at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/iter/adapters/map.rs:84
  25: core::iter::traits::iterator::Iterator::fold::h5b81f8d74a878476
      at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/iter/traits/iterator.rs:2414
  26: <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::fold::h3ee0cef87da7d4bf
      at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/iter/adapters/map.rs:124
  27: core::iter::traits::iterator::Iterator::for_each::hc31e30d7dfbdd9cf
      at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/iter/traits/iterator.rs:831
  28: <alloc::vec::Vec<T,A> as alloc::vec::spec_extend::SpecExtend<T,I>>::spec_extend::h59c06e93ff5849fe
      at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/alloc/src/vec/spec_extend.rs:40
  29: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter::h6bf3ded3c919b384
      at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/alloc/src/vec/spec_from_iter_nested.rs:62
  30: alloc::vec::in_place_collect::<impl alloc::vec::spec_from_iter::SpecFromIter<T,I> for alloc::vec::Vec<T>>::from_iter::h86879f8cbdcd9927
      at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/alloc/src/vec/in_place_collect.rs:167
  31: <alloc::vec::Vec<T> as core::iter::traits::collect::FromIterator<T>>::from_iter::he43b0a086d59a01d
      at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/alloc/src/vec/mod.rs:2757
  32: core::iter::traits::iterator::Iterator::collect::hf1d9aeba2e678be5
      at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/iter/traits/iterator.rs:1836
  33: iter_extended::vecmap::hd8fdfc784c4bcfa1
      at /.../noir/crates/iter-extended/src/lib.rs:14
  34: noirc_frontend::monomorphization::Monomorphizer::block::h6aba6fe817a04b93
      at /.../noir/crates/noirc_frontend/src/monomorphization/mod.rs:459
  35: noirc_frontend::monomorphization::Monomorphizer::expr::h418be6fed07f1970
      at /.../noir/crates/noirc_frontend/src/monomorphization/mod.rs:276
  36: noirc_frontend::monomorphization::Monomorphizer::function::hed3c10cf18b6e193
      at /.../noir/crates/noirc_frontend/src/monomorphization/mod.rs:193
  37: noirc_frontend::monomorphization::monomorphize::h116dc1c796379913
      at /.../noir/crates/noirc_frontend/src/monomorphization/mod.rs:87
  38: noirc_driver::compile_no_check::ha192bac6b09f9ae5
      at /.../noir/crates/noirc_driver/src/lib.rs:303
  39: noirc_driver::compile_main::h159318b5103cbeb1
      at /.../noir/crates/noirc_driver/src/lib.rs:195
  40: nargo_cli::cli::compile_cmd::compile_circuit::h750de128d64d5da7
      at /.../noir/crates/nargo_cli/src/cli/compile_cmd.rs:128
  41: nargo_cli::cli::prove_cmd::prove_with_path::hc2b46b42c1fbf6f9
      at /.../noir/crates/nargo_cli/src/cli/prove_cmd.rs:112
  42: nargo_cli::cli::prove_cmd::run::h9a42555316685c8e
      at /.../noir/crates/nargo_cli/src/cli/prove_cmd.rs:68
  43: nargo_cli::cli::start_cli::hafb3033a71756cdf
      at /.../noir/crates/nargo_cli/src/cli/mod.rs:80
  44: nargo::main::hb051dd85bb9ddb14
      at /.../noir/crates/nargo_cli/src/main.rs:14
  45: core::ops::function::FnOnce::call_once::he90185e242af6ac8
      at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library/core/src/ops/function.rs:251
                                ⋮ 13 frames hidden ⋮                              
exit 101

Which is confusing to users given that it is issued as a result of println being called with a slice and not whether there are actually slice parameters to main.