Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ build/
*.gcda
*.trs
*.log
src/main/resources/org/
src/main/resources/org/bitcoin/
src/main/resources/co/rsk
altbn128/pkg/
altbn128/src/
51 changes: 35 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
FROM ubuntu:18.04

# CUSTOM VARIABLES
ENV GOCUSTOM=/usr/local
ENV GOLANG=go1.13.5.linux-amd64.tar.gz

# ENV VARIABLES
ENV GOROOT=$GOCUSTOM/go
ENV GOLANG=go1.13.5.linux-amd64.tar.gz
ENV GOROOT=/usr/local/go
ENV GOPATH=/native/altbn128
ENV GOBIN=$GOPATH/bin
ENV PATH=$GOPATH/bin:$GOROOT/bin:$PATH
ENV JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
ENV CGO_CFLAGS="-I$JAVA_HOME/include -I$JAVA_HOME/include/linux"
ENV PATH=$GOPATH/bin:$GOROOT/bin:$PATH

# This variable makes __DATE__ & __TIME__ constant (needed for gcc deterministic compile)
# makes eth_pairings.dll reproducible, therefore the whole project
ENV SOURCE_DATE_EPOCH=1612383945

RUN apt-get update && \
apt-get -y install git && \
apt-get -y install tree && \
apt-get install -y -o APT::Install-Suggests="false" git curl openjdk-8-jdk build-essential=12.4ubuntu1 && \
apt-get install -y -o APT::Install-Suggests="true" autoconf && \
curl "https://dl.google.com/go/"$GOLANG -o $GOLANG -# && \
echo "512103d7ad296467814a6e3f635631bd35574cab3369a97a323c9a585ccaa569 go1.13.5.linux-amd64.tar.gz" > goChecksum.txt && \
# DEPENDENCIES

# common
RUN apt-get update
RUN apt-get install -y build-essential
RUN apt-get install -y -o APT::Install-Suggests="false" git tree curl openjdk-8-jdk

# secp25k1 dependencies
RUN apt-get install -y autoconf libtool

# altbn128 dependencies
RUN curl "https://dl.google.com/go/"$GOLANG -o $GOLANG -# && \
echo "512103d7ad296467814a6e3f635631bd35574cab3369a97a323c9a585ccaa569 $GOLANG" > goChecksum.txt && \
cat goChecksum.txt && \
sha256sum -c goChecksum.txt && \
tar -xvf $GOLANG && mkdir -p $GOCUSTOM && mv go $GOCUSTOM
tar -xvf $GOLANG && \
mkdir -p /usr/local && \
mv go /usr/local

# bls12-381 dependencies
RUN curl "https://sh.rustup.rs" -sSf | bash -s -- -y
RUN apt-get install -y clang gcc g++ zlib1g-dev libmpc-dev libmpfr-dev libgmp-dev wget cmake libxml2-dev libssl-dev gcc-mingw-w64-x86-64
RUN git clone https://github.com/tpoechtrager/osxcross && \
cd osxcross && \
wget -nc https://s3.dockerproject.org/darwin/v2/MacOSX10.10.sdk.tar.xz&& \
mv MacOSX10.10.sdk.tar.xz tarballs/ && \
UNATTENDED=yes OSX_VERSION_MIN=10.7 ./build.sh

RUN apt-get install -y vim

#Cloning native's repo
#RUN git clone https://github.com/rsksmart/native.git
# Cloning native's repo
COPY . /native
WORKDIR /native

Expand Down
91 changes: 36 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,86 +6,67 @@ In this project you'll find all the native libraries used in rskj.

- altbn128
- [secp256k1](secp256k1/README.md)
- [bls12-381](bls12_381/README.md)

## Build

### Productive

Requirements
- Docker
### Compatibility

First build docker container
| Library | Linux | Mac OS | Windows (x64) |
| ------------- |:-------------:| :--------:| :-------:|
| altbn128 | x | | |
| secp256k1 | x | | |
| bls12-381 | x | x | x |

```bash
docker build -t native-libs .
```
## Build

Then run it
Builds each library, runs all the tests (only for linux) and bundles `native-x.y.z.jar`

```bash
docker run --rm -v $(pwd)/build:/native/build native-libs
> docker build -t native-libs .
> docker run --rm -v $(pwd)/build:/native/build native-libs
```

### Dev

There is a second experimental build process. With this one you'll be able to do a cross platform build for secp256k1.

```bash
./gradlew buildDev
```
## Tests

NOTE: Linux, Mac & Win binaries at `resources/`
You'll only be able to run tests if your system it's compatible (linux recommended).

## Build a Specific Library

### Altbn128

Requirements
- go

```
./gradlew buildAltbn128
```
To build an specific library

