rename PrivateKey -> SecretKey

This commit is contained in:
sachaservan committed 2018-08-18 13:14:21 -04:00
1 parent 753322e473
commit 5adb70bc7b
7 files changed
+87 -87

No files matched your search

+33 -33
View File
@@ -32,18 +32,18 @@ func TestComputePhi(t *testing.T) {
}
}
func TestCreatePrivateKey(t *testing.T) {
func TestCreateSecretKey(t *testing.T) {
p := big.NewInt(463)
q := big.NewInt(631)
privateKey := CreatePrivateKey(p, q)
SecretKey := CreateSecretKey(p, q)
if privateKey.N.Cmp(big.NewInt(292153)) != 0 {
t.Errorf("Unexpected N PublicKey value [%v]", privateKey.N)
if SecretKey.N.Cmp(big.NewInt(292153)) != 0 {
t.Errorf("Unexpected N PublicKey value [%v]", SecretKey.N)
}
if privateKey.Lambda.Cmp(big.NewInt(291060)) != 0 {
t.Errorf("Unexpected Lambda Public key value [%v]", privateKey.Lambda)
if SecretKey.Lambda.Cmp(big.NewInt(291060)) != 0 {
t.Errorf("Unexpected Lambda Public key value [%v]", SecretKey.Lambda)
}
}
@@ -51,14 +51,14 @@ func TestEncryptDecryptSmall(t *testing.T) {
p := big.NewInt(13)
q := big.NewInt(11)
for i := 1; i < 10; i++ {
privateKey := CreatePrivateKey(p, q)
SecretKey := CreateSecretKey(p, q)
initialValue := big.NewInt(100)
cypher, err := privateKey.Encrypt(initialValue, rand.Reader)
cypher, err := SecretKey.Encrypt(initialValue, rand.Reader)
if err != nil {
t.Error(err)
}
returnedValue := privateKey.Decrypt(cypher)
returnedValue := SecretKey.Decrypt(cypher)
if initialValue.Cmp(returnedValue) != 0 {
t.Error("wrong decryption ", returnedValue, " is not ", initialValue)
}
@@ -70,7 +70,7 @@ func TestCheckPlaintextSpace(t *testing.T) {
q := big.NewInt(11)
// N = pq = 143 so the plaintext space is [0, 143)
privateKey := CreatePrivateKey(p, q)
SecretKey := CreateSecretKey(p, q)
var tests = map[string]struct {
plaintext *big.Int
@@ -101,7 +101,7 @@ func TestCheckPlaintextSpace(t *testing.T) {
for testName, test := range tests {
t.Run(testName, func(t *testing.T) {
cypher, err := privateKey.Encrypt(test.plaintext, rand.Reader)
cypher, err := SecretKey.Encrypt(test.plaintext, rand.Reader)
if !reflect.DeepEqual(err, test.expectedError) {
t.Errorf(
"Unexpected error\nExpected: %v\nActual: %v",
@@ -111,7 +111,7 @@ func TestCheckPlaintextSpace(t *testing.T) {
}
if test.expectedError == nil {
decrypted := privateKey.Decrypt(cypher)
decrypted := SecretKey.Decrypt(cypher)
if test.plaintext.Cmp(decrypted) != 0 {
t.Errorf(
"Unexpected decryption\nExpected: %v\nActual: %v",
@@ -125,44 +125,44 @@ func TestCheckPlaintextSpace(t *testing.T) {
}
func TestAddCyphers(t *testing.T) {
privateKey := CreatePrivateKey(big.NewInt(17), big.NewInt(13))
SecretKey := CreateSecretKey(big.NewInt(17), big.NewInt(13))
cypher1, _ := privateKey.Encrypt(big.NewInt(5), rand.Reader)
cypher2, _ := privateKey.Encrypt(big.NewInt(6), rand.Reader)
cypher3, _ := privateKey.Encrypt(big.NewInt(7), rand.Reader)
cypher4, _ := privateKey.Encrypt(big.NewInt(8), rand.Reader)
cypher5 := privateKey.Add(cypher1, cypher2, cypher3, cypher4)
cypher1, _ := SecretKey.Encrypt(big.NewInt(5), rand.Reader)
cypher2, _ := SecretKey.Encrypt(big.NewInt(6), rand.Reader)
cypher3, _ := SecretKey.Encrypt(big.NewInt(7), rand.Reader)
cypher4, _ := SecretKey.Encrypt(big.NewInt(8), rand.Reader)
cypher5 := SecretKey.Add(cypher1, cypher2, cypher3, cypher4)
m := privateKey.Decrypt(cypher5)
m := SecretKey.Decrypt(cypher5)
if m.Cmp(big.NewInt(26)) != 0 {
t.Errorf("Unexpected decrypted value [%v]", m)
}
}
func TestAddCypherWithSmallKeyModulus(t *testing.T) {
privateKey := CreatePrivateKey(big.NewInt(7), big.NewInt(5))
SecretKey := CreateSecretKey(big.NewInt(7), big.NewInt(5))
cypher1, _ := privateKey.Encrypt(big.NewInt(30), rand.Reader)
cypher2, _ := privateKey.Encrypt(big.NewInt(25), rand.Reader)
cypher3, _ := privateKey.Encrypt(big.NewInt(11), rand.Reader)
cypher4 := privateKey.Add(cypher1, cypher2, cypher3)
cypher1, _ := SecretKey.Encrypt(big.NewInt(30), rand.Reader)
cypher2, _ := SecretKey.Encrypt(big.NewInt(25), rand.Reader)
cypher3, _ := SecretKey.Encrypt(big.NewInt(11), rand.Reader)
cypher4 := SecretKey.Add(cypher1, cypher2, cypher3)
m := privateKey.Decrypt(cypher4)
m := SecretKey.Decrypt(cypher4)
if m.Cmp(big.NewInt(31)) != 0 {
t.Errorf("Unexpected decrypted value [%v]", m)
}
}
func TestMulCypher(t *testing.T) {
privateKey := CreatePrivateKey(big.NewInt(17), big.NewInt(13))
SecretKey := CreateSecretKey(big.NewInt(17), big.NewInt(13))
cypher, err := privateKey.Encrypt(big.NewInt(3), rand.Reader)
cypher, err := SecretKey.Encrypt(big.NewInt(3), rand.Reader)
if err != nil {
t.Fatal(err)
}
cypherMultiple := privateKey.Mul(cypher, big.NewInt(7))
multiple := privateKey.Decrypt(cypherMultiple)
cypherMultiple := SecretKey.Mul(cypher, big.NewInt(7))
multiple := SecretKey.Decrypt(cypherMultiple)
// 3 * 7 = 21
if multiple.Cmp(big.NewInt(21)) != 0 {
@@ -171,15 +171,15 @@ func TestMulCypher(t *testing.T) {
}
func TestMulCypherWithSmallKeyModulus(t *testing.T) {
privateKey := CreatePrivateKey(big.NewInt(7), big.NewInt(5))
SecretKey := CreateSecretKey(big.NewInt(7), big.NewInt(5))
cypher, err := privateKey.Encrypt(big.NewInt(30), rand.Reader)
cypher, err := SecretKey.Encrypt(big.NewInt(30), rand.Reader)
if err != nil {
t.Fatal(err)
}
cypherMultiple := privateKey.Mul(cypher, big.NewInt(93))
multiple := privateKey.Decrypt(cypherMultiple)
cypherMultiple := SecretKey.Mul(cypher, big.NewInt(93))
multiple := SecretKey.Decrypt(cypherMultiple)
// (30*93) mod (7*5) = 25
if multiple.Cmp(big.NewInt(25)) != 0 {