Skip to content

Commit

Permalink
new version compatible to latest o1js changes
Browse files Browse the repository at this point in the history
  • Loading branch information
phn210 committed Jun 17, 2024
1 parent b96d736 commit 538f53c
Show file tree
Hide file tree
Showing 15 changed files with 1,811 additions and 1,327 deletions.
1 change: 1 addition & 0 deletions jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ export default {
moduleNameMapper: {
'^(\\.{1,2}/.+)\\.js$': '$1',
},
testMatch: ['**/?(*.)+(spec|test).ts'],
};
2,662 changes: 1,430 additions & 1,232 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@auxo-dev/auxo-libs",
"version": "0.5.7",
"version": "0.6.0",
"description": "",
"author": "",
"license": "Apache-2.0",
Expand All @@ -12,6 +12,7 @@
"dkg",
"cryptography"
],
"type": "module",
"main": "./build/esm/src/index.js",
"types": "./build/types/src/index.d.ts",
"scripts": {
Expand Down Expand Up @@ -51,14 +52,14 @@
"@typescript-eslint/parser": "^5.5.0",
"eslint": "^8.7.0",
"eslint-plugin-o1js": "^0.4.0",
"jest": "^28.1.3",
"husky": "^7.0.1",
"jest": "^27.3.1",
"lint-staged": "^11.0.1",
"prettier": "^2.3.2",
"ts-jest": "^27.0.7",
"typescript": "^4.7.2"
"ts-jest": "^28.0.8",
"typescript": "^5.1"
},
"dependencies": {
"o1js": "0.18.0"
"o1js": "1.3.1"
}
}
28 changes: 12 additions & 16 deletions src/Bit255.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import { Bool, Field, Poseidon, Provable, Scalar, Struct } from 'o1js';
import { CustomScalar } from './CustomScalar.js';
import { Bool, Field, Gadgets, Poseidon, Provable, Scalar, Struct } from 'o1js';

