Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
querolita committed Feb 12, 2025
1 parent d17eb66 commit 33cf572
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 192 deletions.
51 changes: 9 additions & 42 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,11 @@ export {
AlmostForeignField,
CanonicalForeignField,
} from './lib/provable/foreign-field.js';
export {
createForeignCurve,
ForeignCurve,
} from './lib/provable/crypto/foreign-curve.js';
export {
createForeignTwisted,
ForeignTwisted,
} from './lib/provable/crypto/foreign-twisted.js';
export {
createEcdsa,
EcdsaSignature,
} from './lib/provable/crypto/foreign-ecdsa.js';
export { createForeignCurve, ForeignCurve } from './lib/provable/crypto/foreign-curve.js';
export { createForeignTwisted, ForeignTwisted } from './lib/provable/crypto/foreign-twisted.js';
export { createEcdsa, EcdsaSignature } from './lib/provable/crypto/foreign-ecdsa.js';
export { ScalarField } from './lib/provable/scalar-field.js';
export {
Poseidon,
TokenSymbol,
ProvableHashable,
} from './lib/provable/crypto/poseidon.js';
export { Poseidon, TokenSymbol, ProvableHashable } from './lib/provable/crypto/poseidon.js';
export { Keccak } from './lib/provable/crypto/keccak.js';
export { Hash } from './lib/provable/crypto/hash.js';

Expand All @@ -38,37 +25,21 @@ export type {
FlexibleProvablePure,
InferProvable,
} from './lib/provable/types/struct.js';
export {
From,
InferValue,
InferJson,
IsPure,
} from './bindings/lib/provable-generic.js';
export { From, InferValue, InferJson, IsPure } from './bindings/lib/provable-generic.js';
export { ProvableType } from './lib/provable/types/provable-intf.js';
export {
provable,
provablePure,
} from './lib/provable/types/provable-derivers.js';
export { provable, provablePure } from './lib/provable/types/provable-derivers.js';
export { Struct } from './lib/provable/types/struct.js';
export { Unconstrained } from './lib/provable/types/unconstrained.js';
export { Provable } from './lib/provable/provable.js';
export {
Circuit,
Keypair,
public_,
circuitMain,
} from './lib/proof-system/circuit.js';
export { Circuit, Keypair, public_, circuitMain } from './lib/proof-system/circuit.js';
export { UInt32, UInt64, Int64, Sign, UInt8 } from './lib/provable/int.js';
export { Bytes } from './lib/provable/wrapped-classes.js';
export { Packed, Hashed } from './lib/provable/packed.js';
export { Gadgets } from './lib/provable/gadgets/gadgets.js';
export { Types } from './bindings/mina-transaction/types.js';

export { MerkleList, MerkleListIterator } from './lib/provable/merkle-list.js';
import {
IndexedMerkleMap,
IndexedMerkleMapBase,
} from './lib/provable/merkle-tree-indexed.js';
import { IndexedMerkleMap, IndexedMerkleMapBase } from './lib/provable/merkle-tree-indexed.js';
export { Option } from './lib/provable/option.js';

export * as Mina from './lib/mina/mina.js';
Expand All @@ -94,11 +65,7 @@ export {
Void,
VerificationKey,
} from './lib/proof-system/zkprogram.js';
export {
type ProofBase,
Proof,
DynamicProof,
} from './lib/proof-system/proof.js';
export { type ProofBase, Proof, DynamicProof } from './lib/proof-system/proof.js';
export { FeatureFlags } from './lib/proof-system/feature-flags.js';
export { Cache, CacheHeader } from './lib/proof-system/cache.js';

