Skip to content

Commit

Permalink
update rollup actions and add math utils
Browse files Browse the repository at this point in the history
  • Loading branch information
phn210 committed Oct 13, 2024
1 parent 19c080e commit d8d230d
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 31 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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": "1.0.5",
"version": "1.0.6",
"description": "",
"author": "",
"license": "Apache-2.0",
Expand Down
6 changes: 0 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
export { ActionMask } from './ActionMask.js';

export { Bit255 } from './Bit255.js';

export { CustomScalar } from './CustomScalar.js';

export {
Bit255DynamicArray,
BoolDynamicArray,
Expand All @@ -13,9 +10,6 @@ export {
PublicKeyDynamicArray,
DynamicArray,
} from './DynamicArray.js';

export { IpfsHash } from './IpfsHash.js';

export { StaticArray } from './StaticArray.js';

export * as Utils from './utils/index.js';
7 changes: 6 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ export {
FetchedEvents,
} from './constants.js';

export { divExact, fromUInt64ToScalar, getBitLength } from './math.js';
export {
divExact,
fieldXOR,
fromUInt64ToScalar,
getBitLength,
} from './math.js';

export {
randomAccounts,
Expand Down
20 changes: 17 additions & 3 deletions src/utils/math.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Bool, Field, Scalar, UInt64 } from 'o1js';
import { Bool, Field, Gadgets, Scalar, UInt64 } from 'o1js';

export { divExact, fromUInt64ToScalar, getBitLength };
export { divExact, fieldXOR, fromUInt64ToScalar, getBitLength };

/**
* @deprecated Use `Scalar.fromField` instead.
Expand All @@ -10,9 +10,23 @@ function fromUInt64ToScalar(number: UInt64): Scalar {
}

function getBitLength(N: number): number {
return Math.floor(Math.log2(N)) + 1;
return N == 0 ? 1 : Math.floor(Math.log2(N)) + 1;
}

function divExact(a: Field, b: Field): Bool {
b.assertNotEquals(Field(0));
return a.div(b).lessThanOrEqual(a);
}

function fieldXOR(a: Field, b: Field): Field {
let a240 = Field.fromBits(a.toBits().slice(0, 240));
let b240 = Field.fromBits(b.toBits().slice(0, 240));
let a14 = Field.fromBits(a.toBits().slice(240));
let b14 = Field.fromBits(b.toBits().slice(240));
let xor240 = Gadgets.xor(a240, b240, 240);
let xor14 = Gadgets.xor(a14, b14, 14);
return Field.fromBits([
...xor240.toBits().slice(0, 240),
...xor14.toBits().slice(0, 14),
]);
}
75 changes: 62 additions & 13 deletions src/utils/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,14 @@ import {
} from './network.js';
import { FeePayer, TX_FEE, ZkApp } from './constants.js';
import { getProfiler } from './benchmark.js';
import { fromUInt64ToScalar } from './math.js';
import {
divExact,
fieldXOR,
fromUInt64ToScalar,
getBitLength,
} from './math.js';
import { checkInvalidAction, updateActionState } from './zkApp.js';

describe('Math', () => {
it('Should convert UInt64 to Scalar', async () => {
expect(fromUInt64ToScalar(UInt64.from(0)).toBigInt()).toEqual(0n);
expect(fromUInt64ToScalar(UInt64.from(123456789)).toBigInt()).toEqual(
123456789n
);
expect(
fromUInt64ToScalar(UInt64.from(UInt64.MAXINT())).toBigInt()
).toEqual(UInt64.MAXINT().toBigInt());
});
});

describe('Network', () => {
class TestContract extends SmartContract {
@state(Field) num = State<Field>();
Expand Down Expand Up @@ -326,3 +319,59 @@ describe('Network', () => {
profiler.store();
});
});

describe('Math', () => {
it('Should convert UInt64 to Scalar', async () => {
expect(fromUInt64ToScalar(UInt64.from(0)).toBigInt()).toEqual(0n);
expect(fromUInt64ToScalar(UInt64.from(123456789)).toBigInt()).toEqual(
123456789n
);
expect(
fromUInt64ToScalar(UInt64.from(UInt64.MAXINT())).toBigInt()
).toEqual(UInt64.MAXINT().toBigInt());
});
it('Should convert UInt64 to Scalar', async () => {
expect(fromUInt64ToScalar(UInt64.from(0)).toBigInt()).toEqual(0n);
expect(fromUInt64ToScalar(UInt64.from(123456789)).toBigInt()).toEqual(
123456789n
);
expect(
fromUInt64ToScalar(UInt64.from(UInt64.MAXINT())).toBigInt()
).toEqual(UInt64.MAXINT().toBigInt());
});

it('Should get correct bit length', async () => {
expect(getBitLength(0)).toEqual(1);
expect(getBitLength(1)).toEqual(1);
expect(getBitLength(2)).toEqual(2);
expect(getBitLength(3)).toEqual(2);
expect(getBitLength(4)).toEqual(3);
expect(getBitLength(255)).toEqual(8);
expect(getBitLength(256)).toEqual(9);
});

it('Should perform exact division', async () => {
expect(divExact(Field(10), Field(2)).toBoolean()).toBe(true);
expect(divExact(Field(10), Field(3)).toBoolean()).toBe(false);
expect(divExact(Field(0), Field(1)).toBoolean()).toBe(true);
expect(() => divExact(Field(1), Field(0)).toBoolean()).toThrow();
});

it('Should perform field XOR', async () => {
expect(fieldXOR(Field(0), Field(0)).toBigInt()).toEqual(0n);
expect(fieldXOR(Field(1), Field(0)).toBigInt()).toEqual(1n);
expect(fieldXOR(Field(1), Field(1)).toBigInt()).toEqual(0n);
expect(fieldXOR(Field(123456789), Field(987654321)).toBigInt()).toEqual(
BigInt(123456789) ^ BigInt(987654321)
);
let f1 = Field.random();
let f2 = Field.random();
expect(fieldXOR(f1, f2).toBigInt()).toEqual(
f1.toBigInt() ^ f2.toBigInt()
);
expect(fieldXOR(f1, f1).toBigInt()).toEqual(0n);
expect(fieldXOR(fieldXOR(f1, f2), f2).toBigInt()).toEqual(
f1.toBigInt()
);
});
});
18 changes: 13 additions & 5 deletions src/utils/zkApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,18 @@ function assertRollupField(
) {
proofValue.assertEquals(
stateValue,
message || 'Incorrect initial rollup value'
(message || '') + ' incorrect initial rollup value'
);
}

function assertRollupFields(
proofValue: Array<Field>,
stateValue: Array<Field>,
numFields: number
numFields: number,
message?: string
) {
for (let i = 0; i < numFields; i++) {
assertRollupField(proofValue[i], stateValue[i]);
assertRollupField(proofValue[i], stateValue[i], message);
}
}

Expand All @@ -123,12 +124,13 @@ function assertRollupActions(
nextActionState: Field;
},
curActionState: Field,
latestActionState: Field,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
actions: MerkleList<MerkleList<any>>,
MAX_ROLLUP_ACTIONS: number,
message?: string
) {
assertRollupField(proof.initialActionState, curActionState);
assertRollupField(proof.initialActionState, curActionState, message);
let checkActionStateExists = Bool(false);
let nextActionState = curActionState;
let iter = actions.startIterating();
Expand All @@ -151,5 +153,11 @@ function assertRollupActions(
)
);
}
checkActionStateExists.assertTrue(message || 'Incorrect next rollup state');
checkActionStateExists.assertTrue(
(message || '') + ' incorrect next rollup state'
);
nextActionState.assertEquals(
latestActionState,
(message || '') + ' incorrect action list'
);
}

0 comments on commit d8d230d

Please sign in to comment.