Skip to content

Commit ae8e608

Browse files
committed
Added crypto primitives for future SDK(Ed25519 + PQC)
1 parent 8772449 commit ae8e608

File tree

7 files changed

+330
-6
lines changed

7 files changed

+330
-6
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.exe
2+
.vscode

crypto_primitives/ed25519.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package crypto_primitives
2+
3+
import (
4+
"crypto/ed25519"
5+
"crypto/rand"
6+
"encoding/base64"
7+
"encoding/hex"
8+
9+
"github.com/btcsuite/btcutil/base58"
10+
)
11+
12+
func GenerateEd25519KeyPair() (string, string) {
13+
14+
publicKey, privateKey, _ := ed25519.GenerateKey(rand.Reader)
15+
16+
return base58.Encode(publicKey), hex.EncodeToString(privateKey[:32])
17+
18+
}
19+
20+
// Returns signature in base64(to use it in transaction later)
21+
22+
func GenerateEd25519Signature(privateKey, msg string) string {
23+
24+
privateKeyAsBytes, _ := hex.DecodeString(privateKey)
25+
26+
privateKeyFromSeed := ed25519.NewKeyFromSeed(privateKeyAsBytes)
27+
28+
msgAsBytes := []byte(msg)
29+
30+
return base64.StdEncoding.EncodeToString(ed25519.Sign(privateKeyFromSeed, msgAsBytes))
31+
32+
}
33+
34+
/*
35+
0 - message that was signed
36+
1 - pubKey
37+
2 - signature
38+
*/
39+
func VerifyEd25519Signature(stringMessage, base58PubKey, base64Signature string) bool {
40+
41+
// Decode evrything
42+
43+
msgAsBytes := []byte(stringMessage)
44+
45+
publicKey := base58.Decode(base58PubKey)
46+
47+
signature, _ := base64.StdEncoding.DecodeString(base64Signature)
48+
49+
return ed25519.Verify(publicKey, msgAsBytes, signature)
50+
51+
}

crypto_primitives/pqc.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
3+
4+
Links:
5+
6+
https://github.com/LoCCS/bliss/search?q=entropy
7+
https://github.com/LoCCS/bliss
8+
9+
10+
*/
11+
12+
package crypto_primitives
13+
14+
import (
15+
"math/rand"
16+
17+
"time"
18+
19+
"github.com/cloudflare/circl/sign/dilithium"
20+
21+
"github.com/LoCCS/bliss/sampler"
22+
23+
"github.com/LoCCS/bliss"
24+
25+
"encoding/hex"
26+
)
27+
28+
//______________________________ Dilithium ______________________________
29+
30+
var modename string = "Dilithium5" // Dilithium2-AES Dilithium3 Dilithium3-AES Dilithium5 Dilithium5-AES
31+
32+
var mode = dilithium.ModeByName(modename)
33+
34+
func GenerateDilithiumKeypair() (string, string) {
35+
36+
publicKey, privateKey, _ := mode.GenerateKey(nil)
37+
38+
return hex.EncodeToString(publicKey.Bytes()), hex.EncodeToString(privateKey.Bytes())
39+
40+
}
41+
42+
/*
43+
0 - privateKey
44+
1 - message
45+
*/
46+
func GenerateDilithiumSignature(privateKey, msg string) string {
47+
48+
privateKeyAsBytes, _ := hex.DecodeString(privateKey)
49+
50+
msgAsBytes := []byte(msg)
51+
52+
return hex.EncodeToString(mode.Sign(mode.PrivateKeyFromBytes(privateKeyAsBytes), msgAsBytes))
53+
54+
}
55+
56+
/*
57+
0 - message that was signed
58+
1 - pubKey
59+
2 - signature
60+
*/
61+
func VerifyDilithiumSignature(msg, pubKey, hexSignature string) bool {
62+
63+
msgAsBytes := []byte(msg)
64+
65+
publicKey, _ := hex.DecodeString(pubKey)
66+
67+
signature, _ := hex.DecodeString(hexSignature)
68+
69+
return mode.Verify(mode.PublicKeyFromBytes(publicKey), msgAsBytes, signature)
70+
71+
}
72+
73+
//________________________________ BLISS ________________________________
74+
75+
func GenerateBlissKeypair() (string, string) {
76+
77+
rand.Seed(time.Now().UnixNano())
78+
79+
seed := make([]byte, sampler.SHA_512_DIGEST_LENGTH)
80+
81+
rand.Read(seed)
82+
83+
entropy, _ := sampler.NewEntropy(seed)
84+
85+
prv, _ := bliss.GeneratePrivateKey(0, entropy)
86+
87+
pub := prv.PublicKey()
88+
89+
return hex.EncodeToString(pub.Encode()), hex.EncodeToString(seed)
90+
91+
}
92+
93+
/*
94+
0 - privateKey
95+
1 - message
96+
*/
97+
func GenerateBlissSignature(pritaveKey, msg string) string {
98+
99+
//Decode msg an seed => entropy => privateKey
100+
101+
sid, _ := hex.DecodeString(pritaveKey)
102+
103+
msgAsBytes := []byte(msg)
104+
105+
seed := []byte(sid) // uint8/byte array
106+
107+
entropy, _ := sampler.NewEntropy(seed)
108+
109+
key, _ := bliss.GeneratePrivateKey(0, entropy)
110+
111+
//Gen signature
112+
sig, _ := key.Sign(msgAsBytes, entropy)
113+
114+
return hex.EncodeToString(sig.Encode())
115+
116+
}
117+
118+
/*
119+
0 - message
120+
1 - publicKey
121+
2 - signature
122+
*/
123+
func VerifyBlissSignature(msg, hexPublicKey, hexSignature string) bool {
124+
125+
//Decode msg an publicKey
126+
msgAsBytes := []byte(msg)
127+
128+
hexEncodedPublicKey, _ := hex.DecodeString(hexPublicKey)
129+
130+
publicKey, _ := bliss.DecodePublicKey(hexEncodedPublicKey)
131+
132+
//Decode signature
133+
decodedSignature, _ := hex.DecodeString(hexSignature)
134+
135+
signature, _ := bliss.DecodeSignature(decodedSignature)
136+
137+
//Verification itself
138+
_, err := publicKey.Verify(msgAsBytes, signature)
139+
140+
return err == nil
141+
142+
}

