forked from ChainSafe/blst-ts
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from fetchai/feat/browser-support
- Loading branch information
Showing
37 changed files
with
6,150 additions
and
1,132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,4 +89,5 @@ module.exports = { | |
}, | ||
}, | ||
], | ||
"ignorePatterns": ["pre.js", "post.js", "webpack.config.js", "karma.conf.js"], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[submodule "blst"] | ||
path = blst | ||
url = https://github.com/supranational/blst.git | ||
url = https://github.com/fetchai/blst.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,102 @@ | ||
# blst-ts | ||
This repository is a fork of [chainsafe/blst-ts](https://github.com/chainsafe/blst-ts) which may be used as a staging area for changes that may (or may not) be upstreamed. | ||
|
||
![ETH2.0_Spec_Version 0.12.0](https://img.shields.io/badge/ETH2.0_Spec_Version-0.12.0-2e86c1.svg) | ||
![ES Version](https://img.shields.io/badge/ES-2017-yellow) | ||
![Node Version](https://img.shields.io/badge/node-12.x-green) | ||
|
||
Typescript wrapper for [supranational/blst](https://github.com/supranational/blst) native bindings, a highly performant BLS12-381 signature library. | ||
## Developing | ||
|
||
## Usage | ||
Note that this repo contains a git submodule. Make sure the git submodule `blst` is populated before attempting to build locally. After cloning run: | ||
|
||
```bash | ||
yarn add @chainsafe/blst | ||
git submodule update --init --recursive | ||
``` | ||
|
||
This library comes with pre-compiled bindings for most platforms. You can check current support in [releases](https://github.com/ChainSafe/blst-ts/releases). If your platform is not supported, bindings will be compiled from source as a best effort with node-gyp. | ||
### Using emscripten bindings | ||
|
||
```ts | ||
import {SecretKey, verify} from "@chainsafe/blst"; | ||
#### `package.json`: | ||
```javascript | ||
depencencies: { | ||
"@chainsafe/blst": "git+https://github.com/fechai/blst-ts#feat/browser-support", | ||
... | ||
} | ||
``` | ||
|
||
const msg = Buffer.from("sample-msg"); | ||
const sk = SecretKey.fromKeygen(Buffer.alloc(32, 1)); | ||
const pk = sk.toPublicKey(); | ||
const sig = sk.sign(msg); | ||
#### `webpack.config.js` (V5): | ||
```javascript | ||
module.exports = { | ||
resolve: { | ||
fallback: { | ||
"crypto": require.resolve("crypto-browserify"), | ||
"stream": require.resolve("stream-browserify"), | ||
"buffer": require.resolve("buffer"), | ||
"assert": require.resolve("assert-browserify"), | ||
"path": require.resolve("path-browserify"), | ||
"fs": false, | ||
}, | ||
... | ||
}, | ||
ignoreWarnings: [ | ||
{ | ||
module: /^(fs|process)$/, | ||
} | ||
] | ||
} | ||
``` | ||
|
||
console.log(verify(msg, pk, sig)); // true | ||
#### `webpack.config.js` (V4): | ||
```javascript | ||
module.exports = { | ||
plugins: [ | ||
new webpack.IgnorePlugin(/^(fs|process)$/), | ||
], | ||
... | ||
} | ||
``` | ||
|
||
This library exposes two types of classes for public keys and signatures: `PublicKey` & `AggregatePublicKey`, `Signature` & `AggregateSignature` | ||
### Building WebAssembly module | ||
|
||
- `PublicKey`: Contains an affine point (x,y). It's the default representation of the point and what you need to serialize to and deserialize from. | ||
- `AggregatePublicKey`: Contains a jacobian point (x,y,z). It's optimal to perform aggregation operations. | ||
_(NOTE: docker is required to use the script)_ | ||
|
||
## Spec versioning | ||
```bash | ||
yarn build:emscripten | ||
``` | ||
|
||
This library has a hardcoded configuration compatible with Eth2.0 spec: | ||
##### Manual build | ||
_(NOTE: a copy of [emsdk](https://github.com/emscripten-core/emsdk) is required to build for web assembly. See [emscripten docs -> Building Projects](https://emscripten.org/docs/compiling/Building-Projects.html) for more information)_ | ||
|
||
| Setting | value | | ||
| -------------- | --------------------------------------------- | | ||
| PK_IN | `G1` | | ||
| HASH_OR_ENCODE | `true` | | ||
| DST | `BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_` | | ||
| RAND_BITS | `64` | | ||
```bash | ||
# ensure enscripten sdk is active | ||
/path/to/emsdk/emsdk activate | ||
source /path/to/emsdk/emsdk_env.sh | ||
|
||
> [spec](https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#bls-signatures) | ||
CROSS_COMPILE=em CFLAGS="-o ./prebuild/emscripten/blst.js --pre-js ./prebuild/emscripten/pre.js --post-js ./prebuild/emscripten/post.js ./prebuild/emscripten/blst_glue_wrapper.cpp" ./blst/build.sh -link -no-archive | ||
``` | ||
|
||
> [test vectors](https://github.com/ethereum/eth2.0-spec-tests/tree/master/tests/bls) | ||
## Testing in the browser | ||
|
||
## Developing | ||
Once built (see [building WebAssembly module](#building-webassembly-module)), applicable mocha tests can be run in the browser using webpack: | ||
|
||
Note that this repo contains a git submodule. Make sure the git submodule `blst` is populated before attempting to build locally. After cloning run: | ||
```bash | ||
yarn test:browser | ||
``` | ||
|
||
#### Karma | ||
|
||
Additionally, [Karma](https://karma-runner.github.io/6.3/index.html) is configured to run browser-relevant tests in chrome by starting and running: | ||
|
||
```bash | ||
# Optionally, CHROME_BIN=$(which <non-standard-chromium-flavor>) | ||
# (see: https://github.com/karma-runner/karma-chrome-launcher) | ||
|
||
yarn karma start | ||
``` | ||
git submodule update --init --recursive | ||
|
||
```bash | ||
# separate shell | ||
|
||
yarn karma run | ||
``` | ||
|
||
_(NOTE: it currently seems like Karma has to be run a few times before it picks up all the tests)_ | ||
|
||
## License | ||
|
||
Apache-2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const {module: _module, resolve, ignoreWarnings} = require("./webpack.config"); | ||
|
||
module.exports = (config) => { | ||
config.set({ | ||
basePath: "", | ||
files: [ | ||
"test/unit/lib/index.test.ts", | ||
"test/unit/bindings/*.test.ts", | ||
], | ||
frameworks: ["webpack", "mocha", "chai"], | ||
browsers: ["Chrome"], | ||
plugins: ["karma-chrome-launcher", "karma-webpack", "karma-mocha", "karma-chai"], | ||
preprocessors: { | ||
"test/**/*.ts": ["webpack"], | ||
// "src/**/*.ts": ["webpack"], | ||
}, | ||
webpack: { | ||
mode: "production", | ||
module: _module, | ||
resolve, | ||
ignoreWarnings, | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { | ||
P1_AffineConstructor, | ||
P1Constructor, | ||
P2_AffineConstructor, | ||
P2Constructor, | ||
PairingConstructor, | ||
PTConstructor, | ||
SecretKeyConstructor, | ||
} from "../../src/bindings"; | ||
|
||
export const initialized: Promise<void>; | ||
|
||
// BLS12_381_G1: P1_Affine; | ||
// BLS12_381_NEG_G1: P1_Affine; | ||
// BLS12_381_G2: P2_Affine; | ||
// BLS12_381_NEG_G2: P2_Affine; | ||
export const SecretKey: SecretKeyConstructor; | ||
export const P1_Affine: P1_AffineConstructor; | ||
export const P2_Affine: P2_AffineConstructor; | ||
export const P1: P1Constructor; | ||
export const P2: P2Constructor; | ||
export const PT: PTConstructor; | ||
export const Pairing: PairingConstructor; | ||
export const G1: () => P1; | ||
export const G2: () => P2; |
Oops, something went wrong.