Skip to content

Commit

Permalink
fix fromFields for DynamicArray
Browse files Browse the repository at this point in the history
  • Loading branch information
phn210 committed Nov 14, 2023
1 parent b3016b7 commit 4775d65
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@auxo-dev/auxo-libs",
"version": "0.2.8",
"version": "0.2.9",
"description": "",
"author": "",
"license": "Apache-2.0",
Expand Down
12 changes: 3 additions & 9 deletions src/DynamicArray.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ describe('DynamicArray', () => {
);
fieldDeserialized.length.assertEquals(fieldArray.length);
for (let i = 0; i < fieldArray.values.length; i++) {
(fieldDeserialized as DynamicFieldArray)
.get(Field(i))
.assertEquals(fieldArray.get(Field(i)));
fieldDeserialized.get(Field(i)).assertEquals(fieldArray.get(Field(i)));
}

// Group
Expand All @@ -69,9 +67,7 @@ describe('DynamicArray', () => {
);
groupDeserialized.length.assertEquals(groupArray.length);
for (let i = 0; i < groupArray.values.length; i++) {
(groupDeserialized as DynamicGroupArray)
.get(Field(i))
.assertEquals(groupArray.get(Field(i)));
groupDeserialized.get(Field(i)).assertEquals(groupArray.get(Field(i)));
}

// Scalar
Expand All @@ -86,9 +82,7 @@ describe('DynamicArray', () => {
);
scalarDeserialized.length.assertEquals(scalarArray.length);
for (let i = 0; i < scalarArray.values.length; i++) {
(scalarDeserialized as DynamicScalarArray)
.get(Field(i))
.assertEquals(scalarArray.get(Field(i)));
scalarDeserialized.get(Field(i)).assertEquals(scalarArray.get(Field(i)));
}
});
});
17 changes: 16 additions & 1 deletion src/DynamicArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export function PublicKeyDynamicArray(maxLength: number) {

export function DynamicArray<T>(type: ProvablePure<T>, maxLength: number) {
const _type = hashable(type);

return class _DynamicArray extends Struct({
length: Field,
values: Provable.Array(type, maxLength),
Expand All @@ -54,6 +53,22 @@ export function DynamicArray<T>(type: ProvablePure<T>, maxLength: number) {
return new _DynamicArray(values);
}

static fromFields(fields: Field[]): _DynamicArray {
let length = fields[0];
let sizeInFields = type.sizeInFields();
let values: T[] = [];
Provable.asProver(() => {
for (let i = 0; i < Number(length); i++) {
values.push(
type.fromFields(
fields.slice(1 + sizeInFields * i, 1 + sizeInFields * (i + 1))
)
);
}
});
return new _DynamicArray(values);
}

static empty(length?: Field): _DynamicArray {
const arr = new _DynamicArray();
arr.length = length ?? Field(0);
Expand Down

0 comments on commit 4775d65

Please sign in to comment.