Skip to content

Commit

Permalink
Move to main branch - clean up for 1.0 RC
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffmang9 committed Mar 10, 2021
1 parent 82dabf5 commit e095a3e
Show file tree
Hide file tree
Showing 19 changed files with 188 additions and 167 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-aarch64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

steps:
- name: Cancel previous runs on the same branch
if: ${{ github.ref != 'refs/heads/master' }}
if: ${{ github.ref != 'refs/heads/main' }}
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

steps:
- name: Cancel previous runs on the same branch
if: ${{ github.ref != 'refs/heads/master' }}
if: ${{ github.ref != 'refs/heads/main' }}
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

steps:
- name: Cancel previous runs on the same branch
if: ${{ github.ref != 'refs/heads/master' }}
if: ${{ github.ref != 'refs/heads/main' }}
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/relic-nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

steps:
- name: Cancel previous runs on the same branch
if: ${{ github.ref != 'refs/heads/master' }}
if: ${{ github.ref != 'refs/heads/main' }}
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}
Expand All @@ -30,7 +30,7 @@ jobs:
echo "Relic origin/master commit:"
curl -H "application/vnd.github.v3.sha" \
https://api.github.com/repos/relic-toolkit/relic/commits/master | \
head -10
head -10
sudo apt-get update
sudo apt-get install snap -y
sudo apt-get remove --purge cmake -y
Expand Down
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

