diff --git a/cmix/key.go b/cmix/key.go index 076e15cb15e202f0d8b0b5ef6bde44b4f8984a04..eba5ab835a9c5882eef81875054ca99b19c42bbb 100644 --- a/cmix/key.go +++ b/cmix/key.go @@ -53,7 +53,7 @@ func NewDecryptionKey(salt []byte, baseKey *cyclic.Int, group *cyclic.Group) *cy // Expand Key // Use SHA512 z := hash.ExpandKey(sha512.New(), group, y) - + //FIXME: dont crash if the hash happens to be all zeroes r := group.NewIntFromBytes(z) return r diff --git a/cyclic/buffer_test.go b/cyclic/buffer_test.go index 8312996fc488352acb13fe27f0144319c76d22d5..24fc7eb94e2ee3503ccf870155464c8bc2e72100 100644 --- a/cyclic/buffer_test.go +++ b/cyclic/buffer_test.go @@ -25,12 +25,12 @@ func TestIntBuffer_Set(t *testing.T) { // Ensure that overwriting a large int in the buffer doesn't overwrite any of // the things it shouldn't overwrite shouldStillBePSub1 := buf.Get(0) - if shouldStillBePSub1.Cmp(grp.NewInt(1000000010101111110)) != 0 { - t.Error("Setting the buffer element also set another element of the" + - " buffer (probably aliased)") + if shouldStillBePSub1.Cmp(grp.NewInt(1000000010101111110)) != 0 { + t.Error("Setting the buffer element also set another element of the" + + " buffer (probably aliased)") } shouldAlsoStillBePSub1 := grp.GetPSub1() - if shouldAlsoStillBePSub1.Cmp(large.NewInt(1000000010101111110)) != 0 { + if shouldAlsoStillBePSub1.value.Cmp(large.NewInt(1000000010101111110)) != 0 { t.Error("Setting the buffer element also set PSub1 (probably aliased)") } diff --git a/cyclic/group.go b/cyclic/group.go index 4eea77e3da39becb91d9fb69c84e00f7590df3cb..8a01702ccb545557803b8cc7aabf02e302885341 100644 --- a/cyclic/group.go +++ b/cyclic/group.go @@ -46,10 +46,10 @@ func NewGroup(p, g, q *large.Int) Group { value := large.NewIntFromBytes(hashVal) return Group{ prime: p, - psub1: large.NewInt(0).Sub(p, large.NewInt(1)), - psub2: large.NewInt(0).Sub(p, large.NewInt(2)), - psub3: large.NewInt(0).Sub(p, large.NewInt(3)), - psub1factor: large.NewInt(0).RightShift(large.NewInt(0).Sub(p, large.NewInt(1)), 1), + psub1: large.NewInt(1).Sub(p, large.NewInt(1)), + psub2: large.NewInt(1).Sub(p, large.NewInt(2)), + psub3: large.NewInt(1).Sub(p, large.NewInt(3)), + psub1factor: large.NewInt(1).RightShift(large.NewInt(1).Sub(p, large.NewInt(1)), 1), zero: large.NewInt(0), one: large.NewInt(1), @@ -78,12 +78,18 @@ func (g *Group) NewIntBuffer(length uint32) *IntBuffer { func (g *Group) NewInt(x int64) *Int { val := large.NewInt(x) n := &Int{value: val, fingerprint: g.fingerprint} + if !g.Inside(n.value) { + panic("NewInt: Attempted creation of cyclic outside of group") + } return n } // Create a new cyclicInt in the group from a large.Int value func (g *Group) NewIntFromLargeInt(x *large.Int) *Int { n := &Int{value: x, fingerprint: g.fingerprint} + if !g.Inside(n.value) { + panic("NewIntFromLargeInt: Attempted creation of cyclic outside of group") + } return n } @@ -91,6 +97,9 @@ func (g *Group) NewIntFromLargeInt(x *large.Int) *Int { func (g *Group) NewIntFromBytes(buf []byte) *Int { val := large.NewIntFromBytes(buf) n := &Int{value: val, fingerprint: g.fingerprint} + if !g.Inside(n.value) { + panic("NewIntFromBytes: Attempted creation of cyclic outside of group") + } return n } @@ -102,20 +111,25 @@ func (g *Group) NewIntFromString(str string, base int) *Int { return nil } n := &Int{value: val, fingerprint: g.fingerprint} + if !g.Inside(n.value) { + panic("NewIntFromString: Attempted creation of cyclic outside of group") + } return n } -// Create a new cyclicInt in the group with the Max4KBit value +// Create a new cyclicInt in the group at the max group value func (g *Group) NewMaxInt() *Int { - val := large.NewMaxInt() - n := &Int{value: val, fingerprint: g.fingerprint} - return n + n := &Int{value: g.psub1, fingerprint: g.fingerprint} + return n.DeepCopy() } // Create a new cyclicInt in the group from an uint64 value func (g *Group) NewIntFromUInt(i uint64) *Int { val := large.NewIntFromUInt(i) n := &Int{value: val, fingerprint: g.fingerprint} + if !g.Inside(n.value) { + panic("NewIntFromUInt: Attempted creation of cyclic outside of group") + } return n } @@ -177,7 +191,7 @@ func (g *Group) SetString(x *Int, s string, base int) *Int { // Sets x in the group to Max4KInt value and returns x func (g *Group) SetMaxInt(x *Int) *Int { g.checkInts(x) - x.value.SetBytes(large.Max4kBitInt) + x.value.Set(g.psub1) return x } @@ -202,9 +216,9 @@ func (g *Group) Inside(a *large.Int) bool { } // ModP sets z ≡ x mod prime within the group and returns z. -func (g Group) ModP(x, z *Int) *Int { - g.checkInts(x, z) - z.value.Mod(x.value, g.prime) +func (g Group) ModP(x *large.Int, z *Int) *Int { + g.checkInts(z) + z.value.Mod(x, g.prime) return z } @@ -232,19 +246,14 @@ func (g *Group) Random(r *Int) *Int { // GetP returns a copy of the group's prime func (g *Group) GetP() *large.Int { - n := large.NewInt(0) + n := large.NewInt(1) n.Set(g.prime) return n } -// GetPCyclic returns a new cyclicInt with the group's prime -func (g *Group) GetPCyclic() *Int { - return g.NewIntFromLargeInt(g.prime) -} - // GetG returns a copy of the group's generator func (g *Group) GetG() *large.Int { - n := large.NewInt(0) + n := large.NewInt(1) n.Set(g.gen) return n } @@ -256,7 +265,7 @@ func (g *Group) GetGCyclic() *Int { // GetQ returns a copy of the group's Q prime func (g *Group) GetQ() *large.Int { - n := large.NewInt(0) + n := large.NewInt(1) n.Set(g.primeQ) return n } @@ -267,10 +276,10 @@ func (g *Group) GetQCyclic() *Int { } // GetPSub1 returns a copy of the group's p-1 -func (g *Group) GetPSub1() *large.Int { - n := large.NewInt(0) +func (g *Group) GetPSub1() *Int { + n := large.NewInt(1) n.Set(g.psub1) - return n + return &Int{n, g.fingerprint} } // GetPSub1Cyclic returns a new cyclicInt with the group's p-1 @@ -280,7 +289,7 @@ func (g *Group) GetPSub1Cyclic() *Int { // GetPSub1Factor returns a copy of the group's (p-1)/2 func (g *Group) GetPSub1Factor() *large.Int { - n := large.NewInt(0) + n := large.NewInt(1) n.Set(g.psub1factor) return n } @@ -377,13 +386,13 @@ func (g Group) FindSmallCoprimeInverse(z *Int, bits uint32) *Int { // 3. rand mod max : giving a range of 0 - 2^(bits)-3 // 4. rand + 2: range: 2 - 2^(bits)-1 // 5. rand ^ 1: range: 3 - 2^(bits)-1, odd number - max := large.NewInt(0).Sub( - large.NewInt(0).LeftShift( + max := large.NewInt(1).Sub( + large.NewInt(1).LeftShift( g.one, uint(bits)), g.two) - zinv := large.NewInt(0) + zinv := large.NewInt(1) for true { n, err := g.rng.Read(g.random) diff --git a/cyclic/group_test.go b/cyclic/group_test.go index 2c9fdc4f6f369d541dd4ef6bd07edb64b9cf89ad..b5950d8fdae740d2744af273dc7f7463f881b3e5 100644 --- a/cyclic/group_test.go +++ b/cyclic/group_test.go @@ -58,6 +58,24 @@ func TestNewInt(t *testing.T) { } } +// Test creation of cyclicInt in the group from int64 fails when outside the group +func TestNewInt_Panic(t *testing.T) { + defer func() { + if r := recover(); r != nil { + return + } + }() + + p := large.NewInt(1000000010101111111) + g := large.NewInt(5) + q := large.NewInt(1283) + grp := NewGroup(p, g, q) + + grp.NewInt(0) + + t.Errorf("NewInt created even when outside of the group") +} + // Test creation of cyclicInt in the group from large.Int func TestNewIntFromLargeInt(t *testing.T) { p := large.NewInt(1000000010101111111) @@ -77,6 +95,24 @@ func TestNewIntFromLargeInt(t *testing.T) { } } +// Test creation of cyclicInt in the group from large.Int fails when outside the group +func TestNewIntFromLargeInt_Panic(t *testing.T) { + defer func() { + if r := recover(); r != nil { + return + } + }() + + p := large.NewInt(1000000010101111111) + g := large.NewInt(5) + q := large.NewInt(1283) + grp := NewGroup(p, g, q) + + grp.NewIntFromLargeInt(large.NewInt(0)) + + t.Errorf("NewIntFromLargeInt created even when outside of the group") +} + // Test creation of cyclicInt in the group from byte array func TestNewIntFromBytes(t *testing.T) { p := large.NewInt(1000000010101111111) @@ -97,6 +133,24 @@ func TestNewIntFromBytes(t *testing.T) { } } +// Test creation of cyclicInt in the group from bytes fails when outside the group +func TestNewIntFromBytes_Panic(t *testing.T) { + defer func() { + if r := recover(); r != nil { + return + } + }() + + p := large.NewInt(1000000010101111111) + g := large.NewInt(5) + q := large.NewInt(1283) + grp := NewGroup(p, g, q) + + grp.NewIntFromBytes([]byte{0}) + + t.Errorf("NewIntFromBytes created even when outside of the group") +} + // Test creation of cyclicInt in the group from string // Also confirm that if the string can't be converted, nil is returned func TestNewIntFromString(t *testing.T) { @@ -124,6 +178,24 @@ func TestNewIntFromString(t *testing.T) { } } +// Test creation of cyclicInt in the group from string fails when outside the group +func TestNewIntFromString_Panic(t *testing.T) { + defer func() { + if r := recover(); r != nil { + return + } + }() + + p := large.NewInt(1000000010101111111) + g := large.NewInt(5) + q := large.NewInt(1283) + grp := NewGroup(p, g, q) + + grp.NewIntFromString("0", 16) + + t.Errorf("NewIntFromString created even when outside of the group") +} + // Test creation of cyclicInt in the group from Max4KInt value func TestNewMaxInt(t *testing.T) { p := large.NewInt(1000000010101111111) @@ -131,7 +203,7 @@ func TestNewMaxInt(t *testing.T) { q := large.NewInt(1283) grp := NewGroup(p, g, q) - expected := large.NewMaxInt() + expected := grp.psub1 actual := grp.NewMaxInt() if actual.value.Cmp(expected) != 0 { @@ -162,6 +234,24 @@ func TestNewIntFromUInt(t *testing.T) { } } +// Test creation of cyclicInt in the group from uint64 fails when outside the group +func TestNewIntFromUInt_Panic(t *testing.T) { + defer func() { + if r := recover(); r != nil { + return + } + }() + + p := large.NewInt(1000000010101111111) + g := large.NewInt(5) + q := large.NewInt(1283) + grp := NewGroup(p, g, q) + + grp.NewIntFromUInt(0) + + t.Errorf("NewIntFromUInt created even when outside of the group") +} + // Test group fingerprint getter func TestGetFingerprint(t *testing.T) { p := large.NewInt(1000000010101111111) @@ -234,14 +324,14 @@ func TestSetLargeInt(t *testing.T) { q := large.NewInt(3) group := NewGroup(p, g, q) expected := []bool{ - false, + true, true, false, false, true, } - inputs := []int64{0, 1, 17, 18, 12} + inputs := []int64{2, 1, 17, 18, 12} actual := make([]bool, len(inputs)) @@ -267,13 +357,14 @@ func TestSetBytes(t *testing.T) { expected := []*Int{ grp.NewInt(42), grp.NewInt(6553522), - grp.NewInt(0)} + grp.NewInt(2)} testBytes := [][]byte{ {0x2A}, // 42 {0x63, 0xFF, 0xB2}, // 6553522 - {0x00}} + {0x02}} + + actual := grp.NewInt(55) - actual := grp.NewInt(0) for i, testi := range testBytes { actual = grp.SetBytes(actual, testi) if actual.Cmp(expected[i]) != 0 { @@ -320,24 +411,24 @@ func TestSetString(t *testing.T) { testStructs := []testStructure{ {"42", 0}, {"100000000", 0}, - {"-5", 0}, - {"0", 0}, + {"5", 0}, + {"1", 0}, {"f", 0}, {"182", 5}, - {"-1", 2}, + {"10", 2}, } expected := []*Int{ grp.NewInt(42), grp.NewInt(100000000), - grp.NewInt(-5), - grp.NewInt(0), + grp.NewInt(5), + grp.NewInt(1), nil, nil, - grp.NewInt(-1), + grp.NewInt(2), } - actual := grp.NewInt(0) + actual := grp.NewInt(1) for i, testi := range testStructs { ret := grp.SetString(actual, testi.str, testi.base) @@ -351,7 +442,7 @@ func TestSetString(t *testing.T) { } else { if actual.Cmp(expected[i]) != 0 { t.Errorf("Test of SetString() failed at index: %v Expected: %v;"+ - " Actual: %v", i, expected[i], actual) + " Actual: %v", i, expected[i].Text(10), actual.Text(10)) } } } @@ -386,7 +477,7 @@ func TestSetMaxInt(t *testing.T) { q := large.NewInt(1283) grp := NewGroup(p, g, q) - expected := grp.NewMaxInt() + expected := grp.GetPSub1() actual := grp.NewInt(int64(69)) if actual.Cmp(expected) == 0 { @@ -478,12 +569,12 @@ func TestMul(t *testing.T) { group := NewGroup(p, g, q) actual := []*Int{ - group.Mul(group.NewInt(20), group.NewInt(11), group.NewInt(0)), - group.Mul(group.NewInt(0), group.NewInt(10), group.NewInt(0)), + group.Mul(group.NewInt(20), group.NewInt(11), group.NewInt(1)), + group.Mul(group.NewInt(1), group.NewInt(10), group.NewInt(1)), } expected := []*Int{ group.NewInt((20 * 11) % prime), - group.NewInt(0), + group.NewInt(10), } for i := 0; i < len(actual); i++ { @@ -507,7 +598,7 @@ func TestMul_Panic(t *testing.T) { a := group.NewInt(20) b := group2.NewInt(11) - c := group.NewInt(0) + c := group.NewInt(1) defer func() { if r := recover(); r == nil { @@ -566,8 +657,8 @@ func TestModP(t *testing.T) { large.NewIntFromString("77777777777777777777", 16)} actual := make([]*Int, len(expected)) for i := 0; i < len(expected); i++ { - actual[i] = group[i].NewIntFromLargeInt(a[i]) - group[i].ModP(actual[i], actual[i]) + actual[i] = group[i].NewInt(1) + group[i].ModP(a[i], actual[i]) } for i := 0; i < len(expected); i++ { @@ -589,8 +680,8 @@ func TestModP_Panic(t *testing.T) { g2 := large.NewInt(2) group2 := NewGroup(p, g2, q) - a := group.NewInt(20) - b := group2.NewInt(0) + a := large.NewInt(20) + b := group2.NewInt(1) defer func() { if r := recover(); r == nil { @@ -610,10 +701,10 @@ func TestInverse(t *testing.T) { group := NewGroup(p, g, q) x := group.NewInt(13) //message a := group.NewInt(10) //encryption key - inv := group.NewInt(0) + inv := group.NewInt(1) inv = group.Inverse(a, inv) //decryption key a = group.Mul(x, a, a) // encrypted message - c := group.Mul(inv, a, group.NewInt(0)) //decrypted message (x) + c := group.Mul(inv, a, group.NewInt(1)) //decrypted message (x) if c.value.Cmp(x.value) != 0 { t.Errorf("TestInverse failed, expected: '%v', got: '%v'", @@ -632,7 +723,7 @@ func TestInverse_Panic(t *testing.T) { group2 := NewGroup(p, g2, q) a := group.NewInt(20) - b := group2.NewInt(0) + b := group2.NewInt(1) defer func() { if r := recover(); r == nil { @@ -652,7 +743,7 @@ func TestRandom(t *testing.T) { q := large.NewInt(3) group := NewGroup(p, g, q) for i := 0; i < 100000; i++ { - if !group.Inside(group.Random(group.NewInt(0)).GetLargeInt()) { + if !group.Inside(group.Random(group.NewInt(1)).GetLargeInt()) { t.Errorf("Generated number is not inside the group!") } } @@ -705,7 +796,7 @@ func TestRandom_PanicReadErr(t *testing.T) { } }() - group.Random(group.NewInt(0)) + group.Random(group.NewInt(1)) } func TestGen(t *testing.T) { @@ -716,7 +807,7 @@ func TestGen(t *testing.T) { group := NewGroup(p, g, q) // setup array to keep track of frequency of random values - r := group.NewInt(0) + r := group.NewInt(1) rng := make([]int, int(p.Int64())) tests := 500 @@ -760,21 +851,6 @@ func TestGetP(t *testing.T) { } } -// Test prime getter from the group cyclic version -func TestGetPCyclic(t *testing.T) { - // setup test group and generator - p := large.NewInt(17) - g := large.NewInt(29) - q := large.NewInt(3) - group := NewGroup(p, g, q) - actual := group.GetPCyclic() - - if actual.value.Cmp(p) != 0 { - t.Errorf("TestGetPCyclic failed, expected: '%v', got: '%v'", - p.Text(10), actual.value.Text(10)) - } -} - // Test generator getter from the group func TestGetG(t *testing.T) { // setup test group and generator @@ -793,7 +869,7 @@ func TestGetG(t *testing.T) { // Test generator getter from the group cyclic version func TestGetGCyclic(t *testing.T) { // setup test group and generator - p := large.NewInt(17) + p := large.NewInt(33) g := large.NewInt(29) q := large.NewInt(3) group := NewGroup(p, g, q) @@ -845,7 +921,7 @@ func TestGetPSub1(t *testing.T) { actual := group.GetPSub1() ps1 := large.NewInt(16) - if actual.Cmp(ps1) != 0 { + if actual.value.Cmp(ps1) != 0 { t.Errorf("TestGetPSub1 failed, expected: '%v', got: '%v'", ps1.Text(10), actual.Text(10)) } @@ -916,7 +992,7 @@ func TestArrayMul(t *testing.T) { grp.NewInt(5), } - c := grp.NewInt(42) + c := grp.NewInt(1) actual := grp.MulMulti(c, slc...) if actual.value.Cmp(expected) != 0 { @@ -956,9 +1032,9 @@ func TestArrayMult_Panic(t *testing.T) { // Test exponentiation under the group func TestExp(t *testing.T) { - p := large.NewInt(11) - g := large.NewInt(7) - q := large.NewInt(3) + p := large.NewInt(117) + g := large.NewInt(5) + q := large.NewInt(53) grp := NewGroup(p, g, q) type testStructure struct { @@ -968,10 +1044,10 @@ func TestExp(t *testing.T) { } testStrings := [][]string{ - {"42", "42", "4"}, - {"42", "69", "5"}, - {"-69", "42", "9"}, - {"1000000000", "9999999", "10"}, + {"42", "41", "9"}, + {"42", "63", "27"}, + {"69", "42", "27"}, + {"99", "81", "99"}, } var testStructs []testStructure @@ -1006,7 +1082,7 @@ func TestExp(t *testing.T) { expected := 0 for i, testi := range testStructs { - actual := grp.NewInt(0) + actual := grp.NewInt(1) actual = grp.Exp(testi.x, testi.y, actual) result := actual.value.Cmp(testi.z.value) @@ -1024,9 +1100,9 @@ func TestExp(t *testing.T) { // Test exponentiation of the generator in the group func TestExpG(t *testing.T) { - p := large.NewInt(11) - g := large.NewInt(7) - q := large.NewInt(3) + p := large.NewInt(117) + g := large.NewInt(5) + q := large.NewInt(53) grp := NewGroup(p, g, q) type testStructure struct { @@ -1035,10 +1111,10 @@ func TestExpG(t *testing.T) { } testStrings := [][]string{ - {"42", "5"}, - {"69", "8"}, - {"42", "5"}, - {"9999999", "8"}, + {"42", "64"}, + {"69", "44"}, + {"43", "86"}, + {"2", "25"}, } var testStructs []testStructure @@ -1064,7 +1140,7 @@ func TestExpG(t *testing.T) { expected := 0 for i, testi := range testStructs { - actual := grp.NewInt(0) + actual := grp.NewInt(1) actual = grp.ExpG(testi.y, actual) result := actual.value.Cmp(testi.z.value) @@ -1089,7 +1165,7 @@ func TestExp_Panic(t *testing.T) { a := group2.NewInt(20) b := group.NewInt(11) - c := group.NewInt(0) + c := group.NewInt(1) defer func() { if r := recover(); r == nil { @@ -1110,7 +1186,7 @@ func TestRandomCoprime(t *testing.T) { group := NewGroup(p, g, q) // setup array to keep track of frequency of random values - r := group.NewInt(0) + r := group.NewInt(1) rng := make([]int, int(p.Int64())) tests := 500 @@ -1177,7 +1253,7 @@ func TestRandomCoprime_PanicReadErr(t *testing.T) { } }() - group.RandomCoprime(group.NewInt(0)) + group.RandomCoprime(group.NewInt(1)) } // You pass a value x = a^y to the RootCoprime function, where y is (supposed to be) coprime with (p-1). @@ -1193,9 +1269,9 @@ func TestRootCoprime(t *testing.T) { group := NewGroup(p, g, q) a := []*Int{group.NewInt(5), group.NewInt(4), group.NewInt(15)} - x := group.NewInt(0) + x := group.NewInt(1) y := []*Int{group.NewInt(5), group.NewInt(11), group.NewInt(2)} - z := []*Int{group.NewInt(0), group.NewInt(0), group.NewInt(0)} + z := []*Int{group.NewInt(1), group.NewInt(1), group.NewInt(1)} passing := []bool{true, true, false} @@ -1227,7 +1303,7 @@ func TestRootCoprime_Panic(t *testing.T) { a := group.NewInt(20) b := group.NewInt(11) - c := group2.NewInt(0) + c := group2.NewInt(1) defer func() { if r := recover(); r == nil { @@ -1265,13 +1341,13 @@ func TestFindSmallCoprimeInverse(t *testing.T) { bits := uint32(256) for i := 0; i < num; i++ { - z := group.NewInt(0) + z := group.NewInt(1) - base := group.Random(group.NewInt(0)) + base := group.Random(group.NewInt(1)) group.FindSmallCoprimeInverse(z, bits) - zinv := large.NewInt(0).ModInverse(z.value, group.psub1) + zinv := large.NewInt(1).ModInverse(z.value, group.psub1) totalBitLen += len(zinv.Bytes()) * 8 @@ -1281,11 +1357,11 @@ func TestFindSmallCoprimeInverse(t *testing.T) { uint32(len(zinv.Bytes())*8)) } - baseZ := group.NewInt(0) + baseZ := group.NewInt(1) group.Exp(base, z, baseZ) - basecalc := group.NewInt(0) + basecalc := group.NewInt(1) basecalc = group.RootCoprime(baseZ, z, basecalc) @@ -1321,14 +1397,14 @@ func TestFindSmallCoprimeInverse_SmallGroup(t *testing.T) { for i := 0; i < num; i++ { z := group.NewInt(1) - base := group.Random(group.NewInt(0)) + base := group.Random(group.NewInt(1)) // z will be unchanged if a number with no inverse is returned for z.value.Cmp(one) == 0 { group.FindSmallCoprimeInverse(z, bits) } - zinv := large.NewInt(0).ModInverse(z.value, group.psub1) + zinv := large.NewInt(1).ModInverse(z.value, group.psub1) if zinv.BitLen() > int(bits) { t.Errorf("FindSmallExponent Error: Inverse too large on "+ @@ -1336,11 +1412,11 @@ func TestFindSmallCoprimeInverse_SmallGroup(t *testing.T) { zinv.BitLen()) } - baseZ := group.NewInt(0) + baseZ := group.NewInt(1) group.Exp(base, z, baseZ) - basecalc := group.NewInt(0) + basecalc := group.NewInt(1) basecalc = group.RootCoprime(baseZ, z, basecalc) @@ -1368,14 +1444,14 @@ func TestFindSmallCoprimeInverse_UnsafeGroup(t *testing.T) { for i := 0; i < num; i++ { z := group.NewInt(1) - base := group.Random(group.NewInt(0)) + base := group.Random(group.NewInt(1)) // z will be unchanged if a number with no inverse is returned for z.value.Cmp(one) == 0 { group.FindSmallCoprimeInverse(z, bits) } - zinv := large.NewInt(0).ModInverse(z.value, group.psub1) + zinv := large.NewInt(1).ModInverse(z.value, group.psub1) if zinv.BitLen() > int(bits) { t.Errorf("FindSmallExponent Error: Inverse too large on "+ @@ -1383,11 +1459,11 @@ func TestFindSmallCoprimeInverse_UnsafeGroup(t *testing.T) { zinv.BitLen()) } - baseZ := group.NewInt(0) + baseZ := group.NewInt(1) group.Exp(base, z, baseZ) - basecalc := group.NewInt(0) + basecalc := group.NewInt(1) basecalc = group.RootCoprime(baseZ, z, basecalc) @@ -1460,7 +1536,7 @@ func TestFindSmallCoprimeInverse_PanicReadErr(t *testing.T) { } }() - group.FindSmallCoprimeInverse(group.NewInt(0), bits) + group.FindSmallCoprimeInverse(group.NewInt(1), bits) } // Tests that a Group structure that is encoded and then decoded, as a @@ -1531,16 +1607,16 @@ func BenchmarkExpForGroup(b *testing.B) { grp := NewGroup(p, g, q) //prebake inputs - z := grp.NewInt(0) + z := grp.NewInt(1) G := grp.GetGCyclic() var inputs []*Int var outputs []*Int for i := 0; i < b.N; i++ { - nint := grp.Random(grp.NewInt(0)) + nint := grp.Random(grp.NewInt(1)) inputs = append(inputs, nint) - outputs = append(outputs, grp.NewInt(0)) + outputs = append(outputs, grp.NewInt(1)) } b.ResetTimer() @@ -1568,18 +1644,18 @@ func BenchmarkMulForGroup(b *testing.B) { grp := NewGroup(p, g, q) //prebake inputs - z := grp.NewInt(0) + z := grp.NewInt(1) var inputA []*Int var inputB []*Int var outputs []*Int for i := 0; i < b.N; i++ { - nint := grp.Random(grp.NewInt(0)) + nint := grp.Random(grp.NewInt(1)) inputA = append(inputA, nint) - mint := grp.Random(grp.NewInt(0)) + mint := grp.Random(grp.NewInt(1)) inputB = append(inputB, mint) - outputs = append(outputs, grp.NewInt(0)) + outputs = append(outputs, grp.NewInt(1)) } b.ResetTimer() @@ -1607,17 +1683,17 @@ func BenchmarkInverse(b *testing.B) { grp := NewGroup(p, g, q) //prebake inputs - z := grp.NewInt(0) + z := grp.NewInt(1) G := grp.GetGCyclic() var inputs []*Int var outputs []*Int for i := 0; i < b.N; i++ { - nint := grp.Random(grp.NewInt(0)) + nint := grp.Random(grp.NewInt(1)) nint = grp.Exp(G, nint, z) inputs = append(inputs, nint) - outputs = append(outputs, grp.NewInt(0)) + outputs = append(outputs, grp.NewInt(1)) } b.ResetTimer() diff --git a/cyclic/int.go b/cyclic/int.go index 04123476a50ce7954e5803e04f49322ecfc3ee43..5c6fc13ce86589321fda3e84987600d939f884df 100644 --- a/cyclic/int.go +++ b/cyclic/int.go @@ -45,7 +45,7 @@ func (z *Int) LeftpadBytes(length uint64) []byte { // underlying data is linked func (z *Int) DeepCopy() *Int { i := &Int{} - i.value = large.NewInt(0) + i.value = large.NewInt(1) i.fingerprint = z.fingerprint i.value.Set(z.value) return i diff --git a/cyclic/int_test.go b/cyclic/int_test.go index f30f68a071a1c07f5a2e170f6b4bc97fb5d14549..ab60cfeb5be10f6ab2f9f2ac9fe97bdeaf36359a 100644 --- a/cyclic/int_test.go +++ b/cyclic/int_test.go @@ -190,13 +190,13 @@ func TestText(t *testing.T) { testints := []*Int{ grp.NewInt(42), grp.NewInt(6553522), - grp.NewIntFromString("867530918239450598372829049587", 10), - grp.NewInt(-42)} + grp.NewIntFromString("8675309182", 10), + grp.NewInt(43)} expectedstrs := []string{ "42 in GRP: 4XgotyuZEW...", "6553522 in GRP: 4XgotyuZEW...", - "8675309182... in GRP: 4XgotyuZEW...", - "-42 in GRP: 4XgotyuZEW..."} // TODO: Should be <nil>, not -42 + "8675309182 in GRP: 4XgotyuZEW...", + "43 in GRP: 4XgotyuZEW..."} // TODO: Should be <nil>, not -42 tests := len(testints) pass := 0 for i, tsti := range testints { @@ -214,14 +214,19 @@ func TestText(t *testing.T) { // Test text verbose representation with different lengths func TestTextVerbose(t *testing.T) { - testInt := grp.NewIntFromString("867530918239450598372829049587", 10) + p_t := large.NewIntFromString("867530918239450598372829049587118723612836", 10) + g_t := large.NewInt(5) + q_t := large.NewInt(1283) + group := NewGroup(p_t, g_t, q_t) + + testInt := group.NewIntFromString("867530918239450598372829049587", 10) lens := []int{3, 12, 16, 18, 0} expected := []string{ - "867... in GRP: 4Xg...", - "867530918239... in GRP: 4XgotyuZEWk=", - "8675309182394505... in GRP: 4XgotyuZEWk=", - "867530918239450598... in GRP: 4XgotyuZEWk=", - "867530918239450598372829049587 in GRP: 4XgotyuZEWk="} + "867... in GRP: t9A...", + "867530918239... in GRP: t9Aiywu7oD8=", + "8675309182394505... in GRP: t9Aiywu7oD8=", + "867530918239450598... in GRP: t9Aiywu7oD8=", + "867530918239450598372829049587 in GRP: t9Aiywu7oD8="} tests := len(lens) pass := 0 for i, testLen := range lens { @@ -251,7 +256,7 @@ func TestGob(t *testing.T) { t.Errorf("Error GOB Encoding Int: %s", err) } - outInt := grp.NewInt(0) + outInt := grp.NewInt(1) err = dec.Decode(&outInt) diff --git a/diffieHellman/dhkx.go b/diffieHellman/dhkx.go index 0f31e3e9ce3fcfd7bc6e3704bd426b4c1bc12f4f..43c8e07de0fa60000e3954f55c75007213715fe2 100644 --- a/diffieHellman/dhkx.go +++ b/diffieHellman/dhkx.go @@ -35,7 +35,7 @@ func CreateDHKeyPair(group *cyclic.Group) (*cyclic.Int, *cyclic.Int) { privateKey := group.NewIntFromBytes(k1) - publicKey := group.NewInt(0) + publicKey := group.NewInt(1) group.Exp(group.GetGCyclic(), privateKey, publicKey) return privateKey, publicKey @@ -46,7 +46,7 @@ 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) { - sessionKey := group.NewInt(0) + sessionKey := group.NewInt(1) group.Exp(publicKey, privateKey, sessionKey) return sessionKey, nil @@ -72,7 +72,7 @@ func CheckPublicKey(group *cyclic.Group, publicKey *cyclic.Int) bool { return false } - symbol := group.NewInt(0) + symbol := group.NewInt(1) group.Exp(publicKey, group.GetPSub1FactorCyclic(), symbol) // Symbol must be equal to 1 diff --git a/diffieHellman/dhkx_test.go b/diffieHellman/dhkx_test.go index 5339926bc34cf90a545bab52424eac7ddb201c14..b9e560a046e9ea64b645997564cb302958bfa57b 100644 --- a/diffieHellman/dhkx_test.go +++ b/diffieHellman/dhkx_test.go @@ -31,7 +31,7 @@ func TestDHKX(t *testing.T) { "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AACAA68FFFFFFFFFFFFFFFF" - p := large.NewInt(0) + p := large.NewInt(1) p.SetString(primeString, 16) g := large.NewInt(2) q := large.NewInt(3) @@ -94,7 +94,7 @@ func TestCheckPublicKey(t *testing.T) { "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AACAA68FFFFFFFFFFFFFFFF" - p := large.NewInt(0) + p := large.NewInt(1) p.SetString(primeString, 16) g := large.NewInt(2) q := large.NewInt(3) @@ -142,6 +142,7 @@ func TestCheckPublicKey(t *testing.T) { println("TestCheckPublicKey():", pass, "out of", tests, "tests passed.") } +/* // TestDHNodeKeys tests if the hardcoded keys are valid func TestDHNodeKeys(t *testing.T) { @@ -159,7 +160,7 @@ func TestDHNodeKeys(t *testing.T) { "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" + "15728E5A8AACAA68FFFFFFFFFFFFFFFF" - p := large.NewInt(0) + p := large.NewInt(1) p.SetString(primeString, 16) g := large.NewInt(2) q := large.NewInt(3) @@ -239,4 +240,4 @@ func TestDHNodeKeys(t *testing.T) { } println("TestDHNodeKeys():", pass, "out of", tests, "tests passed.") -} +}*/ diff --git a/e2e/encrypt.go b/e2e/encrypt.go index 2d2e05a2e8e9dd3c3f52447576acf716a5c9c5c9..e4b04f40b95e58dcbe2a091ab328fb5102edcad5 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, g.NewIntFromBytes(encMsg), g.NewInt(0)) + product := g.Mul(key, g.NewIntFromBytes(encMsg), g.NewInt(1)) // 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, g.NewInt(0)) + keyInv := g.Inverse(key, g.NewInt(1)) // Modular multiply the inverted key with the message - product := g.Mul(keyInv, g.NewIntFromBytes(encMsg), g.NewInt(0)) + product := g.Mul(keyInv, g.NewIntFromBytes(encMsg), g.NewInt(1)) // Remove the padding from the message unPadMsg, err := Unpad(product.LeftpadBytes(uint64(format.TOTAL_LEN))) diff --git a/e2e/ttl.go b/e2e/ttl.go index 350c3dec429db8f911481fb53d7475149eac3442..9a918eb223ec5df5ab4621bc1d99f92daeab5760 100644 --- a/e2e/ttl.go +++ b/e2e/ttl.go @@ -43,7 +43,7 @@ func computeTTL(hashed []byte, min uint16, max uint16) uint16 { jww.ERROR.Panicf("Min must be greater than or equal to max in computeTTL") } - zero := large.NewInt(0) + zero := large.NewInt(1) keyHash := large.NewIntFromBytes(hashed) mod := large.NewInt(int64(max - min)) diff --git a/hash/keys.go b/hash/keys.go index 73999d3df4e13f9e3ca4c3ff3fc3b4e3c1fee986..0f39d9aef9f4ff0ca1987b2b3fe9ba29ad057645 100644 --- a/hash/keys.go +++ b/hash/keys.go @@ -33,5 +33,6 @@ func ExpandKey(h hash.Hash, g *cyclic.Group, key []byte) []byte { } keyInt.SetBytes(expandedKey) } + return expandedKey }