noir-lang / noir

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

Compiler panics when implementing the NoteInterface #5384

Closed GustaveCharles closed 2 months ago

GustaveCharles commented 3 months ago

Aim

use dep::aztec::{
    prelude::{AztecAddress, NoteHeader, NoteInterface, PrivateContext},
    protocol_types::{constants::GENERATOR_INDEX__NOTE_NULLIFIER, grumpkin_point::GrumpkinPoint, hash::poseidon2_hash},
    note::utils::compute_note_hash_for_consumption, oracle::unsafe_rand::unsafe_rand,
    keys::getters::get_nsk_app
};

global TOKEN_NOTE_LEN: Field = 3; 

#[aztec(note)]
struct TokenNote {
    amount: U128,
    npk_m_hash: Field,
    randomness: Field,
}

impl NoteInterface<TOKEN_NOTE_LEN> for TokenNote {
    fn compute_nullifier(self, context: &mut PrivateContext) -> Field {
        let note_hash_for_nullify = compute_note_hash_for_consumption(self);
        let secret = context.request_nsk_app(self.npk_m_hash);
        poseidon2_hash([
            note_hash_for_nullify,
            secret,
            GENERATOR_INDEX__NOTE_NULLIFIER as Field,
        ])
    }

    fn compute_nullifier_without_context(self) -> Field {
        let note_hash_for_nullify = compute_note_hash_for_consumption(self);
        let secret = get_nsk_app(self.npk_m_hash);
        poseidon2_hash([
            note_hash_for_nullify,
            secret,
            GENERATOR_INDEX__NOTE_NULLIFIER as Field,
        ])
    }

    fn broadcast(self, context: &mut PrivateContext, slot: Field, ivpk_m: GrumpkinPoint) {
      if !(self.amount == U128::from_integer(0)) {
          context.encrypt_and_emit_note(
              (*context).this_address(),
              slot,
              Self::get_note_type_id(),
              ivpk_m,
              self,
          );
      }
    }
}

Expected Behavior

No error, the code should compile.

Bug

The application panicked (crashed). Message: called Result::unwrap() on an Err value: ["TOKEN_NOTE_LEN"] Location: aztec_macros/src/transforms/note_interface.rs:102

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

To Reproduce

  1. Start a new Aztec project with aztec-nargo new
  2. Copy the Aim code in main
  3. run aztec-cargo compile

Project Impact

Blocker

Impact Context

No response

Workaround

None

Workaround Description

No response

Additional Context

No response

Installation Method

Binary (noirup default)

Nargo Version

nargo version = 0.30.0 noirc version = 0.30.0+48d9df4ff227c08a6e66f21c0286bc6349151671

NoirJS Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

critesjosh commented 3 months ago

You are putting this code into main.nr?

You should put your contract into main.nr and import any new types into your contract. You can see the token contract folder for an example.

jfecher commented 3 months ago

We should avoid panicking in this case but this code is temporary and will be removed when Noir has proper macro support

GustaveCharles commented 3 months ago

You are putting this code into main.nr?

You should put your contract into main.nr and import any new types into your contract. You can see the token contract folder for an example.

I put it in main for example purposes. Putting it in a folder gives the same error.

GustaveCharles commented 3 months ago

We should avoid panicking in this case but this code is temporary and will be removed when Noir has proper macro support

The problem on my side was that I didn't pass the second generic parameter to the NoteInterface. A better error message could also be useful :)

TomAFrench commented 3 months ago

fyi @Thunkar as this seems to be an issue inside aztec_macros.

Thunkar commented 2 months ago

Closing since https://github.com/AztecProtocol/aztec-packages/pull/7451 adds a nicer error message while we implement NoteInterface using metaprogramming.