go.mod

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
module github.com/KLYN74R/Web1337Golang
22

3-
go 1.18
3+
go 1.21.1
4+
5+
require (
6+
github.com/LoCCS/bliss v0.0.0-20180223025823-07585ac9b817 // indirect
7+
github.com/btcsuite/btcutil v1.0.2 // indirect
8+
github.com/cloudflare/circl v1.3.6 // indirect
9+
golang.org/x/crypto v0.16.0 // indirect
10+
golang.org/x/sys v0.15.0 // indirect
11+
)

go.sum

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

web1337.go

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,71 @@ For Golang devs
88

99
package web1337
1010

11-
func Hello() string {
11+
import (
12+
"fmt"
1213

13-
return "Hello from Web1337"
14+
"github.com/KLYN74R/Web1337Golang/crypto_primitives"
15+
)
16+
17+
func Ed25519Process() bool {
18+
19+
myPubKey, myPrivateKey := crypto_primitives.GenerateEd25519KeyPair()
20+
21+
fmt.Println("PubKey is ", myPubKey)
22+
23+
fmt.Println("PrivateKey is ", myPrivateKey)
24+
25+
signa := crypto_primitives.GenerateEd25519Signature(myPrivateKey, "Hello KLY")
26+
27+
fmt.Println("Signa is ", signa)
28+
29+
isOk := crypto_primitives.VerifyEd25519Signature("Hello KLY", myPubKey, signa)
30+
31+
fmt.Println("Is ok =>", isOk)
32+
33+
return isOk
34+
35+
}
36+
37+
func BlissProcess() bool {
38+
39+
myPubKey, myPrivateKey := crypto_primitives.GenerateBlissKeypair()
40+
41+
fmt.Println("PubKey is ", myPubKey)
42+
43+
fmt.Println("PrivateKey is ", myPrivateKey)
44+
45+
signa := crypto_primitives.GenerateBlissSignature(myPrivateKey, "Hello KLY")
46+
47+
fmt.Println("Signa is ", signa)
48+
49+
isOk := crypto_primitives.VerifyBlissSignature("Hello KLY", myPubKey, signa)
50+
51+
fmt.Println("Is ok =>", isOk)
52+
53+
return isOk
54+
55+
}
56+
57+
func DilithiumProcess() bool {
58+
59+
myPubKey, myPrivateKey := crypto_primitives.GenerateDilithiumKeypair()
60+
61+
fmt.Println("PubKey is ", myPubKey)
62+
63+
fmt.Println("PrivateKey is ", myPrivateKey)
64+
65+
signa := crypto_primitives.GenerateDilithiumSignature(myPrivateKey, "Hello KLY")
66+
67+
fmt.Println("Signa is ", signa)
68+
69+
isOk := crypto_primitives.VerifyDilithiumSignature("Hello KLY", myPubKey, signa)
70+
71+
fmt.Println("Is ok =>", isOk)
72+
73+
return isOk
74+
75+
}
76+
77+
type Web1337 struct {
1478
}

web1337_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,21 @@ import (
1212
"testing"
1313
)
1414

15-
func TestHello(t *testing.T) {
15+
func TestEd25519(t *testing.T) {
1616

17-
if Hello() != "Hello from Web1337" {
17+
if !(Ed25519Process()) {
1818

19-
t.Errorf("Hello() func didn't return greeting")
19+
t.Error("Signature verification failed")
20+
21+
}
22+
23+
}
24+
25+
func TestPQC(t *testing.T) {
26+
27+
if !(DilithiumProcess() && BlissProcess()) {
28+
29+
t.Error("Signature verification failed")
2030

2131
}
2232

0 commit comments

Comments
 (0)