Skip to content

Commit

Permalink
remove denom also
Browse files Browse the repository at this point in the history
  • Loading branch information
mj850 committed Dec 8, 2024
1 parent f379d04 commit ed5f511
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 114 deletions.
11 changes: 4 additions & 7 deletions pkg/encryption/aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
32 changes: 4 additions & 28 deletions pkg/encryption/aes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,48 +25,28 @@ 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,
},
}

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")
Expand All @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/encryption/elgamal/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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"))
Expand Down
42 changes: 15 additions & 27 deletions pkg/encryption/elgamal/encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{}

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion pkg/encryption/elgamal/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 15 additions & 15 deletions pkg/zkproofs/ciphertext_ciphertext_equality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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))
Expand Down
10 changes: 5 additions & 5 deletions pkg/zkproofs/ciphertext_commitment_equality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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))
Expand Down
Loading

0 comments on commit ed5f511

Please sign in to comment.