63 changes: 36 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,31 @@
[![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/Chia-Network/bls-signatures.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/Chia-Network/bls-signatures/context:python)
[![Language grade: C/C++](https://img.shields.io/lgtm/grade/cpp/g/Chia-Network/bls-signatures.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/Chia-Network/bls-signatures/context:cpp)

NOTE: THIS LIBRARY IS A DRAFT AND NOT YET REVIEWED FOR SECURITY
NOTE: THIS LIBRARY IS NOT YET FORMALLY REVIEWED FOR SECURITY

NOTE: THIS LIBRARY WAS SHIFTED TO THE IETF BLS SPECIFICATION ON 7/16/20

Implements BLS signatures with aggregation using [relic toolkit](https://github.com/relic-toolkit/relic)
for cryptographic primitives (pairings, EC, hashing) according to the [IETF BLS RFC](https://datatracker.ietf.org/doc/draft-irtf-cfrg-bls-signature/) with [these curve parameters](https://datatracker.ietf.org/doc/draft-irtf-cfrg-pairing-friendly-curves/) for BLS12-381.
for cryptographic primitives (pairings, EC, hashing) according to the
[IETF BLS RFC](https://datatracker.ietf.org/doc/draft-irtf-cfrg-bls-signature/)
with [these curve parameters](https://datatracker.ietf.org/doc/draft-irtf-cfrg-pairing-friendly-curves/)
for BLS12-381.

Features:

* Non-interactive signature aggregation following IETF specification
* Works on Windows, Mac, Linux, BSD
* Efficient verification using Proof of Posssesion (only one pairing per distinct message)
* Aggregate public keys and private keys
* EIP-2333 key derivation (including unhardened BIP-32-like keys)
* [EIP-2333](https://eips.ethereum.org/EIPS/eip-2333) key derivation (including unhardened BIP-32-like keys)
* Key and signature serialization
* Batch verification
* [Python bindings](https://github.com/Chia-Network/bls-signatures/tree/master/python-bindings)
* [Pure python bls12-381 and signatures](https://github.com/Chia-Network/bls-signatures/tree/master/python-impl)
* [JavaScript bindings](https://github.com/Chia-Network/bls-signatures/tree/master/js-bindings) (currently out of date)
* [Python bindings](https://github.com/Chia-Network/bls-signatures/tree/main/python-bindings)
* [Pure python bls12-381 and signatures](https://github.com/Chia-Network/bls-signatures/tree/main/python-impl)
* [JavaScript bindings](https://github.com/Chia-Network/bls-signatures/tree/main/js-bindings) (currently out of date - a great first issue!)

## Before you start

This library uses minimum public key sizes (MPL). A G2Element is a signature (96 bytes), and a G1Element is a public key (48 bytes). A private key is a 32 byte integer. There are three schemes: Basic, Augmented, and ProofOfPossession. Augmented should be enough for most use cases, and ProofOfPossession can be used where verification must be fast.

## Import the library
Expand Down Expand Up @@ -194,6 +198,7 @@ cmake --build . -- -j 6
```bash
./build/src/runbench
```

On a 3.5 GHz i7 Mac, verification takes about 1.1ms per signature, and signing takes 1.3ms.

### Link the library to use it
Expand All @@ -205,12 +210,13 @@ g++ -Wl,-no_pie -std=c++11 -Ibls-signatures/build/_deps/relic-src/include -Ibls
## Notes on dependencies

Libsodium and GMP are optional dependencies: libsodium gives secure memory
allocation, and GMP speeds up the library by ~ 3x. To install them, either
download them from github and follow the instructions for each repo, or use
a package manager like APT or brew. You can follow the recipe used to build
python wheels for multiple platforms in `.github/workflows/`. libsodium is
dynamically linked unless the environment variable $CIBUILDWHEEL is set which
will then cause libsodium to statically link.
allocation, and GMP speeds up the library by ~ 3x. MPIR is used on Windows via
GitHub Actions instead. To install them, either download them from github and
follow the instructions for each repo, or use a package manager like APT or
brew. You can follow the recipe used to build python wheels for multiple
platforms in `.github/workflows/`. libsodium is dynamically linked unless
the environment variable $CIBUILDWHEEL is set which will then cause
libsodium to statically link.

## Discussion

Expand All @@ -228,39 +234,39 @@ channel of Chia's [public Keybase channels](https://keybase.io/team/chia_network
* Use cpplint with default rules
* Use SecAlloc and SecFree when handling secrets


## ci Building

The primary build process for this repository is to use GitHub Actions to
build binary wheels for MacOS, Linux (x64 and aarch64), and Windows and publish
them with a source wheel on PyPi. See `.github/workflows/build.yml`. CMake uses
them with a source wheel on PyPi. MacOS ARM64 is supported but not automated
due to a lack of M1 CI runners. See `.github/workflows/build.yml`. CMake uses
[FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html)
to download [pybind11](https://github.com/pybind/pybind11) for the Python
bindings and relic from a chia relic forked repository. Building is then
managed by [cibuildwheel](https://github.com/joerick/cibuildwheel).
bindings and relic from a chia relic forked repository for Windows. Building
is then managed by [cibuildwheel](https://github.com/joerick/cibuildwheel).
Further installation is then available via `pip install blspy` e.g. The ci
builds include GMP and a statically linked libsodium.

## Contributing and workflow

Contributions are welcome and more details are available in chia-blockchain's
[CONTRIBUTING.md](https://github.com/Chia-Network/chia-blockchain/blob/master/CONTRIBUTING.md).
[CONTRIBUTING.md](https://github.com/Chia-Network/chia-blockchain/blob/main/CONTRIBUTING.md).

The master branch is usually the currently released latest version on PyPI.
The main branch is usually the currently released latest version on PyPI.
Note that at times bls-signatures/blspy will be ahead of the release version
that chia-blockchain requires in it's master/release version in preparation
for a new chia-blockchain release. Please branch or fork master and then create
a pull request to the master branch. Linear merging is enforced on master and
merging requires a completed review. PRs will kick off a GitHub actions ci build
and analysis of bls-signatures at
that chia-blockchain requires in it's main/release version in preparation
for a new chia-blockchain release. Please branch or fork main and then create
a pull request to the main branch. Linear merging is enforced on main and
merging requires a completed review. PRs will kick off a GitHub actions ci
build and analysis of bls-signatures at
[lgtm.com](https://lgtm.com/projects/g/Chia-Network/bls-signatures/?mode=list).
Please make sure your build is passing and that it does not increase alerts
at lgtm.

## Specification and test vectors

The [IETF bls draft](https://datatracker.ietf.org/doc/draft-irtf-cfrg-hash-to-curve/) is followed.
Test vectors can also be seen in the python and cpp test files.
The [IETF bls draft](https://datatracker.ietf.org/doc/draft-irtf-cfrg-hash-to-curve/)
is followed. Test vectors can also be seen in the python and cpp test files.

## Libsodium license

Expand All @@ -286,7 +292,10 @@ the following copyright notice.
## GMP license

GMP is distributed under the [GNU LGPL v3 license](https://www.gnu.org/licenses/lgpl-3.0.html)
GMP is distributed under the
[GNU LGPL v3 license](https://www.gnu.org/licenses/lgpl-3.0.html)

## Relic license
Relic is used with the [Apache 2.0 license](https://github.com/relic-toolkit/relic/blob/master/LICENSE.Apache-2.0)

Relic is used with the
[Apache 2.0 license](https://github.com/relic-toolkit/relic/blob/master/LICENSE.Apache-2.0)
18 changes: 15 additions & 3 deletions python-bindings/README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
## Python bindings
# Python bindings

Use the full power and efficiency of the C++ bls library, but in a few lines of python!

## Install

```bash
pip3 install blspy

```

Alternatively, to install from source, run the following, in the project root directory:

```bash
pip3 install .
```

Cmake, a c++ compiler, and a recent version of pip3 (v18) are required for source install.
GMP(speed) and libsodium(secure memory alloc) are optional dependencies.
Public keys are G1Elements, and signatures are G2Elements.


Then, to use:

## Import the library

```python
from blspy import (PrivateKey, Util, AugSchemeMPL, PopSchemeMPL,
G1Element, G2Element)
Expand All @@ -41,7 +46,9 @@ signature: G2Element = AugSchemeMPL.sign(sk, message)
ok: bool = AugSchemeMPL.verify(pk, message, signature)
assert ok
```

## Serializing keys and signatures to bytes

```python
sk_bytes: bytes = bytes(sk) # 32 bytes
pk_bytes: bytes = bytes(pk) # 48 bytes
Expand All @@ -51,13 +58,15 @@ print(sk_bytes.hex(), pk_bytes.hex(), signature_bytes.hex())
```

## Loading keys and signatures from bytes

```python
sk = PrivateKey.from_bytes(sk_bytes)
pk = G1Element.from_bytes(pk_bytes)
signature = G2Element.from_bytes(signature_bytes)
```

## Create aggregate signatures

```python
# Generate some more private keys
seed = bytes([1]) + seed[1:]
Expand All @@ -81,6 +90,7 @@ ok = AugSchemeMPL.aggregate_verify([pk1, pk2], [message, message2], agg_sig)
```

## Arbitrary trees of aggregates

```python
seed = bytes([3]) + seed[1:]
sk3: PrivateKey = AugSchemeMPL.key_gen(seed)
Expand All @@ -93,6 +103,7 @@ ok = AugSchemeMPL.aggregate_verify([pk1, pk2, pk3], [message, message2, message3
```

## Very fast verification with Proof of Possession scheme

```python
# If the same message is signed, you can use Proof of Posession (PopScheme) for efficiency
# A proof of possession MUST be passed around with the PK to ensure security.
Expand Down Expand Up @@ -121,6 +132,7 @@ ok = PopSchemeMPL.sign(pop_agg_sk, message) == pop_sig_agg
```

## HD keys using [EIP-2333](https://github.com/ethereum/EIPs/pull/2333)

```python
master_sk: PrivateKey = AugSchemeMPL.key_gen(seed)
child: PrivateKey = AugSchemeMPL.derive_child_sk(master_sk, 152)
Expand All @@ -134,4 +146,4 @@ child_u_pk: G1Element = AugSchemeMPL.derive_child_pk_unhardened(master_pk, 22)
grandchild_u_pk: G1Element = AugSchemeMPL.derive_child_pk_unhardened(child_u_pk, 0)

ok = (grandchild_u_pk == grandchild_u.get_g1())
```
```
14 changes: 8 additions & 6 deletions python-bindings/test.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# flake8: noqa: E501
import binascii
from copy import deepcopy

from blspy import (
PrivateKey,
Util,
BasicSchemeMPL,
AugSchemeMPL,
PopSchemeMPL,
BasicSchemeMPL,
G1Element,
G2Element,
PopSchemeMPL,
PrivateKey,
Util,
)
from copy import deepcopy
import binascii


def test_schemes():
Expand Down Expand Up @@ -69,6 +70,7 @@ def test_schemes():
sigU_child = Scheme.sign(childU, msg)
assert Scheme.verify(childUPk, msg, sigU_child)


def test_vectors_invalid():
# Invalid inputs from https://github.com/algorand/bls_sigs_ref/blob/master/python-impl/serdesZ.py
invalid_inputs_1 = [
Expand Down
6 changes: 3 additions & 3 deletions python-impl/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
### BLS12-381 and Signatures in python
# BLS12-381 and Signatures in python

Implements the BLS12 curve and optimal ate pairing, as well
as BLS signatures and aggregation. Use for reference / educational purposes only.

For an optimized implementation, use the [Python bindings](https://github.com/Chia-Network/bls-signatures/tree/master/python-bindings).
For an optimized implementation, use the [Python bindings](https://github.com/Chia-Network/bls-signatures/tree/main/python-bindings).

For a good introduction to pairings, read [Pairings for Beginners](http://www.craigcostello.com.au/pairings/PairingsForBeginners.pdf) by Craig Costello.

Map to curve implementation from https://github.com/algorand/bls_sigs_ref/.
Map to curve implementation from [Algorand](https://github.com/algorand/bls_sigs_ref/).

Run the tests with `python impl-test.py`.
6 changes: 4 additions & 2 deletions python-impl/ec.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations
from typing import List, Optional
import bls12381

from collections import namedtuple
from copy import deepcopy
from typing import List, Optional

import bls12381
from fields import FieldExtBase, Fq, Fq2, Fq6, Fq12
from util import hash256

Expand Down
1 change: 1 addition & 0 deletions python-impl/fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import annotations

from copy import deepcopy
from typing import Any

Expand Down
2 changes: 1 addition & 1 deletion python-impl/hd_keys.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ec import G1Generator, G2Generator, JacobianPoint, default_ec
from hkdf import extract_expand
from private_key import PrivateKey
from util import hash256
from ec import JacobianPoint, default_ec, G1Generator, G2Generator


def key_gen(seed: bytes) -> PrivateKey:
Expand Down
4 changes: 2 additions & 2 deletions python-impl/hkdf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from math import ceil
import hmac
import hashlib
import hmac
from math import ceil

BLOCK_SIZE = 32

Expand Down
26 changes: 7 additions & 19 deletions python-impl/impl-test.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import hashlib
from copy import deepcopy
from secrets import token_bytes
import hashlib

from ec import (G1FromBytes, G1Generator, G1Infinity, G2FromBytes, G2Generator,
G2Infinity, JacobianPoint, default_ec, default_ec_twist,
sign_Fq2, twist, untwist, y_for_x)
from fields import Fq, Fq2, Fq6, Fq12
from ec import (
JacobianPoint,
G1Generator,
G2Generator,
G1Infinity,
G2Infinity,
G1FromBytes,
G2FromBytes,
default_ec,
default_ec_twist,
sign_Fq2,
twist,
untwist,
y_for_x,
)
from pairing import ate_pairing
from hash_to_field import expand_message_xmd
from hkdf import expand, extract
from op_swu_g2 import g2_map
from schemes import AugSchemeMPL, PopSchemeMPL, BasicSchemeMPL
from pairing import ate_pairing
from private_key import PrivateKey
from hkdf import extract, expand
from schemes import AugSchemeMPL, BasicSchemeMPL, PopSchemeMPL

G1Element = JacobianPoint
G2Element = JacobianPoint
Expand Down
Loading

0 comments on commit e095a3e

Please sign in to comment.