Skip to content

Commit

Permalink
fix popping multiple elements
Browse files Browse the repository at this point in the history
  • Loading branch information
querolita committed Oct 31, 2024
1 parent 5fb42f8 commit aceb442
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 17 deletions.
12 changes: 5 additions & 7 deletions src/lib/provable/dynamic-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {
Option,
Provable,
provable as struct,
UInt32,
Gadgets,
type ProvableHashable,
} from 'o1js';
import { ProvableType } from './types/provable-intf.js';
Expand Down Expand Up @@ -324,15 +322,15 @@ class DynamicArrayBase<T = any, V = any> {
dec.assertLessThanOrEqual(oldLength);
this.length = oldLength.sub(dec).seal();

let NULL = ProvableType.synthesize(this.innerType);
let NULL: T = ProvableType.synthesize(this.innerType);
if (n !== undefined) {
// set the last n elements to NULL
for (let i = 0; i <= this.capacity; i++) {
for (let i = 0; i < this.capacity; i++) {
this.array[i] = Provable.if(
new Field(i).greaterThan(this.length),
new Field(i).lessThanOrEqual(this.length),
this.innerType,
NULL,
this.array[i]
this.array[i],
NULL
);
}
} else {
Expand Down
12 changes: 2 additions & 10 deletions src/lib/provable/test/dynamic-array-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,11 @@ let List = ZkProgram({
}

// Popping zero elements should not change the array
//bytes.pop(new Field(0));
bytes.pop(new Field(0));

// Popping multiple elements should decrease length by the specified amount
assert(bytes.length.equals(new Field(8)));
// TODO: substitute for bytes.pop(new Field(8));
bytes.pop();
bytes.pop();
bytes.pop();
bytes.pop();
bytes.pop();
bytes.pop();
bytes.pop();
bytes.pop();
bytes.pop(new Field(8));
assert(bytes.length.equals(new Field(0)));

// Cannot pop more elements than the current length
Expand Down

0 comments on commit aceb442

Please sign in to comment.