Skip to content

Commit

Permalink
Added crypto primitives for future SDK(Ed25519 + PQC)
Browse files Browse the repository at this point in the history
  • Loading branch information
VladChernenko committed Dec 18, 2023
1 parent 8772449 commit ae8e608
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.exe
.vscode
51 changes: 51 additions & 0 deletions crypto_primitives/ed25519.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package crypto_primitives

import (
"crypto/ed25519"
"crypto/rand"
"encoding/base64"
"encoding/hex"

"github.com/btcsuite/btcutil/base58"
)

func GenerateEd25519KeyPair() (string, string) {

publicKey, privateKey, _ := ed25519.GenerateKey(rand.Reader)

return base58.Encode(publicKey), hex.EncodeToString(privateKey[:32])

}

// Returns signature in base64(to use it in transaction later)

func GenerateEd25519Signature(privateKey, msg string) string {

privateKeyAsBytes, _ := hex.DecodeString(privateKey)

privateKeyFromSeed := ed25519.NewKeyFromSeed(privateKeyAsBytes)

msgAsBytes := []byte(msg)

return base64.StdEncoding.EncodeToString(ed25519.Sign(privateKeyFromSeed, msgAsBytes))

}

/*
0 - message that was signed
1 - pubKey
2 - signature
*/
func VerifyEd25519Signature(stringMessage, base58PubKey, base64Signature string) bool {

// Decode evrything

msgAsBytes := []byte(stringMessage)

publicKey := base58.Decode(base58PubKey)

signature, _ := base64.StdEncoding.DecodeString(base64Signature)

return ed25519.Verify(publicKey, msgAsBytes, signature)

}
142 changes: 142 additions & 0 deletions crypto_primitives/pqc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
Links:
https://github.com/LoCCS/bliss/search?q=entropy
https://github.com/LoCCS/bliss
*/

package crypto_primitives

import (
"math/rand"

"time"

"github.com/cloudflare/circl/sign/dilithium"

"github.com/LoCCS/bliss/sampler"

"github.com/LoCCS/bliss"

"encoding/hex"
)

//______________________________ Dilithium ______________________________

var modename string = "Dilithium5" // Dilithium2-AES Dilithium3 Dilithium3-AES Dilithium5 Dilithium5-AES

var mode = dilithium.ModeByName(modename)

func GenerateDilithiumKeypair() (string, string) {

publicKey, privateKey, _ := mode.GenerateKey(nil)

return hex.EncodeToString(publicKey.Bytes()), hex.EncodeToString(privateKey.Bytes())

}

/*
0 - privateKey
1 - message
*/
func GenerateDilithiumSignature(privateKey, msg string) string {

privateKeyAsBytes, _ := hex.DecodeString(privateKey)

msgAsBytes := []byte(msg)

return hex.EncodeToString(mode.Sign(mode.PrivateKeyFromBytes(privateKeyAsBytes), msgAsBytes))

}

/*
0 - message that was signed
1 - pubKey
2 - signature
*/
func VerifyDilithiumSignature(msg, pubKey, hexSignature string) bool {

msgAsBytes := []byte(msg)

publicKey, _ := hex.DecodeString(pubKey)

signature, _ := hex.DecodeString(hexSignature)

return mode.Verify(mode.PublicKeyFromBytes(publicKey), msgAsBytes, signature)

}

//________________________________ BLISS ________________________________

func GenerateBlissKeypair() (string, string) {

rand.Seed(time.Now().UnixNano())

seed := make([]byte, sampler.SHA_512_DIGEST_LENGTH)

rand.Read(seed)

entropy, _ := sampler.NewEntropy(seed)

prv, _ := bliss.GeneratePrivateKey(0, entropy)

pub := prv.PublicKey()

return hex.EncodeToString(pub.Encode()), hex.EncodeToString(seed)

}

/*
0 - privateKey
1 - message
*/
func GenerateBlissSignature(pritaveKey, msg string) string {

//Decode msg an seed => entropy => privateKey

sid, _ := hex.DecodeString(pritaveKey)

msgAsBytes := []byte(msg)

seed := []byte(sid) // uint8/byte array

entropy, _ := sampler.NewEntropy(seed)

key, _ := bliss.GeneratePrivateKey(0, entropy)

//Gen signature
sig, _ := key.Sign(msgAsBytes, entropy)

return hex.EncodeToString(sig.Encode())

}

