Skip to content

Commit 3f98022

Browse files
committed
Improve interfaces from chainsafe/bls usage
1 parent aa6466f commit 3f98022

File tree

5 files changed

+51
-18
lines changed

5 files changed

+51
-18
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ coverage
33
build
44
dist
55
node_modules
6-
prebuilds
6+
prebuild/*.node
77

88
npm-debug.log
99
yarn-error.log

src/bindings.ts

+3
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,7 @@ export enum BLST_ERROR {
242242
BLST_AGGR_TYPE_MISMATCH = 4,
243243
BLST_VERIFY_FAIL = 5,
244244
BLST_PK_IS_INFINITY = 6,
245+
246+
// Extra errors not in native bindings
247+
EMPTY_AGGREGATE_ARRAY = "EMPTY_AGGREGATE_ARRAY",
245248
}

src/lib.ts

+43-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const HASH_OR_ENCODE = true;
55
const DST = "BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_";
66
const RAND_BITS = 64;
77

8-
class ErrorBLST extends Error {
8+
export { BLST_ERROR };
9+
export class ErrorBLST extends Error {
910
constructor(blstError: BLST_ERROR) {
1011
super(BLST_ERROR[blstError]);
1112
}
@@ -45,6 +46,11 @@ export class SecretKey {
4546
return new SecretKey(sk);
4647
}
4748

49+
toAggregatePublicKey(): AggregatePublicKey {
50+
const pk = new PkConstructor(this.value);
51+
return new AggregatePublicKey(pk);
52+
}
53+
4854
toPublicKey(): PublicKey {
4955
const pk = new PkConstructor(this.value);
5056
return new PublicKey(pk.to_affine());
@@ -98,14 +104,6 @@ export class Signature extends SerializeAffine<SigAffine> {
98104
}
99105
}
100106

101-
function aggregate<P extends Pn_Affine<any, any>>(points: { value: P }[]) {
102-
const agg = points[0].value.to_jacobian();
103-
for (const pk of points.slice(1)) {
104-
agg.aggregate(pk.value);
105-
}
106-
return agg;
107-
}
108-
109107
export class AggregatePublicKey {
110108
value: Pk;
111109

@@ -117,7 +115,9 @@ export class AggregatePublicKey {
117115
return new AggregatePublicKey(pk.value.to_jacobian());
118116
}
119117
static fromPublicKeys(pks: PublicKey[]): AggregatePublicKey {
120-
return new AggregatePublicKey(aggregate(pks));
118+
return aggregatePubkeys(
119+
pks.map((pk) => AggregatePublicKey.fromPublicKey(pk))
120+
);
121121
}
122122
static fromPublicKeysBytes(pks: Uint8Array[]): AggregatePublicKey {
123123
return AggregatePublicKey.fromPublicKeys(pks.map(PublicKey.fromBytes));
@@ -145,7 +145,9 @@ export class AggregateSignature {
145145
return new AggregateSignature(sig.value.to_jacobian());
146146
}
147147
static fromSignatures(sigs: Signature[]): AggregateSignature {
148-
return new AggregateSignature(aggregate(sigs));
148+
return aggregateSignatures(
149+
sigs.map((sig) => AggregateSignature.fromSignature(sig))
150+
);
149151
}
150152
static fromSignaturesBytes(sigs: Uint8Array[]): AggregateSignature {
151153
return AggregateSignature.fromSignatures(sigs.map(Signature.fromBytes));
@@ -162,6 +164,34 @@ export class AggregateSignature {
162164
}
163165
}
164166

167+
export function aggregatePubkeys(
168+
pks: AggregatePublicKey[]
169+
): AggregatePublicKey {
170+
if (pks.length === 0) {
171+
throw new ErrorBLST(BLST_ERROR.EMPTY_AGGREGATE_ARRAY);
172+
}
173+
174+
const agg = pks
175+
.map((pk) => pk.value)
176+
.reduce((_agg, pk) => blst.P1.add(_agg, pk));
177+
178+
return new AggregatePublicKey(agg);
179+
}
180+
181+
export function aggregateSignatures(
182+
sigs: AggregateSignature[]
183+
): AggregateSignature {
184+
if (sigs.length === 0) {
185+
throw new ErrorBLST(BLST_ERROR.EMPTY_AGGREGATE_ARRAY);
186+
}
187+
188+
const agg = sigs
189+
.map((sig) => sig.value)
190+
.reduce((_agg, sig) => blst.P2.add(_agg, sig));
191+
192+
return new AggregateSignature(agg);
193+
}
194+
165195
export function verify(
166196
msg: Uint8Array,
167197
pk: PublicKey,
@@ -172,10 +202,10 @@ export function verify(
172202

173203
export function fastAggregateVerify(
174204
msg: Uint8Array,
175-
pks: PublicKey[],
205+
pks: AggregatePublicKey[],
176206
sig: Signature
177207
): boolean {
178-
const aggPk = AggregatePublicKey.fromPublicKeys(pks);
208+
const aggPk = aggregatePubkeys(pks);
179209
const pk = aggPk.toPublicKey();
180210
return aggregateVerify([msg], [pk], sig);
181211
}

src/scripts/downloadBindings.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { testBindings } from "./testBindings";
44
import { ensureDirFromFilepath, getBinaryName, packageJsonPath } from "./paths";
55

66
const githubReleasesDownloadUrl =
7-
"https://github.com/ChainSafe/blst-ts/releases/download";
7+
"https://github.com/ChainSafe/blst/releases/download";
88

99
export async function checkAndDownloadBinary(binaryPath: string) {
1010
const packageJson = require(packageJsonPath);

test/lib/index.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ describe("bls lib", () => {
1818
describe("1 msg, N pks", () => {
1919
const msg = Buffer.from("sample-msg");
2020
const sks: bls.SecretKey[] = [];
21-
const pks: bls.PublicKey[] = [];
21+
const pks: bls.AggregatePublicKey[] = [];
2222
const sigs: bls.Signature[] = [];
2323

2424
for (let i = 0; i < n; i++) {
2525
const sk = bls.SecretKey.fromKeygen(Uint8Array.from(Buffer.alloc(32, i)));
2626
sks.push(sk);
27-
pks.push(sk.toPublicKey());
27+
pks.push(sk.toAggregatePublicKey());
2828
sigs.push(sk.sign(msg));
2929
}
3030

3131
it("verify", () => {
3232
for (let i = 0; i < n; i++) {
33-
bls.verify(msg, pks[i], sigs[i]);
33+
bls.verify(msg, pks[i].toPublicKey(), sigs[i]);
3434
}
3535
});
3636

0 commit comments

Comments
 (0)