Skip to content

Commit f379d04

Browse files
committed
no more ecdsa
1 parent 8d08733 commit f379d04

14 files changed

+142
-136
lines changed

go.mod

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
11
module github.com/sei-protocol/sei-cryptography
22

3-
go 1.21
3+
go 1.22
4+
5+
toolchain go1.23.3
46

57
require (
68
github.com/bwesterb/go-ristretto v1.2.3
79
github.com/coinbase/kryptology v1.8.0
8-
github.com/ethereum/go-ethereum v1.13.15
10+
github.com/ethereum/go-ethereum v1.14.12
911
github.com/gtank/merlin v0.1.1
1012
github.com/stretchr/testify v1.9.0
1113
golang.org/x/crypto v0.27.0
1214
)
1315

1416
require (
1517
filippo.io/edwards25519 v1.1.0 // indirect
18+
github.com/bits-and-blooms/bitset v1.13.0 // indirect
1619
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
20+
github.com/consensys/bavard v0.1.13 // indirect
21+
github.com/consensys/gnark-crypto v0.12.1 // indirect
22+
github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c // indirect
23+
github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect
1724
github.com/davecgh/go-spew v1.1.1 // indirect
1825
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
26+
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
27+
github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9 // indirect
28+
github.com/holiman/uint256 v1.3.1 // indirect
1929
github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643 // indirect
30+
github.com/mmcloughlin/addchain v0.4.0 // indirect
2031
github.com/pkg/errors v0.9.1 // indirect
2132
github.com/pmezard/go-difflib v1.0.0 // indirect
33+
github.com/supranational/blst v0.3.13 // indirect
34+
golang.org/x/sync v0.7.0 // indirect
2235
golang.org/x/sys v0.25.0 // indirect
2336
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
2437
gopkg.in/yaml.v3 v3.0.1 // indirect
38+
rsc.io/tmplfunc v0.0.3 // indirect
2539
)
2640

2741
replace github.com/coinbase/kryptology => github.com/sei-protocol/coinbase-kryptology v0.0.0-20241015231206-08f61b7965cd

go.sum