/*
0 - message
1 - publicKey
2 - signature
*/
func VerifyBlissSignature(msg, hexPublicKey, hexSignature string) bool {

//Decode msg an publicKey
msgAsBytes := []byte(msg)

hexEncodedPublicKey, _ := hex.DecodeString(hexPublicKey)

publicKey, _ := bliss.DecodePublicKey(hexEncodedPublicKey)

//Decode signature
decodedSignature, _ := hex.DecodeString(hexSignature)

signature, _ := bliss.DecodeSignature(decodedSignature)

//Verification itself
_, err := publicKey.Verify(msgAsBytes, signature)

return err == nil

}
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/KLYN74R/Web1337Golang

go 1.18
go 1.21.1

require (
github.com/LoCCS/bliss v0.0.0-20180223025823-07585ac9b817 // indirect
github.com/btcsuite/btcutil v1.0.2 // indirect
github.com/cloudflare/circl v1.3.6 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/sys v0.15.0 // indirect
)
47 changes: 47 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
github.com/LoCCS/bliss v0.0.0-20180223025823-07585ac9b817 h1:NDsBS8kyF3QPOIQ6Yk8a1VD+8N56oiNAQPykm0TA2H4=
github.com/LoCCS/bliss v0.0.0-20180223025823-07585ac9b817/go.mod h1:NcO8qUafnT7MIuK/fQb7DxFVk5hWUqY8BlFqISQTJKQ=
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
github.com/btcsuite/btcutil v1.0.2 h1:9iZ1Terx9fMIOtq1VrwdqfsATL9MC2l8ZrUY6YZ2uts=
github.com/btcsuite/btcutil v1.0.2/go.mod h1:j9HUFwoQRsZL3V4n+qG+CUnEGHOarIxfC3Le2Yhbcts=
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg=
github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVaaLLH7j4eDXPRvw78tMflu7Ie2bzYOH4Y8rRKBY=
github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc=
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY=
github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs=
github.com/cloudflare/circl v1.3.6 h1:/xbKIqSHbZXHwkhbrhrt2YOHIwYJlXH94E3tI/gDlUg=
github.com/cloudflare/circl v1.3.6/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a h1:diz9pEYuTIuLMJLs3rGDkeaTsNyRs6duYdFyPAxzE/U=
golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
68 changes: 66 additions & 2 deletions web1337.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,71 @@ For Golang devs

package web1337

func Hello() string {
import (
"fmt"

return "Hello from Web1337"
"github.com/KLYN74R/Web1337Golang/crypto_primitives"
)

func Ed25519Process() bool {

myPubKey, myPrivateKey := crypto_primitives.GenerateEd25519KeyPair()

fmt.Println("PubKey is ", myPubKey)

fmt.Println("PrivateKey is ", myPrivateKey)

signa := crypto_primitives.GenerateEd25519Signature(myPrivateKey, "Hello KLY")

fmt.Println("Signa is ", signa)

isOk := crypto_primitives.VerifyEd25519Signature("Hello KLY", myPubKey, signa)

fmt.Println("Is ok =>", isOk)

return isOk

}

func BlissProcess() bool {

myPubKey, myPrivateKey := crypto_primitives.GenerateBlissKeypair()

fmt.Println("PubKey is ", myPubKey)

fmt.Println("PrivateKey is ", myPrivateKey)

signa := crypto_primitives.GenerateBlissSignature(myPrivateKey, "Hello KLY")

fmt.Println("Signa is ", signa)

isOk := crypto_primitives.VerifyBlissSignature("Hello KLY", myPubKey, signa)

fmt.Println("Is ok =>", isOk)

return isOk

}

func DilithiumProcess() bool {

myPubKey, myPrivateKey := crypto_primitives.GenerateDilithiumKeypair()

fmt.Println("PubKey is ", myPubKey)

fmt.Println("PrivateKey is ", myPrivateKey)

signa := crypto_primitives.GenerateDilithiumSignature(myPrivateKey, "Hello KLY")

fmt.Println("Signa is ", signa)

isOk := crypto_primitives.VerifyDilithiumSignature("Hello KLY", myPubKey, signa)

fmt.Println("Is ok =>", isOk)

return isOk

}

type Web1337 struct {
}
16 changes: 13 additions & 3 deletions web1337_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,21 @@ import (
"testing"
)

func TestHello(t *testing.T) {
func TestEd25519(t *testing.T) {

if Hello() != "Hello from Web1337" {
if !(Ed25519Process()) {

t.Errorf("Hello() func didn't return greeting")
t.Error("Signature verification failed")

}

}

func TestPQC(t *testing.T) {

if !(DilithiumProcess() && BlissProcess()) {

t.Error("Signature verification failed")

}

Expand Down

0 comments on commit ae8e608

Please sign in to comment.