diff --git a/src/abstract/bls.ts b/src/abstract/bls.ts index 917a8ed..bfab1a8 100644 --- a/src/abstract/bls.ts +++ b/src/abstract/bls.ts @@ -14,7 +14,11 @@ import { AffinePoint } from './curve.js'; import { IField, getMinHashLength, mapHashToField } from './modular.js'; import { Hex, PrivKey, CHash, bitLen, bitGet, ensureBytes } from './utils.js'; -import * as htf from './hash-to-curve.js'; +// prettier-ignore +import { + MapToCurve, Opts as HTFOpts, H2CPointConstructor, htfBasicOpts, + createHasher +} from './hash-to-curve.js'; import { CurvePointsType, ProjPointType as ProjPointType, @@ -42,13 +46,13 @@ export type SignatureCoder = { export type CurveType = { G1: Omit, 'n'> & { ShortSignature: SignatureCoder; - mapToCurve: htf.MapToCurve; - htfDefaults: htf.Opts; + mapToCurve: MapToCurve; + htfDefaults: HTFOpts; }; G2: Omit, 'n'> & { Signature: SignatureCoder; - mapToCurve: htf.MapToCurve; - htfDefaults: htf.Opts; + mapToCurve: MapToCurve; + htfDefaults: HTFOpts; }; fields: { Fp: IField; @@ -70,7 +74,7 @@ export type CurveType = { x: bigint; r: bigint; }; - htfDefaults: htf.Opts; + htfDefaults: HTFOpts; hash: CHash; // Because we need outputLen for DRBG randomBytes: (bytesLength?: number) => Uint8Array; }; @@ -89,17 +93,20 @@ export type CurveFn = { verify: ( signature: Hex | ProjPointType, message: Hex | ProjPointType, - publicKey: Hex | ProjPointType + publicKey: Hex | ProjPointType, + htfOpts?: htfBasicOpts ) => boolean; verifyShortSignature: ( signature: Hex | ProjPointType, message: Hex | ProjPointType, - publicKey: Hex | ProjPointType + publicKey: Hex | ProjPointType, + htfOpts?: htfBasicOpts ) => boolean; verifyBatch: ( signature: Hex | ProjPointType, messages: (Hex | ProjPointType)[], - publicKeys: (Hex | ProjPointType)[] + publicKeys: (Hex | ProjPointType)[], + htfOpts?: htfBasicOpts ) => boolean; aggregatePublicKeys: { (publicKeys: Hex[]): Uint8Array; @@ -115,8 +122,8 @@ export type CurveFn = { }; millerLoop: (ell: [Fp2, Fp2, Fp2][], g1: [Fp, Fp]) => Fp12; pairing: (P: ProjPointType, Q: ProjPointType, withFinalExponent?: boolean) => Fp12; - G1: CurvePointsRes & ReturnType>; - G2: CurvePointsRes & ReturnType>; + G1: CurvePointsRes & ReturnType>; + G2: CurvePointsRes & ReturnType>; Signature: SignatureCoder; ShortSignature: ShortSignatureCoder; params: { @@ -220,7 +227,7 @@ export function bls( const G1_ = weierstrassPoints({ n: Fr.ORDER, ...CURVE.G1 }); const G1 = Object.assign( G1_, - htf.createHasher(G1_.ProjectivePoint, CURVE.G1.mapToCurve, { + createHasher(G1_.ProjectivePoint, CURVE.G1.mapToCurve, { ...CURVE.htfDefaults, ...CURVE.G1.htfDefaults, }) @@ -246,7 +253,7 @@ export function bls( const G2_ = weierstrassPoints({ n: Fr.ORDER, ...CURVE.G2 }); const G2 = Object.assign( G2_, - htf.createHasher(G2_.ProjectivePoint as htf.H2CPointConstructor, CURVE.G2.mapToCurve, { + createHasher(G2_.ProjectivePoint as H2CPointConstructor, CURVE.G2.mapToCurve, { ...CURVE.htfDefaults, ...CURVE.G2.htfDefaults, }) @@ -274,7 +281,7 @@ export function bls( function normP1(point: G1Hex): G1 { return point instanceof G1.ProjectivePoint ? (point as G1) : G1.ProjectivePoint.fromHex(point); } - function normP1Hash(point: G1Hex, htfOpts?: htf.htfBasicOpts): G1 { + function normP1Hash(point: G1Hex, htfOpts?: htfBasicOpts): G1 { return point instanceof G1.ProjectivePoint ? point : (G1.hashToCurve(ensureBytes('point', point), htfOpts) as G1); @@ -282,7 +289,7 @@ export function bls( function normP2(point: G2Hex): G2 { return point instanceof G2.ProjectivePoint ? point : Signature.fromHex(point); } - function normP2Hash(point: G2Hex, htfOpts?: htf.htfBasicOpts): G2 { + function normP2Hash(point: G2Hex, htfOpts?: htfBasicOpts): G2 { return point instanceof G2.ProjectivePoint ? point : (G2.hashToCurve(ensureBytes('point', point), htfOpts) as G2); @@ -302,9 +309,9 @@ export function bls( // Executes `hashToCurve` on the message and then multiplies the result by private key. // S = pk x H(m) - function sign(message: Hex, privateKey: PrivKey, htfOpts?: htf.htfBasicOpts): Uint8Array; - function sign(message: G2, privateKey: PrivKey, htfOpts?: htf.htfBasicOpts): G2; - function sign(message: G2Hex, privateKey: PrivKey, htfOpts?: htf.htfBasicOpts): Uint8Array | G2 { + function sign(message: Hex, privateKey: PrivKey, htfOpts?: htfBasicOpts): Uint8Array; + function sign(message: G2, privateKey: PrivKey, htfOpts?: htfBasicOpts): G2; + function sign(message: G2Hex, privateKey: PrivKey, htfOpts?: htfBasicOpts): Uint8Array | G2 { const msgPoint = normP2Hash(message, htfOpts); msgPoint.assertValidity(); const sigPoint = msgPoint.multiply(G1.normPrivateKeyToScalar(privateKey)); @@ -315,13 +322,13 @@ export function bls( function signShortSignature( message: Hex, privateKey: PrivKey, - htfOpts?: htf.htfBasicOpts + htfOpts?: htfBasicOpts ): Uint8Array; - function signShortSignature(message: G1, privateKey: PrivKey, htfOpts?: htf.htfBasicOpts): G1; + function signShortSignature(message: G1, privateKey: PrivKey, htfOpts?: htfBasicOpts): G1; function signShortSignature( message: G1Hex, privateKey: PrivKey, - htfOpts?: htf.htfBasicOpts + htfOpts?: htfBasicOpts ): Uint8Array | G1 { const msgPoint = normP1Hash(message, htfOpts); msgPoint.assertValidity(); @@ -336,7 +343,7 @@ export function bls( signature: G2Hex, message: G2Hex, publicKey: G1Hex, - htfOpts?: htf.htfBasicOpts + htfOpts?: htfBasicOpts ): boolean { const P = normP1(publicKey); const Hm = normP2Hash(message, htfOpts); @@ -356,7 +363,7 @@ export function bls( signature: G1Hex, message: G1Hex, publicKey: G2Hex, - htfOpts?: htf.htfBasicOpts + htfOpts?: htfBasicOpts ): boolean { const P = normP2(publicKey); const Hm = normP1Hash(message, htfOpts); @@ -420,7 +427,7 @@ export function bls( signature: G2Hex, messages: G2Hex[], publicKeys: G1Hex[], - htfOpts?: htf.htfBasicOpts + htfOpts?: htfBasicOpts ): boolean { // @ts-ignore // console.log('verifyBatch', bytesToHex(signature as any), messages, publicKeys.map(bytesToHex));