Skip to content

Commit

Permalink
feat(stdlib): Update stdlib to use explicit numeric generics (#5306)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Builds upon changes in #5155 as to
avoid warnings in the stdlib.

## Summary\*

This PR simply switches away from all usages of implicit numeric
generics in the stdlib as to avoid the warning created in #5155.

## Additional Context



## Documentation\*

Check one:
- [X] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [X] I have tested the changes locally.
- [X] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: jfecher <[email protected]>
  • Loading branch information
vezenovm and jfecher authored Jun 25, 2024
1 parent cb9db55 commit 8456185
Show file tree
Hide file tree
Showing 28 changed files with 85 additions and 76 deletions.
2 changes: 1 addition & 1 deletion compiler/noirc_driver/tests/stdlib_warnings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn stdlib_does_not_produce_constant_warnings() -> Result<(), ErrorsAndWarnings>
let ((), warnings) =
noirc_driver::check_crate(&mut context, root_crate_id, false, false, false)?;

assert_eq!(warnings, Vec::new(), "stdlib is producing warnings");
assert_eq!(warnings, Vec::new(), "stdlib is producing {} warnings", warnings.len());

Ok(())
}
2 changes: 1 addition & 1 deletion noir_stdlib/src/aes128.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[foreign(aes128_encrypt)]
// docs:start:aes128
pub fn aes128_encrypt<N>(input: [u8; N], iv: [u8; 16], key: [u8; 16]) -> [u8] {}
pub fn aes128_encrypt<let N: u32>(input: [u8; N], iv: [u8; 16], key: [u8; 16]) -> [u8] {}
// docs:end:aes128
4 changes: 2 additions & 2 deletions noir_stdlib/src/array.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::cmp::Ord;

