From f379d04e2d4791806583a5baab16d56ea59c480d Mon Sep 17 00:00:00 2001 From: mj Date: Sat, 7 Dec 2024 22:41:39 -0500 Subject: [PATCH 1/5] no more ecdsa --- go.mod | 18 +++++++- go.sum | 28 +++++++++++++ pkg/encryption/aes.go | 25 ++++------- pkg/encryption/aes_test.go | 41 ++++++++----------- pkg/encryption/elgamal/common.go | 15 +++---- pkg/encryption/elgamal/encryption_test.go | 31 +++++++------- pkg/encryption/elgamal/types_test.go | 2 +- pkg/testing/utils.go | 11 +++-- .../ciphertext_ciphertext_equality_test.go | 30 +++++++------- .../ciphertext_commitment_equality_test.go | 10 ++--- pkg/zkproofs/ciphertext_validity_test.go | 10 ++--- pkg/zkproofs/pubkey_validity_test.go | 11 +++-- pkg/zkproofs/range_test.go | 23 ++++------- pkg/zkproofs/zero_balance_test.go | 23 ++++------- 14 files changed, 142 insertions(+), 136 deletions(-) diff --git a/go.mod b/go.mod index 6261f16..238a041 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,13 @@ module github.com/sei-protocol/sei-cryptography -go 1.21 +go 1.22 + +toolchain go1.23.3 require ( github.com/bwesterb/go-ristretto v1.2.3 github.com/coinbase/kryptology v1.8.0 - github.com/ethereum/go-ethereum v1.13.15 + github.com/ethereum/go-ethereum v1.14.12 github.com/gtank/merlin v0.1.1 github.com/stretchr/testify v1.9.0 golang.org/x/crypto v0.27.0 @@ -13,15 +15,27 @@ require ( require ( filippo.io/edwards25519 v1.1.0 // indirect + github.com/bits-and-blooms/bitset v1.13.0 // indirect github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect + github.com/consensys/bavard v0.1.13 // indirect + github.com/consensys/gnark-crypto v0.12.1 // indirect + github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c // indirect + github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect + github.com/ethereum/c-kzg-4844 v1.0.0 // indirect + github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9 // indirect + github.com/holiman/uint256 v1.3.1 // indirect github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643 // indirect + github.com/mmcloughlin/addchain v0.4.0 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/supranational/blst v0.3.13 // indirect + golang.org/x/sync v0.7.0 // indirect golang.org/x/sys v0.25.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + rsc.io/tmplfunc v0.0.3 // indirect ) replace github.com/coinbase/kryptology => github.com/sei-protocol/coinbase-kryptology v0.0.0-20241015231206-08f61b7965cd diff --git a/go.sum b/go.sum index b9a4c91..e5e58ff 100644 --- a/go.sum +++ b/go.sum @@ -2,19 +2,38 @@ filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmG filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= +github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJRUA0wFAVE= +github.com/bits-and-blooms/bitset v1.13.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8= github.com/btcsuite/btcd/btcec/v2 v2.3.4 h1:3EJjcN70HCu/mwqlUsGK8GcNVyLVxFDlWurTXGPFfiQ= github.com/btcsuite/btcd/btcec/v2 v2.3.4/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04= github.com/bwesterb/go-ristretto v1.2.3 h1:1w53tCkGhCQ5djbat3+MH0BAQ5Kfgbt56UZQ/JMzngw= github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= +github.com/consensys/bavard v0.1.13 h1:oLhMLOFGTLdlda/kma4VOJazblc7IM5y5QPd2A/YjhQ= +github.com/consensys/bavard v0.1.13/go.mod h1:9ItSMtA/dXMAiL7BG6bqW2m3NdSEObYWoH223nGHukI= +github.com/consensys/gnark-crypto v0.12.1 h1:lHH39WuuFgVHONRl3J0LRBtuYdQTumFSDtJF7HpyG8M= +github.com/consensys/gnark-crypto v0.12.1/go.mod h1:v2Gy7L/4ZRosZ7Ivs+9SfUDr0f5UlG+EM5t7MPHiLuY= +github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c h1:uQYC5Z1mdLRPrZhHjHxufI8+2UG/i25QG92j0Er9p6I= +github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c/go.mod h1:geZJZH3SzKCqnz5VT0q/DyIG/tvu/dZk+VIfXicupJs= +github.com/crate-crypto/go-kzg-4844 v1.0.0 h1:TsSgHwrkTKecKJ4kadtHi4b3xHW5dCFUDFnUp1TsawI= +github.com/crate-crypto/go-kzg-4844 v1.0.0/go.mod h1:1kMhvPgI0Ky3yIa+9lFySEBUBXkYxeOi8ZF1sYioxhc= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs= +github.com/ethereum/c-kzg-4844 v1.0.0 h1:0X1LBXxaEtYD9xsyj9B9ctQEZIpnvVDeoBx8aHEwTNA= +github.com/ethereum/c-kzg-4844 v1.0.0/go.mod h1:VewdlzQmpT5QSrVhbBuGoCdFJkpaJlO1aQputP83wc0= github.com/ethereum/go-ethereum v1.13.15 h1:U7sSGYGo4SPjP6iNIifNoyIAiNjrmQkz6EwQG+/EZWo= github.com/ethereum/go-ethereum v1.13.15/go.mod h1:TN8ZiHrdJwSe8Cb6x+p0hs5CxhJZPbqB7hHkaUXcmIU= +github.com/ethereum/go-ethereum v1.14.12 h1:8hl57x77HSUo+cXExrURjU/w1VhL+ShCTJrTwcCQSe4= +github.com/ethereum/go-ethereum v1.14.12/go.mod h1:RAC2gVMWJ6FkxSPESfbshrcKpIokgQKsVKmAuqdekDY= +github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9 h1:8NfxH2iXvJ60YRB8ChToFTUzl8awsc3cJ8CbLjGIl/A= +github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9/go.mod h1:M3b90YRnzqKyyzBEWJGqj8Qff4IDeXnzFw0P9bFw3uk= +github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= github.com/gtank/merlin v0.1.1 h1:eQ90iG7K9pOhtereWsmyRJ6RAwcP4tHTDBHXNg+u5is= github.com/gtank/merlin v0.1.1/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s= +github.com/holiman/uint256 v1.3.1 h1:JfTzmih28bittyHM8z360dCjIA9dbPIBlcTI6lmctQs= +github.com/holiman/uint256 v1.3.1/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= 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= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643 h1:hLDRPB66XQT/8+wG9WsDpiCvZf1yKO7sz7scAjSlBa0= github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643/go.mod h1:43+3pMjjKimDBf5Kr4ZFNGbLql1zKkbImw+fZbw3geM= +github.com/mmcloughlin/addchain v0.4.0 h1:SobOdjm2xLj1KkXN5/n0xTIWyZA2+s99UCY1iPfkHRY= +github.com/mmcloughlin/addchain v0.4.0/go.mod h1:A86O+tHqZLMNO4w6ZZ4FlVQEadcoqkyU72HC5wJ4RlU= +github.com/mmcloughlin/profile v0.1.1/go.mod h1:IhHD7q1ooxgwTgjxQYkACGA77oFTDdFVejUS1/tS/qU= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -34,8 +56,12 @@ github.com/sei-protocol/coinbase-kryptology v0.0.0-20241015231206-08f61b7965cd h github.com/sei-protocol/coinbase-kryptology v0.0.0-20241015231206-08f61b7965cd/go.mod h1:vAKKp7/qgfMtPXMseamOlZMqK7BytjfOm0rFKWph5c4= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/supranational/blst v0.3.13 h1:AYeSxdOMacwu7FBmpfloBz5pbFXDmJL33RuwnKtmTjk= +github.com/supranational/blst v0.3.13/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw= golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= @@ -43,3 +69,5 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +rsc.io/tmplfunc v0.0.3 h1:53XFQh69AfOa8Tw0Jm7t+GV7KZhOi6jzsCzTtKbMvzU= +rsc.io/tmplfunc v0.0.3/go.mod h1:AG3sTPzElb1Io3Yg4voV9AGZJuleGAwaVRxL9M49PhA= diff --git a/pkg/encryption/aes.go b/pkg/encryption/aes.go index c00fffc..0481f72 100644 --- a/pkg/encryption/aes.go +++ b/pkg/encryption/aes.go @@ -3,7 +3,6 @@ package encryption import ( "crypto/aes" "crypto/cipher" - "crypto/ecdsa" "crypto/rand" "crypto/sha256" "encoding/base64" @@ -12,33 +11,25 @@ import ( "io" "math/big" - "github.com/ethereum/go-ethereum/crypto/secp256k1" - "golang.org/x/crypto/hkdf" ) -// GenerateKey generates a new ECDSA private key using the secp256k1 curve. -func GenerateKey() (*ecdsa.PrivateKey, error) { - return ecdsa.GenerateKey(secp256k1.S256(), rand.Reader) -} - -// GetAESKey derives a 32-byte AES key using the provided ECDSA private key and denomination string. -// It employs HKDF with SHA-256, using the ECDSA private key bytes and a SHA-256 hash of the denom as salt. -func GetAESKey(privKey ecdsa.PrivateKey, denom string) ([]byte, error) { - if privKey.D == nil { - return nil, fmt.Errorf("private key D is nil") - } +// GetAESKey derives a 32-byte AES key using the provided bytes and denomination string. +// 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. +// It employs HKDF with SHA-256, using the private key bytes and a SHA-256 hash of the denom as salt. +func GetAESKey(privateBytes []byte, denom string) ([]byte, error) { if len(denom) == 0 { return nil, fmt.Errorf("denom is empty") } - // Convert the ECDSA private key to bytes - privKeyBytes := privKey.D.Bytes() + if len(privateBytes) == 0 { + return nil, fmt.Errorf("bytes is empty") + } // Use a SHA-256 hash of the denom string as the salt salt := sha256.Sum256([]byte(denom)) // Create an HKDF reader using SHA-256 - hkdf := hkdf.New(sha256.New, privKeyBytes, salt[:], []byte("aes key derivation")) + hkdf := hkdf.New(sha256.New, privateBytes, salt[:], []byte("aes key derivation")) // Allocate a 32-byte array for the AES key aesKey := make([]byte, 32) diff --git a/pkg/encryption/aes_test.go b/pkg/encryption/aes_test.go index 095d01a..a321c2b 100644 --- a/pkg/encryption/aes_test.go +++ b/pkg/encryption/aes_test.go @@ -1,9 +1,9 @@ package encryption import ( - "crypto/ecdsa" "math/big" "testing" + "time" "github.com/stretchr/testify/require" ) @@ -16,57 +16,57 @@ const ( func TestGetAESKey(t *testing.T) { tests := []struct { name string - privateKey *ecdsa.PrivateKey + privateKey []byte denom string expectEqual bool - anotherKey *ecdsa.PrivateKey + anotherKey []byte anotherDenom string }{ { name: "Deterministic Key Generation", - privateKey: generateTestKey(t), + privateKey: generateTestKey(), denom: TestDenom, expectEqual: true, }, { name: "Different Denom (Salt) Generates Different Key", - privateKey: generateTestKey(t), + privateKey: generateTestKey(), denom: TestDenom, anotherDenom: TestDenom + "1", expectEqual: false, }, { name: "Different Denom (Salt) of same length Generates Different Key", - privateKey: generateTestKey(t), + privateKey: generateTestKey(), denom: TestDenom + "1", anotherDenom: TestDenom + "2", expectEqual: false, }, { name: "Different PrivateKey Generates Different Key", - privateKey: generateTestKey(t), + privateKey: generateTestKey(), denom: TestDenom + "N", - anotherKey: generateTestKey(t), + anotherKey: generateTestKey(), expectEqual: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - aesPK, err := GetAESKey(*tt.privateKey, tt.denom) + aesPK, err := GetAESKey(tt.privateKey, tt.denom) require.Nil(t, err, "Should not have error here") if tt.anotherKey != nil { - aesPKDiff, err := GetAESKey(*tt.anotherKey, tt.denom) + aesPKDiff, err := GetAESKey(tt.anotherKey, tt.denom) require.Nil(t, err) require.NotEqual(t, aesPK, aesPKDiff, "PK should be different for different private keys") } else if tt.anotherDenom != "" { - aesPKDiff, err := GetAESKey(*tt.privateKey, tt.anotherDenom) + aesPKDiff, err := GetAESKey(tt.privateKey, tt.anotherDenom) require.Nil(t, err) require.NotEqual(t, aesPK, aesPKDiff, "PK should be different for different salts") } else { - aesPKAgain, err := GetAESKey(*tt.privateKey, tt.denom) + aesPKAgain, err := GetAESKey(tt.privateKey, tt.denom) require.Nil(t, err, "Should not have error here") if tt.expectEqual { require.Equal(t, aesPK, aesPKAgain, "PK should be deterministically generated") @@ -80,15 +80,11 @@ func TestGetAESKey(t *testing.T) { func TestGetAESKey_InvalidInput(t *testing.T) { // Nil private key - _, err := GetAESKey(*new(ecdsa.PrivateKey), TestDenom) + _, err := GetAESKey([]byte{}, TestDenom) require.Error(t, err, "Should return error for nil private key") - invalidPrivateKey := &ecdsa.PrivateKey{ /* Invalid key data */ } - _, err = GetAESKey(*invalidPrivateKey, TestDenom) - require.Error(t, err, "Should return error for invalid private key") - - validPrivateKey := generateTestKey(t) - _, err = GetAESKey(*validPrivateKey, "") + validPrivateKey := generateTestKey() + _, err = GetAESKey(validPrivateKey, "") require.Error(t, err, "Should not allow empty denom(salt)") } @@ -218,8 +214,7 @@ func TestDecryptAESGCM_InvalidCiphertext(t *testing.T) { } // Helper function to generate a test private key -func generateTestKey(t *testing.T) *ecdsa.PrivateKey { - privateKey, err := GenerateKey() - require.Nil(t, err, "Failed to generate private key") - return privateKey +func generateTestKey() []byte { + randomString := time.Now() + return []byte(randomString.String()) } diff --git a/pkg/encryption/elgamal/common.go b/pkg/encryption/elgamal/common.go index fe5e42a..e218d94 100644 --- a/pkg/encryption/elgamal/common.go +++ b/pkg/encryption/elgamal/common.go @@ -1,7 +1,6 @@ package elgamal import ( - "crypto/ecdsa" "crypto/sha256" "io" @@ -13,11 +12,12 @@ import ( const H_STRING = "gPt25pi0eDphSiXWu0BIeIvyVATCtwhslTqfqvNhW2c" // KeyGen generates a new key pair for the Twisted ElGamal encryption scheme. -func (teg TwistedElGamal) KeyGen(privateKey ecdsa.PrivateKey, denom string) (*KeyPair, error) { +// The private key is derived from the provided privateBytes and denom string. Ensure that the privateBytes passed is not exposed. +func (teg TwistedElGamal) KeyGen(privateBytes []byte, denom string) (*KeyPair, error) { // Fixed base point H H := teg.GetH() - s, err := teg.getPrivateKey(privateKey, denom) + s, err := teg.getPrivateKeyFromBytes(privateBytes, denom) if err != nil { return nil, err } @@ -47,17 +47,12 @@ func (teg TwistedElGamal) GetH() curves.Point { return teg.curve.Point.Hash(bytes) } -// getPrivateKey derives a private key for the ElGamal cryptosystem. -// It takes an ECDSA private key and a denomination string to generate the scalar. -func (teg TwistedElGamal) getPrivateKey(privateKey ecdsa.PrivateKey, denom string) (curves.Scalar, error) { - // Convert the ECDSA private key to bytes - privKeyBytes := privateKey.D.Bytes() - +func (teg TwistedElGamal) getPrivateKeyFromBytes(privateBytes []byte, denom string) (curves.Scalar, error) { // Hash the denom to get a salt. salt := sha256.Sum256([]byte(denom)) // Create an HKDF reader using SHA-256 - hkdf := hkdf.New(sha256.New, privKeyBytes, salt[:], []byte("elgamal scalar derivation")) + hkdf := hkdf.New(sha256.New, privateBytes, salt[:], []byte("elgamal scalar derivation")) // Generate 64 bytes of randomness from HKDF output var scalarBytes [64]byte diff --git a/pkg/encryption/elgamal/encryption_test.go b/pkg/encryption/elgamal/encryption_test.go index 5d4fb45..5b1ecf3 100644 --- a/pkg/encryption/elgamal/encryption_test.go +++ b/pkg/encryption/elgamal/encryption_test.go @@ -13,8 +13,7 @@ import ( const DefaultTestDenom = "factory/sei1239081236472sd/testToken" func TestKeyGeneration(t *testing.T) { - privateKey, err := testutils.GenerateKey() - require.Nil(t, err) + privateKey := testutils.GenerateKey() eg := NewTwistedElgamal() keyPair, err := eg.KeyGen(*privateKey, DefaultTestDenom) @@ -38,16 +37,15 @@ func TestKeyGeneration(t *testing.T) { require.NotEqual(t, keyPair, keyPairDiffSalt, "PK should be different for different salt") // Test that different privateKey should generate different PK - altPrivateKey, err := testutils.GenerateKey() - require.Nil(t, err) + altPrivateKey := testutils.GenerateKey() keyPairDiffPK, err := eg.KeyGen(*altPrivateKey, altDenom) require.Nil(t, err) require.NotEqual(t, keyPair, keyPairDiffPK, "PK should be different for different ESDCA Private Key") } func TestEncryptionDecryption(t *testing.T) { - privateKey, _ := testutils.GenerateKey() - altPrivateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() + altPrivateKey := testutils.GenerateKey() eg := NewTwistedElgamal() @@ -83,8 +81,7 @@ func TestEncryptionDecryption(t *testing.T) { // Due to the size of 48 bit numbers, this test takes a really long time (~1hr) to run. func Test48BitEncryptionDecryption(t *testing.T) { - privateKey, err := testutils.GenerateKey() - require.Nil(t, err) + privateKey := testutils.GenerateKey() eg := NewTwistedElgamal() keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom) @@ -124,8 +121,8 @@ func Test48BitEncryptionDecryption(t *testing.T) { } func TestAddCiphertext(t *testing.T) { - privateKey, _ := testutils.GenerateKey() - altPrivateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() + altPrivateKey := testutils.GenerateKey() eg := NewTwistedElgamal() @@ -172,7 +169,7 @@ func TestAddCiphertext(t *testing.T) { func TestTwistedElGamal_InvalidCiphertext(t *testing.T) { eg := NewTwistedElgamal() - privateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom) invalidCt := &Ciphertext{} @@ -187,7 +184,7 @@ func TestTwistedElGamal_NilPrivateKey(t *testing.T) { eg := NewTwistedElgamal() // Generate a valid key pair for comparison - privateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom) // Encrypt a value with a valid public key @@ -206,7 +203,7 @@ func TestTwistedElGamal_EncryptDecryptWithRand(t *testing.T) { eg := NewTwistedElgamal() // Generate a valid key pair for comparison - privateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom) message := big.NewInt(555555555) @@ -224,7 +221,7 @@ func TestTwistedElGamal_EncryptMessageTwice(t *testing.T) { eg := NewTwistedElgamal() // Generate a valid key pair for comparison - privateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom) message := big.NewInt(555555555) @@ -240,7 +237,7 @@ func TestTwistedElGamal_DecryptWithZeroBits(t *testing.T) { eg := NewTwistedElgamal() // Generate a valid key pair for comparison - privateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom) message := big.NewInt(555555555) @@ -266,7 +263,7 @@ func TestTwistedElGamal_EncryptInvalidRandomFactor(t *testing.T) { eg := NewTwistedElgamal() // Generate a valid key pair for comparison - privateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom) // Test with nil public key @@ -279,7 +276,7 @@ func TestTwistedElGamal_EncryptBoundaryValues(t *testing.T) { eg := NewTwistedElgamal() // Generate a valid key pair for comparison - privateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom) // Test with the smallest possible value (0) diff --git a/pkg/encryption/elgamal/types_test.go b/pkg/encryption/elgamal/types_test.go index 9c890c6..2eb5bc7 100644 --- a/pkg/encryption/elgamal/types_test.go +++ b/pkg/encryption/elgamal/types_test.go @@ -10,7 +10,7 @@ import ( ) func TestCiphertext_MarshalJSON(t *testing.T) { - privateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() eg := NewTwistedElgamal() keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom) diff --git a/pkg/testing/utils.go b/pkg/testing/utils.go index 6e9f8d0..4eb7fef 100644 --- a/pkg/testing/utils.go +++ b/pkg/testing/utils.go @@ -1,12 +1,11 @@ package testing import ( - "crypto/ecdsa" - "crypto/rand" - "github.com/ethereum/go-ethereum/crypto/secp256k1" + "time" ) -// GenerateKey generates a new ECDSA key pair. -func GenerateKey() (*ecdsa.PrivateKey, error) { - return ecdsa.GenerateKey(secp256k1.S256(), rand.Reader) +// GenerateKey generates a new private bytes object used to dervie the keypair. +func GenerateKey() *[]byte { + result := []byte(time.Now().String()) + return &result } diff --git a/pkg/zkproofs/ciphertext_ciphertext_equality_test.go b/pkg/zkproofs/ciphertext_ciphertext_equality_test.go index 70295a5..140a19c 100644 --- a/pkg/zkproofs/ciphertext_ciphertext_equality_test.go +++ b/pkg/zkproofs/ciphertext_ciphertext_equality_test.go @@ -55,15 +55,15 @@ func TestCiphertextCiphertextEqualityProof(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // Key generation - sourcePrivateKey, _ := testutils.GenerateKey() - destPrivateKey, _ := testutils.GenerateKey() + sourcePrivateKey := testutils.GenerateKey() + destPrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) destinationKeypair, _ := eg.KeyGen(*destPrivateKey, TestDenom) var actualDestinationPubkey *curves.Point if tt.useDifferentPublicKey { - altDestPrivateKey, _ := testutils.GenerateKey() + altDestPrivateKey := testutils.GenerateKey() // Generate an alternative keypair for destination altDestinationKeypair, _ := eg.KeyGen(*altDestPrivateKey, TestDenom) actualDestinationPubkey = &altDestinationKeypair.PublicKey @@ -112,8 +112,8 @@ func TestCiphertextCiphertextEqualityProof(t *testing.T) { func TestCiphertextCiphertextEqualityProof_EdgeCases(t *testing.T) { t.Run("Zero Amounts", func(t *testing.T) { // Key generation - sourcePrivateKey, _ := testutils.GenerateKey() - destPrivateKey, _ := testutils.GenerateKey() + sourcePrivateKey := testutils.GenerateKey() + destPrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) destinationKeypair, _ := eg.KeyGen(*destPrivateKey, TestDenom) @@ -152,8 +152,8 @@ func TestCiphertextCiphertextEqualityProof_EdgeCases(t *testing.T) { t.Run("Maximum Amount", func(t *testing.T) { // Key generation - sourcePrivateKey, _ := testutils.GenerateKey() - destPrivateKey, _ := testutils.GenerateKey() + sourcePrivateKey := testutils.GenerateKey() + destPrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) @@ -194,8 +194,8 @@ func TestCiphertextCiphertextEqualityProof_EdgeCases(t *testing.T) { } func TestCiphertextCiphertextEqualityProof_UnmarshalJSON_Valid(t *testing.T) { - sourcePrivateKey, _ := testutils.GenerateKey() - destPrivateKey, _ := testutils.GenerateKey() + sourcePrivateKey := testutils.GenerateKey() + destPrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) destinationKeypair, _ := eg.KeyGen(*destPrivateKey, TestDenom) @@ -241,8 +241,8 @@ func TestCiphertextCiphertextEqualityProof_UnmarshalJSON_Valid(t *testing.T) { // Invalid input tests for NewCiphertextCiphertextEqualityProof func TestNewCiphertextCiphertextEqualityProof_InvalidInputs(t *testing.T) { - sourcePrivateKey, _ := testutils.GenerateKey() - destPrivateKey, _ := testutils.GenerateKey() + sourcePrivateKey := testutils.GenerateKey() + destPrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) destinationKeypair, _ := eg.KeyGen(*destPrivateKey, TestDenom) @@ -323,8 +323,8 @@ func TestNewCiphertextCiphertextEqualityProof_InvalidInputs(t *testing.T) { // Invalid input tests for VerifyCiphertextCiphertextEquality func TestVerifyCiphertextCiphertextEquality_InvalidInputs(t *testing.T) { - sourcePrivateKey, _ := testutils.GenerateKey() - destPrivateKey, _ := testutils.GenerateKey() + sourcePrivateKey := testutils.GenerateKey() + destPrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) destinationKeypair, _ := eg.KeyGen(*destPrivateKey, TestDenom) @@ -456,8 +456,8 @@ func TestVerifyCiphertextCiphertextEquality_InvalidInputs(t *testing.T) { // Test that the proof is still valid for cases where Ciphertext.D is the identity point. func TestCiphertextCiphertextEqualityProof_IdentityD(t *testing.T) { // Key generation - sourcePrivateKey, _ := testutils.GenerateKey() - destPrivateKey, _ := testutils.GenerateKey() + sourcePrivateKey := testutils.GenerateKey() + destPrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) destinationKeypair, _ := eg.KeyGen(*destPrivateKey, TestDenom) diff --git a/pkg/zkproofs/ciphertext_commitment_equality_test.go b/pkg/zkproofs/ciphertext_commitment_equality_test.go index e03ab0d..185535e 100644 --- a/pkg/zkproofs/ciphertext_commitment_equality_test.go +++ b/pkg/zkproofs/ciphertext_commitment_equality_test.go @@ -54,7 +54,7 @@ func TestCiphertextCommitmentEqualityProof(t *testing.T) { tt := tt // Capture range variable t.Run(tt.name, func(t *testing.T) { // Key generation - sourcePrivateKey, _ := testutils.GenerateKey() + sourcePrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) @@ -121,7 +121,7 @@ func TestCiphertextCommitmentEqualityProof(t *testing.T) { } func TestCiphertextCommitmentEqualityProof_MarshalUnmarshalJSON(t *testing.T) { - sourcePrivateKey, _ := testutils.GenerateKey() + sourcePrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) @@ -161,7 +161,7 @@ func TestCiphertextCommitmentEqualityProof_MarshalUnmarshalJSON(t *testing.T) { } func TestNewCiphertextCommitmentEqualityProof_InvalidInput(t *testing.T) { - sourcePrivateKey, _ := testutils.GenerateKey() + sourcePrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) @@ -221,7 +221,7 @@ func TestNewCiphertextCommitmentEqualityProof_InvalidInput(t *testing.T) { } func TestVerifyCiphertextCommitmentEquality_InvalidInput(t *testing.T) { - sourcePrivateKey, _ := testutils.GenerateKey() + sourcePrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) @@ -331,7 +331,7 @@ func TestVerifyCiphertextCommitmentEquality_InvalidInput(t *testing.T) { // Test that the proof is still valid for cases where Ciphertext.D is the identity point. func TestCiphertextCommitmentEqualityProof_IdentityD(t *testing.T) { // Key generation - sourcePrivateKey, _ := testutils.GenerateKey() + sourcePrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) diff --git a/pkg/zkproofs/ciphertext_validity_test.go b/pkg/zkproofs/ciphertext_validity_test.go index cb2a0ec..1cc9a21 100644 --- a/pkg/zkproofs/ciphertext_validity_test.go +++ b/pkg/zkproofs/ciphertext_validity_test.go @@ -12,8 +12,8 @@ import ( ) func TestValidityProof(t *testing.T) { - privateKey, _ := testutils.GenerateKey() - altPrivateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() + altPrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keys, _ := eg.KeyGen(*privateKey, TestDenom) @@ -63,7 +63,7 @@ func TestValidityProof(t *testing.T) { } func TestCiphertextValidityProof_MarshalUnmarshalJSON(t *testing.T) { - privateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keys, _ := eg.KeyGen(*privateKey, TestDenom) @@ -89,7 +89,7 @@ func TestCiphertextValidityProof_MarshalUnmarshalJSON(t *testing.T) { } func TestNewCiphertextValidityProof_InvalidInput(t *testing.T) { - privateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keys, _ := eg.KeyGen(*privateKey, TestDenom) @@ -135,7 +135,7 @@ func TestNewCiphertextValidityProof_InvalidInput(t *testing.T) { } func TestVerifyCiphertextValidityProof_Invalid_Input(t *testing.T) { - privateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keys, _ := eg.KeyGen(*privateKey, TestDenom) diff --git a/pkg/zkproofs/pubkey_validity_test.go b/pkg/zkproofs/pubkey_validity_test.go index 2d8247b..83cca09 100644 --- a/pkg/zkproofs/pubkey_validity_test.go +++ b/pkg/zkproofs/pubkey_validity_test.go @@ -11,8 +11,8 @@ import ( ) func TestPubKeyValidityProof(t *testing.T) { - privateKey, _ := testutils.GenerateKey() - altPrivateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() + altPrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keys, _ := eg.KeyGen(*privateKey, TestDenom) @@ -37,7 +37,7 @@ func TestPubKeyValidityProof(t *testing.T) { } func TestPubKeyValidityProof_MarshalUnmarshalJSON(t *testing.T) { - privateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keys, _ := eg.KeyGen(*privateKey, TestDenom) @@ -58,7 +58,7 @@ func TestPubKeyValidityProof_MarshalUnmarshalJSON(t *testing.T) { } func TestNewPubKeyValidityProof_InvalidInput(t *testing.T) { - privateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keys, _ := eg.KeyGen(*privateKey, TestDenom) @@ -70,8 +70,7 @@ func TestNewPubKeyValidityProof_InvalidInput(t *testing.T) { } func TestVerifyPubKeyValidityProof_InvalidInput(t *testing.T) { - privateKey, err := testutils.GenerateKey() - require.Nil(t, err) + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keys, err := eg.KeyGen(*privateKey, TestDenom) diff --git a/pkg/zkproofs/range_test.go b/pkg/zkproofs/range_test.go index 3b2bef3..7b6d49d 100644 --- a/pkg/zkproofs/range_test.go +++ b/pkg/zkproofs/range_test.go @@ -31,8 +31,7 @@ func TestValueIsInRange(t *testing.T) { v, _ := curve.Scalar.SetBigInt(value) n := 64 // the range is [0, 2^64] - privateKey, err := testutils.GenerateKey() - require.Nil(t, err, "Error generating private key") + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keyPair, err := eg.KeyGen(*privateKey, TestDenom) @@ -73,8 +72,7 @@ func TestRangeAttacksAreInfeasible(t *testing.T) { v, _ := curve.Scalar.SetBigInt(value) n := 64 // the range is [0, 2^64] - privateKey, err := testutils.GenerateKey() - require.Nil(t, err, "Error generating private key") + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keyPair, err := eg.KeyGen(*privateKey, TestDenom) @@ -162,8 +160,7 @@ func TestRangeProofs(t *testing.T) { value := big.NewInt(100) n := 64 // the range is [0, 2^64] - privateKey, err := testutils.GenerateKey() - require.Nil(t, err, "Error generating private key") + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keyPair, err := eg.KeyGen(*privateKey, TestDenom) @@ -193,8 +190,7 @@ func TestRangeProofsLargeN(t *testing.T) { value := big.NewInt(100) n := 128 // the range is [0, 2^128] - privateKey, err := testutils.GenerateKey() - require.Nil(t, err, "Error generating private key") + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keyPair, err := eg.KeyGen(*privateKey, TestDenom) @@ -227,8 +223,7 @@ func TestRangeProofsWithMarshaling(t *testing.T) { value := big.NewInt(100) n := 64 // the range is [0, 2^64] - privateKey, err := testutils.GenerateKey() - require.Nil(t, err, "Error generating private key") + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keyPair, err := eg.KeyGen(*privateKey, TestDenom) @@ -257,8 +252,7 @@ func TestRangeProofsWithMarshaling(t *testing.T) { } func TestRangeProofs_InvalidInput(t *testing.T) { - privateKey, err := testutils.GenerateKey() - require.Nil(t, err, "Error generating private key") + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keyPair, err := eg.KeyGen(*privateKey, TestDenom) @@ -284,7 +278,7 @@ func TestRangeProofs_InvalidInput(t *testing.T) { } func TestVerifyRangeProof_InvalidInput(t *testing.T) { - privateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() value := big.NewInt(10) keyPair, _ := eg.KeyGen(*privateKey, TestDenom) @@ -329,8 +323,7 @@ func TestRangeProofVerifierReuse(t *testing.T) { value := big.NewInt(10) n := 128 // the range is [0, 2^128] - privateKey, err := testutils.GenerateKey() - require.Nil(t, err, "Error generating private key") + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keyPair, err := eg.KeyGen(*privateKey, TestDenom) diff --git a/pkg/zkproofs/zero_balance_test.go b/pkg/zkproofs/zero_balance_test.go index 4398002..fe4199b 100644 --- a/pkg/zkproofs/zero_balance_test.go +++ b/pkg/zkproofs/zero_balance_test.go @@ -42,8 +42,8 @@ func TestZeroBalanceProof(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // Setup keypair - privateKey, _ := testutils.GenerateKey() - altPrivateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() + altPrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keypair, _ := eg.KeyGen(*privateKey, TestDenom) @@ -75,7 +75,7 @@ func TestZeroBalanceProof(t *testing.T) { } func TestZeroBalanceProof_MarshalUnmarshalJSON(t *testing.T) { - privateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keypair, _ := eg.KeyGen(*privateKey, TestDenom) @@ -100,8 +100,7 @@ func TestZeroBalanceProof_MarshalUnmarshalJSON(t *testing.T) { } func TestZeroBalanceProof_InvalidRandomness(t *testing.T) { - privateKey, err := testutils.GenerateKey() - require.NoError(t, err, "Failed to generate private key") + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keypair, err := eg.KeyGen(*privateKey, TestDenom) @@ -127,8 +126,7 @@ func TestZeroBalanceProof_InvalidRandomness(t *testing.T) { } func TestZeroBalanceProof_ExtremelyLargeScalars(t *testing.T) { - privateKey, err := testutils.GenerateKey() - require.NoError(t, err, "Failed to generate private key") + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keypair, err := eg.KeyGen(*privateKey, TestDenom) @@ -157,8 +155,7 @@ func TestZeroBalanceProof_ExtremelyLargeScalars(t *testing.T) { } func TestZeroBalanceProof_TamperedProof(t *testing.T) { - privateKey, err := testutils.GenerateKey() - require.NoError(t, err, "Failed to generate private key") + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keypair, err := eg.KeyGen(*privateKey, TestDenom) @@ -203,8 +200,7 @@ func TestZeroBalanceProof_InvalidInput(t *testing.T) { require.Error(t, err, "Should return an error when ciphertext is nil") require.Contains(t, err.Error(), "keypair is invalid") - privateKey, err := testutils.GenerateKey() - require.NoError(t, err, "Failed to generate private key") + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keypair, _ := eg.KeyGen(*privateKey, TestDenom) @@ -231,8 +227,7 @@ func TestZeroBalanceProof_InvalidInput(t *testing.T) { } func TestVerifyZeroProof_InvalidInput(t *testing.T) { - privateKey, err := testutils.GenerateKey() - require.NoError(t, err, "Failed to generate private key") + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keypair, err := eg.KeyGen(*privateKey, TestDenom) @@ -298,7 +293,7 @@ func TestVerifyZeroProof_InvalidInput(t *testing.T) { func TestZeroBalanceProof_IdentityD(t *testing.T) { // Setup keypair - privateKey, _ := testutils.GenerateKey() + privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() keypair, _ := eg.KeyGen(*privateKey, TestDenom) From ed5f5110017a119a65ec90212455ba3ad856fb3d Mon Sep 17 00:00:00 2001 From: mj Date: Sat, 7 Dec 2024 23:17:55 -0500 Subject: [PATCH 2/5] remove denom also --- pkg/encryption/aes.go | 11 ++--- pkg/encryption/aes_test.go | 32 ++------------ pkg/encryption/elgamal/common.go | 8 ++-- pkg/encryption/elgamal/encryption_test.go | 42 +++++++------------ pkg/encryption/elgamal/types_test.go | 2 +- .../ciphertext_ciphertext_equality_test.go | 30 ++++++------- .../ciphertext_commitment_equality_test.go | 10 ++--- pkg/zkproofs/ciphertext_validity_test.go | 10 ++--- pkg/zkproofs/pubkey_validity_test.go | 10 ++--- pkg/zkproofs/range_test.go | 16 +++---- pkg/zkproofs/zero_balance_test.go | 18 ++++---- 11 files changed, 75 insertions(+), 114 deletions(-) diff --git a/pkg/encryption/aes.go b/pkg/encryption/aes.go index 0481f72..3dd7a83 100644 --- a/pkg/encryption/aes.go +++ b/pkg/encryption/aes.go @@ -14,19 +14,16 @@ import ( "golang.org/x/crypto/hkdf" ) -// GetAESKey derives a 32-byte AES key using the provided bytes and denomination string. +// GetAESKey derives a 32-byte AES key using the provided bytes. // 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. -// It employs HKDF with SHA-256, using the private key bytes and a SHA-256 hash of the denom as salt. -func GetAESKey(privateBytes []byte, denom string) ([]byte, error) { - if len(denom) == 0 { - return nil, fmt.Errorf("denom is empty") - } +// It employs HKDF with SHA-256, using the private key bytes. +func GetAESKey(privateBytes []byte) ([]byte, error) { if len(privateBytes) == 0 { return nil, fmt.Errorf("bytes is empty") } // Use a SHA-256 hash of the denom string as the salt - salt := sha256.Sum256([]byte(denom)) + salt := sha256.Sum256([]byte("aes key derivation salt")) // Create an HKDF reader using SHA-256 hkdf := hkdf.New(sha256.New, privateBytes, salt[:], []byte("aes key derivation")) diff --git a/pkg/encryption/aes_test.go b/pkg/encryption/aes_test.go index a321c2b..0a7f493 100644 --- a/pkg/encryption/aes_test.go +++ b/pkg/encryption/aes_test.go @@ -25,27 +25,11 @@ func TestGetAESKey(t *testing.T) { { name: "Deterministic Key Generation", privateKey: generateTestKey(), - denom: TestDenom, expectEqual: true, }, - { - name: "Different Denom (Salt) Generates Different Key", - privateKey: generateTestKey(), - denom: TestDenom, - anotherDenom: TestDenom + "1", - expectEqual: false, - }, - { - name: "Different Denom (Salt) of same length Generates Different Key", - privateKey: generateTestKey(), - denom: TestDenom + "1", - anotherDenom: TestDenom + "2", - expectEqual: false, - }, { name: "Different PrivateKey Generates Different Key", privateKey: generateTestKey(), - denom: TestDenom + "N", anotherKey: generateTestKey(), expectEqual: false, }, @@ -53,20 +37,16 @@ func TestGetAESKey(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - aesPK, err := GetAESKey(tt.privateKey, tt.denom) + aesPK, err := GetAESKey(tt.privateKey) require.Nil(t, err, "Should not have error here") if tt.anotherKey != nil { - aesPKDiff, err := GetAESKey(tt.anotherKey, tt.denom) + aesPKDiff, err := GetAESKey(tt.anotherKey) require.Nil(t, err) require.NotEqual(t, aesPK, aesPKDiff, "PK should be different for different private keys") - } else if tt.anotherDenom != "" { - aesPKDiff, err := GetAESKey(tt.privateKey, tt.anotherDenom) - require.Nil(t, err) - require.NotEqual(t, aesPK, aesPKDiff, "PK should be different for different salts") } else { - aesPKAgain, err := GetAESKey(tt.privateKey, tt.denom) + aesPKAgain, err := GetAESKey(tt.privateKey) require.Nil(t, err, "Should not have error here") if tt.expectEqual { require.Equal(t, aesPK, aesPKAgain, "PK should be deterministically generated") @@ -80,12 +60,8 @@ func TestGetAESKey(t *testing.T) { func TestGetAESKey_InvalidInput(t *testing.T) { // Nil private key - _, err := GetAESKey([]byte{}, TestDenom) + _, err := GetAESKey([]byte{}) require.Error(t, err, "Should return error for nil private key") - - validPrivateKey := generateTestKey() - _, err = GetAESKey(validPrivateKey, "") - require.Error(t, err, "Should not allow empty denom(salt)") } func TestAESEncryptionDecryption(t *testing.T) { diff --git a/pkg/encryption/elgamal/common.go b/pkg/encryption/elgamal/common.go index e218d94..fe98ebd 100644 --- a/pkg/encryption/elgamal/common.go +++ b/pkg/encryption/elgamal/common.go @@ -13,11 +13,11 @@ const H_STRING = "gPt25pi0eDphSiXWu0BIeIvyVATCtwhslTqfqvNhW2c" // KeyGen generates a new key pair for the Twisted ElGamal encryption scheme. // The private key is derived from the provided privateBytes and denom string. Ensure that the privateBytes passed is not exposed. -func (teg TwistedElGamal) KeyGen(privateBytes []byte, denom string) (*KeyPair, error) { +func (teg TwistedElGamal) KeyGen(privateBytes []byte) (*KeyPair, error) { // Fixed base point H H := teg.GetH() - s, err := teg.getPrivateKeyFromBytes(privateBytes, denom) + s, err := teg.getPrivateKeyFromBytes(privateBytes) if err != nil { return nil, err } @@ -47,9 +47,9 @@ func (teg TwistedElGamal) GetH() curves.Point { return teg.curve.Point.Hash(bytes) } -func (teg TwistedElGamal) getPrivateKeyFromBytes(privateBytes []byte, denom string) (curves.Scalar, error) { +func (teg TwistedElGamal) getPrivateKeyFromBytes(privateBytes []byte) (curves.Scalar, error) { // Hash the denom to get a salt. - salt := sha256.Sum256([]byte(denom)) + salt := sha256.Sum256([]byte("elgamal scalar derivation salt")) // Create an HKDF reader using SHA-256 hkdf := hkdf.New(sha256.New, privateBytes, salt[:], []byte("elgamal scalar derivation")) diff --git a/pkg/encryption/elgamal/encryption_test.go b/pkg/encryption/elgamal/encryption_test.go index 5b1ecf3..e22710e 100644 --- a/pkg/encryption/elgamal/encryption_test.go +++ b/pkg/encryption/elgamal/encryption_test.go @@ -16,29 +16,17 @@ func TestKeyGeneration(t *testing.T) { privateKey := testutils.GenerateKey() eg := NewTwistedElgamal() - keyPair, err := eg.KeyGen(*privateKey, DefaultTestDenom) + keyPair, err := eg.KeyGen(*privateKey) require.Nil(t, err) // Test that keyPair is deterministically generated - keyPairAgain, err := eg.KeyGen(*privateKey, DefaultTestDenom) + keyPairAgain, err := eg.KeyGen(*privateKey) require.Nil(t, err) require.Equal(t, keyPair, keyPairAgain, "PK should be deterministically generated") - // Test that changing the salt should generate a different key - altDenom := "factory/sei1239081236470/testToken1" - keyPairDiffSalt, err := eg.KeyGen(*privateKey, altDenom) - require.Nil(t, err) - require.NotEqual(t, keyPair, keyPairDiffSalt, "PK should be different for different salt") - - // Test same thing for salt of same length - altDenom = "factory/sei1239081236470/testTokeN" - keyPairDiffSalt, err = eg.KeyGen(*privateKey, altDenom) - require.Nil(t, err) - require.NotEqual(t, keyPair, keyPairDiffSalt, "PK should be different for different salt") - // Test that different privateKey should generate different PK altPrivateKey := testutils.GenerateKey() - keyPairDiffPK, err := eg.KeyGen(*altPrivateKey, altDenom) + keyPairDiffPK, err := eg.KeyGen(*altPrivateKey) require.Nil(t, err) require.NotEqual(t, keyPair, keyPairDiffPK, "PK should be different for different ESDCA Private Key") } @@ -49,8 +37,8 @@ func TestEncryptionDecryption(t *testing.T) { eg := NewTwistedElgamal() - keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom) - altKeys, _ := eg.KeyGen(*altPrivateKey, DefaultTestDenom) + keys, _ := eg.KeyGen(*privateKey) + altKeys, _ := eg.KeyGen(*altPrivateKey) // Happy Path value := big.NewInt(108) @@ -84,7 +72,7 @@ func Test48BitEncryptionDecryption(t *testing.T) { privateKey := testutils.GenerateKey() eg := NewTwistedElgamal() - keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom) + keys, _ := eg.KeyGen(*privateKey) // First decrypt a 32 bit number (sets up the decryptor for a later test) value := big.NewInt(108092) @@ -126,8 +114,8 @@ func TestAddCiphertext(t *testing.T) { eg := NewTwistedElgamal() - keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom) - altKeys, _ := eg.KeyGen(*altPrivateKey, DefaultTestDenom) + keys, _ := eg.KeyGen(*privateKey) + altKeys, _ := eg.KeyGen(*altPrivateKey) // Happy Path value1 := big.NewInt(30842) @@ -170,7 +158,7 @@ func TestAddCiphertext(t *testing.T) { func TestTwistedElGamal_InvalidCiphertext(t *testing.T) { eg := NewTwistedElgamal() privateKey := testutils.GenerateKey() - keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom) + keys, _ := eg.KeyGen(*privateKey) invalidCt := &Ciphertext{} @@ -185,7 +173,7 @@ func TestTwistedElGamal_NilPrivateKey(t *testing.T) { // Generate a valid key pair for comparison privateKey := testutils.GenerateKey() - keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom) + keys, _ := eg.KeyGen(*privateKey) // Encrypt a value with a valid public key value := big.NewInt(12345) @@ -204,7 +192,7 @@ func TestTwistedElGamal_EncryptDecryptWithRand(t *testing.T) { // Generate a valid key pair for comparison privateKey := testutils.GenerateKey() - keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom) + keys, _ := eg.KeyGen(*privateKey) message := big.NewInt(555555555) randomFactor := curves.ED25519().Scalar.Random(rand.Reader) @@ -222,7 +210,7 @@ func TestTwistedElGamal_EncryptMessageTwice(t *testing.T) { // Generate a valid key pair for comparison privateKey := testutils.GenerateKey() - keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom) + keys, _ := eg.KeyGen(*privateKey) message := big.NewInt(555555555) randomFactor := curve.Scalar.Random(rand.Reader) @@ -238,7 +226,7 @@ func TestTwistedElGamal_DecryptWithZeroBits(t *testing.T) { // Generate a valid key pair for comparison privateKey := testutils.GenerateKey() - keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom) + keys, _ := eg.KeyGen(*privateKey) message := big.NewInt(555555555) randomFactor := curve.Scalar.Random(rand.Reader) @@ -264,7 +252,7 @@ func TestTwistedElGamal_EncryptInvalidRandomFactor(t *testing.T) { // Generate a valid key pair for comparison privateKey := testutils.GenerateKey() - keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom) + keys, _ := eg.KeyGen(*privateKey) // Test with nil public key _, _, err := eg.encryptWithRand(keys.PublicKey, big.NewInt(12345), nil) @@ -277,7 +265,7 @@ func TestTwistedElGamal_EncryptBoundaryValues(t *testing.T) { // Generate a valid key pair for comparison privateKey := testutils.GenerateKey() - keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom) + keys, _ := eg.KeyGen(*privateKey) // Test with the smallest possible value (0) _, _, err := eg.Encrypt(keys.PublicKey, big.NewInt(0)) diff --git a/pkg/encryption/elgamal/types_test.go b/pkg/encryption/elgamal/types_test.go index 2eb5bc7..428ca56 100644 --- a/pkg/encryption/elgamal/types_test.go +++ b/pkg/encryption/elgamal/types_test.go @@ -13,7 +13,7 @@ func TestCiphertext_MarshalJSON(t *testing.T) { privateKey := testutils.GenerateKey() eg := NewTwistedElgamal() - keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom) + keys, _ := eg.KeyGen(*privateKey) value := big.NewInt(108) ciphertext, _, _ := eg.Encrypt(keys.PublicKey, value) diff --git a/pkg/zkproofs/ciphertext_ciphertext_equality_test.go b/pkg/zkproofs/ciphertext_ciphertext_equality_test.go index 140a19c..3a7a20b 100644 --- a/pkg/zkproofs/ciphertext_ciphertext_equality_test.go +++ b/pkg/zkproofs/ciphertext_ciphertext_equality_test.go @@ -58,14 +58,14 @@ func TestCiphertextCiphertextEqualityProof(t *testing.T) { sourcePrivateKey := testutils.GenerateKey() destPrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) - destinationKeypair, _ := eg.KeyGen(*destPrivateKey, TestDenom) + sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey) + destinationKeypair, _ := eg.KeyGen(*destPrivateKey) var actualDestinationPubkey *curves.Point if tt.useDifferentPublicKey { altDestPrivateKey := testutils.GenerateKey() // Generate an alternative keypair for destination - altDestinationKeypair, _ := eg.KeyGen(*altDestPrivateKey, TestDenom) + altDestinationKeypair, _ := eg.KeyGen(*altDestPrivateKey) actualDestinationPubkey = &altDestinationKeypair.PublicKey } else { actualDestinationPubkey = &destinationKeypair.PublicKey @@ -115,8 +115,8 @@ func TestCiphertextCiphertextEqualityProof_EdgeCases(t *testing.T) { sourcePrivateKey := testutils.GenerateKey() destPrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) - destinationKeypair, _ := eg.KeyGen(*destPrivateKey, TestDenom) + sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey) + destinationKeypair, _ := eg.KeyGen(*destPrivateKey) amount := big.NewInt(0) @@ -155,9 +155,9 @@ func TestCiphertextCiphertextEqualityProof_EdgeCases(t *testing.T) { sourcePrivateKey := testutils.GenerateKey() destPrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) + sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey) - destinationKeypair, _ := eg.KeyGen(*destPrivateKey, TestDenom) + destinationKeypair, _ := eg.KeyGen(*destPrivateKey) amount := big.NewInt(1 << 60) // A large amount to test scalability @@ -197,8 +197,8 @@ func TestCiphertextCiphertextEqualityProof_UnmarshalJSON_Valid(t *testing.T) { sourcePrivateKey := testutils.GenerateKey() destPrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) - destinationKeypair, _ := eg.KeyGen(*destPrivateKey, TestDenom) + sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey) + destinationKeypair, _ := eg.KeyGen(*destPrivateKey) amount := big.NewInt(100) @@ -244,8 +244,8 @@ func TestNewCiphertextCiphertextEqualityProof_InvalidInputs(t *testing.T) { sourcePrivateKey := testutils.GenerateKey() destPrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) - destinationKeypair, _ := eg.KeyGen(*destPrivateKey, TestDenom) + sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey) + destinationKeypair, _ := eg.KeyGen(*destPrivateKey) amount := big.NewInt(100) @@ -326,8 +326,8 @@ func TestVerifyCiphertextCiphertextEquality_InvalidInputs(t *testing.T) { sourcePrivateKey := testutils.GenerateKey() destPrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) - destinationKeypair, _ := eg.KeyGen(*destPrivateKey, TestDenom) + sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey) + destinationKeypair, _ := eg.KeyGen(*destPrivateKey) amount := big.NewInt(100) @@ -459,8 +459,8 @@ func TestCiphertextCiphertextEqualityProof_IdentityD(t *testing.T) { sourcePrivateKey := testutils.GenerateKey() destPrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) - destinationKeypair, _ := eg.KeyGen(*destPrivateKey, TestDenom) + sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey) + destinationKeypair, _ := eg.KeyGen(*destPrivateKey) // Encrypt the source amount sourceCiphertext, _, err := eg.Encrypt(sourceKeypair.PublicKey, big.NewInt(100)) diff --git a/pkg/zkproofs/ciphertext_commitment_equality_test.go b/pkg/zkproofs/ciphertext_commitment_equality_test.go index 185535e..30235dc 100644 --- a/pkg/zkproofs/ciphertext_commitment_equality_test.go +++ b/pkg/zkproofs/ciphertext_commitment_equality_test.go @@ -56,7 +56,7 @@ func TestCiphertextCommitmentEqualityProof(t *testing.T) { // Key generation sourcePrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) + sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey) // Encrypt the source amount sourceCiphertext, sourceRandomness, err := eg.Encrypt(sourceKeypair.PublicKey, tt.sourceAmount) @@ -123,7 +123,7 @@ func TestCiphertextCommitmentEqualityProof(t *testing.T) { func TestCiphertextCommitmentEqualityProof_MarshalUnmarshalJSON(t *testing.T) { sourcePrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) + sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey) amount := big.NewInt(232436) // Encrypt the source amount @@ -163,7 +163,7 @@ func TestCiphertextCommitmentEqualityProof_MarshalUnmarshalJSON(t *testing.T) { func TestNewCiphertextCommitmentEqualityProof_InvalidInput(t *testing.T) { sourcePrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) + sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey) amount := big.NewInt(100) @@ -223,7 +223,7 @@ func TestNewCiphertextCommitmentEqualityProof_InvalidInput(t *testing.T) { func TestVerifyCiphertextCommitmentEquality_InvalidInput(t *testing.T) { sourcePrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) + sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey) amount := big.NewInt(100) @@ -333,7 +333,7 @@ func TestCiphertextCommitmentEqualityProof_IdentityD(t *testing.T) { // Key generation sourcePrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey, TestDenom) + sourceKeypair, _ := eg.KeyGen(*sourcePrivateKey) // Encrypt the source amount sourceCiphertext, _, err := eg.Encrypt(sourceKeypair.PublicKey, big.NewInt(100)) diff --git a/pkg/zkproofs/ciphertext_validity_test.go b/pkg/zkproofs/ciphertext_validity_test.go index 1cc9a21..9ba713d 100644 --- a/pkg/zkproofs/ciphertext_validity_test.go +++ b/pkg/zkproofs/ciphertext_validity_test.go @@ -16,8 +16,8 @@ func TestValidityProof(t *testing.T) { altPrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keys, _ := eg.KeyGen(*privateKey, TestDenom) - altKeys, _ := eg.KeyGen(*altPrivateKey, TestDenom) + keys, _ := eg.KeyGen(*privateKey) + altKeys, _ := eg.KeyGen(*altPrivateKey) message12 := big.NewInt(12) ciphertext12, randomness12, err := eg.Encrypt(keys.PublicKey, message12) @@ -65,7 +65,7 @@ func TestValidityProof(t *testing.T) { func TestCiphertextValidityProof_MarshalUnmarshalJSON(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keys, _ := eg.KeyGen(*privateKey, TestDenom) + keys, _ := eg.KeyGen(*privateKey) message12 := big.NewInt(12) ciphertext12, randomness12, _ := eg.Encrypt(keys.PublicKey, message12) @@ -91,7 +91,7 @@ func TestCiphertextValidityProof_MarshalUnmarshalJSON(t *testing.T) { func TestNewCiphertextValidityProof_InvalidInput(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keys, _ := eg.KeyGen(*privateKey, TestDenom) + keys, _ := eg.KeyGen(*privateKey) amount := big.NewInt(100) // Encrypt the amount using source and destination public keys @@ -137,7 +137,7 @@ func TestNewCiphertextValidityProof_InvalidInput(t *testing.T) { func TestVerifyCiphertextValidityProof_Invalid_Input(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keys, _ := eg.KeyGen(*privateKey, TestDenom) + keys, _ := eg.KeyGen(*privateKey) amount := big.NewInt(100) // Encrypt the amount using source and destination public keys diff --git a/pkg/zkproofs/pubkey_validity_test.go b/pkg/zkproofs/pubkey_validity_test.go index 83cca09..16fbe6b 100644 --- a/pkg/zkproofs/pubkey_validity_test.go +++ b/pkg/zkproofs/pubkey_validity_test.go @@ -15,8 +15,8 @@ func TestPubKeyValidityProof(t *testing.T) { altPrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keys, _ := eg.KeyGen(*privateKey, TestDenom) - altKeys, _ := eg.KeyGen(*altPrivateKey, TestDenom) + keys, _ := eg.KeyGen(*privateKey) + altKeys, _ := eg.KeyGen(*altPrivateKey) // Prove knowledge of the private key proof, err := NewPubKeyValidityProof(keys.PublicKey, keys.PrivateKey) @@ -39,7 +39,7 @@ func TestPubKeyValidityProof(t *testing.T) { func TestPubKeyValidityProof_MarshalUnmarshalJSON(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keys, _ := eg.KeyGen(*privateKey, TestDenom) + keys, _ := eg.KeyGen(*privateKey) original, err := NewPubKeyValidityProof(keys.PublicKey, keys.PrivateKey) require.NoError(t, err, "Proof generation should not produce an error") @@ -60,7 +60,7 @@ func TestPubKeyValidityProof_MarshalUnmarshalJSON(t *testing.T) { func TestNewPubKeyValidityProof_InvalidInput(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keys, _ := eg.KeyGen(*privateKey, TestDenom) + keys, _ := eg.KeyGen(*privateKey) _, err := NewPubKeyValidityProof(nil, keys.PrivateKey) require.Error(t, err, "Generating proof with nil public key should produce an error") @@ -73,7 +73,7 @@ func TestVerifyPubKeyValidityProof_InvalidInput(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keys, err := eg.KeyGen(*privateKey, TestDenom) + keys, err := eg.KeyGen(*privateKey) require.Nil(t, err) // Prove knowledge of the private key diff --git a/pkg/zkproofs/range_test.go b/pkg/zkproofs/range_test.go index 7b6d49d..112422e 100644 --- a/pkg/zkproofs/range_test.go +++ b/pkg/zkproofs/range_test.go @@ -34,7 +34,7 @@ func TestValueIsInRange(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keyPair, err := eg.KeyGen(*privateKey, TestDenom) + keyPair, err := eg.KeyGen(*privateKey) require.Nil(t, err, "Error generating key pair") ciphertext, gamma, _ := eg.Encrypt(keyPair.PublicKey, value) @@ -75,7 +75,7 @@ func TestRangeAttacksAreInfeasible(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keyPair, err := eg.KeyGen(*privateKey, TestDenom) + keyPair, err := eg.KeyGen(*privateKey) require.Nil(t, err, "Error generating key pair") ciphertext, gamma, _ := eg.Encrypt(keyPair.PublicKey, value) @@ -163,7 +163,7 @@ func TestRangeProofs(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keyPair, err := eg.KeyGen(*privateKey, TestDenom) + keyPair, err := eg.KeyGen(*privateKey) require.Nil(t, err, "Error generating key pair") ciphertext, gamma, _ := eg.Encrypt(keyPair.PublicKey, value) @@ -193,7 +193,7 @@ func TestRangeProofsLargeN(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keyPair, err := eg.KeyGen(*privateKey, TestDenom) + keyPair, err := eg.KeyGen(*privateKey) require.Nil(t, err, "Error generating key pair") ciphertext, gamma, _ := eg.Encrypt(keyPair.PublicKey, value) @@ -226,7 +226,7 @@ func TestRangeProofsWithMarshaling(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keyPair, err := eg.KeyGen(*privateKey, TestDenom) + keyPair, err := eg.KeyGen(*privateKey) require.Nil(t, err, "Error generating key pair") ciphertext, gamma, _ := eg.Encrypt(keyPair.PublicKey, value) @@ -255,7 +255,7 @@ func TestRangeProofs_InvalidInput(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keyPair, err := eg.KeyGen(*privateKey, TestDenom) + keyPair, err := eg.KeyGen(*privateKey) require.Nil(t, err, "Error generating key pair") _, gamma, _ := eg.Encrypt(keyPair.PublicKey, big.NewInt(10)) @@ -281,7 +281,7 @@ func TestVerifyRangeProof_InvalidInput(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() value := big.NewInt(10) - keyPair, _ := eg.KeyGen(*privateKey, TestDenom) + keyPair, _ := eg.KeyGen(*privateKey) ciphertext, gamma, _ := eg.Encrypt(keyPair.PublicKey, value) proof, err := NewRangeProof(64, value, gamma) @@ -326,7 +326,7 @@ func TestRangeProofVerifierReuse(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keyPair, err := eg.KeyGen(*privateKey, TestDenom) + keyPair, err := eg.KeyGen(*privateKey) require.Nil(t, err, "Error generating key pair") ciphertext, gamma, _ := eg.Encrypt(keyPair.PublicKey, value) diff --git a/pkg/zkproofs/zero_balance_test.go b/pkg/zkproofs/zero_balance_test.go index fe4199b..fb1a333 100644 --- a/pkg/zkproofs/zero_balance_test.go +++ b/pkg/zkproofs/zero_balance_test.go @@ -46,8 +46,8 @@ func TestZeroBalanceProof(t *testing.T) { altPrivateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keypair, _ := eg.KeyGen(*privateKey, TestDenom) - alternativeKeypair, _ := eg.KeyGen(*altPrivateKey, TestDenom) + keypair, _ := eg.KeyGen(*privateKey) + alternativeKeypair, _ := eg.KeyGen(*altPrivateKey) actualPublicKey := keypair.PublicKey if tt.useDifferentPubKey { @@ -78,7 +78,7 @@ func TestZeroBalanceProof_MarshalUnmarshalJSON(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keypair, _ := eg.KeyGen(*privateKey, TestDenom) + keypair, _ := eg.KeyGen(*privateKey) ciphertext, _, _ := eg.Encrypt(keypair.PublicKey, big.NewInt(0)) original, err := NewZeroBalanceProof(keypair, ciphertext) @@ -103,7 +103,7 @@ func TestZeroBalanceProof_InvalidRandomness(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keypair, err := eg.KeyGen(*privateKey, TestDenom) + keypair, err := eg.KeyGen(*privateKey) require.NoError(t, err, "Failed to generate key pair") ciphertext, _, err := eg.Encrypt(keypair.PublicKey, big.NewInt(0)) @@ -129,7 +129,7 @@ func TestZeroBalanceProof_ExtremelyLargeScalars(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keypair, err := eg.KeyGen(*privateKey, TestDenom) + keypair, err := eg.KeyGen(*privateKey) require.NoError(t, err, "Failed to generate key pair") ciphertext, _, err := eg.Encrypt(keypair.PublicKey, big.NewInt(0)) @@ -158,7 +158,7 @@ func TestZeroBalanceProof_TamperedProof(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keypair, err := eg.KeyGen(*privateKey, TestDenom) + keypair, err := eg.KeyGen(*privateKey) require.NoError(t, err, "Failed to generate key pair") ciphertext, _, err := eg.Encrypt(keypair.PublicKey, big.NewInt(0)) @@ -203,7 +203,7 @@ func TestZeroBalanceProof_InvalidInput(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keypair, _ := eg.KeyGen(*privateKey, TestDenom) + keypair, _ := eg.KeyGen(*privateKey) _, err = NewZeroBalanceProof(&elgamal.KeyPair{PublicKey: keypair.PublicKey}, nil) require.Error(t, err, "Should return an error when ciphertext is nil") require.Contains(t, err.Error(), "keypair is invalid") @@ -230,7 +230,7 @@ func TestVerifyZeroProof_InvalidInput(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keypair, err := eg.KeyGen(*privateKey, TestDenom) + keypair, err := eg.KeyGen(*privateKey) require.NoError(t, err, "Failed to generate key pair") ciphertext, _, err := eg.Encrypt(keypair.PublicKey, big.NewInt(0)) @@ -296,7 +296,7 @@ func TestZeroBalanceProof_IdentityD(t *testing.T) { privateKey := testutils.GenerateKey() eg := elgamal.NewTwistedElgamal() - keypair, _ := eg.KeyGen(*privateKey, TestDenom) + keypair, _ := eg.KeyGen(*privateKey) ciphertext, _, err := eg.Encrypt(keypair.PublicKey, big.NewInt(100)) require.NoError(t, err, "Failed to encrypt amount") From 441fb76875a2529482c8654ba94f6d36b9628147 Mon Sep 17 00:00:00 2001 From: mj Date: Sat, 7 Dec 2024 23:37:44 -0500 Subject: [PATCH 3/5] remove unused --- go.mod | 14 +------------- go.sum | 35 +++-------------------------------- 2 files changed, 4 insertions(+), 45 deletions(-) diff --git a/go.mod b/go.mod index 238a041..b4b2d9c 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ toolchain go1.23.3 require ( github.com/bwesterb/go-ristretto v1.2.3 github.com/coinbase/kryptology v1.8.0 - github.com/ethereum/go-ethereum v1.14.12 github.com/gtank/merlin v0.1.1 github.com/stretchr/testify v1.9.0 golang.org/x/crypto v0.27.0 @@ -15,27 +14,16 @@ require ( require ( filippo.io/edwards25519 v1.1.0 // indirect - github.com/bits-and-blooms/bitset v1.13.0 // indirect github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect - github.com/consensys/bavard v0.1.13 // indirect - github.com/consensys/gnark-crypto v0.12.1 // indirect - github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c // indirect - github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect - github.com/ethereum/c-kzg-4844 v1.0.0 // indirect - github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9 // indirect - github.com/holiman/uint256 v1.3.1 // indirect + github.com/kr/pretty v0.3.1 // indirect github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643 // indirect - github.com/mmcloughlin/addchain v0.4.0 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/supranational/blst v0.3.13 // indirect - golang.org/x/sync v0.7.0 // indirect golang.org/x/sys v0.25.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect - rsc.io/tmplfunc v0.0.3 // indirect ) replace github.com/coinbase/kryptology => github.com/sei-protocol/coinbase-kryptology v0.0.0-20241015231206-08f61b7965cd diff --git a/go.sum b/go.sum index e5e58ff..a45108e 100644 --- a/go.sum +++ b/go.sum @@ -1,39 +1,17 @@ -filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU= -filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= -github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJRUA0wFAVE= -github.com/bits-and-blooms/bitset v1.13.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8= github.com/btcsuite/btcd/btcec/v2 v2.3.4 h1:3EJjcN70HCu/mwqlUsGK8GcNVyLVxFDlWurTXGPFfiQ= github.com/btcsuite/btcd/btcec/v2 v2.3.4/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04= github.com/bwesterb/go-ristretto v1.2.3 h1:1w53tCkGhCQ5djbat3+MH0BAQ5Kfgbt56UZQ/JMzngw= github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= -github.com/consensys/bavard v0.1.13 h1:oLhMLOFGTLdlda/kma4VOJazblc7IM5y5QPd2A/YjhQ= -github.com/consensys/bavard v0.1.13/go.mod h1:9ItSMtA/dXMAiL7BG6bqW2m3NdSEObYWoH223nGHukI= -github.com/consensys/gnark-crypto v0.12.1 h1:lHH39WuuFgVHONRl3J0LRBtuYdQTumFSDtJF7HpyG8M= -github.com/consensys/gnark-crypto v0.12.1/go.mod h1:v2Gy7L/4ZRosZ7Ivs+9SfUDr0f5UlG+EM5t7MPHiLuY= -github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c h1:uQYC5Z1mdLRPrZhHjHxufI8+2UG/i25QG92j0Er9p6I= -github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c/go.mod h1:geZJZH3SzKCqnz5VT0q/DyIG/tvu/dZk+VIfXicupJs= -github.com/crate-crypto/go-kzg-4844 v1.0.0 h1:TsSgHwrkTKecKJ4kadtHi4b3xHW5dCFUDFnUp1TsawI= -github.com/crate-crypto/go-kzg-4844 v1.0.0/go.mod h1:1kMhvPgI0Ky3yIa+9lFySEBUBXkYxeOi8ZF1sYioxhc= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs= -github.com/ethereum/c-kzg-4844 v1.0.0 h1:0X1LBXxaEtYD9xsyj9B9ctQEZIpnvVDeoBx8aHEwTNA= -github.com/ethereum/c-kzg-4844 v1.0.0/go.mod h1:VewdlzQmpT5QSrVhbBuGoCdFJkpaJlO1aQputP83wc0= -github.com/ethereum/go-ethereum v1.13.15 h1:U7sSGYGo4SPjP6iNIifNoyIAiNjrmQkz6EwQG+/EZWo= -github.com/ethereum/go-ethereum v1.13.15/go.mod h1:TN8ZiHrdJwSe8Cb6x+p0hs5CxhJZPbqB7hHkaUXcmIU= -github.com/ethereum/go-ethereum v1.14.12 h1:8hl57x77HSUo+cXExrURjU/w1VhL+ShCTJrTwcCQSe4= -github.com/ethereum/go-ethereum v1.14.12/go.mod h1:RAC2gVMWJ6FkxSPESfbshrcKpIokgQKsVKmAuqdekDY= -github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9 h1:8NfxH2iXvJ60YRB8ChToFTUzl8awsc3cJ8CbLjGIl/A= -github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9/go.mod h1:M3b90YRnzqKyyzBEWJGqj8Qff4IDeXnzFw0P9bFw3uk= -github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= github.com/gtank/merlin v0.1.1 h1:eQ90iG7K9pOhtereWsmyRJ6RAwcP4tHTDBHXNg+u5is= github.com/gtank/merlin v0.1.1/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s= -github.com/holiman/uint256 v1.3.1 h1:JfTzmih28bittyHM8z360dCjIA9dbPIBlcTI6lmctQs= -github.com/holiman/uint256 v1.3.1/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= @@ -43,25 +21,20 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643 h1:hLDRPB66XQT/8+wG9WsDpiCvZf1yKO7sz7scAjSlBa0= github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643/go.mod h1:43+3pMjjKimDBf5Kr4ZFNGbLql1zKkbImw+fZbw3geM= -github.com/mmcloughlin/addchain v0.4.0 h1:SobOdjm2xLj1KkXN5/n0xTIWyZA2+s99UCY1iPfkHRY= -github.com/mmcloughlin/addchain v0.4.0/go.mod h1:A86O+tHqZLMNO4w6ZZ4FlVQEadcoqkyU72HC5wJ4RlU= -github.com/mmcloughlin/profile v0.1.1/go.mod h1:IhHD7q1ooxgwTgjxQYkACGA77oFTDdFVejUS1/tS/qU= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/sei-protocol/coinbase-kryptology v0.0.0-20241015231206-08f61b7965cd h1:R/g4pa6pgegLAAt1NTrO1qVJ3uZH9hfcMcc4yLz1cgg= github.com/sei-protocol/coinbase-kryptology v0.0.0-20241015231206-08f61b7965cd/go.mod h1:vAKKp7/qgfMtPXMseamOlZMqK7BytjfOm0rFKWph5c4= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -github.com/supranational/blst v0.3.13 h1:AYeSxdOMacwu7FBmpfloBz5pbFXDmJL33RuwnKtmTjk= -github.com/supranational/blst v0.3.13/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw= golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= -golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= @@ -69,5 +42,3 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -rsc.io/tmplfunc v0.0.3 h1:53XFQh69AfOa8Tw0Jm7t+GV7KZhOi6jzsCzTtKbMvzU= -rsc.io/tmplfunc v0.0.3/go.mod h1:AG3sTPzElb1Io3Yg4voV9AGZJuleGAwaVRxL9M49PhA= From 929548da55885df2f52bab74bced53f974c0cd8b Mon Sep 17 00:00:00 2001 From: mj Date: Sat, 7 Dec 2024 23:39:43 -0500 Subject: [PATCH 4/5] 1.21 --- go.mod | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/go.mod b/go.mod index b4b2d9c..82805e7 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,6 @@ module github.com/sei-protocol/sei-cryptography -go 1.22 - -toolchain go1.23.3 +go 1.21 require ( github.com/bwesterb/go-ristretto v1.2.3 From ddf40bcdb19ffbb5a12598cb597c68cf6a7d30a1 Mon Sep 17 00:00:00 2001 From: mj Date: Wed, 11 Dec 2024 02:51:23 +0800 Subject: [PATCH 5/5] update comments --- go.sum | 1 + pkg/encryption/aes.go | 7 ++----- pkg/encryption/elgamal/common.go | 7 +++---- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/go.sum b/go.sum index a45108e..d2ba48b 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,7 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs= +github.com/ethereum/go-ethereum v1.13.15/go.mod h1:TN8ZiHrdJwSe8Cb6x+p0hs5CxhJZPbqB7hHkaUXcmIU= github.com/gtank/merlin v0.1.1 h1:eQ90iG7K9pOhtereWsmyRJ6RAwcP4tHTDBHXNg+u5is= github.com/gtank/merlin v0.1.1/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= diff --git a/pkg/encryption/aes.go b/pkg/encryption/aes.go index 3dd7a83..e58a1e2 100644 --- a/pkg/encryption/aes.go +++ b/pkg/encryption/aes.go @@ -15,18 +15,15 @@ import ( ) // GetAESKey derives a 32-byte AES key using the provided bytes. -// 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. // It employs HKDF with SHA-256, using the private key bytes. +// No additional salt is added here so ensure that the privateBytes are already salted or hashed. func GetAESKey(privateBytes []byte) ([]byte, error) { if len(privateBytes) == 0 { return nil, fmt.Errorf("bytes is empty") } - // Use a SHA-256 hash of the denom string as the salt - salt := sha256.Sum256([]byte("aes key derivation salt")) - // Create an HKDF reader using SHA-256 - hkdf := hkdf.New(sha256.New, privateBytes, salt[:], []byte("aes key derivation")) + hkdf := hkdf.New(sha256.New, privateBytes, nil, []byte("aes key derivation")) // Allocate a 32-byte array for the AES key aesKey := make([]byte, 32) diff --git a/pkg/encryption/elgamal/common.go b/pkg/encryption/elgamal/common.go index fe98ebd..e624bdd 100644 --- a/pkg/encryption/elgamal/common.go +++ b/pkg/encryption/elgamal/common.go @@ -47,12 +47,11 @@ func (teg TwistedElGamal) GetH() curves.Point { return teg.curve.Point.Hash(bytes) } +// Creates an el gamal private key from the provided bytes. +// No additional salt is added here so ensure that the privateBytes are already salted or hashed. func (teg TwistedElGamal) getPrivateKeyFromBytes(privateBytes []byte) (curves.Scalar, error) { - // Hash the denom to get a salt. - salt := sha256.Sum256([]byte("elgamal scalar derivation salt")) - // Create an HKDF reader using SHA-256 - hkdf := hkdf.New(sha256.New, privateBytes, salt[:], []byte("elgamal scalar derivation")) + hkdf := hkdf.New(sha256.New, privateBytes, nil, []byte("elgamal scalar derivation")) // Generate 64 bytes of randomness from HKDF output var scalarBytes [64]byte