// WARNING - Convert between Scalar and Bit255 does not preserve bigint value
export class Bit255 extends Struct({
head: Field,
tail: Field,
}) {
static fromScalar(scalar: Scalar): Bit255 {
let bits = scalar.toFields().map((e) => Bool.fromFields([e]));
return new Bit255({
head: Field.fromBits(bits.slice(0, 127)),
tail: Field.fromBits(bits.slice(127)),
head: scalar.toFields()[0],
tail: scalar.toFields()[1],
});
}

static toScalar(b: Bit255): Scalar {
return new CustomScalar({
head: b.head,
tail: b.tail,
}).toScalar();
return Scalar.fromFields([b.head, b.tail]);
}

static fromFields(fields: Field[]): Bit255 {
Expand All @@ -37,16 +32,17 @@ export class Bit255 extends Struct({
}

static xor(a: Bit255, b: Bit255): Bit255 {
return Provable.witness(Bit255, () =>
Bit255.fromBigInt(a.toBigInt() ^ b.toBigInt())
);
return new Bit255({
head: Gadgets.xor(a.head, b.head, 1),
tail: Gadgets.xor(a.tail, b.tail, 254),
});
}

static fromBits(bits: Bool[]): Bit255 {
if (bits.length !== 255) throw new Error('Invalid input length');
return new Bit255({
head: Field.fromBits(bits.slice(0, 127)),
tail: Field.fromBits(bits.slice(127)),
head: Field.fromBits(bits.slice(0, 1)),
tail: Field.fromBits(bits.slice(1)),
});
}

Expand All @@ -64,8 +60,8 @@ export class Bit255 extends Struct({
static toBigInt(b: Bit255): bigint {
let bits = b.head
.toBits()
.slice(0, 127)
.concat(b.tail.toBits().slice(0, 128));
.slice(0, 1)
.concat(b.tail.toBits().slice(0, 254));
let res = 0n;
for (let i = 0; i < 255; i++) {
if (bits[i].toBoolean()) res += BigInt(Math.pow(2, i));
Expand Down
20 changes: 19 additions & 1 deletion src/CustomScalar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('CustomScalar', () => {
let min = UInt64.zero;
let max = UInt64.MAXINT();
let odd = UInt64.from(BigInt(10 ** 16) + 1n);
let even = UInt64.from(BigInt(10 ** 8) + 1n);
let even = UInt64.from(BigInt(10 ** 8));
expect(CustomScalar.fromUInt64(min).toScalar().toBigInt()).toEqual(
min.toBigInt()
);
Expand All @@ -27,4 +27,22 @@ describe('CustomScalar', () => {
even.toBigInt()
);
});

it('Should assert equality correctly with message', async () => {
let a = CustomScalar.fromScalar(Scalar.from(1n));
let b = CustomScalar.fromScalar(Scalar.from(1n));
let c = CustomScalar.fromScalar(Scalar.from(2n));
expect(() => a.assertEquals(b, 'a and b are equal')).not.toThrow();
expect(() => a.assertEquals(c, 'a and c are not equal')).toThrow();
});

it('Should convert to and from fields correctly', async () => {
let original = CustomScalar.fromScalar(Scalar.random());
expect(original.toFields().length).toEqual(CustomScalar.sizeInFields());
expect(
CustomScalar.fromFields(original.toFields())
.equals(original)
.toBoolean()
).toEqual(true);
});
});
29 changes: 8 additions & 21 deletions src/CustomScalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,26 @@ import { Bool, Field, Poseidon, Scalar, Struct, UInt64 } from 'o1js';
import { fromUInt64ToScalar } from './utils/math.js';

export class CustomScalar extends Struct({
head: Field,
tail: Field,
scalar: Scalar,
}) {
static fromScalar(scalar: Scalar): CustomScalar {
let bits = scalar.toFields().map((e) => Bool.fromFields([e]));
return new CustomScalar({
head: Field.fromBits(bits.slice(0, 127)),
tail: Field.fromBits(bits.slice(127)),
scalar,
});
}

static fromFields(fields: Field[]): CustomScalar {
return new CustomScalar({
head: fields[0],
tail: fields[1],
scalar: Scalar.fromFields(fields),
});
}

static toScalar(scalar: CustomScalar): Scalar {
return Scalar.fromBits(
scalar.head
.toBits()
.slice(0, 127)
.concat(scalar.tail.toBits().slice(0, 128))
);
return scalar.scalar;
}

static toFields(value: { head: Field; tail: Field }): Field[] {
return [value.head].concat([value.tail]);
static toFields({ scalar }: { scalar: Scalar }): Field[] {
return scalar.toFields();
}

static sizeInFields(): number {
Expand All @@ -42,11 +33,11 @@ export class CustomScalar extends Struct({
}

hash(): Field {
return Poseidon.hash(this.toScalar().toFields());
return Poseidon.hash(this.toFields());
}

equals(s: CustomScalar): Bool {
return this.head.equals(s.head).and(this.tail.equals(s.tail));
return this.hash().equals(s.hash());
}

assertEquals(s: CustomScalar, message?: string | undefined): void {
Expand All @@ -60,8 +51,4 @@ export class CustomScalar extends Struct({
toFields(): Field[] {
return CustomScalar.toFields(this);
}

fromFields(fields: Field[]): CustomScalar {
return CustomScalar.fromFields(fields);
}
}
11 changes: 4 additions & 7 deletions src/DynamicArray.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
PublicKeyDynamicArray,
ScalarDynamicArray,
} from './DynamicArray.js';
import { CustomScalar } from './CustomScalar.js';
import { Bit255 } from './Bit255.js';

describe('DynamicArray', () => {
Expand Down Expand Up @@ -83,7 +82,9 @@ describe('DynamicArray', () => {
.assertEquals(
Poseidon.hash([
bit255Array.length,
...bit255Array.values.map((e) => e.toFields()).flat(),
...bit255Array.values
.map((e) => new Bit255(e).toFields())
.flat(),
])
);
});
Expand Down Expand Up @@ -129,11 +130,7 @@ describe('DynamicArray', () => {
});

it('Should serialize ScalarDynamicArray correctly', async () => {
let scalarValues = [
CustomScalar.fromScalar(Scalar.random()),
CustomScalar.fromScalar(Scalar.random()),
CustomScalar.fromScalar(Scalar.random()),
];
let scalarValues = [Scalar.random(), Scalar.random(), Scalar.random()];
let scalarArray = new ScalarArray(scalarValues);
ScalarArray.fromFields(scalarArray.toFields())
.hash()
Expand Down
6 changes: 3 additions & 3 deletions src/DynamicArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
Provable,
ProvablePure,
PublicKey,
Scalar,
Struct,
} from 'o1js';

import { Bit255 } from './Bit255.js';
import { CustomScalar } from './CustomScalar.js';
import { hashable } from './Hashable.js';

export {
Expand Down Expand Up @@ -45,7 +45,7 @@ function GroupDynamicArray(maxLength: number) {

function ScalarDynamicArray(maxLength: number) {
if (maxLength > 128) throw new Error('Exceed maximum size');
return DynamicArray(CustomScalar, maxLength);
return DynamicArray(Scalar, maxLength);
}

function PublicKeyDynamicArray(maxLength: number) {
Expand Down Expand Up @@ -74,7 +74,7 @@ function DynamicArray<T>(type: ProvablePure<T>, maxLength: number) {
}

static hash(value: T): Field {
return Poseidon.hash(type.toFields(value));
return _type.hash(value);
}

static Null(): T {
Expand Down
2 changes: 1 addition & 1 deletion src/StaticArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function StaticArray<T>(type: ProvablePure<T>, length: number) {
}

static hash(value: T): Field {
return Poseidon.hash(type.toFields(value));
return _type.hash(value);
}

static Null(): T {
Expand Down
18 changes: 18 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Cache,
Field,
IncludedTransaction,
PendingTransaction,
Expand All @@ -25,6 +26,8 @@ export {
TxResult,
FetchedActions,
FetchedEvents,
ZkAppOptions,
UtilsOptions,
};

const MAX_RETRY = 3;
Expand Down Expand Up @@ -125,3 +128,18 @@ type FetchedEvents = {
globalSlot: UInt32;
chainStatus: string;
};

type ZkAppOptions = {
name?: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
initArgs?: Record<string, any>;
actionStates?: Field[];
actions?: Field[][];
events?: Field[][];
};

type UtilsOptions = {
cache?: Cache;
profiler?: Profiler;
logger?: Logger;
};
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export {
updateActionState,
// updateActionStateWithHash,
packNumberArray,
unpackNumberArray,
buildAssertMessage,
requireSignature,
requireCaller,
Expand Down
10 changes: 1 addition & 9 deletions src/utils/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,5 @@ const SHIFT_CONST_EVEN = Scalar.from(SHIFT_CONST_ODD)
.toBigInt();

function fromUInt64ToScalar(number: UInt64): Scalar {
return Provable.if(
number.value.isEven(),
Scalar.fromBits(
number.value.div(2).add(Field(SHIFT_CONST_EVEN)).toBits()
),
Scalar.fromBits(
number.value.add(Field(SHIFT_CONST_ODD)).div(2).toBits()
)
);
return Scalar.fromBits(number.value.toBits());
}
Loading

0 comments on commit 538f53c

Please sign in to comment.