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

Incorrect implementation of trait causes Noir to panic #6954

Open
benesjan opened this issue Jan 6, 2025 · 2 comments
Open

Incorrect implementation of trait causes Noir to panic #6954

benesjan opened this issue Jan 6, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@benesjan
Copy link
Contributor

benesjan commented Jan 6, 2025

Aim

I was trying to modify Serialize trait in noir-protocol-circuits in aztec-packages such that only 1 trait implementation is allowed. This led me to stumble upon this panic.

Expected Behavior

No panic

Bug

When a Noir trait implementation is ill-defined it causes Noir to panic.

Tagging @jfecher as we've discussed this on Slack.

To Reproduce

Compile the following code:

struct MockStruct {
    value: Field,
}

pub trait Serialize {
    let N: u32;

    fn serialize(self) -> [Field; N];
}

impl Serialize for MockStruct {
    let N = 1;

    fn serialize(self) -> [Field; Self::N] {
        [self.value]
    }
}

pub struct Counted<T> {
    pub inner: T,
    pub counter: u32,
}

impl<T> Counted<T> {
    pub fn new(inner: T, counter: u32) -> Self {
        Self { inner, counter }
    }
}

impl<T> Serialize for Counted<T> where T: Serialize {
    type N = <T as Serialize>::N + 1;

    fn serialize(self) -> [Field; Self::N] {
        array_concat(self.inner.serialize(), [self.counter as Field])
    }
}

// impl<T, let NT: u32> Serialize for Counted<T>
//     where T: Serialize<N = NT>
// {
//     let N = NT + 1;

//     fn serialize(self) -> [Field; NT + 1] {
//         array_concat(self.inner.serialize(), [self.counter as Field])
//     }
// }

pub fn array_concat<T, let N: u32, let M: u32>(array1: [T; N], array2: [T; M]) -> [T; N + M] {
    let mut result = [array1[0]; N + M];
    for i in 1..N {
        result[i] = array1[i];
    }
    for i in 0..M {
        result[i + N] = array2[i];
    }
    result
}

You should get the following panic:

Image

Nargo Version

nargo version = 1.0.0-beta.1 noirc version = 1.0.0-beta.1+74f0a50ed44e5712 (git version hash: 74f0a50ed44e5712, is dirty: false)

@jfecher
Copy link
Contributor

jfecher commented Jan 6, 2025

impl<T> Serialize for Counted<T> where T: Serialize {
    type N = <T as Serialize>::N + 1;

    fn serialize(self) -> [Field; Self::N] {
        array_concat(self.inner.serialize(), [self.counter as Field])
    }
}

This should use let N = ... here but we shouldn't panic either way.
From the error message, this looks related to #6383

@benesjan
Copy link
Contributor Author

benesjan commented Jan 6, 2025

This should use let N = ... here but we shouldn't panic either way.

@jfecher I specifically wanted to point out the panic here.

If you think #6383 tracks the issue sufficiently feel free to close this one. Thanks

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
Status: 📋 Backlog
Development

No branches or pull requests

2 participants