Skip to content

Commit

Permalink
Merge pull request dashpay#19 from xdustinface/pr-catch-up-03-04
Browse files Browse the repository at this point in the history
backport: Merge Chia-Network branch `main` into dashpay branch `develop`
  • Loading branch information
xdustinface authored Apr 5, 2021
2 parents 29ab284 + f06980b commit a83b071
Show file tree
Hide file tree
Showing 28 changed files with 314 additions and 256 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
12 changes: 8 additions & 4 deletions .github/workflows/relic-nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Build and Test C++ with Relic Nightly

on:
schedule:
- cron: "* 14 * * *"
- cron: "0 11 * * *"
workflow_dispatch:

jobs:
Expand All @@ -16,17 +16,21 @@ 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 }}

- name: Checkout code
uses: actions/checkout@v2

- name: Ubuntu build C++ and test Relic at origin/master
- name: Ubuntu build C++ and test Relic at origin/main
if: startsWith(matrix.os, 'ubuntu')
run: |
echo "Relic origin/main commit:"
curl -H "application/vnd.github.v3.sha" \
https://api.github.com/repos/relic-toolkit/relic/commits/main | \
head -10
sudo apt-get update
sudo apt-get install snap -y
sudo apt-get remove --purge cmake -y
Expand All @@ -44,7 +48,7 @@ jobs:
cd ..
echo "Setting libsodium to static compile."
export CIBUILDWHEEL=1
export RELIC_MASTER=1
export RELIC_MAIN=1
mkdir -p build
cd build
cmake ../
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)
1 change: 0 additions & 1 deletion contrib/pybind11
Submodule pybind11 deleted from 3b1dbe
59 changes: 40 additions & 19 deletions js-bindings/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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())
```
```
Loading

0 comments on commit a83b071

Please sign in to comment.