Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat pure go bls #5

Open
wants to merge 6 commits into
base: feat-bls-only
Choose a base branch
from
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,47 @@
# Build & Test
# Tenderbls

This copies the BLS parts from Tenderdash to be able to run standalone, and compare against Pure Go implementations of Chia / Relic BLS.

- Go (Kilic)
- TinyGo
- WASM
- CGO (Relic)

# Build & Test Go / TinyGo

```sh
curl https://webi.sh/go | sh
source ~/.config/envman/PATH.env

go build ./cmd/chiabls
```

```sh
curl https://webi.sh/tinygo | sh
source ~/.config/envman/PATH.env

tinygo build --tags generic ./cmd/chiabls
```

## WASM

```sh
curl https://webi.sh/rust | sh
curl https://webi.sh/tinygo | sh
source ~/.config/envman/PATH.env

cargo install wasm-opt

tinygo build --tags generic \
-o chiabls.wasm -target=wasi \
./cmd/chiabls
```

```sh
GOOS=wasip1 GOARCH=wasm go build -o chiabls.wasm ./cmd/chiabls
```

# Build & Test CGO

```sh
git submodule init
Expand Down
69 changes: 69 additions & 0 deletions blsaug/blsaug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package blsaug

import (
chiabls "github.com/chuwt/chia-bls-go"
"github.com/dashpay/tenderdash/internal/blscore"
bls12381 "github.com/kilic/bls12-381"
)

var AugSchemeDst = blscore.DomainSeparationTag(
"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_AUG_",
)

type AugSchemeMPL struct {
// domainSeparationValue string
}

// Sign
func (asm *AugSchemeMPL) Sign(sk *chiabls.PrivateKey, message []byte) []byte {

pk := sk.GetPublicKey()
pkBytes := pk.Bytes()
message = append(pkBytes, message...)
sigPoint := blscore.SignMpl(sk, message, AugSchemeDst)
sigBytes := bls12381.NewG2().ToCompressed(sigPoint)

return sigBytes
}

// SignWithPrependPK
func (asm *AugSchemeMPL) SignWithPrependPK(sk *chiabls.PrivateKey, prependPK *chiabls.PublicKey, message []byte) []byte {

pkBytes := prependPK.Bytes()
message = append(pkBytes, message...)
sigPoint := blscore.SignMpl(sk, message, AugSchemeDst)
sigBytes := bls12381.NewG2().ToCompressed(sigPoint)

return sigBytes
}

// Verify
func (asm *AugSchemeMPL) Verify(pk *chiabls.PublicKey, message []byte, sig []byte) bool {

pkBytes := pk.Bytes()
message = append(pkBytes, message...)
verified := blscore.VerifyMpl(pk, message, sig, AugSchemeDst)

return verified
}

// Aggregate
func (asm *AugSchemeMPL) Aggregate(signatures ...[]byte) ([]byte, error) {
sigBytes, err := blscore.AggregateMpl(signatures...)

return sigBytes, err
}

// AggregateVerify
func (asm *AugSchemeMPL) AggregateVerify(pksBytes [][]byte, messages [][]byte, sig []byte) bool {

pkMessages := [][]byte{}
for i, pkBytes := range pksBytes {
message := messages[i]
message = append(pkBytes, message...)
pkMessages = append(pkMessages, message)
}
verified := blscore.AggregateVerify(pksBytes, pkMessages, sig, AugSchemeDst)

return verified
}
67 changes: 67 additions & 0 deletions blsbasic/blsbasic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package blsbasic

import (
"bytes"
"fmt"

chiabls "github.com/chuwt/chia-bls-go"
"github.com/dashpay/tenderdash/internal/blscore"
bls12381 "github.com/kilic/bls12-381"
)

var BasicSchemeDst = blscore.DomainSeparationTag(
"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_",
)

type BasicSchemeMPL struct {
// domainSeparationValue string
}

func New() blscore.SchemeMPL {
bsm := &BasicSchemeMPL{}

return bsm
}

// Sign
func (bsm *BasicSchemeMPL) Sign(sk *chiabls.PrivateKey, message []byte) []byte {
sigPoint := blscore.SignMpl(sk, message, BasicSchemeDst)
sigBytes := bls12381.NewG2().ToCompressed(sigPoint)

return sigBytes
}

// Verify
func (bsm *BasicSchemeMPL) Verify(pk *chiabls.PublicKey, message []byte, sig []byte) bool {
verified := blscore.VerifyMpl(pk, message, sig, BasicSchemeDst)

return verified
}

// Aggregate
func (bsm *BasicSchemeMPL) Aggregate(signatures ...[]byte) ([]byte, error) {
sigBytes, err := blscore.AggregateMpl(signatures...)

return sigBytes, err
}

// AggregateVerify
func (bsm *BasicSchemeMPL) AggregateVerify(pks [][]byte, messages [][]byte, sig []byte) (bool, error) {
n := len(messages)
m := 1
for i, a := range messages {
for j, b := range messages[m:] {
if bytes.Equal(a, b) {
ib := m + j
return false, fmt.Errorf("messages at indexes %d and %d are identical", i, ib)
}
}
m++
if m >= n {
break
}
}

verified := blscore.AggregateVerify(pks, messages, sig, BasicSchemeDst)
return verified, nil
}
45 changes: 45 additions & 0 deletions cmd/chiabls/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"log"

blsbasic "github.com/dashpay/tenderdash/blsbasic"

chiabls "github.com/chuwt/chia-bls-go"
)

func main() {
secret := []byte("it's a secret")
message := []byte("Hello, World!")

seed := sha256.Sum256(secret)
privKey := chiabls.KeyGen(seed[:])
pubKey := privKey.GetPublicKey()
privBytes := privKey.Bytes()
pubBytes := pubKey.Bytes()
privStr := hex.EncodeToString(privBytes)
pubStr := hex.EncodeToString(pubBytes)

fmt.Printf("Secret: %q\n", secret)
fmt.Printf("Private: %#v\n", privStr)
fmt.Printf("Pub: %#v\n", pubStr)

scheme := blsbasic.New()

sigBytes := scheme.Sign(&privKey, message)
sigStr := hex.EncodeToString(sigBytes)

fmt.Printf("\n")
fmt.Printf("Message: %s\n", message)
fmt.Printf("Signature: %#v\n", sigStr)
verify := scheme.Verify(&pubKey, message, sigBytes)
if !verify {
log.Fatal("bad signature using pubkey")
}
fmt.Printf("Verified: %#v\n", verify)

fmt.Printf("\n")
}
26 changes: 26 additions & 0 deletions crypto/gobls/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package gobls

import (
"io"
"testing"

"github.com/dashpay/tenderdash/crypto"
"github.com/dashpay/tenderdash/crypto/internal/benchmarking"
)

func BenchmarkKeyGeneration(b *testing.B) {
benchmarkKeygenWrapper := func(reader io.Reader) crypto.PrivKey {
return genPrivKey(reader)
}
benchmarking.BenchmarkKeyGeneration(b, benchmarkKeygenWrapper)
}

func BenchmarkSigning(b *testing.B) {
priv := GenPrivKey()
benchmarking.BenchmarkSigning(b, priv)
}

func BenchmarkVerification(b *testing.B) {
priv := GenPrivKey()
benchmarking.BenchmarkVerification(b, priv)
}
Loading