github.com/incognitochain/go-incognito-sdk@v1.0.1/privacy/elgamal_test.go (about) 1 package privacy 2 3 import ( 4 "fmt" 5 "github.com/stretchr/testify/assert" 6 "testing" 7 ) 8 9 /* 10 Unit test for elgamal encryption 11 */ 12 13 func TestElGamalCipherText_Bytes(t *testing.T) { 14 privKey := new(elGamalPrivateKey) 15 privKey.x = RandomScalar() 16 17 // generate public key 18 pubKey := new(elGamalPublicKey) 19 pubKey.h = new(Point).ScalarMultBase(privKey.x) 20 21 message := RandomPoint() 22 23 // Encrypt message using public key 24 c := pubKey.encrypt(message) 25 cBytes := c.Bytes() 26 fmt.Println(len(cBytes)) 27 } 28 29 func TestElGamalPublicKey_Encryption(t *testing.T) { 30 for i := 0; i < 5000; i++ { 31 // generate private key 32 privKey := new(elGamalPrivateKey) 33 privKey.x = RandomScalar() 34 35 // generate public key 36 pubKey := new(elGamalPublicKey) 37 pubKey.h = new(Point).ScalarMultBase(privKey.x) 38 39 // random message (msg is an elliptic point) 40 message := RandomPoint() 41 42 // Encrypt message using public key 43 ciphertext1 := pubKey.encrypt(message) 44 45 // convert ciphertext1 to bytes array 46 ciphertext1Bytes := ciphertext1.Bytes() 47 48 // new ciphertext2 49 ciphertext2 := new(elGamalCipherText) 50 ciphertext2.SetBytes(ciphertext1Bytes) 51 52 assert.Equal(t, ciphertext1, ciphertext2) 53 54 // decrypt ciphertext using privateKey 55 decryptedCiphertext, err := privKey.decrypt(ciphertext1) 56 57 assert.Equal(t, nil, err) 58 assert.Equal(t, message, decryptedCiphertext) 59 } 60 }