+28
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,38 @@ filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmG
22
filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
33
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
44
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
5+
github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJRUA0wFAVE=
6+
github.com/bits-and-blooms/bitset v1.13.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
57
github.com/btcsuite/btcd/btcec/v2 v2.3.4 h1:3EJjcN70HCu/mwqlUsGK8GcNVyLVxFDlWurTXGPFfiQ=
68
github.com/btcsuite/btcd/btcec/v2 v2.3.4/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04=
79
github.com/bwesterb/go-ristretto v1.2.3 h1:1w53tCkGhCQ5djbat3+MH0BAQ5Kfgbt56UZQ/JMzngw=
810
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
11+
github.com/consensys/bavard v0.1.13 h1:oLhMLOFGTLdlda/kma4VOJazblc7IM5y5QPd2A/YjhQ=
12+
github.com/consensys/bavard v0.1.13/go.mod h1:9ItSMtA/dXMAiL7BG6bqW2m3NdSEObYWoH223nGHukI=
13+
github.com/consensys/gnark-crypto v0.12.1 h1:lHH39WuuFgVHONRl3J0LRBtuYdQTumFSDtJF7HpyG8M=
14+
github.com/consensys/gnark-crypto v0.12.1/go.mod h1:v2Gy7L/4ZRosZ7Ivs+9SfUDr0f5UlG+EM5t7MPHiLuY=
15+
github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c h1:uQYC5Z1mdLRPrZhHjHxufI8+2UG/i25QG92j0Er9p6I=
16+
github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c/go.mod h1:geZJZH3SzKCqnz5VT0q/DyIG/tvu/dZk+VIfXicupJs=
17+
github.com/crate-crypto/go-kzg-4844 v1.0.0 h1:TsSgHwrkTKecKJ4kadtHi4b3xHW5dCFUDFnUp1TsawI=
18+
github.com/crate-crypto/go-kzg-4844 v1.0.0/go.mod h1:1kMhvPgI0Ky3yIa+9lFySEBUBXkYxeOi8ZF1sYioxhc=
919
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1020
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1121
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
1222
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc=
1323
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs=
24+
github.com/ethereum/c-kzg-4844 v1.0.0 h1:0X1LBXxaEtYD9xsyj9B9ctQEZIpnvVDeoBx8aHEwTNA=
25+
github.com/ethereum/c-kzg-4844 v1.0.0/go.mod h1:VewdlzQmpT5QSrVhbBuGoCdFJkpaJlO1aQputP83wc0=
1426
github.com/ethereum/go-ethereum v1.13.15 h1:U7sSGYGo4SPjP6iNIifNoyIAiNjrmQkz6EwQG+/EZWo=
1527
github.com/ethereum/go-ethereum v1.13.15/go.mod h1:TN8ZiHrdJwSe8Cb6x+p0hs5CxhJZPbqB7hHkaUXcmIU=
28+
github.com/ethereum/go-ethereum v1.14.12 h1:8hl57x77HSUo+cXExrURjU/w1VhL+ShCTJrTwcCQSe4=
29+
github.com/ethereum/go-ethereum v1.14.12/go.mod h1:RAC2gVMWJ6FkxSPESfbshrcKpIokgQKsVKmAuqdekDY=
30+
github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9 h1:8NfxH2iXvJ60YRB8ChToFTUzl8awsc3cJ8CbLjGIl/A=
31+
github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9/go.mod h1:M3b90YRnzqKyyzBEWJGqj8Qff4IDeXnzFw0P9bFw3uk=
32+
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
1633
github.com/gtank/merlin v0.1.1 h1:eQ90iG7K9pOhtereWsmyRJ6RAwcP4tHTDBHXNg+u5is=
1734
github.com/gtank/merlin v0.1.1/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s=
35+
github.com/holiman/uint256 v1.3.1 h1:JfTzmih28bittyHM8z360dCjIA9dbPIBlcTI6lmctQs=
36+
github.com/holiman/uint256 v1.3.1/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
1837
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
1938
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
2039
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
@@ -24,6 +43,9 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
2443
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2544
github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643 h1:hLDRPB66XQT/8+wG9WsDpiCvZf1yKO7sz7scAjSlBa0=
2645
github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643/go.mod h1:43+3pMjjKimDBf5Kr4ZFNGbLql1zKkbImw+fZbw3geM=
46+
github.com/mmcloughlin/addchain v0.4.0 h1:SobOdjm2xLj1KkXN5/n0xTIWyZA2+s99UCY1iPfkHRY=
47+
github.com/mmcloughlin/addchain v0.4.0/go.mod h1:A86O+tHqZLMNO4w6ZZ4FlVQEadcoqkyU72HC5wJ4RlU=
48+
github.com/mmcloughlin/profile v0.1.1/go.mod h1:IhHD7q1ooxgwTgjxQYkACGA77oFTDdFVejUS1/tS/qU=
2749
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
2850
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
2951
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -34,12 +56,18 @@ github.com/sei-protocol/coinbase-kryptology v0.0.0-20241015231206-08f61b7965cd h
3456
github.com/sei-protocol/coinbase-kryptology v0.0.0-20241015231206-08f61b7965cd/go.mod h1:vAKKp7/qgfMtPXMseamOlZMqK7BytjfOm0rFKWph5c4=
3557
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
3658
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
59+
github.com/supranational/blst v0.3.13 h1:AYeSxdOMacwu7FBmpfloBz5pbFXDmJL33RuwnKtmTjk=
60+
github.com/supranational/blst v0.3.13/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
3761
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
3862
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
63+
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
64+
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
3965
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
4066
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
4167
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4268
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
4369
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
4470
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
4571
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
72+
rsc.io/tmplfunc v0.0.3 h1:53XFQh69AfOa8Tw0Jm7t+GV7KZhOi6jzsCzTtKbMvzU=
73+
rsc.io/tmplfunc v0.0.3/go.mod h1:AG3sTPzElb1Io3Yg4voV9AGZJuleGAwaVRxL9M49PhA=

pkg/encryption/aes.go

