Skip to content

Commit

Permalink
add utils
Browse files Browse the repository at this point in the history
  • Loading branch information
phn210 committed Oct 12, 2024
1 parent 0fe7ea6 commit 19c080e
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 16 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.4",
"version": "1.0.5",
"description": "",
"author": "",
"license": "Apache-2.0",
Expand Down
16 changes: 10 additions & 6 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export {
FetchedEvents,
} from './constants.js';

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

export {
randomAccounts,
Expand All @@ -32,12 +32,16 @@ export {
} from './network.js';

export {
updateActionState,
packNumberArray,
unpackNumberArray,
assertRollupActions,
assertRollupField,
assertRollupFields,
buildAssertMessage,
buildInvalidActionMessage,
checkCondition,
checkInvalidAction,
requireSignature,
packNumberArray,
requireCaller,
checkCondition,
requireSignature,
updateActionState,
unpackNumberArray,
} from './zkApp.js';
15 changes: 13 additions & 2 deletions src/utils/math.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { Scalar, UInt64 } from 'o1js';
import { Bool, Field, Scalar, UInt64 } from 'o1js';

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

/**
* @deprecated Use `Scalar.fromField` instead.
*/
function fromUInt64ToScalar(number: UInt64): Scalar {
return Scalar.fromBits(number.value.toBits());
}

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

function divExact(a: Field, b: Field): Bool {
return a.div(b).lessThanOrEqual(a);
}
81 changes: 76 additions & 5 deletions src/utils/zkApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
AccountUpdate,
Bool,
Field,
MerkleList,
Provable,
PublicKey,
SmartContract,
Expand All @@ -10,14 +11,18 @@ import {
} from 'o1js';

export {
updateActionState,
packNumberArray,
unpackNumberArray,
assertRollupActions,
assertRollupField,
assertRollupFields,
buildAssertMessage,
buildInvalidActionMessage,
checkCondition,
checkInvalidAction,
requireSignature,
packNumberArray,
requireCaller,
checkCondition,
requireSignature,
updateActionState,
unpackNumberArray,
};

function updateActionState(state: Field, action: Field[][]) {
Expand Down Expand Up @@ -51,6 +56,14 @@ function buildAssertMessage(
return `${circuit}::${method}: ${errorMsg}`;
}

function buildInvalidActionMessage(
circuit: string,
method: string,
errorMsg: string
): string {
return `${circuit}::${method}: Skipping invalid action: ${errorMsg}`;
}

function checkInvalidAction(flag: Bool, check: Bool, message?: string) {
Provable.witness(Void, () => {
if (check.not().toBoolean()) {
Expand Down Expand Up @@ -82,3 +95,61 @@ function checkCondition(condition: Bool, message?: string) {
});
return condition;
}

function assertRollupField(
proofValue: Field,
stateValue: Field,
message?: string
) {
proofValue.assertEquals(
stateValue,
message || 'Incorrect initial rollup value'
);
}

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

function assertRollupActions(
proof: {
initialActionState: Field;
nextActionState: Field;
},
curActionState: Field,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
actions: MerkleList<MerkleList<any>>,
MAX_ROLLUP_ACTIONS: number,
message?: string
) {
assertRollupField(proof.initialActionState, curActionState);
let checkActionStateExists = Bool(false);
let nextActionState = curActionState;
let iter = actions.startIterating();
for (let i = 0; i < MAX_ROLLUP_ACTIONS; i++) {
let isEmpty = iter.isAtEnd();
let merkleActions = iter.next();
nextActionState = Provable.if(
isEmpty,
nextActionState,
AccountUpdate.Actions.updateSequenceState(
nextActionState,
merkleActions.hash
)
);
checkActionStateExists = checkActionStateExists.or(
Provable.if(
isEmpty,
Bool(false),
nextActionState.equals(proof.nextActionState)
)
);
}
checkActionStateExists.assertTrue(message || 'Incorrect next rollup state');
}

0 comments on commit 19c080e

Please sign in to comment.