Skip to content

Commit b38a3ad

Browse files
committed
unit test with copy
1 parent a6b787f commit b38a3ad

File tree

2 files changed

+60
-21
lines changed

2 files changed

+60
-21
lines changed

src/lib/provable/dynamic-array.ts

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ function DynamicArray<
5757
* Note: Both the actual length and the values beyond the original ones will
5858
* be constant.
5959
*/
60-
from(v: (ProvableValue | Value)[] | DynamicArrayBase<ProvableValue, Value>): DynamicArrayBase<ProvableValue, Value>;
60+
from(
61+
v: (ProvableValue | Value)[] | DynamicArrayBase<ProvableValue, Value>
62+
): DynamicArrayBase<ProvableValue, Value>;
6163
} {
6264
let innerType: Provable<ProvableValue, Value> = ProvableType.get(type);
6365

@@ -308,32 +310,56 @@ class DynamicArrayBase<ProvableValue = any, Value = any> {
308310
/**
309311
* Increments the length of the current array by n elements, checking that the
310312
* new length is within the capacity.
313+
*
314+
* An optional error message can be provided to be used in case the inner
315+
* assertion fails.
316+
*
317+
* @param n
318+
* @param message
311319
*/
312-
increaseLengthBy(n: Field): void {
320+
increaseLengthBy(n: Field, message?: string): void {
321+
let errorMessage =
322+
message ??
323+
`increaseLengthBy: cannot increase length because provided n would exceed capacity ${this.capacity}.`;
324+
313325
let newLength = this.length.add(n).seal();
314-
newLength.lessThanOrEqual(new Field(this.capacity)).assertTrue();
326+
newLength.assertLessThanOrEqual(new Field(this.capacity), errorMessage);
315327
this.length = newLength;
316328
}
317329

318330
/**
319-
* Decrements the length of the current array by n elements, checking that the
320-
* n is less or equal than the current length.
331+
* Decrements the length of the current array by `n` elements, checking that
332+
* the `n` is less or equal than the current length.
333+
*
334+
* An optional error message can be provided to be used in case the inner
335+
* assertion fails.
336+
*
337+
* @param n
338+
* @param message
321339
*/
322-
decreaseLengthBy(n: Field): void {
340+
decreaseLengthBy(n: Field, message?: string): void {
341+
let errorMessage =
342+
message ??
343+
`decreaseLengthBy: cannot decrease length because provided n is larger than current array length ${this.length}`;
344+
323345
let oldLength = this.length;
324-
n.assertLessThanOrEqual(this.length);
346+
n.assertLessThanOrEqual(this.length, errorMessage);
325347
this.length = oldLength.sub(n).seal();
326348
}
327349

328350
/**
329351
* Sets the length of the current array to a new value, checking that the
330352
* new length is less or equal than the capacity.
331-
*
332-
* @param newLength
333-
*
353+
*
354+
* An optional error message can be provided to be used in case the inner
355+
* assertion fails.
356+
*
357+
* @param newLength
358+
* @param message
359+
*
334360
* **Warning**: This does not change (add nor remove) the values of the array.
335361
*/
336-
setLengthTo(n: Field): void {
362+
setLengthTo(n: Field, message?: string): void {
337363
n.assertLessThanOrEqual(new Field(this.capacity));
338364
this.length = n;
339365
}
@@ -436,7 +462,11 @@ class DynamicArrayBase<ProvableValue = any, Value = any> {
436462
}
437463

438464
/**
439-
* @returns a new DynamicArray instance with the same values as the current
465+
* Copies the current dynamic array, returning a new instance with the same
466+
* values and length.
467+
*
468+
* @returns a new DynamicArray instance with the same values as the current.
469+
*
440470
*/
441471
copy(): this {
442472
let newArr = new (<any>this.constructor)();

src/lib/provable/test/dynamic-array.unit-test.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { Provable } from '../provable.js';
2727
assert(fromArray.capacity === 8);
2828

2929
let fromLength = new Bytestring(undefined, new Field(0));
30-
assert(fromLength.length.equals(new Field(0)))
30+
assert(fromLength.length.equals(new Field(0)));
3131
assert(fromLength.capacity === 8);
3232

3333
let fromArrayLength = new Bytestring(
@@ -38,7 +38,18 @@ import { Provable } from '../provable.js';
3838
assert(fromArrayLength.get(new Field(1)).value.equals(new Field(2)));
3939
assert(fromArrayLength.get(new Field(2)).value.equals(new Field(3)));
4040
fromArrayLength.getOption(new Field(3)).assertNone();
41-
41+
42+
// Copy into new dynamic arrays
43+
let copySame = fromArray.copy();
44+
assert(copySame.length.equals(fromArray.length));
45+
assert(copySame.capacity === fromArray.capacity);
46+
assert(copySame.get(new Field(0)).value.equals(fromArray.get(new Field(1)).value));
47+
assert(copySame.get(new Field(1)).value.equals(fromArray.get(new Field(2)).value));
48+
assert(copySame.get(new Field(2)).value.equals(fromArray.get(new Field(3)).value));
49+
for (let i = 3; i < 8; i++) {
50+
copySame.getOption(new Field(i)).assertNone();
51+
}
52+
4253
// Initialize an empty dynamic array
4354
let bytes = new Bytestring();
4455

@@ -349,17 +360,16 @@ import { Provable } from '../provable.js';
349360

350361
// Provable behaviour
351362
{
352-
class Bytestring extends DynamicArray(UInt8, { capacity: 8 }) {}
363+
class Bytestring extends DynamicArray(UInt8, { capacity: 8 }) {}
353364

354-
await Provable.runAndCheck(() => {
355-
let b = Provable.witness(Bytestring, () => new Bytestring());
356-
b.push(UInt8.from(1));
357-
});
365+
await Provable.runAndCheck(() => {
366+
let b = Provable.witness(Bytestring, () => new Bytestring());
367+
b.push(UInt8.from(1));
368+
});
358369
}
359370

360371
// In-circuit test for dynamic arrays
361372
{
362-
363373
let List = ZkProgram({
364374
name: 'dynamicarrays-circuit',
365375
methods: {
@@ -416,7 +426,6 @@ await Provable.runAndCheck(() => {
416426
assert(longerArray.get(new Field(0)).value.equals(sameArray.get(new Field(0)).value));
417427
assert(otherArray.get(new Field(0)).value.equals(bytes.get(new Field(0)).value));
418428

419-
420429
// Mapping over elements should work correctly
421430
bytes.push(v0.add(new UInt8(1)));
422431
bytes.push(v0.mul(new UInt8(0)));

0 commit comments

Comments
 (0)