+8-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package encryption
33
import (
44
"crypto/aes"
55
"crypto/cipher"
6-
"crypto/ecdsa"
76
"crypto/rand"
87
"crypto/sha256"
98
"encoding/base64"
@@ -12,33 +11,25 @@ import (
1211
"io"
1312
"math/big"
1413

15-
"github.com/ethereum/go-ethereum/crypto/secp256k1"
16-
1714
"golang.org/x/crypto/hkdf"
1815
)
1916

20-
// GenerateKey generates a new ECDSA private key using the secp256k1 curve.
21-
func GenerateKey() (*ecdsa.PrivateKey, error) {
22-
return ecdsa.GenerateKey(secp256k1.S256(), rand.Reader)
23-
}
24-
25-
// GetAESKey derives a 32-byte AES key using the provided ECDSA private key and denomination string.
26-
// It employs HKDF with SHA-256, using the ECDSA private key bytes and a SHA-256 hash of the denom as salt.
27-
func GetAESKey(privKey ecdsa.PrivateKey, denom string) ([]byte, error) {
28-
if privKey.D == nil {
29-
return nil, fmt.Errorf("private key D is nil")
30-
}
17+
// GetAESKey derives a 32-byte AES key using the provided bytes and denomination string.
18+
// The bytes can be anything, but we strongly suggest using something that is private to the use, such as the ecdas Private Key or a signed message.
19+
// It employs HKDF with SHA-256, using the private key bytes and a SHA-256 hash of the denom as salt.
20+
func GetAESKey(privateBytes []byte, denom string) ([]byte, error) {
3121
if len(denom) == 0 {
3222
return nil, fmt.Errorf("denom is empty")
3323
}
34-
// Convert the ECDSA private key to bytes
35-
privKeyBytes := privKey.D.Bytes()
24+
if len(privateBytes) == 0 {
25+
return nil, fmt.Errorf("bytes is empty")
26+
}
3627

3728
// Use a SHA-256 hash of the denom string as the salt
3829
salt := sha256.Sum256([]byte(denom))
3930

4031
// Create an HKDF reader using SHA-256
41-
hkdf := hkdf.New(sha256.New, privKeyBytes, salt[:], []byte("aes key derivation"))
32+
hkdf := hkdf.New(sha256.New, privateBytes, salt[:], []byte("aes key derivation"))
4233

4334
// Allocate a 32-byte array for the AES key
4435
aesKey := make([]byte, 32)

pkg/encryption/aes_test.go

+18-23
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package encryption
22

33
import (
4-
"crypto/ecdsa"
54
"math/big"
65
"testing"
6+
"time"
77

88
"github.com/stretchr/testify/require"
99
)
@@ -16,57 +16,57 @@ const (
1616
func TestGetAESKey(t *testing.T) {
1717
tests := []struct {
1818
name string
19-
privateKey *ecdsa.PrivateKey
19+
privateKey []byte
2020
denom string
2121
expectEqual bool
22-
anotherKey *ecdsa.PrivateKey
22+
anotherKey []byte
2323
anotherDenom string
2424
}{
2525
{
2626
name: "Deterministic Key Generation",
27-
privateKey: generateTestKey(t),
27+
privateKey: generateTestKey(),
2828
denom: TestDenom,
2929
expectEqual: true,
3030
},
3131
{
3232
name: "Different Denom (Salt) Generates Different Key",
33-
privateKey: generateTestKey(t),
33+
privateKey: generateTestKey(),
3434
denom: TestDenom,
3535
anotherDenom: TestDenom + "1",
3636
expectEqual: false,
3737
},
3838
{
3939
name: "Different Denom (Salt) of same length Generates Different Key",
40-
privateKey: generateTestKey(t),
40+
privateKey: generateTestKey(),
4141
denom: TestDenom + "1",
4242
anotherDenom: TestDenom + "2",
4343
expectEqual: false,
4444
},
4545
{
4646
name: "Different PrivateKey Generates Different Key",
47-
privateKey: generateTestKey(t),
47+
privateKey: generateTestKey(),
4848
denom: TestDenom + "N",
49-
anotherKey: generateTestKey(t),
49+
anotherKey: generateTestKey(),
5050
expectEqual: false,
5151
},
5252
}
5353

5454
for _, tt := range tests {
5555
t.Run(tt.name, func(t *testing.T) {
56-
aesPK, err := GetAESKey(*tt.privateKey, tt.denom)
56+
aesPK, err := GetAESKey(tt.privateKey, tt.denom)
5757
require.Nil(t, err, "Should not have error here")
5858

5959
if tt.anotherKey != nil {
60-
aesPKDiff, err := GetAESKey(*tt.anotherKey, tt.denom)
60+
aesPKDiff, err := GetAESKey(tt.anotherKey, tt.denom)
6161
require.Nil(t, err)
6262
require.NotEqual(t, aesPK, aesPKDiff, "PK should be different for different private keys")
6363
} else if tt.anotherDenom != "" {
64-
aesPKDiff, err := GetAESKey(*tt.privateKey, tt.anotherDenom)
64+
aesPKDiff, err := GetAESKey(tt.privateKey, tt.anotherDenom)
6565
require.Nil(t, err)
6666
require.NotEqual(t, aesPK, aesPKDiff, "PK should be different for different salts")
6767
} else {
6868

69-
aesPKAgain, err := GetAESKey(*tt.privateKey, tt.denom)
69+
aesPKAgain, err := GetAESKey(tt.privateKey, tt.denom)
7070
require.Nil(t, err, "Should not have error here")
7171
if tt.expectEqual {
7272
require.Equal(t, aesPK, aesPKAgain, "PK should be deterministically generated")
@@ -80,15 +80,11 @@ func TestGetAESKey(t *testing.T) {
8080

8181
func TestGetAESKey_InvalidInput(t *testing.T) {
8282
// Nil private key
83-
_, err := GetAESKey(*new(ecdsa.PrivateKey), TestDenom)
83+
_, err := GetAESKey([]byte{}, TestDenom)
8484
require.Error(t, err, "Should return error for nil private key")
8585

86-
invalidPrivateKey := &ecdsa.PrivateKey{ /* Invalid key data */ }
87-
_, err = GetAESKey(*invalidPrivateKey, TestDenom)
88-
require.Error(t, err, "Should return error for invalid private key")
89-
90-
validPrivateKey := generateTestKey(t)
91-
_, err = GetAESKey(*validPrivateKey, "")
86+
validPrivateKey := generateTestKey()
87+
_, err = GetAESKey(validPrivateKey, "")
9288
require.Error(t, err, "Should not allow empty denom(salt)")
9389
}
9490

@@ -218,8 +214,7 @@ func TestDecryptAESGCM_InvalidCiphertext(t *testing.T) {
218214
}
219215

220216
// Helper function to generate a test private key
221-
func generateTestKey(t *testing.T) *ecdsa.PrivateKey {
222-
privateKey, err := GenerateKey()
223-
require.Nil(t, err, "Failed to generate private key")
224-
return privateKey
217+
func generateTestKey() []byte {
218+
randomString := time.Now()
219+
return []byte(randomString.String())
225220
}

pkg/encryption/elgamal/common.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package elgamal
22

33
import (
4-
"crypto/ecdsa"
54
"crypto/sha256"
65
"io"
76

@@ -13,11 +12,12 @@ import (
1312
const H_STRING = "gPt25pi0eDphSiXWu0BIeIvyVATCtwhslTqfqvNhW2c"
1413

1514
// KeyGen generates a new key pair for the Twisted ElGamal encryption scheme.
16-
func (teg TwistedElGamal) KeyGen(privateKey ecdsa.PrivateKey, denom string) (*KeyPair, error) {
15+
// The private key is derived from the provided privateBytes and denom string. Ensure that the privateBytes passed is not exposed.
16+
func (teg TwistedElGamal) KeyGen(privateBytes []byte, denom string) (*KeyPair, error) {
1717
// Fixed base point H
1818
H := teg.GetH()
1919

20-
s, err := teg.getPrivateKey(privateKey, denom)
20+
s, err := teg.getPrivateKeyFromBytes(privateBytes, denom)
2121
if err != nil {
2222
return nil, err
2323
}
@@ -47,17 +47,12 @@ func (teg TwistedElGamal) GetH() curves.Point {
4747
return teg.curve.Point.Hash(bytes)
4848
}
4949

50-
// getPrivateKey derives a private key for the ElGamal cryptosystem.
51-
// It takes an ECDSA private key and a denomination string to generate the scalar.
52-
func (teg TwistedElGamal) getPrivateKey(privateKey ecdsa.PrivateKey, denom string) (curves.Scalar, error) {
53-
// Convert the ECDSA private key to bytes
54-
privKeyBytes := privateKey.D.Bytes()
55-
50+
func (teg TwistedElGamal) getPrivateKeyFromBytes(privateBytes []byte, denom string) (curves.Scalar, error) {
5651
// Hash the denom to get a salt.
5752
salt := sha256.Sum256([]byte(denom))
5853

5954
// Create an HKDF reader using SHA-256
60-
hkdf := hkdf.New(sha256.New, privKeyBytes, salt[:], []byte("elgamal scalar derivation"))
55+
hkdf := hkdf.New(sha256.New, privateBytes, salt[:], []byte("elgamal scalar derivation"))
6156

6257
// Generate 64 bytes of randomness from HKDF output
6358
var scalarBytes [64]byte

0 commit comments

Comments
 (0)