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

for loop unused variable message error #1754

Closed
hhamud opened this issue Jun 19, 2023 · 3 comments
Closed

for loop unused variable message error #1754

hhamud opened this issue Jun 19, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@hhamud
Copy link

hhamud commented Jun 19, 2023

Aim

I would like to be able to use for loops in noir

Expected Behavior

It should execute the for loop without error

Bug

I would like to verify signatures in a group and this is the function that I have

struct SignedMessage {
  public_key: [u8; 128],
  signature: [u8; 65],
  hashed_message: [u8],
}

fn checkNSignatures<N>(sm: [SignedMessage; N]) {

    for i in 0..sm.len() {
        let pkx = sm[i].public_key[0..64];
        let pky = sm[i].public_key[64..];
        let verify = std::ecdsa_sep256k1::verify_signatures(pkx, pky, sm[i].signature, sm[i].hashed_message);
        assert verify == 1;
    }

}

When trying to do this I get a misfiring of an error

error: unused variable i
   ┌─ /Users/user/Documents/projects/zk-multisig/circuits/src/main.nr:50:9
   │
50 │     for i in 0..sm.len() {
   │         - unused variable

To Reproduce

  1. paste the code in your ide
  2. run nargo check
    3.??
  3. error

Installation Method

Binary

Nargo Version

No response

Additional Context

No response

Would you like to submit a PR for this Issue?

No

Support Needs

No response

@hhamud hhamud added the bug Something isn't working label Jun 19, 2023
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Jun 19, 2023
@hhamud
Copy link
Author

hhamud commented Jun 19, 2023

I spoke with @signorecello on discord I think if his handle is Zpedro

@jfecher
Copy link
Contributor

jfecher commented Jun 20, 2023

I haven't looked into the unused variable warning yet (you can convert it to a warning with --allow-warnings) but for the rest of your code, I spotted another issue. In your struct declaration:

struct SignedMessage {
  public_key: [u8; 128],
  signature: [u8; 65],
  hashed_message: [u8],
}

You declare a field of a slice type, but this type does not work when used outside of function inputs since a slice type [T] expands into [T; N] but there is no generic parameter on the struct for the size of the slice. The fix here is to specify the size:

struct SignedMessage<N> {
  public_key: [u8; 128],
  signature: [u8; 65],
  hashed_message: [u8; N],
}

It's possible this may have been causing other issues in your code as well. Sorry for the confusing bug here. It is a known issue but something that I wish was clearer when using this syntax.

I'll also add this is something we're looking at fixing by adding true slice types to Noir that don't desugar into array types. See PR #1728

@jfecher
Copy link
Contributor

jfecher commented Jul 12, 2023

I should have looked at this closer originally. So when you run the original program, nargo gives an error and a warning:

error: Expected a ; separating these two statements
  ┌─ src/main.nr:9:35
  │
9 │         let pkx = sm[i].public_key[0..64];
  │                                   -

warning: unused variable i
  ┌─ src/main.nr:8:9
  │
8 │     for i in 0..sm.len() {
  │         - unused variable 

Error: Aborting due to 1 previous error

The first parse error is more important here. It isn't the most helpful, but it's a result of Noir not supporting the range operator .. or the y[0..64]; syntax in general. If you remove that, the parse error and unused variable warning will be fixed.

Slices are eventually coming to Noir but aren't here quite yet unfortunately. For now if you want this code to work you'll likely need a helper function:

fn split<T>(array: [T; 128]) -> ([T; 64], [T; 64]) {
    let mut first_half = dep::std::unsafe::zeroed();
    for i in 0 .. 64 {
        first_half[i] = array[i];
    }
    let mut second_half = dep::std::unsafe::zeroed();
    for i in 64 .. 128 {
        second_half[i] = array[i];
    }
    (first_half, second_half)
}

Apologies for the late answer! I'm going to close this since the original bug is related to slices which we already have tracking issues for (namely #1889 which is preventing us from enabling slices for users currently).

@jfecher jfecher closed this as completed Jul 12, 2023
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Jul 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

No branches or pull requests

2 participants