Expand Down
9 changes: 2 additions & 7 deletions src/lib/provable/crypto/foreign-twisted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,8 @@ class ForeignTwistedNotNeeded extends ForeignTwisted {
* {@link ForeignTwistedNotNeeded} also includes to associated foreign fields:
* `ForeignCurve.Field` and `ForeignCurve.Scalar`, see {@link createForeignField}.
*/
function createForeignTwisted(
params: TwistedCurveParams
): typeof ForeignTwisted {
assert(
params.modulus > l2Mask + 1n,
'Base field moduli smaller than 2^176 are not supported'
);
function createForeignTwisted(params: TwistedCurveParams): typeof ForeignTwisted {
assert(params.modulus > l2Mask + 1n, 'Base field moduli smaller than 2^176 are not supported');

const FieldUnreduced = createForeignField(params.modulus);
const ScalarUnreduced = createForeignField(params.order);
Expand Down
66 changes: 13 additions & 53 deletions src/lib/provable/gadgets/elliptic-curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { assert } from './common.js';
import { Field3, ForeignField, split, weakBound } from './foreign-field.js';
import { l, l2, l2Mask, multiRangeCheck } from './range-check.js';
import { sha256 } from 'js-sha256';
import {
bigIntToBytes,
bytesToBigInt,
} from '../../../bindings/crypto/bigint-helpers.js';
import { bigIntToBytes, bytesToBigInt } from '../../../bindings/crypto/bigint-helpers.js';
import {
CurveAffine,
GroupAffine,
Expand All @@ -27,12 +24,7 @@ import { ProvableType } from '../types/provable-intf.js';
export { EllipticCurve, Point, Ecdsa };

// internal API
export {
verifyEcdsaConstant,
initialAggregator,
simpleMapToCurve,
arrayGetGeneric,
};
export { verifyEcdsaConstant, initialAggregator, simpleMapToCurve, arrayGetGeneric };

const EllipticCurve = {
add,
Expand Down Expand Up @@ -71,10 +63,7 @@ function add(p1: Point, p2: Point, Curve: { modulus: bigint; a: bigint }) {
return Point.from(p3);
}

assert(
Curve.modulus > l2Mask + 1n,
'Base field moduli smaller than 2^176 are not supported'
);
assert(Curve.modulus > l2Mask + 1n, 'Base field moduli smaller than 2^176 are not supported');

// witness and range-check slope, x3, y3
let witnesses = exists(9, () => {
Expand Down Expand Up @@ -151,8 +140,7 @@ function double(p1: Point, Curve: { modulus: bigint; a: bigint }) {
// 2*y1*m = 3*x1x1 + a
let y1Times2 = ForeignField.Sum(y1).add(y1);
let x1x1Times3PlusA = ForeignField.Sum(x1x1).add(x1x1).add(x1x1);
if (Curve.a !== 0n)
x1x1Times3PlusA = x1x1Times3PlusA.add(Field3.from(Curve.a));
if (Curve.a !== 0n) x1x1Times3PlusA = x1x1Times3PlusA.add(Field3.from(Curve.a));
ForeignField.assertMul(y1Times2, m, x1x1Times3PlusA, f);

// m^2 = 2*x1 + x3
Expand All @@ -171,10 +159,7 @@ function negate({ x, y }: Point, Curve: { modulus: bigint }) {
return { x, y: ForeignField.negate(y, Curve.modulus) };
}

function assertOnCurve(
p: Point,
{ modulus: f, a, b }: { modulus: bigint; b: bigint; a: bigint }
) {
function assertOnCurve(p: Point, { modulus: f, a, b }: { modulus: bigint; b: bigint; a: bigint }) {
let { x, y } = p;
let x2 = ForeignField.mul(x, x, f);

Expand Down Expand Up @@ -425,10 +410,7 @@ function multiScalarMul(
scalars: Field3[],
points: Point[],
Curve: CurveAffine,
tableConfigs: (
| { windowSize?: number; multiples?: Point[] }
| undefined
)[] = [],
tableConfigs: ({ windowSize?: number; multiples?: Point[] } | undefined)[] = [],
mode: 'assert-nonzero' | 'assert-zero' = 'assert-nonzero',
ia?: point
): Point {
Expand Down Expand Up @@ -473,12 +455,8 @@ function multiScalarMul(
mrcStack.push(betaXBound);
return phiP;
});
tables2[2 * i] = table.map((P) =>
negateIf(s0.isNegative, P, Curve.modulus)
);
tables2[2 * i + 1] = endoTable.map((P) =>
negateIf(s1.isNegative, P, Curve.modulus)
);
tables2[2 * i] = table.map((P) => negateIf(s0.isNegative, P, Curve.modulus));
tables2[2 * i + 1] = endoTable.map((P) => negateIf(s1.isNegative, P, Curve.modulus));
points2[2 * i] = tables2[2 * i][1];
points2[2 * i + 1] = tables2[2 * i + 1][1];

Expand All @@ -494,9 +472,7 @@ function multiScalarMul(
}

// slice scalars
let scalarChunks = scalars.map((s, i) =>
sliceField3(s, { maxBits, chunkSize: windowSizes[i] })
);
let scalarChunks = scalars.map((s, i) => sliceField3(s, { maxBits, chunkSize: windowSizes[i] }));

// initialize sum to the initial aggregator, which is expected to be unrelated to any point that this gadget is used with
// note: this is a trick to ensure _completeness_ of the gadget
Expand All @@ -511,10 +487,7 @@ function multiScalarMul(
if (i % windowSize === 0) {
// pick point to add based on the scalar chunk
let sj = scalarChunks[j][i / windowSize];
let sjP =
windowSize === 1
? points[j]
: arrayGetGeneric(Point.provable, tables[j], sj);
let sjP = windowSize === 1 ? points[j] : arrayGetGeneric(Point.provable, tables[j], sj);

// ec addition
let added = add(sum, sjP, Curve);
Expand Down Expand Up @@ -549,12 +522,7 @@ function multiScalarMul(
}

function negateIf(condition: Field, P: Point, f: bigint) {
let y = Provable.if(
Bool.Unsafe.fromField(condition),
Field3,
ForeignField.negate(P.y, f),
P.y
);
let y = Provable.if(Bool.Unsafe.fromField(condition), Field3, ForeignField.negate(P.y, f), P.y);
return { x: P.x, y };
}

Expand All @@ -570,10 +538,7 @@ function endomorphism(Curve: CurveAffine, P: Point) {
* Note: This assumes that s0 and s1 are range-checked externally; in scalar multiplication this happens because they are split into chunks.
*/
function decomposeNoRangeCheck(Curve: CurveAffine, s: Field3) {
assert(
Curve.Endo.decomposeMaxBits < l2,
'decomposed scalars assumed to be < 2*88 bits'
);
assert(Curve.Endo.decomposeMaxBits < l2, 'decomposed scalars assumed to be < 2*88 bits');
// witness s0, s1
let witnesses = exists(6, () => {
let [s0, s1] = Curve.Endo.decompose(Field3.toBigint(s));
Expand Down Expand Up @@ -632,12 +597,7 @@ function signEcdsa(Curve: CurveAffine, msgHash: bigint, privateKey: bigint) {
* Given a point P, create the list of multiples [0, P, 2P, 3P, ..., (2^windowSize-1) * P].
* This method is provable, but won't create any constraints given a constant point.
*/
function getPointTable(
Curve: CurveAffine,
P: Point,
windowSize: number,
table?: Point[]
): Point[] {
function getPointTable(Curve: CurveAffine, P: Point, windowSize: number, table?: Point[]): Point[] {
assertPositiveInteger(windowSize, 'invalid window size');
let n = 1 << windowSize; // n >= 2

Expand Down
41 changes: 8 additions & 33 deletions src/lib/provable/gadgets/twisted-curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ const Point = {
provable: provable({ x: Field3, y: Field3 }),
};

function add(
p1: Point,
p2: Point,
Curve: { modulus: bigint; a: bigint; d: bigint }
) {
function add(p1: Point, p2: Point, Curve: { modulus: bigint; a: bigint; d: bigint }) {
let { x: x1, y: y1 } = p1;
let { x: x2, y: y2 } = p2;
let f = Curve.modulus;
Expand All @@ -74,10 +70,7 @@ function add(
return Point.from(p3);
}

assert(
Curve.modulus > l2Mask + 1n,
'Base field moduli smaller than 2^176 are not supported'
);
assert(Curve.modulus > l2Mask + 1n, 'Base field moduli smaller than 2^176 are not supported');

// the formula for point addition is well defined for curves in use,
// so we don't need to check that the denominators are non-zero
Expand Down Expand Up @@ -112,10 +105,7 @@ function add(
return { x: x3, y: y3 };
}

function double(
p1: Point,
Curve: { modulus: bigint; a: bigint; d: bigint }
): Point {
function double(p1: Point, Curve: { modulus: bigint; a: bigint; d: bigint }): Point {
let { x: x1, y: y1 } = p1;
let f = Curve.modulus;
let d = Curve.d;
Expand Down Expand Up @@ -152,10 +142,7 @@ function negate({ x, y }: Point, Curve: { modulus: bigint }) {
return { x: ForeignField.negate(x, Curve.modulus), y };
}

function assertOnCurve(
p: Point,
{ modulus: f, a, d }: { modulus: bigint; a: bigint; d: bigint }
) {
function assertOnCurve(p: Point, { modulus: f, a, d }: { modulus: bigint; a: bigint; d: bigint }) {
let { x, y } = p;
let one = Field3.from(1n);

Expand All @@ -164,11 +151,7 @@ function assertOnCurve(
let x2 = ForeignField.mul(x, x, f);
let y2 = ForeignField.mul(y, y, f);

let aTimesX2PlusY2 = ForeignField.add(
ForeignField.mul(Field3.from(a), x2, f),
y2,
f
);
let aTimesX2PlusY2 = ForeignField.add(ForeignField.mul(Field3.from(a), x2, f), y2, f);

let aTimesX2PlusY2Minus1 = ForeignField.sub(aTimesX2PlusY2, one, f);
let dTimesX2 = ForeignField.mul(Field3.from(d), x2, f);
Expand Down Expand Up @@ -250,10 +233,7 @@ function multiScalarMul(
scalars: Field3[],
points: Point[],
Curve: AffineTwistedCurve,
tableConfigs: (
| { windowSize?: number; multiples?: Point[] }
| undefined
)[] = [],
tableConfigs: ({ windowSize?: number; multiples?: Point[] } | undefined)[] = [],
mode?: 'assert-zero' | 'assert-nonzero'
): Point {
let n = points.length;
Expand All @@ -274,9 +254,7 @@ function multiScalarMul(
let maxBits = Curve.Scalar.sizeInBits;

// slice scalars
let scalarChunks = scalars.map((s, i) =>
sliceField3(s, { maxBits, chunkSize: windowSizes[i] })
);
let scalarChunks = scalars.map((s, i) => sliceField3(s, { maxBits, chunkSize: windowSizes[i] }));

// soundness follows because add() and double() are sound, on all inputs that
// are valid non-zero curve points
Expand All @@ -289,10 +267,7 @@ function multiScalarMul(
if (i % windowSize === 0) {
// pick point to add based on the scalar chunk
let sj = scalarChunks[j][i / windowSize];
let sjP =
windowSize === 1
? points[j]
: arrayGetGeneric(Point.provable, tables[j], sj);
let sjP = windowSize === 1 ? points[j] : arrayGetGeneric(Point.provable, tables[j], sj);

// ec addition
sum = add(sum, sjP, Curve);
Expand Down
17 changes: 3 additions & 14 deletions src/lib/provable/test/twisted-curve.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,9 @@ import {
createAffineTwistedCurve,
affineTwistedZero,
} from '../../../bindings/crypto/elliptic-curve.js';
import {
array,
equivalentProvable,
onlyIf,
spec,
unit,
} from '../../testing/equivalent.js';
import { array, equivalentProvable, onlyIf, spec, unit } from '../../testing/equivalent.js';
import { Random } from '../../testing/random.js';
import {
Point,
TwistedCurve,
simpleMapToCurve,
} from '../gadgets/twisted-curve.js';
import { Point, TwistedCurve, simpleMapToCurve } from '../gadgets/twisted-curve.js';
import { foreignField, throwError } from './test-utils.js';
import { Field3 } from '../gadgets/foreign-field.js';

Expand All @@ -41,8 +31,7 @@ for (let Curve of curves) {

let unequalTriple = onlyIf(
array(point, 3),
([p, q, r]) =>
!Curve.equal(p, q) && !Curve.equal(p, r) && !Curve.equal(q, r)
([p, q, r]) => !Curve.equal(p, q) && !Curve.equal(p, r) && !Curve.equal(q, r)
);

// test twisted curve gadgets witness generation
Expand Down
Loading

0 comments on commit 33cf572

Please sign in to comment.