### Secp256k1
```bash
# altbn128 (req: go)

Requirements
- Autoconf
- Libtool
> ./gradlew buildAltbn128

```
./gradlew buildSecp256k1CurrentOs
```
# secp256k1 (req: Autoconf, Libtool)

Build for Linux, Mac & Windows. Notice this is an experimental feature because Mac and Windows binaries aren't reproducibles
> ./gradlew buildSecp256k1

Requirements
- Docker
# bls12-381 (req: rust)

```
./gradlew buildSecp256k1Cross
> ./gradlew buildBls12_381
```

## Tests
## Checksums

```bash
./gradlew test
```
# altbn128
libbn128.so: ee41baa43b5a3927e99c2d0f826666e5baf2885ec0d689ea3c591db35ad9ae47

## Checksums
# secp256k1
libsecp256k1.so: dd55be8c14220f6c846a52ac891ff1d6f1f35d1f8bb5feb89364e420a50752a9

```
libbn128.so: 669543be939058001ffb5ca3b816d3961f39db82dad60dfd0ffc89540956dc6f
libsecp256k1.so: 0b99909b0c86b3f4bcad90f70eef81d7b5fd77f7d29997195b3291526e5cd7f0
native-1.2.0.jar: 43f2308bd6bf982604d960c4d5438d2f3b12fd09e9c041395634dcf50aded48b
# bls12-381
libeth_pairings.so: da214a3c3d66b057a6d3636161da977fdd8dee638a82919e86c18a6137ca0d04
libeth_pairings.dylib: 2bc21d8648319d2e23c71c337728a33828eacd258d9702abb45584644b6b75a3
eth_pairings.dll: 7814015cf244031df2a75d0455fc964540356e698d7c05368456013ee218bd43

native-1.3.0.jar: 85249d0333ec4997a61726e9665d5a7df3285599f6dab20cbdf9eb94488ebce0
go1.13.5.linux-amd64.tar.gz: 512103d7ad296467814a6e3f635631bd35574cab3369a97a323c9a585ccaa569

