Skip to content

Commit

Permalink
Merge pull request #105 from ChainSafe/mkeil/rebuild-sync-functions
Browse files Browse the repository at this point in the history
feat(rebuild): functions with unit tests
  • Loading branch information
matthewkeil committed Jan 5, 2024
2 parents 768a240 + 201f33a commit 2a25955
Show file tree
Hide file tree
Showing 30 changed files with 1,849 additions and 106 deletions.
File renamed without changes.
13 changes: 12 additions & 1 deletion rebuild/.eslintrc.js → rebuild/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ module.exports = {
},
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 10,
ecmaVersion: 12,
project: "./tsconfig.json",
sourceType: "module",
ecmaFeatures:{
modules: true
}
},
plugins: ["@typescript-eslint", "eslint-plugin-import", "eslint-plugin-node", "prettier"],
extends: [
Expand Down Expand Up @@ -51,6 +55,7 @@ module.exports = {
"@typescript-eslint/explicit-member-accessibility": ["error", {accessibility: "no-public"}],
"@typescript-eslint/no-unsafe-call": "error",
"@typescript-eslint/no-unsafe-return": "error",
"import/no-unresolved": "off",
"import/no-extraneous-dependencies": [
"error",
{
Expand Down Expand Up @@ -79,6 +84,12 @@ module.exports = {
},
settings: {
"import/core-modules": ["node:child_process", "node:crypto", "node:fs", "node:os", "node:path", "node:util"],
'import/resolver': {
node: {
extensions: [".js", ".ts"],
moduleDirectory: ["lib/", "test/", "node_modules/"]
}
},
},
overrides: [
{
Expand Down
6 changes: 6 additions & 0 deletions rebuild/.mocharc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
colors: true,
require: "ts-node/register",
exit: true,
loader: "ts-node/esm"
}
3 changes: 0 additions & 3 deletions rebuild/.mocharc.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion rebuild/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'src/secret_key.cc',
'src/public_key.cc',
'src/signature.cc',
# 'src/functions.cc',
'src/functions.cc',
],
'include_dirs': [
'deps/blst/bindings',
Expand Down
151 changes: 150 additions & 1 deletion rebuild/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type BlstBuffer = Uint8Array;
export type PublicKeyArg = BlstBuffer | PublicKey;
export type SignatureArg = BlstBuffer | Signature;
export interface SignatureSet {
msg: BlstBuffer;
message: BlstBuffer;
publicKey: PublicKeyArg;
signature: SignatureArg;
}
Expand Down Expand Up @@ -71,6 +71,7 @@ export class SecretKey implements Serializable {
* Serialize a secret key into a Buffer.
*/
serialize(): Buffer;
toHex(): string;
toPublicKey(): PublicKey;
sign(msg: BlstBuffer): Signature;
}
Expand All @@ -79,12 +80,160 @@ export class PublicKey implements Serializable {
private constructor();
static deserialize(skBytes: BlstBuffer, coordType?: CoordType): PublicKey;
serialize(compress?: boolean): Buffer;
toHex(compress?: boolean): string;
keyValidate(): void;
}

export class Signature implements Serializable {
private constructor();
static deserialize(skBytes: BlstBuffer, coordType?: CoordType): Signature;
serialize(compress?: boolean): Buffer;
toHex(compress?: boolean): string;
sigValidate(): void;
}

/**
* Aggregates an array of PublicKeyArgs. Can pass mixed deserialized PublicKey
* objects and serialized Uint8Array in the `keys` array. Passing serialized
* objects requires deserialization of the blst::P1
*
* @param {PublicKeyArg} keys - Array of public keys to aggregate
*
* @return {PublicKey} - Aggregated jacobian public key
*
* @throw {TypeError} - Invalid input
* @throw {Error} - Invalid aggregation
*/
export function aggregatePublicKeys(keys: PublicKeyArg[]): PublicKey;

/**
* Aggregates an array of SignatureArgs. Can pass mixed deserialized Signature
* objects and serialized Uint8Array in the `signatures` array. Passing serialized
* objects requires deserialization of the blst::P2
*
* @param {SignatureArg} signatures - Array of signatures to aggregate
*
* @return {Signature} - Aggregated jacobian signature
*
* @throw {TypeError} - Invalid input
* @throw {Error} - Invalid aggregation
*/
export function aggregateSignatures(signatures: SignatureArg[]): Signature;

/**
* Bls verification of a message against a public key and signature.
*
* @param {BlstBuffer} msg - Message to verify
* @param {PublicKeyArg} publicKey - Public key to verify against
* @param {SignatureArg} signature - Signature of the message
*
* @return {boolean} - True if the signature is valid, false otherwise
*
* @throw {TypeError} - Invalid input
*/
export function verify(msg: BlstBuffer, publicKey: PublicKeyArg, signature: SignatureArg): boolean;

/**
* Bls verification of a message against a set of public keys and an aggregated signature.
*
* @param {BlstBuffer} msg - Message to verify
* @param {PublicKeyArg} publicKeys - Public keys to aggregate and verify against
* @param {SignatureArg} signature - Aggregated signature of the message
*
* @return {boolean} - True if the signature is valid, false otherwise
*
* @throw {TypeError} - Invalid input
* @throw {Error} - Invalid aggregation
*/
export function fastAggregateVerify(msg: BlstBuffer, publicKeys: PublicKeyArg[], signature: SignatureArg): boolean;

/**
* Bls verification of a set of messages, with corresponding public keys, and a single
* aggregated signature.
*
* @param {BlstBuffer} msgs - Messages to verify
* @param {PublicKeyArg} publicKeys - Corresponding public keys to verify against
* @param {SignatureArg} signature - Aggregated signature of the message
*
* @return {boolean} - True if the signature is valid, false otherwise
*
* @throw {TypeError} - Invalid input
* @throw {Error} - Invalid aggregation
*/
export function aggregateVerify(msgs: BlstBuffer[], publicKeys: PublicKeyArg[], signature: SignatureArg): boolean;

/**
* Bls batch verification for groups with a message and corresponding public key
* and signature. Only returns true if all signatures are valid.
*
* @param {SignatureSet} signatureSets - Array of SignatureSet objects to batch verify
*
* @return {boolean} - True if all signatures are valid, false otherwise
*
* @throw {TypeError} - Invalid input
* @throw {Error} - Invalid aggregation
*/
export function verifyMultipleAggregateSignatures(signatureSets: SignatureSet[]): boolean;

/**
* Bls verification of a message against a public key and signature.
*
* @param {BlstBuffer} msg - Message to verify
* @param {PublicKeyArg} publicKey - Public key to verify against
* @param {SignatureArg} signature - Signature of the message
*
* @return {Promise<boolean>} - True if the signature is valid, false otherwise
*
* @throw {TypeError} - Invalid input
*/
export function asyncVerify(msg: BlstBuffer, publicKey: PublicKeyArg, signature: SignatureArg): Promise<boolean>;

/**
* Bls verification of a message against a set of public keys and an aggregated signature.
*
* @param {BlstBuffer} msg - Message to verify
* @param {PublicKeyArg} publicKeys - Public keys to aggregate and verify against
* @param {SignatureArg} signature - Aggregated signature of the message
*
* @return {Promise<boolean>} - True if the signature is valid, false otherwise
*
* @throw {TypeError} - Invalid input
* @throw {Error} - Invalid aggregation
*/
export function asyncFastAggregateVerify(
msg: BlstBuffer,
publicKey: PublicKeyArg[],
signature: SignatureArg
): Promise<boolean>;

/**
* Bls verification of a set of messages, with corresponding public keys, and a single
* aggregated signature.
*
* @param {BlstBuffer} msgs - Messages to verify
* @param {PublicKeyArg} publicKeys - Corresponding public keys to verify against
* @param {SignatureArg} signature - Aggregated signature of the message
*
* @return {Promise<boolean>} - True if the signature is valid, false otherwise
*
* @throw {TypeError} - Invalid input
* @throw {Error} - Invalid aggregation
*/
export function asyncAggregateVerify(
msg: BlstBuffer[],
publicKey: PublicKeyArg[],
signature: SignatureArg
): Promise<boolean>;

/**
* Bls batch verification for groups with a message and corresponding public key
* and signature. Only returns true if all signatures are valid.
*
* @param {SignatureSet} signatureSets - Array of SignatureSet objects to batch verify
*
* @return {Promise<boolean>} - True if all signatures are valid, false otherwise
*
* @throw {TypeError} - Invalid input
* @throw {Error} - Invalid aggregation
*/
export function asyncVerifyMultipleAggregateSignatures(signatureSets: SignatureSet[]): Promise<boolean>;
91 changes: 88 additions & 3 deletions rebuild/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,96 @@
/* eslint-disable @typescript-eslint/no-require-imports */
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable @typescript-eslint/no-unsafe-call */
const bindings = require("bindings")("blst_ts_addon");
/* eslint-disable @typescript-eslint/explicit-function-return-type */
const {default: getBindings} = await import("bindings");
const bindings = getBindings("blst_ts_addon");
const {
BLST_CONSTANTS,
SecretKey,
PublicKey,
Signature,
aggregatePublicKeys,
aggregateSignatures,
aggregateVerify,
verifyMultipleAggregateSignatures,
asyncAggregateVerify,
asyncVerifyMultipleAggregateSignatures,
} = bindings;

bindings.CoordType = {
SecretKey.prototype.toHex = function () {
return `0x${this.serialize().toString("hex")}`;
};

PublicKey.prototype.toHex = function (compress) {
return `0x${this.serialize(compress).toString("hex")}`;
};

Signature.prototype.toHex = function (compress) {
return `0x${this.serialize(compress).toString("hex")}`;
};

export {
BLST_CONSTANTS,
SecretKey,
PublicKey,
Signature,
aggregatePublicKeys,
aggregateSignatures,
aggregateVerify,
verifyMultipleAggregateSignatures,
asyncAggregateVerify,
asyncVerifyMultipleAggregateSignatures,
};

export function verify(msg, pk, sig) {
return aggregateVerify([msg], [pk], sig);
}

export function asyncVerify(msg, pk, sig) {
return asyncAggregateVerify([msg], [pk], sig);
}

export function fastAggregateVerify(msg, pks, sig) {
let key;
try {
// this throws for invalid key, catch and return false
key = aggregatePublicKeys(pks);
} catch {
return false;
}
return aggregateVerify([msg], [key], sig);
}

export function asyncFastAggregateVerify(msg, pks, sig) {
let key;
try {
// this throws for invalid key, catch and return false
key = aggregatePublicKeys(pks);
} catch {
return false;
}
return asyncAggregateVerify([msg], [key], sig);
}

export const CoordType = {
affine: 0,
jacobian: 1,
};

module.exports = exports = bindings;
export default {
CoordType,
BLST_CONSTANTS,
SecretKey,
PublicKey,
Signature,
aggregatePublicKeys,
aggregateSignatures,
verify,
aggregateVerify,
fastAggregateVerify,
verifyMultipleAggregateSignatures,
asyncVerify,
asyncAggregateVerify,
asyncFastAggregateVerify,
asyncVerifyMultipleAggregateSignatures,
};
24 changes: 21 additions & 3 deletions rebuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
"name": "@chainsafe/blst-ts",
"version": "1.0.0",
"description": "Typescript wrapper for supranational/blst native bindings, a highly performant BLS12-381 signature library",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"gypfile": true,
"private": false,
"files": [
Expand All @@ -12,6 +10,26 @@
"lib",
"src"
],
"type": "module",
"main": "./import/index.js",
"module": "./import/index.js",
"types": "./import/index.d.ts",
"exports": {
".": "./import/index.js"
},
"typesVersions": {
"*": {
"*": [
"./import/index.d.ts"
]
}
},
"publishConfig": {
"exports": {
".": "./import/index.js"
},
"types": "./import/index.d.ts"
},
"scripts": {
"preinstall": "mkdir -p deps && cp -r ../blst deps",
"clean": "npm run clean:dist; npm run clean:build",
Expand All @@ -24,7 +42,7 @@
"lint:ts": "eslint --color --ext .ts lib/ test/",
"lint:c": "clang-format -i ./src/*",
"lint": "npm run lint:c && npm run lint:ts",
"test": "yarn test:unit",
"test": "yarn test:unit && yarn test:spec",
"test:unit": "mocha test/unit/**/*.test.ts",
"test:spec": "mocha 'test/spec/**/*.test.ts'",
"download-spec-tests": "node -r ts-node/register test/spec/downloadTests.ts"
Expand Down

0 comments on commit 2a25955

Please sign in to comment.