Skip to content

Commit

Permalink
Merge pull request #16 from ChainSafe/document
Browse files Browse the repository at this point in the history
Document
  • Loading branch information
dapplion authored Nov 29, 2020
2 parents 54e3621 + a4d27b6 commit 461b36e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,50 @@
blst-ts
# blst-ts

![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.

## Usage

```bash
yarn add @chainsafe/blst
```

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.

```ts
import { SecretKey, verify } from "@chainsafe/blst";

const msg = Buffer.from("sample-msg");
const sk = SecretKey.fromKeygen(Buffer.alloc(32, 1));
const pk = sk.toPublicKey();
const sig = sk.sign(msg);

console.log(verify(msg, pk, sig)); // true
```

This library exposes two types of classes for public keys and signatures: `PublicKey` & `AggregatePublicKey`, `Signature` & `AggregateSignature`

- `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.

## Spec versioning

This library has a hardcoded configuration compatible with Eth2.0 spec:

| Setting | value |
| -------------- | --------------------------------------------- |
| PK_IN | `G1` |
| HASH_OR_ENCODE | `true` |
| DST | `BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_` |
| RAND_BITS | `64` |

> [spec](https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/beacon-chain.md#bls-signatures)
> [test vectors](https://github.com/ethereum/eth2.0-spec-tests/tree/master/tests/bls)
## License

Apache-2.0
8 changes: 4 additions & 4 deletions test/lib/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe("bls lib", () => {
describe("1 msg, 1 pk", () => {
const msg = Buffer.from("sample-msg");

const sk = bls.SecretKey.fromKeygen(Uint8Array.from(Buffer.alloc(32, 1)));
const sk = bls.SecretKey.fromKeygen(Buffer.alloc(32, 1));
const pk = sk.toPublicKey();
const sig = sk.sign(msg);

Expand All @@ -22,7 +22,7 @@ describe("bls lib", () => {
const sigs: bls.Signature[] = [];

for (let i = 0; i < n; i++) {
const sk = bls.SecretKey.fromKeygen(Uint8Array.from(Buffer.alloc(32, i)));
const sk = bls.SecretKey.fromKeygen(Buffer.alloc(32, i));
sks.push(sk);
pks.push(sk.toAggregatePublicKey());
sigs.push(sk.sign(msg));
Expand Down Expand Up @@ -50,8 +50,8 @@ describe("bls lib", () => {
const sigs: bls.Signature[] = [];

for (let i = 0; i < n; i++) {
const msg = Uint8Array.from(Buffer.alloc(32, i));
const sk = bls.SecretKey.fromKeygen(Uint8Array.from(Buffer.alloc(32, 1)));
const msg = Buffer.alloc(32, i);
const sk = bls.SecretKey.fromKeygen(Buffer.alloc(32, 1));
msgs.push(msg);
sks.push(sk);
pks.push(sk.toPublicKey());
Expand Down
2 changes: 1 addition & 1 deletion test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function expectHex(value: Bufferish, expected: Bufferish): void {
}

export function fromHex(hexString: string): Uint8Array {
return Uint8Array.from(Buffer.from(hexString, "hex"));
return Buffer.from(hexString, "hex");
}

/**
Expand Down

0 comments on commit 461b36e

Please sign in to comment.