$ java -version
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (build 1.8.0_265-8u265-b01-0ubuntu2~18.04-b01)
OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode)
openjdk version "1.8.0_275"
OpenJDK Runtime Environment (build 1.8.0_275-8u275-b01-0ubuntu1~18.04-b01)
OpenJDK 64-Bit Server VM (build 25.275-b01, mixed mode)
```

## Disclaimer

Experimental/dev features should only be used under your own risk.
16 changes: 16 additions & 0 deletions altbn128/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

# delete old build dir, if exists
rm -rf /native/src/main/resources/co/rsk/altbn128 && \
mkdir -p /native/src/main/resources/co/rsk/altbn128/linux && \

# go, clean, test & build
go get && \
make clean && \
make test && \
make linux && \

# move to resources
mv libbn128.so /native/src/main/resources/co/rsk/altbn128/linux


3 changes: 0 additions & 3 deletions altbn128/buildAltbn128.sh

This file was deleted.

12 changes: 6 additions & 6 deletions altbn128/precompile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
bn256 "./bn256"
)

//export Java_co_rsk_altbn128_cloudflare_JniBn128_add
func Java_co_rsk_altbn128_cloudflare_JniBn128_add(env *C.JNIEnv, clazz C.jclass, x C.jbyteArray, len C.jint, y C.jbyteArray) C.jint {
//export Java_co_rsk_altbn128_JniBn128_add
func Java_co_rsk_altbn128_JniBn128_add(env *C.JNIEnv, clazz C.jclass, x C.jbyteArray, len C.jint, y C.jbyteArray) C.jint {
cData := C.getCByteArray(env, x)
slice := C.GoBytes(unsafe.Pointer(cData), len)
defer C.releaseCByteArray(env, x, cData)
Expand All @@ -36,8 +36,8 @@ func Java_co_rsk_altbn128_cloudflare_JniBn128_add(env *C.JNIEnv, clazz C.jclass,
return 1
}

//export Java_co_rsk_altbn128_cloudflare_JniBn128_mul
func Java_co_rsk_altbn128_cloudflare_JniBn128_mul(env *C.JNIEnv, clazz C.jclass, x C.jbyteArray, len C.jint, y C.jbyteArray) C.jint {
//export Java_co_rsk_altbn128_JniBn128_mul
func Java_co_rsk_altbn128_JniBn128_mul(env *C.JNIEnv, clazz C.jclass, x C.jbyteArray, len C.jint, y C.jbyteArray) C.jint {
cData := C.getCByteArray(env, x)
slice := C.GoBytes(unsafe.Pointer(cData), len)
defer C.releaseCByteArray(env, x, cData)
Expand All @@ -52,8 +52,8 @@ func Java_co_rsk_altbn128_cloudflare_JniBn128_mul(env *C.JNIEnv, clazz C.jclass,
return 1
}

//export Java_co_rsk_altbn128_cloudflare_JniBn128_pairing
func Java_co_rsk_altbn128_cloudflare_JniBn128_pairing(env *C.JNIEnv, clazz C.jclass, x C.jbyteArray, len C.jint, y C.jbyteArray) C.jint {
//export Java_co_rsk_altbn128_JniBn128_pairing
func Java_co_rsk_altbn128_JniBn128_pairing(env *C.JNIEnv, clazz C.jclass, x C.jbyteArray, len C.jint, y C.jbyteArray) C.jint {
cData := C.getCByteArray(env, x)
slice := C.GoBytes(unsafe.Pointer(cData), len)
defer C.releaseCByteArray(env, x, cData)
Expand Down
3 changes: 3 additions & 0 deletions bls12_381/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[target.x86_64-apple-darwin]
linker = "x86_64-apple-darwin14-clang"
ar = "x86_64-apple-darwin14-ar"
7 changes: 7 additions & 0 deletions bls12_381/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/target
**/*.rs.bk
Cargo.lock
.vscode
# src/test/gas_meter/**/*.csv
src/test/gas_meter/**/.ipynb_checkpoints/
*.svg
62 changes: 62 additions & 0 deletions bls12_381/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[package]
name = "eth_pairings"
version = "0.6.0"
authors = ["Alex Vlasov <alex.m.vlasov@gmail.com>"]
edition = "2018"
description = "EIP1962 reference implementation"
homepage = "https://github.com/matter-labs/eip1962"
license = "Apache-2.0"
exclude = [
"*.sh",
"src/test",
"src/bench"
]

[lib]
name = "eth_pairings"
crate-type = ["rlib", "cdylib", "staticlib"]

[dependencies]
byteorder = "1.3"
eth_pairings_repr_derive = {version = "0.2.0", path = "repr_derive"}
fixed_width_field = {version = "0.1", path = "fixed_width_field" }
fixed_width_group_and_loop = {version = "0.1", path = "fixed_width_group_and_loop" }
static_assertions = "1.1.*"
serde = { version = "1.0", features = ["derive"], optional = true }
serde_json = {version = "1.0", optional = true }
once_cell = {version = "1.*", optional = true }
num-bigint = {version = "0.2", optional = true }
num-traits = {version = "0.2", optional = true }

[dev-dependencies]
num-bigint = "0.2"
num-traits = "0.2"
num-integer = "0.1"
hex = "0.4"
rand = "0.7"
rand_xorshift = "0.2"
csv = "1"
pbr = "*"
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0" }
bn = { git = "https://github.com/paritytech/bn", default-features = false }
rayon = "1.*"
indicatif = {version = "*", features = ["with_rayon"]}
parity-crypto = {version = "0.4", features = ["publickey"]}
ethereum-types = "0.8"
keccak-hash = "0.4"

[features]
default = ["eip_2537", "gas_metering", "mappings", "eip_2357_c_api"]
benchmarks = []
mappings = ["once_cell", "num-bigint", "num-traits"]
gas_metering = ["serde", "serde_json", "once_cell"]
c_api = []
gas_metering_mode = []
fuzzing_mode = []
external_tests = []
eip_2537 = ["mappings"]
eip_2357_c_api = ["eip_2537"]

[profile.release]
incremental = false
13 changes: 13 additions & 0 deletions bls12_381/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2019 Alex Vlasov <alex.m.vlasov@gmail.com>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
46 changes: 46 additions & 0 deletions bls12_381/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Status

This Rust implementation of EIP1962 is complete to the large extend. If course it's possible to polish further (e.g. make it `no_std` compatible), but largest part is done:

Features:
- [x] Fields implementation
- [x] Weierstrass curves implementation
- [x] a = 0
- [x] generic case (a != 0, b != 0)
- [x] Extension towers
- [x] Fp2
- [x] Fp3
- [x] Fp4 as 2 over 2
- [x] Fp6 as 2 over 3
- [x] Fp6 as 3 over 2
- [x] Fp12 as 2 over 3 over 2
- [x] Pairings
- [x] BLS12 curves family
- [x] BN family
- [x] MNT6 family
- [x] MNT4 family
- [x] Cocks-Pinch method generated curves in Weierstrass form (Ate pairing) with k=6

Testing:

- Basic properties are tested during development (whitebox testing) in a form of e.g. bilinearity checks for pairings
- Fuzzy testing in cross-checks mode with C++ and Go implementations that catches both crashes in any of the libraries and tests for a consistent output (for consensus purposes)
- During such testing most of the checks are disabled, e.g. points are allowed to be not on the curve cause it would be difficult for a fuzzer to find a proper test vector. So such testing covers more edge cases then would be possible in production

# Documentation about EIP1962

See [documentation](https://github.com/matter-labs/eip1962/tree/master/documentation) folder for a complete description and the single source of truth about EIP.

## Original proposal

Original EIP is [here](https://eips.ethereum.org/EIPS/eip-1962)

# Contributors

- Kobi Gurkan, [kobigurk@gmail.com](mailto://kobigurk@gmail.com)

# Resources to consult and use

- https://eprint.iacr.org/2012/072.pdf
- https://eprint.iacr.org/2013/722.pdf
- https://eprint.iacr.org/2016/130.pdf
Loading