From 2065ac67e73f2ca2362f8b0a8cc1e3db796b38d3 Mon Sep 17 00:00:00 2001 From: querolita Date: Mon, 25 Nov 2024 16:53:23 +0100 Subject: [PATCH 1/7] add pad and zip for arrays, improve chunk error message --- src/lib/util/arrays.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/lib/util/arrays.ts b/src/lib/util/arrays.ts index 891de81015..d282ba607e 100644 --- a/src/lib/util/arrays.ts +++ b/src/lib/util/arrays.ts @@ -1,9 +1,12 @@ import { assert } from './errors.js'; -export { chunk, chunkString }; +export { chunk, chunkString, zip, pad }; function chunk(array: T[], size: number): T[][] { - assert(array.length % size === 0, 'invalid input length'); + assert( + array.length % size === 0, + 'chunk(): invalid input length, it must be a multiple of ${size}' + ); return Array.from({ length: array.length / size }, (_, i) => array.slice(size * i, size * (i + 1)) ); @@ -12,3 +15,16 @@ function chunk(array: T[], size: number): T[][] { function chunkString(str: string, size: number): string[] { return chunk([...str], size).map((c) => c.join('')); } + +function zip(a: T[], b: S[]) { + assert(a.length === b.length, 'zip(): arrays of unequal length'); + return a.map((a, i): [T, S] => [a, b[i]!]); +} + +function pad(array: T[], size: number, value: T): T[] { + assert( + array.length <= size, + `target size ${size} should be greater or equal than the length of the array ${array.length}` + ); + return array.concat(Array.from({ length: size - array.length }, () => value)); +} From 1da031ae6e470fe169fa225455bec52ce1d87c03 Mon Sep 17 00:00:00 2001 From: querolita Date: Mon, 25 Nov 2024 16:57:34 +0100 Subject: [PATCH 2/7] update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index da97589a32..348b2a3fee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Support secp256r1 in elliptic curve and ECDSA gadgets https://github.com/o1-labs/o1js/pull/1885 +- Provide `zip` and `pad` for the array utility https://github.com/o1-labs/o1js/pull/1921 + ### Fixed - Witness generation error in `Gadgets.arrayGet()` when accessing out-of-bounds indices https://github.com/o1-labs/o1js/pull/1886 From e34353edcf107623c67539bcae5980b0e23e9115 Mon Sep 17 00:00:00 2001 From: querolita Date: Mon, 25 Nov 2024 16:59:39 +0100 Subject: [PATCH 3/7] modify changelog section --- CHANGELOG.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 348b2a3fee..fffa8f30db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,14 +21,16 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Compiling stuck in the browser for recursive zkprograms https://github.com/o1-labs/o1js/pull/1906 +### Added + +- Provide `zip` and `pad` for the array utility https://github.com/o1-labs/o1js/pull/1921 + ## [2.1.0](https://github.com/o1-labs/o1js/compare/b04520d...e1bac02) - 2024-11-13 ### Added - Support secp256r1 in elliptic curve and ECDSA gadgets https://github.com/o1-labs/o1js/pull/1885 -- Provide `zip` and `pad` for the array utility https://github.com/o1-labs/o1js/pull/1921 - ### Fixed - Witness generation error in `Gadgets.arrayGet()` when accessing out-of-bounds indices https://github.com/o1-labs/o1js/pull/1886 From 4e4f8d8a1408faa17842de14bc22f8e997643741 Mon Sep 17 00:00:00 2001 From: querolita Date: Tue, 26 Nov 2024 16:39:35 +0100 Subject: [PATCH 4/7] remove pad and zip from external changelog --- CHANGELOG.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fffa8f30db..da97589a32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,10 +21,6 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Compiling stuck in the browser for recursive zkprograms https://github.com/o1-labs/o1js/pull/1906 -### Added - -- Provide `zip` and `pad` for the array utility https://github.com/o1-labs/o1js/pull/1921 - ## [2.1.0](https://github.com/o1-labs/o1js/compare/b04520d...e1bac02) - 2024-11-13 ### Added From d957245ba98868dbb371bdf9005e47cec249ac05 Mon Sep 17 00:00:00 2001 From: querolita Date: Wed, 27 Nov 2024 13:44:38 +0100 Subject: [PATCH 5/7] change behavior of zip to allow nonequal array lengths --- src/lib/util/arrays.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib/util/arrays.ts b/src/lib/util/arrays.ts index d282ba607e..99a33fc0ac 100644 --- a/src/lib/util/arrays.ts +++ b/src/lib/util/arrays.ts @@ -17,7 +17,10 @@ function chunkString(str: string, size: number): string[] { } function zip(a: T[], b: S[]) { - assert(a.length === b.length, 'zip(): arrays of unequal length'); + assert( + a.length <= b.length, + 'zip(): first array must be at least as long as the second array' + ); return a.map((a, i): [T, S] => [a, b[i]!]); } From 02c6d5680be008c06a60544a055da629a8c22d28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ana=C3=AFs=20Querol?= Date: Wed, 27 Nov 2024 13:46:43 +0100 Subject: [PATCH 6/7] use back quotes to correctly format chunk error message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Boray Saygılıer --- src/lib/util/arrays.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/util/arrays.ts b/src/lib/util/arrays.ts index 99a33fc0ac..f3a59b5388 100644 --- a/src/lib/util/arrays.ts +++ b/src/lib/util/arrays.ts @@ -5,7 +5,7 @@ export { chunk, chunkString, zip, pad }; function chunk(array: T[], size: number): T[][] { assert( array.length % size === 0, - 'chunk(): invalid input length, it must be a multiple of ${size}' + `chunk(): invalid input length, it must be a multiple of ${size}` ); return Array.from({ length: array.length / size }, (_, i) => array.slice(size * i, size * (i + 1)) From 1eac01ed8e96f79854113f1ad8d5379eb064f5d3 Mon Sep 17 00:00:00 2001 From: querolita Date: Wed, 27 Nov 2024 14:47:44 +0100 Subject: [PATCH 7/7] fix assertion message --- src/lib/util/arrays.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/util/arrays.ts b/src/lib/util/arrays.ts index 99a33fc0ac..320db44001 100644 --- a/src/lib/util/arrays.ts +++ b/src/lib/util/arrays.ts @@ -19,7 +19,7 @@ function chunkString(str: string, size: number): string[] { function zip(a: T[], b: S[]) { assert( a.length <= b.length, - 'zip(): first array must be at least as long as the second array' + 'zip(): second array must be at least as long as the first array' ); return a.map((a, i): [T, S] => [a, b[i]!]); }