// TODO: Once we fully move to the new SSA pass this module can be removed and replaced
// by the methods in the `slice` module
impl<T, N> [T; N] {
impl<T, let N: u32> [T; N] {
#[builtin(array_len)]
pub fn len(self) -> u32 {}

Expand Down Expand Up @@ -110,7 +110,7 @@ impl<T, N> [T; N] {

// helper function used to look up the position of a value in an array of Field
// Note that function returns 0 if the value is not found
unconstrained fn find_index<N>(a: [u32; N], find: u32) -> u32 {
unconstrained fn find_index<let N: u32>(a: [u32; N], find: u32) -> u32 {
let mut result = 0;
for i in 0..a.len() {
if a[i] == find {
Expand Down
6 changes: 3 additions & 3 deletions noir_stdlib/src/cmp.nr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl Eq for i64 { fn eq(self, other: i64) -> bool { self == other } }
impl Eq for () { fn eq(_self: Self, _other: ()) -> bool { true } }
impl Eq for bool { fn eq(self, other: bool) -> bool { self == other } }

impl<T, N> Eq for [T; N] where T: Eq {
impl<T, let N: u32> Eq for [T; N] where T: Eq {
fn eq(self, other: [T; N]) -> bool {
let mut result = true;
for i in 0 .. self.len() {
Expand All @@ -38,7 +38,7 @@ impl<T> Eq for [T] where T: Eq {
}
}

impl<N> Eq for str<N> {
impl<let N: u32> Eq for str<N> {
fn eq(self, other: str<N>) -> bool {
let self_bytes = self.as_bytes();
let other_bytes = other.as_bytes();
Expand Down Expand Up @@ -203,7 +203,7 @@ impl Ord for bool {
}
}

impl<T, N> Ord for [T; N] where T: Ord {
impl<T, let N: u32> Ord for [T; N] where T: Ord {
// The first non-equal element of both arrays determines
// the ordering for the whole array.
fn cmp(self, other: [T; N]) -> Ordering {
Expand Down
14 changes: 7 additions & 7 deletions noir_stdlib/src/collections/bounded_vec.nr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{cmp::Eq, convert::From};

struct BoundedVec<T, MaxLen> {
struct BoundedVec<T, let MaxLen: u32> {
storage: [T; MaxLen],
len: u32,
}

impl<T, MaxLen> BoundedVec<T, MaxLen> {
impl<T, let MaxLen: u32> BoundedVec<T, MaxLen> {
pub fn new() -> Self {
let zeroed = crate::unsafe::zeroed();
BoundedVec { storage: [zeroed; MaxLen], len: 0 }
Expand Down Expand Up @@ -61,7 +61,7 @@ impl<T, MaxLen> BoundedVec<T, MaxLen> {
self.storage
}

pub fn extend_from_array<Len>(&mut self, array: [T; Len]) {
pub fn extend_from_array<let Len: u32>(&mut self, array: [T; Len]) {
let new_len = self.len + array.len();
assert(new_len <= MaxLen, "extend_from_array out of bounds");
for i in 0..array.len() {
Expand All @@ -79,7 +79,7 @@ impl<T, MaxLen> BoundedVec<T, MaxLen> {
self.len = new_len;
}

pub fn extend_from_bounded_vec<Len>(&mut self, vec: BoundedVec<T, Len>) {
pub fn extend_from_bounded_vec<let Len: u32>(&mut self, vec: BoundedVec<T, Len>) {
let append_len = vec.len();
let new_len = self.len + append_len;
assert(new_len <= MaxLen, "extend_from_bounded_vec out of bounds");
Expand All @@ -94,7 +94,7 @@ impl<T, MaxLen> BoundedVec<T, MaxLen> {
self.len = new_len;
}

pub fn from_array<Len>(array: [T; Len]) -> Self {
pub fn from_array<let Len: u32>(array: [T; Len]) -> Self {
assert(Len <= MaxLen, "from array out of bounds");
let mut vec: BoundedVec<T, MaxLen> = BoundedVec::new();
vec.extend_from_array(array);
Expand Down Expand Up @@ -134,7 +134,7 @@ impl<T, MaxLen> BoundedVec<T, MaxLen> {
}
}

impl<T, MaxLen> Eq for BoundedVec<T, MaxLen> where T: Eq {
impl<T, let MaxLen: u32> Eq for BoundedVec<T, MaxLen> where T: Eq {
fn eq(self, other: BoundedVec<T, MaxLen>) -> bool {
// TODO: https://github.com/noir-lang/noir/issues/4837
//
Expand All @@ -145,7 +145,7 @@ impl<T, MaxLen> Eq for BoundedVec<T, MaxLen> where T: Eq {
}
}

impl<T, MaxLen, Len> From<[T; Len]> for BoundedVec<T, MaxLen> {
impl<T, let MaxLen: u32, let Len: u32> From<[T; Len]> for BoundedVec<T, MaxLen> {
fn from(array: [T; Len]) -> BoundedVec<T, MaxLen> {
BoundedVec::from_array(array)
}
Expand Down
8 changes: 4 additions & 4 deletions noir_stdlib/src/collections/map.nr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ global MAX_LOAD_FACTOR_DEN0MINATOR = 4;
// Size of the underlying table must be known at compile time.
// It is advised to select capacity N as a power of two, or a prime number
// because utilized probing scheme is best tailored for it.
struct HashMap<K, V, N, B> {
struct HashMap<K, V, let N: u32, B> {
_table: [Slot<K, V>; N],

// Amount of valid elements in the map.
Expand Down Expand Up @@ -77,7 +77,7 @@ impl<K, V> Slot<K, V> {
// While conducting lookup, we iterate attempt from 0 to N - 1 due to heuristic,
// that if we have went that far without finding desired,
// it is very unlikely to be after - performance will be heavily degraded.
impl<K, V, N, B, H> HashMap<K, V, N, B> {
impl<K, V, let N: u32, B, H> HashMap<K, V, N, B> {
// Creates a new instance of HashMap with specified BuildHasher.
// docs:start:with_hasher
pub fn with_hasher(_build_hasher: B) -> Self
Expand Down Expand Up @@ -424,7 +424,7 @@ impl<K, V, N, B, H> HashMap<K, V, N, B> {
// equal sets of key-value entries,
// thus one is a subset of the other and vice versa.
// docs:start:eq
impl<K, V, N, B, H> Eq for HashMap<K, V, N, B>
impl<K, V, let N: u32, B, H> Eq for HashMap<K, V, N, B>
where
K: Eq + Hash,
V: Eq,
Expand Down Expand Up @@ -460,7 +460,7 @@ where
}

// docs:start:default
impl<K, V, N, B, H> Default for HashMap<K, V, N, B>
impl<K, V, let N: u32, B, H> Default for HashMap<K, V, N, B>
where
B: BuildHasher<H> + Default,
H: Hasher + Default
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/default.nr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Default for i64 { fn default() -> i64 { 0 } }
impl Default for () { fn default() -> () { () } }
impl Default for bool { fn default() -> bool { false } }

impl<T, N> Default for [T; N] where T: Default {
impl<T, let N: u32> Default for [T; N] where T: Default {
fn default() -> [T; N] {
[T::default(); N]
}
Expand Down
8 changes: 4 additions & 4 deletions noir_stdlib/src/ec/montcurve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ mod affine {

// Scalar multiplication with scalar represented by a bit array (little-endian convention).
// If k is the natural number represented by `bits`, then this computes p + ... + p k times.
fn bit_mul<N>(self, bits: [u1; N], p: Point) -> Point {
fn bit_mul<let N: u32>(self, bits: [u1; N], p: Point) -> Point {
self.into_tecurve().bit_mul(bits, p.into_tecurve()).into_montcurve()
}

Expand All @@ -124,7 +124,7 @@ mod affine {
}

// Multi-scalar multiplication (n[0]*p[0] + ... + n[N]*p[N], where * denotes scalar multiplication)
fn msm<N>(self, n: [Field; N], p: [Point; N]) -> Point {
fn msm<let N: u32>(self, n: [Field; N], p: [Point; N]) -> Point {
let mut out = Point::zero();

for i in 0..N {
Expand Down Expand Up @@ -315,7 +315,7 @@ mod curvegroup {

// Scalar multiplication with scalar represented by a bit array (little-endian convention).
// If k is the natural number represented by `bits`, then this computes p + ... + p k times.
fn bit_mul<N>(self, bits: [u1; N], p: Point) -> Point {
fn bit_mul<let N: u32>(self, bits: [u1; N], p: Point) -> Point {
self.into_tecurve().bit_mul(bits, p.into_tecurve()).into_montcurve()
}

Expand All @@ -325,7 +325,7 @@ mod curvegroup {
}

// Multi-scalar multiplication (n[0]*p[0] + ... + n[N]*p[N], where * denotes scalar multiplication)
fn msm<N>(self, n: [Field; N], p: [Point; N]) -> Point {
fn msm<let N: u32>(self, n: [Field; N], p: [Point; N]) -> Point {
let mut out = Point::zero();

for i in 0..N {
Expand Down
8 changes: 4 additions & 4 deletions noir_stdlib/src/ec/swcurve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ mod affine {

// Scalar multiplication with scalar represented by a bit array (little-endian convention).
// If k is the natural number represented by `bits`, then this computes p + ... + p k times.
fn bit_mul<N>(self, bits: [u1; N], p: Point) -> Point {
fn bit_mul<let N: u32>(self, bits: [u1; N], p: Point) -> Point {
self.into_group().bit_mul(bits, p.into_group()).into_affine()
}

Expand All @@ -144,7 +144,7 @@ mod affine {
}

// Multi-scalar multiplication (n[0]*p[0] + ... + n[N]*p[N], where * denotes scalar multiplication)
pub fn msm<N>(self, n: [Field; N], p: [Point; N]) -> Point {
pub fn msm<let N: u32>(self, n: [Field; N], p: [Point; N]) -> Point {
let mut out = Point::zero();

for i in 0..N {
Expand Down Expand Up @@ -336,7 +336,7 @@ mod curvegroup {

// Scalar multiplication with scalar represented by a bit array (little-endian convention).
// If k is the natural number represented by `bits`, then this computes p + ... + p k times.
fn bit_mul<N>(self, bits: [u1; N], p: Point) -> Point {
fn bit_mul<let N: u32>(self, bits: [u1; N], p: Point) -> Point {
let mut out = Point::zero();

for i in 0..N {
Expand All @@ -363,7 +363,7 @@ mod curvegroup {
}

// Multi-scalar multiplication (n[0]*p[0] + ... + n[N]*p[N], where * denotes scalar multiplication)
fn msm<N>(self, n: [Field; N], p: [Point; N]) -> Point {
fn msm<let N: u32>(self, n: [Field; N], p: [Point; N]) -> Point {
let mut out = Point::zero();

for i in 0..N {
Expand Down
8 changes: 4 additions & 4 deletions noir_stdlib/src/ec/tecurve.nr
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ mod affine {

// Scalar multiplication with scalar represented by a bit array (little-endian convention).
// If k is the natural number represented by `bits`, then this computes p + ... + p k times.
fn bit_mul<N>(self, bits: [u1; N], p: Point) -> Point {
fn bit_mul<let N: u32>(self, bits: [u1; N], p: Point) -> Point {
self.into_group().bit_mul(bits, p.into_group()).into_affine()
}

Expand All @@ -142,7 +142,7 @@ mod affine {
}

// Multi-scalar multiplication (n[0]*p[0] + ... + n[N]*p[N], where * denotes scalar multiplication)
fn msm<N>(self, n: [Field; N], p: [Point; N]) -> Point {
fn msm<let N: u32>(self, n: [Field; N], p: [Point; N]) -> Point {
let mut out = Point::zero();

for i in 0..N {
Expand Down Expand Up @@ -340,7 +340,7 @@ mod curvegroup {

// Scalar multiplication with scalar represented by a bit array (little-endian convention).
// If k is the natural number represented by `bits`, then this computes p + ... + p k times.
fn bit_mul<N>(self, bits: [u1; N], p: Point) -> Point {
fn bit_mul<let N: u32>(self, bits: [u1; N], p: Point) -> Point {
let mut out = Point::zero();

for i in 0..N {
Expand All @@ -367,7 +367,7 @@ mod curvegroup {
}

// Multi-scalar multiplication (n[0]*p[0] + ... + n[N]*p[N], where * denotes scalar multiplication)
fn msm<N>(self, n: [Field; N], p: [Point; N]) -> Point {
fn msm<let N: u32>(self, n: [Field; N], p: [Point; N]) -> Point {
let mut out = Point::zero();

for i in 0..N {
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/ecdsa_secp256k1.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[foreign(ecdsa_secp256k1)]
// docs:start:ecdsa_secp256k1
pub fn verify_signature<N>(
pub fn verify_signature<let N: u32>(
public_key_x: [u8; 32],
public_key_y: [u8; 32],
signature: [u8; 64],
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/ecdsa_secp256r1.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[foreign(ecdsa_secp256r1)]
// docs:start:ecdsa_secp256r1
pub fn verify_signature<N>(
pub fn verify_signature<let N: u32>(
public_key_x: [u8; 32],
public_key_y: [u8; 32],
signature: [u8; 64],
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/embedded_curve_ops.nr
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl EmbeddedCurveScalar {
// underlying proof system.
#[foreign(multi_scalar_mul)]
// docs:start:multi_scalar_mul
pub fn multi_scalar_mul<N>(
pub fn multi_scalar_mul<let N: u32>(
points: [EmbeddedCurvePoint; N],
scalars: [EmbeddedCurveScalar; N]
) -> [Field; 3]
Expand Down
Loading

0 comments on commit 8456185

Please sign in to comment.