Use Cmp to compare Ints, reflections is just to heavy
This commit is contained in:
1 file changed
+13
-13
+13
-13
@@ -3,7 +3,6 @@ package paillier
|
|||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"math/big"
|
"math/big"
|
||||||
"reflect"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,7 +13,7 @@ func TestComputeL(t *testing.T) {
|
|||||||
expected := big.NewInt(6)
|
expected := big.NewInt(6)
|
||||||
actual := L(u, n)
|
actual := L(u, n)
|
||||||
|
|
||||||
if !reflect.DeepEqual(expected, actual) {
|
if expected.Cmp(actual) != 0 {
|
||||||
t.Errorf("Unexpected L function result [%v]", actual)
|
t.Errorf("Unexpected L function result [%v]", actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -26,7 +25,7 @@ func TestComputePhi(t *testing.T) {
|
|||||||
expected := big.NewInt(24)
|
expected := big.NewInt(24)
|
||||||
actual := computePhi(a, b)
|
actual := computePhi(a, b)
|
||||||
|
|
||||||
if !reflect.DeepEqual(expected, actual) {
|
if expected.Cmp(actual) != 0 {
|
||||||
t.Errorf("Unexpected phi value [%v]", actual)
|
t.Errorf("Unexpected phi value [%v]", actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -37,11 +36,11 @@ func TestCreatePrivateKey(t *testing.T) {
|
|||||||
|
|
||||||
privateKey := CreatePrivateKey(p, q)
|
privateKey := CreatePrivateKey(p, q)
|
||||||
|
|
||||||
if !reflect.DeepEqual(privateKey.N, big.NewInt(292153)) {
|
if privateKey.N.Cmp(big.NewInt(292153)) != 0 {
|
||||||
t.Errorf("Unexpected N PublicKey value [%v]", privateKey.N)
|
t.Errorf("Unexpected N PublicKey value [%v]", privateKey.N)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !reflect.DeepEqual(privateKey.Lambda, big.NewInt(291060)) {
|
if privateKey.Lambda.Cmp(big.NewInt(291060)) != 0 {
|
||||||
t.Errorf("Unexpected Lambda Public key value [%v]", privateKey.Lambda)
|
t.Errorf("Unexpected Lambda Public key value [%v]", privateKey.Lambda)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -52,14 +51,14 @@ func TestEncryptDecryptSmall(t *testing.T) {
|
|||||||
for i := 1; i < 10; i++ {
|
for i := 1; i < 10; i++ {
|
||||||
privateKey := CreatePrivateKey(p, q)
|
privateKey := CreatePrivateKey(p, q)
|
||||||
|
|
||||||
inicialValue := big.NewInt(100)
|
initialValue := big.NewInt(100)
|
||||||
cypher, err := privateKey.Encrypt(inicialValue, rand.Reader)
|
cypher, err := privateKey.Encrypt(initialValue, rand.Reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
returnedValue := privateKey.Decrypt(cypher)
|
returnedValue := privateKey.Decrypt(cypher)
|
||||||
if !reflect.DeepEqual(inicialValue, returnedValue) {
|
if initialValue.Cmp(returnedValue) != 0 {
|
||||||
t.Error("wrong decryption ", returnedValue, " is not ", inicialValue)
|
t.Error("wrong decryption ", returnedValue, " is not ", initialValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -74,7 +73,7 @@ func TestAddCyphers(t *testing.T) {
|
|||||||
cypher5 := privateKey.Add(cypher1, cypher2, cypher3, cypher4)
|
cypher5 := privateKey.Add(cypher1, cypher2, cypher3, cypher4)
|
||||||
|
|
||||||
m := privateKey.Decrypt(cypher5)
|
m := privateKey.Decrypt(cypher5)
|
||||||
if !reflect.DeepEqual(m, big.NewInt(26)) {
|
if m.Cmp(big.NewInt(26)) != 0 {
|
||||||
t.Errorf("Unexpected decrypted value [%v]", m)
|
t.Errorf("Unexpected decrypted value [%v]", m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -88,7 +87,7 @@ func TestAddCypherWithSmallKeyModulus(t *testing.T) {
|
|||||||
cypher4 := privateKey.Add(cypher1, cypher2, cypher3)
|
cypher4 := privateKey.Add(cypher1, cypher2, cypher3)
|
||||||
|
|
||||||
m := privateKey.Decrypt(cypher4)
|
m := privateKey.Decrypt(cypher4)
|
||||||
if !reflect.DeepEqual(m, big.NewInt(34)) {
|
if m.Cmp(big.NewInt(34)) != 0 {
|
||||||
t.Errorf("Unexpected decrypted value [%v]", m)
|
t.Errorf("Unexpected decrypted value [%v]", m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -104,7 +103,8 @@ func TestMulCypher(t *testing.T) {
|
|||||||
cypherMultiple := privateKey.Mul(cypher, big.NewInt(7))
|
cypherMultiple := privateKey.Mul(cypher, big.NewInt(7))
|
||||||
multiple := privateKey.Decrypt(cypherMultiple)
|
multiple := privateKey.Decrypt(cypherMultiple)
|
||||||
|
|
||||||
if !reflect.DeepEqual(multiple, big.NewInt(21)) {
|
// 3 * 7 = 21
|
||||||
|
if multiple.Cmp(big.NewInt(21)) != 0 {
|
||||||
t.Errorf("Unexpected decrypted value [%v]", multiple)
|
t.Errorf("Unexpected decrypted value [%v]", multiple)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -121,7 +121,7 @@ func TestMulCypherWithSmallKeyModulus(t *testing.T) {
|
|||||||
multiple := privateKey.Decrypt(cypherMultiple)
|
multiple := privateKey.Decrypt(cypherMultiple)
|
||||||
|
|
||||||
// (30*93) mod (7*5) = 25
|
// (30*93) mod (7*5) = 25
|
||||||
if !reflect.DeepEqual(multiple, big.NewInt(25)) {
|
if multiple.Cmp(big.NewInt(25)) != 0 {
|
||||||
t.Errorf("Unexpected decrypted value [%v]", multiple)
|
t.Errorf("Unexpected decrypted value [%v]", multiple)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user