UNPKG

o1js

Version:

TypeScript framework for zk-SNARKs and zkApps

77 lines 2.39 kB
import { FieldVar } from '../core/fieldvar.js'; import { Tuple } from '../../util/types.js'; import { fieldVar } from '../gates.js'; import { existsOne } from '../core/exists.js'; import { createField, isBool } from '../core/field-constructor.js'; export { toVars, toVar, isVar, assert, bitSlice, bit, divideWithRemainder, packBits, isConstant }; /** * @internal * Given a Field, collapse its AST to a pure Var. See {@link FieldVar}. * * This is useful to prevent rogue Generic gates added in the middle of gate chains, * which are caused by snarky auto-resolving constants, adds and scales to vars. * * Same as `Field.seal()` with the difference that `seal()` leaves constants as is. */ function toVar(x_) { let x = createField(x_); // don't change existing vars if (isVar(x)) return x; let xVar = existsOne(() => x.toBigInt()); xVar.assertEquals(x); return xVar; } function isVar(x) { return FieldVar.isVar(fieldVar(x)); } /** * Apply {@link toVar} to each element of a tuple. */ function toVars(fields) { return Tuple.map(fields, toVar); } /** * Assert that a statement is true. If the statement is false, throws an error with the given message. * Can be used in provable code. */ function assert(stmt, message) { if (typeof stmt === 'boolean') { if (!stmt) throw Error(message ?? 'Assertion failed'); } else { stmt.assertTrue(message ?? 'Assertion failed'); } } function bitSlice(x, start, length) { return (x >> BigInt(start)) & ((1n << BigInt(length)) - 1n); } function bit(x, i) { return (x >> BigInt(i)) & 1n; } function divideWithRemainder(numerator, denominator) { const quotient = numerator / denominator; const remainder = numerator - denominator * quotient; return { quotient, remainder }; } // pack bools into a single field element /** * Helper function to provably pack bits into a single field element. * Just returns the sum without any boolean checks. */ function packBits(bits) { let n = bits.length; let sum = createField(0n); for (let i = 0; i < n; i++) { let bit = bits[i]; if (isBool(bit)) bit = bit.toField(); sum = sum.add(bit.mul(1n << BigInt(i))); } return sum.seal(); } function isConstant(...args) { return args.every((x) => x.isConstant()); } //# sourceMappingURL=common.js.map