From 91c5edc8f35f2cd0fa278ff878c76b8be6dc6d6b Mon Sep 17 00:00:00 2001 From: Bernardo Cardoso <bernardo@elixxir.io> Date: Thu, 21 Mar 2019 14:19:36 -0600 Subject: [PATCH] Refactor other crypto packages in order to conform to cyclic changes --- diffieHellman/dhkx.go | 41 +++++++-------------- diffieHellman/dhkx_test.go | 73 ++++++++++++++++++++----------------- e2e/aes_test.go | 15 ++++---- e2e/dummykeygen.go | 2 +- e2e/dummykeygen_test.go | 4 +- e2e/encrypt.go | 6 +-- e2e/encrypt_test.go | 26 +++++++------ e2e/keygen.go | 2 +- e2e/keygen_test.go | 22 ++++++----- e2e/keys.go | 2 +- e2e/keys_test.go | 15 ++++---- e2e/ttl.go | 10 ++--- e2e/ttl_test.go | 22 +++++------ hash/keys.go | 6 +-- hash/keys_test.go | 16 ++++---- messaging/key.go | 2 +- messaging/key_test.go | 35 ++++++++++-------- registration/keygen.go | 6 +-- registration/keygen_test.go | 20 ++++++---- registration/uidgen.go | 2 + registration/uidgen_test.go | 8 ++-- signature/dsa.go | 68 ++++++++++++++++++---------------- signature/dsa_test.go | 34 ++++++++--------- 23 files changed, 226 insertions(+), 211 deletions(-) diff --git a/diffieHellman/dhkx.go b/diffieHellman/dhkx.go index a1aabdef..bfa664f2 100644 --- a/diffieHellman/dhkx.go +++ b/diffieHellman/dhkx.go @@ -11,15 +11,13 @@ import ( jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/crypto/csprng" "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/large" ) // CreateDHKeyPair is a function that receives the generator and prime and // returns a Diffie-Hellman Key pair withing the group func CreateDHKeyPair(group *cyclic.Group) (*cyclic.Int, *cyclic.Int) { - - prime := group.GetP(cyclic.NewInt(0)) - - if !prime.IsPrime() { + if !group.GetP().IsPrime() { jww.FATAL.Panicf("CreateDHKeyPair(): Passed number is not prime") } @@ -36,10 +34,10 @@ func CreateDHKeyPair(group *cyclic.Group) (*cyclic.Int, *cyclic.Int) { panic(fmt.Sprintf("Key RNG in Diffie Hellman Failed: %s", err.Error())) } - privateKey := cyclic.NewIntFromBytes(k1) + privateKey := group.NewIntFromBytes(k1) - publicKey := cyclic.NewInt(0) - publicKey.Exp(group.G, privateKey, prime) + publicKey := group.NewInt(0) + group.Exp(group.GetGCyclic(), privateKey, publicKey) return privateKey, publicKey } @@ -49,11 +47,8 @@ func CreateDHKeyPair(group *cyclic.Group) (*cyclic.Int, *cyclic.Int) { // v1.0 still does not include the CheckPublicKeyFeature func CreateDHSessionKey(publicKey *cyclic.Int, privateKey *cyclic.Int, group *cyclic.Group) (*cyclic.Int, error) { - - prime := group.GetP(cyclic.NewInt(0)) - - sessionKey := cyclic.NewInt(0) - sessionKey.Exp(publicKey, privateKey, prime) + sessionKey := group.NewInt(0) + group.Exp(publicKey, privateKey, sessionKey) return sessionKey, nil } @@ -63,34 +58,26 @@ func CreateDHSessionKey(publicKey *cyclic.Int, privateKey *cyclic.Int, // A valid public key will never trigger a negative response from this function // Legendre Symbol = a^(p-1)/2 mod p func CheckPublicKey(group *cyclic.Group, publicKey *cyclic.Int) bool { - - prime := cyclic.NewInt(0) - group.GetP(prime) - // Definition of the lower bound to 1 - lowerBound := cyclic.NewInt(1) + lowerBound := large.NewInt(1) // Definition of the upper bound to p-1 - upperBound := cyclic.NewInt(0) - group.GetPSub1(upperBound) + upperBound := group.GetPSub1() //Cmp returns -1 if number is smaller, 0 if the same and 1 if bigger than. - x := publicKey.Cmp(lowerBound) - y := publicKey.Cmp(upperBound) + x := publicKey.GetLargeInt().Cmp(lowerBound) + y := publicKey.GetLargeInt().Cmp(upperBound) // Public Key must be bigger than 1 and smaller than p-1 if x != 1 || y != -1 { return false } - z := cyclic.NewInt(0) - z.Div(upperBound, cyclic.NewInt(2)) - - symbol := cyclic.NewInt(0) - symbol.Exp(publicKey, z, prime) + symbol := group.NewInt(0) + group.Exp(publicKey, group.GetPSub1FactorCyclic(), symbol) // Symbol must be equal to 1 - if symbol.Cmp(lowerBound) == 0 { + if symbol.GetLargeInt().Cmp(lowerBound) == 0 { return true } else { return false diff --git a/diffieHellman/dhkx_test.go b/diffieHellman/dhkx_test.go index e6a5312a..7448d524 100644 --- a/diffieHellman/dhkx_test.go +++ b/diffieHellman/dhkx_test.go @@ -9,6 +9,7 @@ package diffieHellman import ( "encoding/hex" "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/large" "testing" ) @@ -30,14 +31,15 @@ func TestDHKX(t *testing.T) { "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AACAA68FFFFFFFFFFFFFFFF" - p := cyclic.NewInt(0) + p := large.NewInt(0) p.SetString(primeString, 16) - g := cyclic.NewInt(2) - s := cyclic.NewInt(0) - min := cyclic.NewInt(2) + g := large.NewInt(2) + q := large.NewInt(3) + s := large.NewInt(0) + min := large.NewInt(2) max := p rng := cyclic.NewRandom(min, max) - grp := cyclic.NewGroup(p, s, g, rng) + grp := cyclic.NewGroup(p, s, g, q, rng) testGroup := &grp // Creation of two different DH Key Pairs with valid parameters @@ -45,7 +47,7 @@ func TestDHKX(t *testing.T) { privKey2, pubKey2 := CreateDHKeyPair(testGroup) // Check if Public Key is within the group - if pubKey.Cmp(p) != -1 { + if !grp.Inside(pubKey) { t.Errorf("TestNewDHKeyPair(): Public Key is bigger than the prime!") } else { pass++ @@ -56,14 +58,14 @@ func TestDHKX(t *testing.T) { sessionKey2, _ := CreateDHSessionKey(pubKey2, privKey, testGroup) // Comparison of Two Session Keys (0 means they are equal) - if sessionKey1.Cmp(sessionKey2) != 0 { + if sessionKey1.GetLargeInt().Cmp(sessionKey2.GetLargeInt()) != 0 { t.Errorf("TestDHKX(): Error in CreateDHSessionKey() -> Session Keys do not match!") } else { pass++ } // Check if Session Key is within the prime group - if sessionKey1.Cmp(p) != -1 { + if !grp.Inside(sessionKey1) { t.Errorf("TestNewDHKeyPair(): Session Key is bigger than the prime!") } else { pass++ @@ -84,14 +86,15 @@ func Catch(fn string, t *testing.T) { // TestCreateDHKeyPair checks if panic is triggered when passing a number that is not a prime func TestCreateDHKeyPair(t *testing.T) { - p := cyclic.NewInt(4) - g := cyclic.NewInt(2) - s := cyclic.NewInt(0) - min := cyclic.NewInt(2) + p := large.NewInt(4) + g := large.NewInt(2) + q := large.NewInt(3) + s := large.NewInt(0) + min := large.NewInt(2) max := p rng := cyclic.NewRandom(min, max) - grp := cyclic.NewGroup(p, s, g, rng) + grp := cyclic.NewGroup(p, s, g, q, rng) testGroup := &grp defer Catch("TestCreateDHKeyPair():", t) @@ -114,15 +117,16 @@ func TestCheckPublicKey(t *testing.T) { "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AACAA68FFFFFFFFFFFFFFFF" - p := cyclic.NewInt(0) + p := large.NewInt(0) p.SetString(primeString, 16) - g := cyclic.NewInt(2) - s := cyclic.NewInt(0) - min := cyclic.NewInt(2) + g := large.NewInt(2) + q := large.NewInt(3) + s := large.NewInt(0) + min := large.NewInt(2) max := p rng := cyclic.NewRandom(min, max) - grp := cyclic.NewGroup(p, s, g, rng) + grp := cyclic.NewGroup(p, s, g, q, rng) testGroup := &grp // Creation of a DH Key Pair with valid parameters @@ -139,10 +143,10 @@ func TestCheckPublicKey(t *testing.T) { "0a34a6954938d29fd24a72aae3d4a0c2873ed4" a, _ := hex.DecodeString(randomNum) - x := cyclic.NewIntFromBytes(a) + x := grp.NewIntFromBytes(a) rightSymbol := CheckPublicKey(testGroup, pubKey) - fakeSymbol := CheckPublicKey(testGroup, cyclic.NewInt(1)) + fakeSymbol := CheckPublicKey(testGroup, grp.NewInt(1)) falseSymbol := CheckPublicKey(testGroup, x) if rightSymbol { @@ -183,15 +187,16 @@ func TestDHNodeKeys(t *testing.T) { "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AACAA68FFFFFFFFFFFFFFFF" - p := cyclic.NewInt(0) + p := large.NewInt(0) p.SetString(primeString, 16) - g := cyclic.NewInt(2) - s := cyclic.NewInt(0) - min := cyclic.NewInt(2) + g := large.NewInt(2) + q := large.NewInt(3) + s := large.NewInt(0) + min := large.NewInt(2) max := p rng := cyclic.NewRandom(min, max) - grp := cyclic.NewGroup(p, s, g, rng) + grp := cyclic.NewGroup(p, s, g, q, rng) testGroup := &grp // This is a map key(string) -> value (hex string) @@ -237,30 +242,30 @@ func TestDHNodeKeys(t *testing.T) { sk3, _ := hex.DecodeString(nodeDHPrivateKeys["3"]) // Keys between Node 1 & 2 - k12, _ := CreateDHSessionKey(cyclic.NewIntFromBytes(pk2), cyclic.NewIntFromBytes(sk1), testGroup) - k21, _ := CreateDHSessionKey(cyclic.NewIntFromBytes(pk1), cyclic.NewIntFromBytes(sk2), testGroup) + k12, _ := CreateDHSessionKey(grp.NewIntFromBytes(pk2), grp.NewIntFromBytes(sk1), testGroup) + k21, _ := CreateDHSessionKey(grp.NewIntFromBytes(pk1), grp.NewIntFromBytes(sk2), testGroup) // Keys between Node 1 & 3 - k13, _ := CreateDHSessionKey(cyclic.NewIntFromBytes(pk1), cyclic.NewIntFromBytes(sk3), testGroup) - k31, _ := CreateDHSessionKey(cyclic.NewIntFromBytes(pk3), cyclic.NewIntFromBytes(sk1), testGroup) + k13, _ := CreateDHSessionKey(grp.NewIntFromBytes(pk1), grp.NewIntFromBytes(sk3), testGroup) + k31, _ := CreateDHSessionKey(grp.NewIntFromBytes(pk3), grp.NewIntFromBytes(sk1), testGroup) // Keys between Node 2 & 3 - k23, _ := CreateDHSessionKey(cyclic.NewIntFromBytes(pk2), cyclic.NewIntFromBytes(sk3), testGroup) - k32, _ := CreateDHSessionKey(cyclic.NewIntFromBytes(pk3), cyclic.NewIntFromBytes(sk2), testGroup) + k23, _ := CreateDHSessionKey(grp.NewIntFromBytes(pk2), grp.NewIntFromBytes(sk3), testGroup) + k32, _ := CreateDHSessionKey(grp.NewIntFromBytes(pk3), grp.NewIntFromBytes(sk2), testGroup) - if k12.Cmp(k21) != 0 { + if k12.GetLargeInt().Cmp(k21.GetLargeInt()) != 0 { t.Errorf("Keys between Node 1 & 2 do not match!") } else { pass++ } - if k13.Cmp(k31) != 0 { + if k13.GetLargeInt().Cmp(k31.GetLargeInt()) != 0 { t.Errorf("Keys between Node 1 & 3 do not match!") } else { pass++ } - if k23.Cmp(k32) != 0 { + if k23.GetLargeInt().Cmp(k32.GetLargeInt()) != 0 { t.Errorf("Keys between Node 2 & 3 do not match!") } else { pass++ diff --git a/e2e/aes_test.go b/e2e/aes_test.go index 79db8ade..4e0672a9 100644 --- a/e2e/aes_test.go +++ b/e2e/aes_test.go @@ -11,6 +11,7 @@ import ( "encoding/hex" "gitlab.com/elixxir/crypto/csprng" "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/large" "testing" ) @@ -249,9 +250,9 @@ func TestEncDecAES_BadKey(t *testing.T) { // Test AES encryption with various arguments func TestEncAES_Args(t *testing.T) { keys := [][]byte{ - cyclic.NewIntFromString(TEST_KEY_256, 16).Bytes(), - cyclic.NewIntFromString(TEST_KEY_248, 16).Bytes(), - cyclic.NewIntFromString(TEST_KEY_128, 16).Bytes(), + large.NewIntFromString(TEST_KEY_256, 16).Bytes(), + large.NewIntFromString(TEST_KEY_248, 16).Bytes(), + large.NewIntFromString(TEST_KEY_128, 16).Bytes(), []byte(""), nil, } @@ -296,7 +297,7 @@ func TestDecAES_Args(t *testing.T) { } keys := [][]byte{ - cyclic.NewIntFromString(TEST_KEY_256, 16).Bytes(), + large.NewIntFromString(TEST_KEY_256, 16).Bytes(), []byte(""), nil} @@ -395,9 +396,9 @@ func TestAESEnc_Hash(t *testing.T) { // Loop test AES encryption/decryption with random inputs func TestEncDecAES_Random(t *testing.T) { - keyGen := cyclic.NewRandom(cyclic.NewInt(28), cyclic.NewInt(512)) - textGen := cyclic.NewRandom(cyclic.NewInt(1), cyclic.NewInt(4096)) - randSize := cyclic.NewInt(1) + keyGen := cyclic.NewRandom(large.NewInt(28), large.NewInt(512)) + textGen := cyclic.NewRandom(large.NewInt(1), large.NewInt(4096)) + randSize := large.NewInt(1) tests := NUM_TESTS pass := 0 diff --git a/e2e/dummykeygen.go b/e2e/dummykeygen.go index 48a40347..9d788943 100644 --- a/e2e/dummykeygen.go +++ b/e2e/dummykeygen.go @@ -31,7 +31,7 @@ func combinedHash(userA *id.User, userB *id.User, grp *cyclic.Group) *cyclic.Int expKey := hash.ExpandKey(h, grp, combKey) - return cyclic.NewIntFromBytes(expKey) + return grp.NewIntFromBytes(expKey) } diff --git a/e2e/dummykeygen_test.go b/e2e/dummykeygen_test.go index b082adfc..54bee94f 100644 --- a/e2e/dummykeygen_test.go +++ b/e2e/dummykeygen_test.go @@ -57,7 +57,7 @@ func TestDummyKeyGen_KeysMatch(t *testing.T) { l := len(keys1) for i, v := range keys1 { - if v.Cmp(keys2[l-i-1]) != 0 { + if v.GetLargeInt().Cmp(keys2[l-i-1].GetLargeInt()) != 0 { t.Errorf("Key mismatch") } } @@ -75,7 +75,7 @@ func TestDummyKeyGen_CombinedHashCommutes(t *testing.T) { res2 := combinedHash(userB, userA, &grp) - if res1.Cmp(res2) != 0 { + if res1.GetLargeInt().Cmp(res2.GetLargeInt()) != 0 { t.Errorf("Combined hash order should not matter") } } diff --git a/e2e/encrypt.go b/e2e/encrypt.go index 6d8bb2cd..2d2e05a2 100644 --- a/e2e/encrypt.go +++ b/e2e/encrypt.go @@ -24,7 +24,7 @@ func encrypt(g cyclic.Group, key *cyclic.Int, msg []byte, } // Modular multiply the key with the padded message - product := g.Mul(key, cyclic.NewIntFromBytes(encMsg), cyclic.NewInt(0)) + product := g.Mul(key, g.NewIntFromBytes(encMsg), g.NewInt(0)) // Return the result return product.LeftpadBytes(uint64(format.TOTAL_LEN)), nil @@ -34,10 +34,10 @@ func encrypt(g cyclic.Group, key *cyclic.Int, msg []byte, // the encrypted message under the passed group. func Decrypt(g cyclic.Group, key *cyclic.Int, encMsg []byte) ([]byte, error) { // Modular invert the key under the group - keyInv := g.Inverse(key, cyclic.NewInt(0)) + keyInv := g.Inverse(key, g.NewInt(0)) // Modular multiply the inverted key with the message - product := g.Mul(keyInv, cyclic.NewIntFromBytes(encMsg), cyclic.NewInt(0)) + product := g.Mul(keyInv, g.NewIntFromBytes(encMsg), g.NewInt(0)) // Remove the padding from the message unPadMsg, err := Unpad(product.LeftpadBytes(uint64(format.TOTAL_LEN))) diff --git a/e2e/encrypt_test.go b/e2e/encrypt_test.go index 5389bff7..97880270 100644 --- a/e2e/encrypt_test.go +++ b/e2e/encrypt_test.go @@ -4,6 +4,7 @@ import ( b64 "encoding/base64" "errors" "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/large" "math/rand" "os" "reflect" @@ -26,14 +27,15 @@ func TestMain(m *testing.M) { "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AACAA68FFFFFFFFFFFFFFFF" - p := cyclic.NewIntFromString(primeString, 16) - min := cyclic.NewInt(2) - max := cyclic.NewInt(0) - max.Mul(p, cyclic.NewInt(1000)) - seed := cyclic.NewInt(42) + p := large.NewIntFromString(primeString, 16) + min := large.NewInt(2) + max := large.NewInt(0) + max.Mul(p, large.NewInt(1000)) + seed := large.NewInt(42) rng := cyclic.NewRandom(min, max) - g := cyclic.NewInt(2) - grp = cyclic.NewGroup(p, seed, g, rng) + g := large.NewInt(2) + q := large.NewInt(3) + grp = cyclic.NewGroup(p, seed, g, q, rng) os.Exit(m.Run()) } @@ -42,7 +44,7 @@ func TestMain(m *testing.M) { // and check that it is the same when decrypting func TestEncryptDecrypt(t *testing.T) { // Create key and message - key := cyclic.NewInt(3) + key := grp.NewInt(3) msg := []byte{5, 12, 11} // Encrypt key @@ -64,7 +66,7 @@ func TestEncryptDecrypt(t *testing.T) { func TestEncryptDecrypt_LeadingZeroes(t *testing.T) { // Create key and message - key := cyclic.NewInt(3) + key := grp.NewInt(3) msg := []byte{0, 0, 11, 5, 255, 0} // Encrypt key @@ -111,7 +113,7 @@ func TestEncrypt_Consistency(t *testing.T) { rand.Seed(3102644637) msgBytes := make([]byte, 40) for i := 0; i < 16; i++ { - keys = append(keys, cyclic.NewInt(keyPrng.Int63())) + keys = append(keys, grp.NewInt(keyPrng.Int63())) rand.Read(msgBytes) msgs = append(msgs, msgBytes) } @@ -141,7 +143,7 @@ func TestEncrypt_ErrorOnLongMessage(t *testing.T) { msgBytes := make([]byte, 4000) rand.Read(msgBytes) msg := msgBytes - key := cyclic.NewInt(65) + key := grp.NewInt(65) // Encrypt key encMsg, err := Encrypt(grp, key, msg) @@ -162,7 +164,7 @@ func TestDecrypt_ErrorOnPaddingPrefix(t *testing.T) { msgBytes := make([]byte, 40) rand.Read(msgBytes) msg := msgBytes - key := cyclic.NewInt(65) + key := grp.NewInt(65) // Decrypt key dncMsg, err := Decrypt(grp, key, msg) diff --git a/e2e/keygen.go b/e2e/keygen.go index 673cbfca..4688cd53 100644 --- a/e2e/keygen.go +++ b/e2e/keygen.go @@ -17,5 +17,5 @@ import ( // TODO: Implement the proper keygen once defined func Keygen(g *cyclic.Group, salt, basekey *cyclic.Int) *cyclic.Int { jww.WARN.Printf("End to End Encryption is not fully implemented, Keygen is not secure") - return cyclic.NewInt(1) + return g.NewInt(1) } \ No newline at end of file diff --git a/e2e/keygen_test.go b/e2e/keygen_test.go index 8c20f3de..b616160a 100644 --- a/e2e/keygen_test.go +++ b/e2e/keygen_test.go @@ -8,6 +8,7 @@ package e2e import ( "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/large" "testing" ) @@ -24,21 +25,22 @@ func TestKeygen(t *testing.T) { "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AACAA68FFFFFFFFFFFFFFFF" - p := cyclic.NewIntFromString(primeString, 16) - min := cyclic.NewInt(2) - max := cyclic.NewInt(0) - max.Mul(p, cyclic.NewInt(1000)) - seed := cyclic.NewInt(42) + p := large.NewIntFromString(primeString, 16) + min := large.NewInt(2) + max := large.NewInt(0) + max.Mul(p, large.NewInt(1000)) + seed := large.NewInt(42) rng := cyclic.NewRandom(min, max) - g := cyclic.NewInt(2) - grp := cyclic.NewGroup(p, seed, g, rng) + g := large.NewInt(2) + q := large.NewInt(3) + grp := cyclic.NewGroup(p, seed, g, q, rng) testGroup := &grp - salt := cyclic.NewInt(1) - basekey := cyclic.NewInt(1) + salt := grp.NewInt(1) + basekey := grp.NewInt(1) key := Keygen(testGroup, salt, basekey) - if key.Cmp(salt) != 0 { + if key.GetLargeInt().Cmp(salt.GetLargeInt()) != 0 { t.Errorf("Dummy keygen should return 1") } } \ No newline at end of file diff --git a/e2e/keys.go b/e2e/keys.go index 658aba67..853457fc 100644 --- a/e2e/keys.go +++ b/e2e/keys.go @@ -19,7 +19,7 @@ func deriveSingleKey(h hash.Hash, g *cyclic.Group, data []byte, id uint) *cyclic b, _ := hash2.NewCMixHash() b.Write(data) b.Write(idBytes[:n]) - return cyclic.NewIntFromBytes(hash2.ExpandKey(h, g, b.Sum(nil))) + return g.NewIntFromBytes(hash2.ExpandKey(h, g, b.Sum(nil))) } // This function derives multiple keys using the specified hash function diff --git a/e2e/keys_test.go b/e2e/keys_test.go index 2b9815ec..dd180528 100644 --- a/e2e/keys_test.go +++ b/e2e/keys_test.go @@ -35,13 +35,14 @@ type testFun func(a *cyclic.Group, b *cyclic.Int, c *id.User, d uint) []*cyclic. // Test for consistency with hardcoded values func TestDeriveSingleKey(t *testing.T) { userID := id.NewUserFromUint(TEST_USERID, t) - key := cyclic.NewIntFromString(TEST_DHKEY, 16) + key := grp.NewIntFromString(TEST_DHKEY, 16) data := append([]byte{}, key.Bytes()...) data = append(data, userID.Bytes()...) result := deriveSingleKey(sha256.New(), &grp, data, 0) - expected := cyclic.NewIntFromString(EXPECTED_KEY, 16) - if result.Cmp(expected) != 0 { - t.Errorf("Generated Key %v doesn't match expected %v", result.Text(16), EXPECTED_KEY) + expected := grp.NewIntFromString(EXPECTED_KEY, 16) + if result.GetLargeInt().Cmp(expected.GetLargeInt()) != 0 { + t.Errorf("Generated Key %v doesn't match expected %v", + result.GetLargeInt().Text(16), EXPECTED_KEY) } } @@ -49,7 +50,7 @@ func TestDeriveSingleKey(t *testing.T) { func TestDeriveKeys_DeriveEmergencyKeys(t *testing.T) { userID := id.NewUserFromUint(TEST_USERID, t) partnerID := id.NewUserFromUint(TEST_PARTNERID, t) - key := cyclic.NewIntFromString(TEST_DHKEY, 16) + key := grp.NewIntFromString(TEST_DHKEY, 16) nkeys := []uint{10000, 0} total := func(a []uint) (s int) { @@ -107,7 +108,7 @@ func TestDeriveKeys_DeriveEmergencyKeys(t *testing.T) { // Test both functions with same arguments to explicitly show they produce different keys func TestDeriveKeys_DeriveEmergencyKeys_Differ(t *testing.T) { userID := id.NewUserFromUint(TEST_USERID, t) - key := cyclic.NewIntFromString(TEST_DHKEY, 16) + key := grp.NewIntFromString(TEST_DHKEY, 16) nkeys := uint(100) fut := []testFun{DeriveKeys, DeriveEmergencyKeys} var genKeys = make([][]*cyclic.Int, len(fut)) @@ -125,7 +126,7 @@ func TestDeriveKeys_DeriveEmergencyKeys_Differ(t *testing.T) { // Directly compare each key for i:=0; i<int(nkeys); i++ { - if genKeys[0][i].Cmp(genKeys[1][i]) == 0 { + if genKeys[0][i].GetLargeInt().Cmp(genKeys[1][i].GetLargeInt()) == 0 { t.Errorf("Keys are the same when generated with different functions") } } diff --git a/e2e/ttl.go b/e2e/ttl.go index 3a48e7dd..c67a255a 100644 --- a/e2e/ttl.go +++ b/e2e/ttl.go @@ -2,8 +2,8 @@ package e2e import ( jww "github.com/spf13/jwalterweatherman" - "gitlab.com/elixxir/crypto/cyclic" "gitlab.com/elixxir/crypto/hash" + "gitlab.com/elixxir/crypto/large" "math" ) @@ -14,7 +14,7 @@ type TTLParams struct { // Generates Key TTL and num keys given a key and a range. // Returns fair key TTL (num keys before retrigger happens) and num keys (usage capacity) -func GenerateKeyTTL(key *cyclic.Int, min uint16, max uint16, params TTLParams) (uint16, uint32) { +func GenerateKeyTTL(key large.Int, min uint16, max uint16, params TTLParams) (uint16, uint32) { h, err := hash.NewCMixHash() @@ -43,9 +43,9 @@ func computeTTL(hashed []byte, min uint16, max uint16) uint16 { jww.ERROR.Panicf("Min must be greater than or equal to max in computeTTL") } - zero := cyclic.NewInt(0) - keyHash := cyclic.NewIntFromBytes(hashed) - mod := cyclic.NewInt(int64(max - min)) + zero := large.NewInt(0) + keyHash := large.NewIntFromBytes(hashed) + mod := large.NewInt(int64(max - min)) // The formula used is: ttl = (keyHash % mod) + min | s.t. mod = (max - min) ttl := uint16(zero.Mod(keyHash, mod).Int64()) + min diff --git a/e2e/ttl_test.go b/e2e/ttl_test.go index 03fe6139..a51d8455 100644 --- a/e2e/ttl_test.go +++ b/e2e/ttl_test.go @@ -7,7 +7,7 @@ package e2e import ( - "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/large" "testing" ) @@ -20,7 +20,7 @@ func TestGenerateKeyTTL_PanicOnMinMaxEq(t *testing.T) { } }() - sessionKey := *cyclic.NewInt(23) + sessionKey := large.NewInt(23) min := uint16(200) max := uint16(200) @@ -30,7 +30,7 @@ func TestGenerateKeyTTL_PanicOnMinMaxEq(t *testing.T) { 400, } - GenerateKeyTTL(&sessionKey, min, max, params) + GenerateKeyTTL(sessionKey, min, max, params) } // Verify GenerateKeyTTL panics when min is greater than max @@ -42,7 +42,7 @@ func TestGenerateKeyTTL_PanicOnMinGreaterThanMax(t *testing.T) { } }() - sessionKey := *cyclic.NewInt(23) + sessionKey := large.NewInt(23) min := uint16(2000) max := uint16(200) @@ -52,13 +52,13 @@ func TestGenerateKeyTTL_PanicOnMinGreaterThanMax(t *testing.T) { 400, } - GenerateKeyTTL(&sessionKey, min, max, params) + GenerateKeyTTL(sessionKey, min, max, params) } // Verify GenerateKeyTTL generated expected TTL and NumKeys func TestGenerateKeyTTL_ValidTTL(t *testing.T) { - sessionKey := *cyclic.NewInt(23) + sessionKey := large.NewInt(23) min := uint16(2000) max := uint16(20000) @@ -68,7 +68,7 @@ func TestGenerateKeyTTL_ValidTTL(t *testing.T) { 400, } - ttl, numKeys := GenerateKeyTTL(&sessionKey, min, max, params) + ttl, numKeys := GenerateKeyTTL(sessionKey, min, max, params) expectedTTL := uint16(8115) expectedNumKeys := uint32(9738) @@ -109,7 +109,7 @@ func TestGenerateKeyTTL_KeysPerTimeEqZeroShouldPanic(t *testing.T) { } }() - sessionKey := *cyclic.NewInt(23) + sessionKey := large.NewInt(23) min := uint16(20) max := uint16(200) @@ -119,7 +119,7 @@ func TestGenerateKeyTTL_KeysPerTimeEqZeroShouldPanic(t *testing.T) { 400, } - GenerateKeyTTL(&sessionKey, min, max, params) + GenerateKeyTTL(sessionKey, min, max, params) } // TTL scalar should never be negative @@ -130,7 +130,7 @@ func TestGenerateKeyTTL_KeysPerTimeIsLessThanZeroShouldPanic(t *testing.T) { } }() - sessionKey := *cyclic.NewInt(23) + sessionKey := large.NewInt(23) min := uint16(20) max := uint16(200) @@ -140,5 +140,5 @@ func TestGenerateKeyTTL_KeysPerTimeIsLessThanZeroShouldPanic(t *testing.T) { 400, } - GenerateKeyTTL(&sessionKey, min, max, params) + GenerateKeyTTL(sessionKey, min, max, params) } diff --git a/hash/keys.go b/hash/keys.go index 47a1c88f..f19d8389 100644 --- a/hash/keys.go +++ b/hash/keys.go @@ -22,15 +22,15 @@ func ExpandKey(h hash.Hash, g *cyclic.Group, key []byte) []byte { return h } keyGen := hkdf.Expand(foo, key, nil) - keyInt := cyclic.NewInt(0) - expandedKey := make([]byte, g.GetP(nil).BitLen()>>3) + keyInt := g.NewInt(0) + expandedKey := make([]byte, g.GetP().BitLen()>>3) // Make sure generated key is in the group for !g.Inside(keyInt) { size, err := keyGen.Read(expandedKey) if err != nil || size != len(expandedKey) { jww.FATAL.Panicf("Could not expand key: %v", err.Error()) } - keyInt.SetBytes(expandedKey) + keyInt.GetLargeInt().SetBytes(expandedKey) } return expandedKey } diff --git a/hash/keys_test.go b/hash/keys_test.go index 2e504534..4a483a6f 100644 --- a/hash/keys_test.go +++ b/hash/keys_test.go @@ -10,6 +10,7 @@ import ( "crypto/sha512" "encoding/hex" "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/large" "testing" ) @@ -25,13 +26,14 @@ var primeString = "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AACAA68FFFFFFFFFFFFFFFF" -var p = cyclic.NewIntFromString(primeString, 16) -var min = cyclic.NewInt(2) -var max = cyclic.NewInt(0) -var seed = cyclic.NewInt(42) -var rng = cyclic.NewRandom(min, max.Mul(p, cyclic.NewInt(1000))) -var g = cyclic.NewInt(2) -var grp = cyclic.NewGroup(p, seed, g, rng) +var p = large.NewIntFromString(primeString, 16) +var min = large.NewInt(2) +var max = large.NewInt(0) +var seed = large.NewInt(42) +var rng = cyclic.NewRandom(min, max.Mul(p, large.NewInt(1000))) +var g = large.NewInt(2) +var q = large.NewInt(3) +var grp = cyclic.NewGroup(p, seed, g, q, rng) //TestExpandKey verifies ExpandKey with two different hashes func TestExpandKey(t *testing.T) { diff --git a/messaging/key.go b/messaging/key.go index c70a83d4..8cf69ff7 100644 --- a/messaging/key.go +++ b/messaging/key.go @@ -54,7 +54,7 @@ func NewDecryptionKey(salt []byte, baseKey *cyclic.Int, group *cyclic.Group) *cy // Use SHA512 z := hash.ExpandKey(sha512.New(), group, y) - r := cyclic.NewIntFromBytes(z) + r := group.NewIntFromBytes(z) return r } diff --git a/messaging/key_test.go b/messaging/key_test.go index 65955ad8..709328b0 100644 --- a/messaging/key_test.go +++ b/messaging/key_test.go @@ -8,6 +8,7 @@ package messaging import ( "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/large" "testing" ) @@ -33,12 +34,12 @@ var primeStrng = "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" + "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" + "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199" + "FFFFFFFFFFFFFFFF" -var prime = cyclic.NewIntFromString(primeStrng, 16) -var rng = cyclic.NewRandom(cyclic.NewInt(0), - cyclic.NewIntFromString(primeStrng, 16)) -var grp = cyclic.NewGroup(prime, cyclic.NewInt(5), cyclic.NewInt(4), +var prime = large.NewIntFromString(primeStrng, 16) +var rng = cyclic.NewRandom(large.NewInt(0), + large.NewIntFromString(primeStrng, 16)) +var grp = cyclic.NewGroup(prime, large.NewInt(5), large.NewInt(4), large.NewInt(3), rng) -var baseKey = cyclic.NewIntFromString("a906df88f30d6afbfa6165a50cc9e208d16b34e70b367068dc5d6bd6e155b2c3", 16) +var baseKey = grp.NewIntFromString("a906df88f30d6afbfa6165a50cc9e208d16b34e70b367068dc5d6bd6e155b2c3", 16) var salt = []byte("fdecfa52a8ad1688dbfa7d16df74ebf27e535903c469cefc007ebbe1ee895064") var expectStr = "2e4c99e14e0b1cd18c08467c395a4d5c0eb594507595041a5cfa83eb2f5791f3" + "db46c933040d4c9862b91539fb8bc75e0b84ed07dd6a760dda6baec8c5f3f119" + @@ -60,29 +61,33 @@ var expectStr = "2e4c99e14e0b1cd18c08467c395a4d5c0eb594507595041a5cfa83eb2f5791f // Test for functionality of NewDecryptionKey using pre-canned values func TestNewDecryptionKey(t *testing.T) { k := NewDecryptionKey(salt, baseKey, &grp) - expected := cyclic.NewIntFromString(expectStr, 16) + expected := grp.NewIntFromString(expectStr, 16) if k == nil { t.Errorf("Error should have been triggered!") } - if k.Cmp(expected) != 0 { - t.Errorf("Expected: %s, Got: %s", expected.Text(16), k.TextVerbose(16, 0)) + if k.GetLargeInt().Cmp(expected.GetLargeInt()) != 0 { + t.Errorf("Expected: %s, Got: %s", + expected.GetLargeInt().Text(16), + k.GetLargeInt().TextVerbose(16, 0)) } } // Test for functionality of NewEncryptionKey using pre-canned values func TestNewEncryptionKey(t *testing.T) { k := NewEncryptionKey(salt, baseKey, &grp) - expected := cyclic.NewIntFromString(expectStr, 16) + expected := grp.NewIntFromString(expectStr, 16) grp.Inverse(expected, expected) if k == nil { t.Errorf("Error should have been triggered!") } - if k.Cmp(expected) != 0 { - t.Errorf("Expected: %s, Got: %s", expected.Text(16), k.TextVerbose(16, 0)) + if k.GetLargeInt().Cmp(expected.GetLargeInt()) != 0 { + t.Errorf("Expected: %s, Got: %s", + expected.GetLargeInt().Text(16), + k.GetLargeInt().TextVerbose(16, 0)) } } @@ -97,12 +102,12 @@ func makebaseKeys(size int) []*cyclic.Int { // Test that multiple baseKeys return a slice of same size with correct results func TestNewDecryptionKeys(t *testing.T) { keys := NewDecryptionKeys(salt, makebaseKeys(10), &grp) - expected := cyclic.NewIntFromString(expectStr, 16) + expected := grp.NewIntFromString(expectStr, 16) if len(keys) != 10 { t.Errorf("Bad length: expected 10, got %d", len(keys)) } for i := range keys { - if keys[i].Cmp(expected) != 0 { + if keys[i].GetLargeInt().Cmp(expected.GetLargeInt()) != 0 { t.Errorf("Generated key incorrect!") } } @@ -119,13 +124,13 @@ func TestNewDecryptionKeysEmpty(t *testing.T) { // Test that multiple baseKeys return a slice of same size with correct results func TestNewEncryptionKeys(t *testing.T) { keys := NewEncryptionKeys(salt, makebaseKeys(10), &grp) - expected := cyclic.NewIntFromString(expectStr, 16) + expected := grp.NewIntFromString(expectStr, 16) grp.Inverse(expected, expected) if len(keys) != 10 { t.Errorf("Bad length: expected 10, got %d", len(keys)) } for i := range keys { - if keys[i].Cmp(expected) != 0 { + if keys[i].GetLargeInt().Cmp(expected.GetLargeInt()) != 0 { t.Errorf("Generated key incorrect!") } } diff --git a/registration/keygen.go b/registration/keygen.go index bc10eeb0..661f2ff5 100644 --- a/registration/keygen.go +++ b/registration/keygen.go @@ -22,10 +22,10 @@ import ( func GenerateBaseKey(g *cyclic.Group, peerPubKey *signature.DSAPublicKey, ownPrivKey *signature.DSAPrivateKey, h hash.Hash) *cyclic.Int { - pubKey := peerPubKey.GetKey() - privKey := ownPrivKey.GetKey() + pubKey := g.NewIntFromLargeInt(peerPubKey.GetKey()) + privKey := g.NewIntFromLargeInt(ownPrivKey.GetKey()) sessionKey, _ := diffieHellman.CreateDHSessionKey(pubKey, privKey, g) h.Write(sessionKey.Bytes()) - return cyclic.NewIntFromBytes(h.Sum(nil)) + return g.NewIntFromBytes(h.Sum(nil)) } diff --git a/registration/keygen_test.go b/registration/keygen_test.go index 48601187..bcdfa88f 100644 --- a/registration/keygen_test.go +++ b/registration/keygen_test.go @@ -11,6 +11,7 @@ import ( "gitlab.com/elixxir/crypto/csprng" "gitlab.com/elixxir/crypto/cyclic" "gitlab.com/elixxir/crypto/hash" + "gitlab.com/elixxir/crypto/large" "gitlab.com/elixxir/crypto/signature" "testing" ) @@ -25,11 +26,14 @@ var grp cyclic.Group func TestGenerateBaseKey(t *testing.T) { dsaParams := signature.GetDefaultDSAParams() p := dsaParams.GetP() - min := cyclic.NewInt(2) - max := cyclic.NewInt(0) - max.Mul(p, cyclic.NewInt(1000)) - seed := cyclic.NewInt(42) - grp = cyclic.NewGroup(p, seed, dsaParams.GetG(), cyclic.NewRandom(min, max)) + min := large.NewInt(2) + max := large.NewInt(0) + max.Mul(p, large.NewInt(1000)) + seed := large.NewInt(42) + grp = cyclic.NewGroup(p, seed, + dsaParams.GetG(), + dsaParams.GetQ(), + cyclic.NewRandom(min, max)) rng := csprng.NewSystemRNG() ownPrivKey := dsaParams.PrivateKeyGen(rng) @@ -44,7 +48,7 @@ func TestGenerateBaseKey(t *testing.T) { b.Reset() peerBaseTKey := GenerateBaseKey(&grp, ownPubKey, peerPrivKey, b) - if ownBaseTKey.Cmp(peerBaseTKey) != 0 { + if ownBaseTKey.GetLargeInt().Cmp(peerBaseTKey.GetLargeInt()) != 0 { t.Errorf("Generated Base Key using blake2b is different between own and peer") t.Errorf("own: %x", ownBaseTKey.Bytes()) t.Errorf("peer: %x", peerBaseTKey.Bytes()) @@ -56,13 +60,13 @@ func TestGenerateBaseKey(t *testing.T) { h.Reset() peerBaseRKey := GenerateBaseKey(&grp, ownPubKey, peerPrivKey, h) - if ownBaseRKey.Cmp(peerBaseRKey) != 0 { + if ownBaseRKey.GetLargeInt().Cmp(peerBaseRKey.GetLargeInt()) != 0 { t.Errorf("Generated Base Key using sha256 is different between own and peer") t.Errorf("own: %x", ownBaseRKey.Bytes()) t.Errorf("peer: %x", peerBaseRKey.Bytes()) } - if ownBaseTKey.Cmp(ownBaseRKey) == 0 { + if ownBaseTKey.GetLargeInt().Cmp(ownBaseRKey.GetLargeInt()) == 0 { t.Errorf("Generated Base Keys are the same") } } diff --git a/registration/uidgen.go b/registration/uidgen.go index 9cc07a9b..d81a9372 100644 --- a/registration/uidgen.go +++ b/registration/uidgen.go @@ -1,6 +1,7 @@ package registration import ( + "fmt" "gitlab.com/elixxir/crypto/signature" "gitlab.com/elixxir/crypto/hash" "gitlab.com/elixxir/primitives/id" @@ -15,6 +16,7 @@ func GenUserID(pubKey *signature.DSAPublicKey, salt []byte) *id.User { jww.ERROR.Panicf("PubKey and/or Salt are nil") } pubBytes := pubKey.GetKey().Bytes() + fmt.Printf("%x\n", pubBytes) if len(pubBytes) == 0 || len(salt) == 0 { jww.ERROR.Panicf("PubKey and/or Salt are empty") } diff --git a/registration/uidgen_test.go b/registration/uidgen_test.go index 494d42e8..24ab0590 100644 --- a/registration/uidgen_test.go +++ b/registration/uidgen_test.go @@ -4,6 +4,7 @@ import ( "crypto/rand" "encoding/hex" "gitlab.com/elixxir/crypto/csprng" + "gitlab.com/elixxir/crypto/large" "gitlab.com/elixxir/crypto/signature" "testing" ) @@ -54,12 +55,11 @@ func TestGenUserID_NilSalt(t *testing.T) { // Test GenUserID panics with empty byte slice public key func TestGenUserID_EmptyKey(t *testing.T) { params := signature.NewDSAParams(rand.Reader, signature.L2048N256) - privKey := params.PrivateKeyGen(rand.Reader) - pubKey := privKey.PublicKeyGen() + pubKey := signature.ReconstructPublicKey( + params, + large.NewIntFromBytes([]byte(""))) salt := []byte("0123456789ABCDEF0123456789ABCDEF") - pubKey.GetKey().SetBytes([]byte("")) - defer func() { if r := recover(); r == nil { t.Errorf("UserID Generation should panic on empty key") diff --git a/signature/dsa.go b/signature/dsa.go index fc477a27..e7f2aa8e 100644 --- a/signature/dsa.go +++ b/signature/dsa.go @@ -3,7 +3,7 @@ package signature import ( "crypto/dsa" jww "github.com/spf13/jwalterweatherman" - "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/large" "io" ) @@ -39,15 +39,15 @@ const ( ) func GetDefaultDSAParams() *DSAParameters { - bigP := cyclic.NewIntFromString(DSA_GROUP_P, 16) - bigQ := cyclic.NewIntFromString(DSA_GROUP_Q, 16) - bigG := cyclic.NewIntFromString(DSA_GROUP_G, 16) + bigP := large.NewIntFromString(DSA_GROUP_P, 16) + bigQ := large.NewIntFromString(DSA_GROUP_Q, 16) + bigG := large.NewIntFromString(DSA_GROUP_G, 16) jww.WARN.Printf("Using hardcoded DSA Params, should be removed in the future!") return CustomDSAParams(bigP, bigQ, bigG) } -func CustomDSAParams(P, Q, G *cyclic.Int) *DSAParameters { - return &DSAParameters{dsa.Parameters{P: P.GetBigInt(), Q: Q.GetBigInt(), G: G.GetBigInt()}} +func CustomDSAParams(P, Q, G large.Int) *DSAParameters { + return &DSAParameters{dsa.Parameters{P: P.BigInt(), Q: Q.BigInt(), G: G.BigInt()}} } func NewDSAParams(rng io.Reader, pSizes ParameterSizes) *DSAParameters { @@ -81,16 +81,16 @@ func (p *DSAParameters) PrivateKeyGen(rng io.Reader) *DSAPrivateKey { return &pk } -func (p *DSAParameters) GetG() *cyclic.Int { - return cyclic.NewIntFromBigInt(p.params.G) +func (p *DSAParameters) GetG() large.Int { + return large.NewIntFromBigInt(p.params.G) } -func (p *DSAParameters) GetP() *cyclic.Int { - return cyclic.NewIntFromBigInt(p.params.P) +func (p *DSAParameters) GetP() large.Int { + return large.NewIntFromBigInt(p.params.P) } -func (p *DSAParameters) GetQ() *cyclic.Int { - return cyclic.NewIntFromBigInt(p.params.Q) +func (p *DSAParameters) GetQ() large.Int { + return large.NewIntFromBigInt(p.params.Q) } func (k *DSAPublicKey) GetParams() *DSAParameters { @@ -109,33 +109,37 @@ func (p *DSAPrivateKey) Sign(data []byte, rng io.Reader) (*DSASignature, error) r, s, err := dsa.Sign(rng, &p.key, data) - rCyclic := cyclic.NewIntFromBigInt(r) - sCyclic := cyclic.NewIntFromBigInt(s) + rval := large.Int(nil) + sval := large.Int(nil) - return &DSASignature{rCyclic, sCyclic}, err + if err == nil { + rval = large.NewIntFromBigInt(r) + sval = large.NewIntFromBigInt(s) + } + return &DSASignature{rval, sval}, err } -func (p *DSAPrivateKey) GetKey() *cyclic.Int { - return cyclic.NewIntFromBigInt(p.key.X) +func (p *DSAPrivateKey) GetKey() large.Int { + return large.NewIntFromBigInt(p.key.X) } -func (dsaKey *DSAPrivateKey) GetPublicKey() *cyclic.Int { - return cyclic.NewIntFromBigInt(dsaKey.key.PublicKey.Y) +func (dsaKey *DSAPrivateKey) GetPublicKey() large.Int { + return large.NewIntFromBigInt(dsaKey.key.PublicKey.Y) } -func (dsaKey *DSAPrivateKey) GetParams() (p, q, g *cyclic.Int) { - p = cyclic.NewIntFromBigInt(dsaKey.key.P) - q = cyclic.NewIntFromBigInt(dsaKey.key.Q) - g = cyclic.NewIntFromBigInt(dsaKey.key.G) +func (dsaKey *DSAPrivateKey) GetParams() (p, q, g large.Int) { + p = large.NewIntFromBigInt(dsaKey.key.P) + q = large.NewIntFromBigInt(dsaKey.key.Q) + g = large.NewIntFromBigInt(dsaKey.key.G) return p, q, g } -func ReconstructPrivateKey(publicKey *DSAPublicKey, privateKey *cyclic.Int) *DSAPrivateKey { +func ReconstructPrivateKey(publicKey *DSAPublicKey, privateKey large.Int) *DSAPrivateKey { pk := &DSAPrivateKey{} pk.key.PublicKey = publicKey.key - pk.key.X = privateKey.GetBigInt() + pk.key.X = privateKey.BigInt() return pk } @@ -144,25 +148,25 @@ type DSAPublicKey struct { key dsa.PublicKey } -func ReconstructPublicKey(p *DSAParameters, key *cyclic.Int) *DSAPublicKey { +func ReconstructPublicKey(p *DSAParameters, key large.Int) *DSAPublicKey { pk := &DSAPublicKey{} pk.key.Parameters = p.params - pk.key.Y = key.GetBigInt() + pk.key.Y = key.BigInt() return pk } func (p *DSAPublicKey) Verify(hash []byte, sig DSASignature) bool { - return dsa.Verify(&p.key, hash, sig.R.GetBigInt(), sig.S.GetBigInt()) + return dsa.Verify(&p.key, hash, sig.R.BigInt(), sig.S.BigInt()) } -func (p *DSAPublicKey) GetKey() *cyclic.Int { - return cyclic.NewIntFromBigInt(p.key.Y) +func (p *DSAPublicKey) GetKey() large.Int { + return large.NewIntFromBigInt(p.key.Y) } type DSASignature struct { - R *cyclic.Int - S *cyclic.Int + R large.Int + S large.Int } // TODO: Add tests for Gob encode/decode and uncomment impl. diff --git a/signature/dsa_test.go b/signature/dsa_test.go index 0f08c7ae..20f60651 100644 --- a/signature/dsa_test.go +++ b/signature/dsa_test.go @@ -9,7 +9,7 @@ package signature import ( cryptoRand "crypto/rand" "errors" - "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/large" "math/big" "math/rand" "testing" @@ -21,9 +21,9 @@ func TestCustomDSAParams_Accessors(t *testing.T) { var pExpected, qExpected, gExpected int64 = 1, 1, 1 - p := cyclic.NewInt(pExpected) - q := cyclic.NewInt(qExpected) - g := cyclic.NewInt(gExpected) + p := large.NewInt(pExpected) + q := large.NewInt(qExpected) + g := large.NewInt(gExpected) dsaParams := CustomDSAParams(p, q, g) @@ -43,9 +43,9 @@ func TestCustomDSAParams_Accessors(t *testing.T) { pExpected, qExpected, gExpected = 1, 2, 3 - p = cyclic.NewInt(pExpected) - q = cyclic.NewInt(qExpected) - g = cyclic.NewInt(gExpected) + p = large.NewInt(pExpected) + q = large.NewInt(qExpected) + g = large.NewInt(gExpected) dsaParams = CustomDSAParams(p, q, g) @@ -65,9 +65,9 @@ func TestCustomDSAParams_Accessors(t *testing.T) { pExpected, qExpected, gExpected = 123, 456, 789 - p = cyclic.NewInt(pExpected) - q = cyclic.NewInt(qExpected) - g = cyclic.NewInt(gExpected) + p = large.NewInt(pExpected) + q = large.NewInt(qExpected) + g = large.NewInt(gExpected) dsaParams = CustomDSAParams(p, q, g) @@ -207,7 +207,7 @@ func TestDSAPublicKeyGetters(t *testing.T) { params := NewDSAParams(r, L1024N160) - key := cyclic.NewInt(500) + key := large.NewInt(500) pubKey := ReconstructPublicKey(params, key) @@ -272,15 +272,15 @@ func TestPublicKeyGen_Consistent(t *testing.T) { } -// Test helper which converts a hex string into a cyclic int -func fromHex(s string) *cyclic.Int { +// Test helper which converts a hex string into a large int +func fromHex(s string) large.Int { result, ok := new(big.Int).SetString(s, 16) if !ok { panic(s) } - return cyclic.NewIntFromBigInt(result) + return large.NewIntFromBigInt(result) } // Sign and verify that has param, pub & priv key values ported from go sdk dsa impl. tests. @@ -386,12 +386,12 @@ func testParameterGeneration(t *testing.T, sizes ParameterSizes, L, N int) { one := new(big.Int) one.SetInt64(1) - pm1 := new(big.Int).Sub(params.GetP().GetBigInt(), one) - quo, rem := new(big.Int).DivMod(pm1, params.GetQ().GetBigInt(), new(big.Int)) + pm1 := new(big.Int).Sub(params.GetP().BigInt(), one) + quo, rem := new(big.Int).DivMod(pm1, params.GetQ().BigInt(), new(big.Int)) if rem.Sign() != 0 { t.Errorf("%d: p-1 mod q != 0", int(sizes)) } - x := new(big.Int).Exp(params.GetG().GetBigInt(), quo, params.GetP().GetBigInt()) + x := new(big.Int).Exp(params.GetG().BigInt(), quo, params.GetP().BigInt()) if x.Cmp(one) == 0 { t.Errorf("%d: invalid generator", int(sizes)) } -- GitLab