From 3c6123c0e36eddb75a599f7976b1f4c30ae226d8 Mon Sep 17 00:00:00 2001 From: Piotr Dyraga Date: Tue, 7 Aug 2018 16:11:26 +0200 Subject: [PATCH 01/14] Accept PubKey bit length when constructing TKey generator Instead of passing safe prime bit length which is a kind of an implementation detail, we should pass a public key bit length. Public key is a Paillier modulus defining plaintext space. This information is more important than the safe prime value/bit length used under the hood. --- thresholdkey_generator.go | 19 +++++++++++++------ thresholdkey_generator_test.go | 26 +++++++++++++------------- thresholdkey_test.go | 12 ++++++------ 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/thresholdkey_generator.go b/thresholdkey_generator.go index 96948a0..c6b8c60 100644 --- a/thresholdkey_generator.go +++ b/thresholdkey_generator.go @@ -19,7 +19,7 @@ import ( // with Applications to Electronic Voting // Aarhus University, Dept. of Computer Science, BRICS type ThresholdKeyGenerator struct { - nbits int + publicKeyBitLength int TotalNumberOfDecryptionServers int Threshold int Random io.Reader @@ -49,11 +49,16 @@ type ThresholdKeyGenerator struct { // is done on the input values. You need to be sure that nbits is big enough // and that Threshold > TotalNumberOfDecryptionServers / 2. // The plaintext space for the key will be Z_n. -func GetThresholdKeyGenerator(nbits, TotalNumberOfDecryptionServers, Threshold int, random io.Reader) *ThresholdKeyGenerator { +func GetThresholdKeyGenerator( + publicKeyBitLength int, + totalNumberOfDecryptionServers int, + threshold int, + random io.Reader, +) *ThresholdKeyGenerator { ret := new(ThresholdKeyGenerator) - ret.nbits = nbits - ret.TotalNumberOfDecryptionServers = TotalNumberOfDecryptionServers - ret.Threshold = Threshold + ret.publicKeyBitLength = publicKeyBitLength // TODO: at least 32 + ret.TotalNumberOfDecryptionServers = totalNumberOfDecryptionServers + ret.Threshold = threshold ret.Random = random return ret } @@ -61,7 +66,9 @@ func GetThresholdKeyGenerator(nbits, TotalNumberOfDecryptionServers, Threshold i func (tkg *ThresholdKeyGenerator) generateSafePrimes() (*big.Int, *big.Int, error) { concurrencyLevel := 4 timeout := 120 * time.Second - return GenerateSafePrime(tkg.nbits, concurrencyLevel, timeout, tkg.Random) + safePrimeBitLength := tkg.publicKeyBitLength / 2 + + return GenerateSafePrime(safePrimeBitLength, concurrencyLevel, timeout, tkg.Random) } func (tkg *ThresholdKeyGenerator) initPandP1() error { diff --git a/thresholdkey_generator_test.go b/thresholdkey_generator_test.go index bccf4a5..0385978 100644 --- a/thresholdkey_generator_test.go +++ b/thresholdkey_generator_test.go @@ -13,42 +13,42 @@ var MockGenerateSafePrimes = func() (*big.Int, *big.Int, error) { func TestGenerateSafePrimesOfThresholdKeyGenerator(t *testing.T) { tkh := new(ThresholdKeyGenerator) - tkh.nbits = 10 + tkh.publicKeyBitLength = 32 tkh.Random = rand.Reader p, q, err := tkh.generateSafePrimes() if err != nil { t.Error(err) } - AreSafePrimes(p, q, 10, t) + AreSafePrimes(p, q, 16, t) } func TestInitPandP1(t *testing.T) { tkh := new(ThresholdKeyGenerator) - tkh.nbits = 10 + tkh.publicKeyBitLength = 32 tkh.Random = rand.Reader tkh.initPandP1() - AreSafePrimes(tkh.p, tkh.p1, 10, t) + AreSafePrimes(tkh.p, tkh.p1, 16, t) } func TestInitQandQ1(t *testing.T) { tkh := new(ThresholdKeyGenerator) - tkh.nbits = 10 + tkh.publicKeyBitLength = 32 tkh.Random = rand.Reader tkh.initQandQ1() - AreSafePrimes(tkh.q, tkh.q1, 10, t) + AreSafePrimes(tkh.q, tkh.q1, 16, t) } func TestInitPsAndQs(t *testing.T) { tkh := new(ThresholdKeyGenerator) - tkh.nbits = 10 + tkh.publicKeyBitLength = 32 tkh.Random = rand.Reader tkh.initPsAndQs() - AreSafePrimes(tkh.p, tkh.p1, 10, t) - AreSafePrimes(tkh.q, tkh.q1, 10, t) + AreSafePrimes(tkh.p, tkh.p1, 16, t) + AreSafePrimes(tkh.q, tkh.q1, 16, t) } func TestArePsAndQsGood(t *testing.T) { @@ -103,7 +103,7 @@ func TestInitD(t *testing.T) { func TestInitNumerialValues(t *testing.T) { tkh := new(ThresholdKeyGenerator) - tkh.nbits = 10 + tkh.publicKeyBitLength = 32 tkh.Random = rand.Reader if err := tkh.initNumerialValues(); err != nil { @@ -113,7 +113,7 @@ func TestInitNumerialValues(t *testing.T) { func TestGenerateHidingPolynomial(t *testing.T) { tkh := new(ThresholdKeyGenerator) - tkh.nbits = 10 + tkh.publicKeyBitLength = 32 tkh.Threshold = 10 tkh.Random = rand.Reader if err := tkh.initNumerialValues(); err != nil { @@ -138,7 +138,7 @@ func TestGenerateHidingPolynomial(t *testing.T) { func TestComputeShare(t *testing.T) { tkh := new(ThresholdKeyGenerator) - tkh.nbits = 10 + tkh.publicKeyBitLength = 32 tkh.Threshold = 3 tkh.TotalNumberOfDecryptionServers = 5 tkh.nm = b(103) @@ -151,7 +151,7 @@ func TestComputeShare(t *testing.T) { func TestCreateShares(t *testing.T) { tkh := new(ThresholdKeyGenerator) - tkh.nbits = 10 + tkh.publicKeyBitLength = 32 tkh.Threshold = 10 tkh.TotalNumberOfDecryptionServers = 100 tkh.Random = rand.Reader diff --git a/thresholdkey_test.go b/thresholdkey_test.go index b610a85..26428b3 100644 --- a/thresholdkey_test.go +++ b/thresholdkey_test.go @@ -184,7 +184,7 @@ func TestupdateCprime(t *testing.T) { } func TestEncryptingDecryptingSimple(t *testing.T) { - tkh := GetThresholdKeyGenerator(10, 2, 1, rand.Reader) + tkh := GetThresholdKeyGenerator(32, 2, 1, rand.Reader) tpks, err := tkh.Generate() if err != nil { t.Error(err) @@ -205,7 +205,7 @@ func TestEncryptingDecryptingSimple(t *testing.T) { } func TestEncryptingDecrypting(t *testing.T) { - tkh := GetThresholdKeyGenerator(10, 2, 2, rand.Reader) + tkh := GetThresholdKeyGenerator(32, 2, 2, rand.Reader) tpks, err := tkh.Generate() if err != nil { t.Error(err) @@ -227,7 +227,7 @@ func TestEncryptingDecrypting(t *testing.T) { } func TestHomomorphicThresholdEncryption(t *testing.T) { - tkh := GetThresholdKeyGenerator(10, 2, 2, rand.Reader) + tkh := GetThresholdKeyGenerator(32, 2, 2, rand.Reader) tpks, _ := tkh.Generate() plainText1 := b(13) @@ -278,7 +278,7 @@ func TestValidate(t *testing.T) { } func TestCombinePartialDecryptionsZKP(t *testing.T) { - tkh := GetThresholdKeyGenerator(10, 2, 2, rand.Reader) + tkh := GetThresholdKeyGenerator(32, 2, 2, rand.Reader) tpks, err := tkh.Generate() if err != nil { t.Error(err) @@ -311,7 +311,7 @@ func TestCombinePartialDecryptionsZKP(t *testing.T) { } func TestCombinePartialDecryptionsWith100Shares(t *testing.T) { - tkh := GetThresholdKeyGenerator(10, 100, 50, rand.Reader) + tkh := GetThresholdKeyGenerator(32, 100, 50, rand.Reader) tpks, err := tkh.Generate() if err != nil { t.Error(err) @@ -338,7 +338,7 @@ func TestCombinePartialDecryptionsWith100Shares(t *testing.T) { } func TestVerifyDecryption(t *testing.T) { - tkh := GetThresholdKeyGenerator(10, 2, 2, rand.Reader) + tkh := GetThresholdKeyGenerator(32, 2, 2, rand.Reader) tpks, err := tkh.Generate() pk := &tpks[0].ThresholdPublicKey From 408bafa61a32378711419b74a9737cec7e8e39bf Mon Sep 17 00:00:00 2001 From: Piotr Dyraga Date: Tue, 7 Aug 2018 16:20:18 +0200 Subject: [PATCH 02/14] Use GenThresholdKeyGenerator whenever it's possible --- thresholdkey_generator_test.go | 36 +++++++++------------------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/thresholdkey_generator_test.go b/thresholdkey_generator_test.go index 0385978..b5c851c 100644 --- a/thresholdkey_generator_test.go +++ b/thresholdkey_generator_test.go @@ -12,9 +12,8 @@ var MockGenerateSafePrimes = func() (*big.Int, *big.Int, error) { } func TestGenerateSafePrimesOfThresholdKeyGenerator(t *testing.T) { - tkh := new(ThresholdKeyGenerator) - tkh.publicKeyBitLength = 32 - tkh.Random = rand.Reader + tkh := GetThresholdKeyGenerator(32, 4, 3, rand.Reader) + p, q, err := tkh.generateSafePrimes() if err != nil { t.Error(err) @@ -23,27 +22,21 @@ func TestGenerateSafePrimesOfThresholdKeyGenerator(t *testing.T) { } func TestInitPandP1(t *testing.T) { - tkh := new(ThresholdKeyGenerator) - tkh.publicKeyBitLength = 32 - tkh.Random = rand.Reader + tkh := GetThresholdKeyGenerator(32, 4, 3, rand.Reader) tkh.initPandP1() AreSafePrimes(tkh.p, tkh.p1, 16, t) } func TestInitQandQ1(t *testing.T) { - tkh := new(ThresholdKeyGenerator) - tkh.publicKeyBitLength = 32 - tkh.Random = rand.Reader + tkh := GetThresholdKeyGenerator(32, 4, 3, rand.Reader) tkh.initQandQ1() AreSafePrimes(tkh.q, tkh.q1, 16, t) } func TestInitPsAndQs(t *testing.T) { - tkh := new(ThresholdKeyGenerator) - tkh.publicKeyBitLength = 32 - tkh.Random = rand.Reader + tkh := GetThresholdKeyGenerator(32, 4, 3, rand.Reader) tkh.initPsAndQs() @@ -102,9 +95,7 @@ func TestInitD(t *testing.T) { } func TestInitNumerialValues(t *testing.T) { - tkh := new(ThresholdKeyGenerator) - tkh.publicKeyBitLength = 32 - tkh.Random = rand.Reader + tkh := GetThresholdKeyGenerator(32, 4, 3, rand.Reader) if err := tkh.initNumerialValues(); err != nil { t.Error(err) @@ -112,10 +103,7 @@ func TestInitNumerialValues(t *testing.T) { } func TestGenerateHidingPolynomial(t *testing.T) { - tkh := new(ThresholdKeyGenerator) - tkh.publicKeyBitLength = 32 - tkh.Threshold = 10 - tkh.Random = rand.Reader + tkh := GetThresholdKeyGenerator(32, 15, 10, rand.Reader) if err := tkh.initNumerialValues(); err != nil { t.Error(err) } @@ -137,10 +125,7 @@ func TestGenerateHidingPolynomial(t *testing.T) { } func TestComputeShare(t *testing.T) { - tkh := new(ThresholdKeyGenerator) - tkh.publicKeyBitLength = 32 - tkh.Threshold = 3 - tkh.TotalNumberOfDecryptionServers = 5 + tkh := GetThresholdKeyGenerator(32, 5, 3, rand.Reader) tkh.nm = b(103) tkh.polynomialCoefficients = []*big.Int{b(29), b(88), b(51)} share := tkh.computeShare(2) @@ -150,10 +135,7 @@ func TestComputeShare(t *testing.T) { } func TestCreateShares(t *testing.T) { - tkh := new(ThresholdKeyGenerator) - tkh.publicKeyBitLength = 32 - tkh.Threshold = 10 - tkh.TotalNumberOfDecryptionServers = 100 + tkh := GetThresholdKeyGenerator(32, 100, 10, rand.Reader) tkh.Random = rand.Reader if err := tkh.initNumerialValues(); err != nil { t.Error(err) From 3394a6136aa35a2feffd2a46645ccccd411233db Mon Sep 17 00:00:00 2001 From: Piotr Dyraga Date: Tue, 7 Aug 2018 16:44:45 +0200 Subject: [PATCH 03/14] Check threshold public key bit length --- thresholdkey_generator.go | 19 ++++--- thresholdkey_generator_test.go | 101 +++++++++++++++++++++++++++++---- thresholdkey_test.go | 42 +++++++++++--- 3 files changed, 136 insertions(+), 26 deletions(-) diff --git a/thresholdkey_generator.go b/thresholdkey_generator.go index c6b8c60..79e22d0 100644 --- a/thresholdkey_generator.go +++ b/thresholdkey_generator.go @@ -2,6 +2,7 @@ package paillier import ( "crypto/rand" + "errors" "io" "math/big" "time" @@ -54,13 +55,17 @@ func GetThresholdKeyGenerator( totalNumberOfDecryptionServers int, threshold int, random io.Reader, -) *ThresholdKeyGenerator { - ret := new(ThresholdKeyGenerator) - ret.publicKeyBitLength = publicKeyBitLength // TODO: at least 32 - ret.TotalNumberOfDecryptionServers = totalNumberOfDecryptionServers - ret.Threshold = threshold - ret.Random = random - return ret +) (*ThresholdKeyGenerator, error) { + if publicKeyBitLength < 18 { + return nil, errors.New("Public key bit length must be at least 18 bits") + } + + generator := new(ThresholdKeyGenerator) + generator.publicKeyBitLength = publicKeyBitLength + generator.TotalNumberOfDecryptionServers = totalNumberOfDecryptionServers + generator.Threshold = threshold + generator.Random = random + return generator, nil } func (tkg *ThresholdKeyGenerator) generateSafePrimes() (*big.Int, *big.Int, error) { diff --git a/thresholdkey_generator_test.go b/thresholdkey_generator_test.go index b5c851c..4a123df 100644 --- a/thresholdkey_generator_test.go +++ b/thresholdkey_generator_test.go @@ -2,6 +2,7 @@ package paillier import ( "crypto/rand" + "errors" "math/big" "reflect" "testing" @@ -11,8 +12,49 @@ var MockGenerateSafePrimes = func() (*big.Int, *big.Int, error) { return big.NewInt(887), big.NewInt(443), nil } +func TestCreateThresholdKeyGenerator(t *testing.T) { + var tests = map[string]struct { + keyLength int + expectedError error + }{ + "generator successfully created for 20 bit key length": { + keyLength: 20, + }, + + "generator successfully created for 18 bit key length": { + keyLength: 18, + }, + + "generator can't be created for 16 bit key length": { + keyLength: 16, + expectedError: errors.New("Public key bit length must be at least 18 bits"), + }, + } + + for testName, test := range tests { + t.Run(testName, func(t *testing.T) { + gen, err := GetThresholdKeyGenerator(test.keyLength, 4, 3, rand.Reader) + + if !reflect.DeepEqual(test.expectedError, err) { + t.Fatalf( + "Unexpected error\nActual: %v\nExpected: %v", + err, + test.expectedError, + ) + } + + if test.expectedError == nil && gen == nil { + t.Fatal("Got nil generator, it should be successfully created") + } + }) + } +} + func TestGenerateSafePrimesOfThresholdKeyGenerator(t *testing.T) { - tkh := GetThresholdKeyGenerator(32, 4, 3, rand.Reader) + tkh, err := GetThresholdKeyGenerator(32, 4, 3, rand.Reader) + if err != nil { + t.Fatal(err) + } p, q, err := tkh.generateSafePrimes() if err != nil { @@ -22,21 +64,30 @@ func TestGenerateSafePrimesOfThresholdKeyGenerator(t *testing.T) { } func TestInitPandP1(t *testing.T) { - tkh := GetThresholdKeyGenerator(32, 4, 3, rand.Reader) + tkh, err := GetThresholdKeyGenerator(32, 4, 3, rand.Reader) + if err != nil { + t.Fatal(err) + } tkh.initPandP1() AreSafePrimes(tkh.p, tkh.p1, 16, t) } func TestInitQandQ1(t *testing.T) { - tkh := GetThresholdKeyGenerator(32, 4, 3, rand.Reader) + tkh, err := GetThresholdKeyGenerator(32, 4, 3, rand.Reader) + if err != nil { + t.Fatal(err) + } tkh.initQandQ1() AreSafePrimes(tkh.q, tkh.q1, 16, t) } func TestInitPsAndQs(t *testing.T) { - tkh := GetThresholdKeyGenerator(32, 4, 3, rand.Reader) + tkh, err := GetThresholdKeyGenerator(32, 4, 3, rand.Reader) + if err != nil { + t.Fatal(err) + } tkh.initPsAndQs() @@ -95,7 +146,10 @@ func TestInitD(t *testing.T) { } func TestInitNumerialValues(t *testing.T) { - tkh := GetThresholdKeyGenerator(32, 4, 3, rand.Reader) + tkh, err := GetThresholdKeyGenerator(32, 4, 3, rand.Reader) + if err != nil { + t.Fatal(err) + } if err := tkh.initNumerialValues(); err != nil { t.Error(err) @@ -103,7 +157,11 @@ func TestInitNumerialValues(t *testing.T) { } func TestGenerateHidingPolynomial(t *testing.T) { - tkh := GetThresholdKeyGenerator(32, 15, 10, rand.Reader) + tkh, err := GetThresholdKeyGenerator(32, 15, 10, rand.Reader) + if err != nil { + t.Fatal(err) + } + if err := tkh.initNumerialValues(); err != nil { t.Error(err) } @@ -125,7 +183,11 @@ func TestGenerateHidingPolynomial(t *testing.T) { } func TestComputeShare(t *testing.T) { - tkh := GetThresholdKeyGenerator(32, 5, 3, rand.Reader) + tkh, err := GetThresholdKeyGenerator(32, 5, 3, rand.Reader) + if err != nil { + t.Fatal(err) + } + tkh.nm = b(103) tkh.polynomialCoefficients = []*big.Int{b(29), b(88), b(51)} share := tkh.computeShare(2) @@ -135,8 +197,11 @@ func TestComputeShare(t *testing.T) { } func TestCreateShares(t *testing.T) { - tkh := GetThresholdKeyGenerator(32, 100, 10, rand.Reader) - tkh.Random = rand.Reader + tkh, err := GetThresholdKeyGenerator(32, 100, 10, rand.Reader) + if err != nil { + t.Fatal(err) + } + if err := tkh.initNumerialValues(); err != nil { t.Error(err) } @@ -162,14 +227,22 @@ func TestCreateViArray(t *testing.T) { } func TestGetThresholdKeyGenerator(t *testing.T) { - tkh := GetThresholdKeyGenerator(50, 10, 6, rand.Reader) + tkh, err := GetThresholdKeyGenerator(50, 10, 6, rand.Reader) + if err != nil { + t.Fatal(err) + } + if err := tkh.initNumerialValues(); err != nil { t.Error(nil) } } func TestGenerate(t *testing.T) { - tkh := GetThresholdKeyGenerator(32, 10, 6, rand.Reader) + tkh, err := GetThresholdKeyGenerator(32, 10, 6, rand.Reader) + if err != nil { + t.Fatal(err) + } + tpks, err := tkh.Generate() if err != nil { t.Error(err) @@ -195,7 +268,11 @@ func TestGenerate(t *testing.T) { } func TestComputeV(t *testing.T) { - tkh := GetThresholdKeyGenerator(32, 10, 6, rand.Reader) + tkh, err := GetThresholdKeyGenerator(32, 10, 6, rand.Reader) + if err != nil { + t.Fatal(err) + } + tkh.n = b(1907 * 1823) tkh.nSquare = new(big.Int).Mul(tkh.n, tkh.n) for i := 0; i < 100; i++ { diff --git a/thresholdkey_test.go b/thresholdkey_test.go index 26428b3..ce7525e 100644 --- a/thresholdkey_test.go +++ b/thresholdkey_test.go @@ -8,7 +8,11 @@ import ( ) func getThresholdPrivateKey() *ThresholdPrivateKey { - tkh := GetThresholdKeyGenerator(32, 10, 6, rand.Reader) + tkh, err := GetThresholdKeyGenerator(32, 10, 6, rand.Reader) + if err != nil { + panic(err) + } + tpks, err := tkh.Generate() if err != nil { panic(err) @@ -184,7 +188,11 @@ func TestupdateCprime(t *testing.T) { } func TestEncryptingDecryptingSimple(t *testing.T) { - tkh := GetThresholdKeyGenerator(32, 2, 1, rand.Reader) + tkh, err := GetThresholdKeyGenerator(32, 2, 1, rand.Reader) + if err != nil { + t.Fatal(err) + } + tpks, err := tkh.Generate() if err != nil { t.Error(err) @@ -205,7 +213,11 @@ func TestEncryptingDecryptingSimple(t *testing.T) { } func TestEncryptingDecrypting(t *testing.T) { - tkh := GetThresholdKeyGenerator(32, 2, 2, rand.Reader) + tkh, err := GetThresholdKeyGenerator(32, 2, 2, rand.Reader) + if err != nil { + t.Fatal(err) + } + tpks, err := tkh.Generate() if err != nil { t.Error(err) @@ -227,7 +239,11 @@ func TestEncryptingDecrypting(t *testing.T) { } func TestHomomorphicThresholdEncryption(t *testing.T) { - tkh := GetThresholdKeyGenerator(32, 2, 2, rand.Reader) + tkh, err := GetThresholdKeyGenerator(32, 2, 2, rand.Reader) + if err != nil { + t.Fatal(err) + } + tpks, _ := tkh.Generate() plainText1 := b(13) @@ -278,7 +294,11 @@ func TestValidate(t *testing.T) { } func TestCombinePartialDecryptionsZKP(t *testing.T) { - tkh := GetThresholdKeyGenerator(32, 2, 2, rand.Reader) + tkh, err := GetThresholdKeyGenerator(32, 2, 2, rand.Reader) + if err != nil { + t.Fatal(err) + } + tpks, err := tkh.Generate() if err != nil { t.Error(err) @@ -311,7 +331,11 @@ func TestCombinePartialDecryptionsZKP(t *testing.T) { } func TestCombinePartialDecryptionsWith100Shares(t *testing.T) { - tkh := GetThresholdKeyGenerator(32, 100, 50, rand.Reader) + tkh, err := GetThresholdKeyGenerator(32, 100, 50, rand.Reader) + if err != nil { + t.Fatal(err) + } + tpks, err := tkh.Generate() if err != nil { t.Error(err) @@ -338,7 +362,11 @@ func TestCombinePartialDecryptionsWith100Shares(t *testing.T) { } func TestVerifyDecryption(t *testing.T) { - tkh := GetThresholdKeyGenerator(32, 2, 2, rand.Reader) + tkh, err := GetThresholdKeyGenerator(32, 2, 2, rand.Reader) + if err != nil { + t.Fatal(err) + } + tpks, err := tkh.Generate() pk := &tpks[0].ThresholdPublicKey From d5973f86a763735dc6d08a343b6cc67778376e3c Mon Sep 17 00:00:00 2001 From: Piotr Dyraga Date: Wed, 8 Aug 2018 10:56:26 +0200 Subject: [PATCH 04/14] Improved GetThresholdKeyGenerator documentation Added information about the required minimum bit length of the public key. Removed the threshold information as we are not sure now if it's valid. We'll investigate it separately and make sure a proper validation for threshold is made. --- thresholdkey_generator.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/thresholdkey_generator.go b/thresholdkey_generator.go index 79e22d0..70819d4 100644 --- a/thresholdkey_generator.go +++ b/thresholdkey_generator.go @@ -46,10 +46,11 @@ type ThresholdKeyGenerator struct { polynomialCoefficients []*big.Int } -// Preferable way to construct the ThresholdKeyGenerator. No verification -// is done on the input values. You need to be sure that nbits is big enough -// and that Threshold > TotalNumberOfDecryptionServers / 2. -// The plaintext space for the key will be Z_n. +// GetThresholdKeyGenerator is a preferable way to construct the +// ThresholdKeyGenerator. +// Due to the various properties that must be met for the threshold key to be +// considered valid, the minimum public key `N` bit length is 18 bits. +// The plaintext space for the key will be `Z_N`. func GetThresholdKeyGenerator( publicKeyBitLength int, totalNumberOfDecryptionServers int, From 12b3dadef7ea3bf92dd9ee0add814b9cf672980e Mon Sep 17 00:00:00 2001 From: Piotr Dyraga Date: Wed, 8 Aug 2018 11:11:05 +0200 Subject: [PATCH 05/14] Make sure public key bit length is an even number For an odd n-bit number, we can't find two n-1-bit numbers which multiplied gives an n-bit number. That's why public key length n must be an even number. I have also renamed AreSafePrimes to IsSafePrime since only the first function argument is considered to be a safe prime according to a safe prime definition. --- safe_prime_generator_test.go | 2 +- thresholdkey_generator.go | 10 +++++++++- thresholdkey_generator_test.go | 20 +++++++++++++------- utils_test.go | 4 +++- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/safe_prime_generator_test.go b/safe_prime_generator_test.go index 4b57efe..f4a29db 100644 --- a/safe_prime_generator_test.go +++ b/safe_prime_generator_test.go @@ -60,7 +60,7 @@ func TestAsyncGenerator(t *testing.T) { t.Fatal(err) } - AreSafePrimes(p, q, test.bitLen, t) + IsSafePrime(p, q, test.bitLen, t) } }) } diff --git a/thresholdkey_generator.go b/thresholdkey_generator.go index 70819d4..5c2cd22 100644 --- a/thresholdkey_generator.go +++ b/thresholdkey_generator.go @@ -49,7 +49,8 @@ type ThresholdKeyGenerator struct { // GetThresholdKeyGenerator is a preferable way to construct the // ThresholdKeyGenerator. // Due to the various properties that must be met for the threshold key to be -// considered valid, the minimum public key `N` bit length is 18 bits. +// considered valid, the minimum public key `N` bit length is 18 bits and the +// public key bit length should be an even number. // The plaintext space for the key will be `Z_N`. func GetThresholdKeyGenerator( publicKeyBitLength int, @@ -57,7 +58,14 @@ func GetThresholdKeyGenerator( threshold int, random io.Reader, ) (*ThresholdKeyGenerator, error) { + if publicKeyBitLength%2 == 1 { + // For an odd n-bit number, we can't find two n-1-bit numbers which + // multiplied gives an n-bit number. + return nil, errors.New("Public key bit length must be an even number") + } if publicKeyBitLength < 18 { + // We need to find two n-bit safe primes, P and Q which are not equal. + // This is not possible for n<18. return nil, errors.New("Public key bit length must be at least 18 bits") } diff --git a/thresholdkey_generator_test.go b/thresholdkey_generator_test.go index 4a123df..72968a1 100644 --- a/thresholdkey_generator_test.go +++ b/thresholdkey_generator_test.go @@ -20,11 +20,17 @@ func TestCreateThresholdKeyGenerator(t *testing.T) { "generator successfully created for 20 bit key length": { keyLength: 20, }, - + "generator can't be created for 19 bit key length": { + keyLength: 19, + expectedError: errors.New("Public key bit length must be an even number"), + }, "generator successfully created for 18 bit key length": { keyLength: 18, }, - + "generator can't be created for 17 bit key length": { + keyLength: 17, + expectedError: errors.New("Public key bit length must be an even number"), + }, "generator can't be created for 16 bit key length": { keyLength: 16, expectedError: errors.New("Public key bit length must be at least 18 bits"), @@ -60,7 +66,7 @@ func TestGenerateSafePrimesOfThresholdKeyGenerator(t *testing.T) { if err != nil { t.Error(err) } - AreSafePrimes(p, q, 16, t) + IsSafePrime(p, q, 16, t) } func TestInitPandP1(t *testing.T) { @@ -70,7 +76,7 @@ func TestInitPandP1(t *testing.T) { } tkh.initPandP1() - AreSafePrimes(tkh.p, tkh.p1, 16, t) + IsSafePrime(tkh.p, tkh.p1, 16, t) } func TestInitQandQ1(t *testing.T) { @@ -80,7 +86,7 @@ func TestInitQandQ1(t *testing.T) { } tkh.initQandQ1() - AreSafePrimes(tkh.q, tkh.q1, 16, t) + IsSafePrime(tkh.q, tkh.q1, 16, t) } func TestInitPsAndQs(t *testing.T) { @@ -91,8 +97,8 @@ func TestInitPsAndQs(t *testing.T) { tkh.initPsAndQs() - AreSafePrimes(tkh.p, tkh.p1, 16, t) - AreSafePrimes(tkh.q, tkh.q1, 16, t) + IsSafePrime(tkh.p, tkh.p1, 16, t) + IsSafePrime(tkh.q, tkh.q1, 16, t) } func TestArePsAndQsGood(t *testing.T) { diff --git a/utils_test.go b/utils_test.go index cd86b9b..81a3772 100644 --- a/utils_test.go +++ b/utils_test.go @@ -59,7 +59,9 @@ func TestFactorial(t *testing.T) { } } -func AreSafePrimes(p, q *big.Int, expectedLength int, t *testing.T) { +// IsSafePrime checks whether `p` is a safe prime. A safe prime is a prime +// number of the form `2q + 1`, where `q` is also a prime. +func IsSafePrime(p, q *big.Int, expectedLength int, t *testing.T) { if l := p.BitLen(); l != expectedLength { t.Error("p does not have the good length. ", l) } From 0d2a123af76417b75dd91eb1927575f5aa0fb6f2 Mon Sep 17 00:00:00 2001 From: Piotr Dyraga Date: Wed, 8 Aug 2018 11:28:25 +0200 Subject: [PATCH 06/14] Fixed comment for safe primes required bit length Safe primes bit length is n-1 for n-bit public key --- thresholdkey_generator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thresholdkey_generator.go b/thresholdkey_generator.go index 5c2cd22..ebcf305 100644 --- a/thresholdkey_generator.go +++ b/thresholdkey_generator.go @@ -64,7 +64,7 @@ func GetThresholdKeyGenerator( return nil, errors.New("Public key bit length must be an even number") } if publicKeyBitLength < 18 { - // We need to find two n-bit safe primes, P and Q which are not equal. + // We need to find two n-1-bit safe primes, P and Q which are not equal. // This is not possible for n<18. return nil, errors.New("Public key bit length must be at least 18 bits") } From 51b5bac262e177eeaee008c09c7a029f6424c13d Mon Sep 17 00:00:00 2001 From: Piotr Dyraga Date: Wed, 8 Aug 2018 13:16:25 +0200 Subject: [PATCH 07/14] Create and return ThresholdKeyGenerator in one shot. --- thresholdkey_generator.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/thresholdkey_generator.go b/thresholdkey_generator.go index ebcf305..5c98755 100644 --- a/thresholdkey_generator.go +++ b/thresholdkey_generator.go @@ -69,12 +69,12 @@ func GetThresholdKeyGenerator( return nil, errors.New("Public key bit length must be at least 18 bits") } - generator := new(ThresholdKeyGenerator) - generator.publicKeyBitLength = publicKeyBitLength - generator.TotalNumberOfDecryptionServers = totalNumberOfDecryptionServers - generator.Threshold = threshold - generator.Random = random - return generator, nil + return &ThresholdKeyGenerator{ + publicKeyBitLength: publicKeyBitLength, + TotalNumberOfDecryptionServers: totalNumberOfDecryptionServers, + Threshold: threshold, + Random: random, + }, nil } func (tkg *ThresholdKeyGenerator) generateSafePrimes() (*big.Int, *big.Int, error) { From e726ad489b924977a0592336cb4592fe87ff92be Mon Sep 17 00:00:00 2001 From: Piotr Dyraga Date: Wed, 8 Aug 2018 13:19:20 +0200 Subject: [PATCH 08/14] PublicKeyBitLength made an exported value just like all the others --- thresholdkey_generator.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/thresholdkey_generator.go b/thresholdkey_generator.go index 5c98755..1b095e7 100644 --- a/thresholdkey_generator.go +++ b/thresholdkey_generator.go @@ -20,7 +20,7 @@ import ( // with Applications to Electronic Voting // Aarhus University, Dept. of Computer Science, BRICS type ThresholdKeyGenerator struct { - publicKeyBitLength int + PublicKeyBitLength int TotalNumberOfDecryptionServers int Threshold int Random io.Reader @@ -70,7 +70,7 @@ func GetThresholdKeyGenerator( } return &ThresholdKeyGenerator{ - publicKeyBitLength: publicKeyBitLength, + PublicKeyBitLength: publicKeyBitLength, TotalNumberOfDecryptionServers: totalNumberOfDecryptionServers, Threshold: threshold, Random: random, @@ -80,7 +80,7 @@ func GetThresholdKeyGenerator( func (tkg *ThresholdKeyGenerator) generateSafePrimes() (*big.Int, *big.Int, error) { concurrencyLevel := 4 timeout := 120 * time.Second - safePrimeBitLength := tkg.publicKeyBitLength / 2 + safePrimeBitLength := tkg.PublicKeyBitLength / 2 return GenerateSafePrime(safePrimeBitLength, concurrencyLevel, timeout, tkg.Random) } From 533255862585e9f2603f20a9272122cd997d65bc Mon Sep 17 00:00:00 2001 From: Piotr Dyraga Date: Wed, 8 Aug 2018 13:22:02 +0200 Subject: [PATCH 09/14] ThresholdKeyGenerator random reference is no longer exported --- thresholdkey_generator.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/thresholdkey_generator.go b/thresholdkey_generator.go index 1b095e7..9c360dd 100644 --- a/thresholdkey_generator.go +++ b/thresholdkey_generator.go @@ -23,7 +23,7 @@ type ThresholdKeyGenerator struct { PublicKeyBitLength int TotalNumberOfDecryptionServers int Threshold int - Random io.Reader + random io.Reader // Both p1 and q1 are primes of length nbits - 1 p1 *big.Int @@ -73,7 +73,7 @@ func GetThresholdKeyGenerator( PublicKeyBitLength: publicKeyBitLength, TotalNumberOfDecryptionServers: totalNumberOfDecryptionServers, Threshold: threshold, - Random: random, + random: random, }, nil } @@ -82,7 +82,7 @@ func (tkg *ThresholdKeyGenerator) generateSafePrimes() (*big.Int, *big.Int, erro timeout := 120 * time.Second safePrimeBitLength := tkg.PublicKeyBitLength / 2 - return GenerateSafePrime(safePrimeBitLength, concurrencyLevel, timeout, tkg.Random) + return GenerateSafePrime(safePrimeBitLength, concurrencyLevel, timeout, tkg.random) } func (tkg *ThresholdKeyGenerator) initPandP1() error { @@ -133,7 +133,7 @@ func (tkg *ThresholdKeyGenerator) initPsAndQs() error { // v generates a cyclic group of squares in Zn^2. func (tkg *ThresholdKeyGenerator) computeV() error { var err error - tkg.v, err = GetRandomGeneratorOfTheQuadraticResidue(tkg.nSquare, tkg.Random) + tkg.v, err = GetRandomGeneratorOfTheQuadraticResidue(tkg.nSquare, tkg.random) return err } @@ -186,7 +186,7 @@ func (tkg *ThresholdKeyGenerator) generateHidingPolynomial() error { tkg.polynomialCoefficients[0] = tkg.d var err error for i := 1; i < tkg.Threshold; i++ { - tkg.polynomialCoefficients[i], err = rand.Int(tkg.Random, tkg.nm) + tkg.polynomialCoefficients[i], err = rand.Int(tkg.random, tkg.nm) if err != nil { return err } From 5d09ad8609386eb123159bcdca61d5fd43ecf52f Mon Sep 17 00:00:00 2001 From: Piotr Dyraga Date: Wed, 8 Aug 2018 13:46:51 +0200 Subject: [PATCH 10/14] Added test ensuring correct n,p,q bit length for threshold key-gen --- thresholdkey_generator_test.go | 69 ++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 8 deletions(-) diff --git a/thresholdkey_generator_test.go b/thresholdkey_generator_test.go index 72968a1..f344e15 100644 --- a/thresholdkey_generator_test.go +++ b/thresholdkey_generator_test.go @@ -56,17 +56,70 @@ func TestCreateThresholdKeyGenerator(t *testing.T) { } } -func TestGenerateSafePrimesOfThresholdKeyGenerator(t *testing.T) { - tkh, err := GetThresholdKeyGenerator(32, 4, 3, rand.Reader) - if err != nil { - t.Fatal(err) +func TestGenerateNumbersOfCorrectBitLength(t *testing.T) { + var tests = map[string]struct { + keyLength int + expectedPrimeLength int + expectedModulusLength int + }{ + "key bit length = 32, prime bit length = 16, modulus bit length = 32": { + keyLength: 32, + expectedPrimeLength: 16, + expectedModulusLength: 32, + }, + "key bit length = 64, prime bit length = 32, modulus bit length = 64": { + keyLength: 64, + expectedPrimeLength: 32, + expectedModulusLength: 64, + }, + "key bit length = 128, prime bit length = 64, modulus bit length = 128": { + keyLength: 128, + expectedPrimeLength: 64, + expectedModulusLength: 128, + }, } - p, q, err := tkh.generateSafePrimes() - if err != nil { - t.Error(err) + for testName, test := range tests { + t.Run(testName, func(t *testing.T) { + gen, err := GetThresholdKeyGenerator(test.keyLength, 10, 6, rand.Reader) + if err != nil { + t.Fatal(err) + } + + err = gen.initNumerialValues() + if err != nil { + t.Fatal(err) + } + + if gen.p.BitLen() != test.expectedPrimeLength { + t.Fatalf( + "Unexpected prime bit length\nExpected %v\n Actual %v", + test.expectedModulusLength, + gen.p.BitLen(), + ) + } + + if gen.q.BitLen() != test.expectedPrimeLength { + t.Fatalf( + "Unexpected prime bit length\nExpected %v\n Actual %v", + test.expectedModulusLength, + gen.q.BitLen(), + ) + } + + if gen.n.BitLen() != test.expectedModulusLength { + t.Fatalf( + "Unexpected modulus bit length\nExpected %v\n Actual %v", + test.expectedModulusLength, + gen.n.BitLen(), + ) + } + + if new(big.Int).Mul(gen.p, gen.q).Cmp(gen.n) != 0 { + t.Fatal("n != pq") + } + }) } - IsSafePrime(p, q, 16, t) } func TestInitPandP1(t *testing.T) { From 0084a3762b0a63248be6dfd6d51ed9ca1616fdcf Mon Sep 17 00:00:00 2001 From: Piotr Dyraga Date: Wed, 8 Aug 2018 13:50:23 +0200 Subject: [PATCH 11/14] Fixed documentation about the bit length of p,q and n --- thresholdkey_generator.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/thresholdkey_generator.go b/thresholdkey_generator.go index 9c360dd..f7ef687 100644 --- a/thresholdkey_generator.go +++ b/thresholdkey_generator.go @@ -25,13 +25,13 @@ type ThresholdKeyGenerator struct { Threshold int random io.Reader - // Both p1 and q1 are primes of length nbits - 1 - p1 *big.Int - q1 *big.Int + p *big.Int // p is prime of `PublicKeyBitLength/2` bits and `p = 2*p1 + 1` + q *big.Int // q is prime of `PublicKeyBitLength/2` bits and `q = 2*q1 + 1` - p *big.Int // p is prime and p=2*p1+1 - q *big.Int // q is prime and q=2*q1+1 - n *big.Int // n=p*q + p1 *big.Int // p1 is prime of `PublicKeyBitLength/2 - 1` bits + q1 *big.Int // q1 is prime of `PublicKeyBitLength/2 - 1` bits + + n *big.Int // n=p*q and is of `PublicKeyBitLength` bits m *big.Int // m = p1*q1 nSquare *big.Int // nSquare = n*n nm *big.Int // nm = n*m @@ -59,12 +59,13 @@ func GetThresholdKeyGenerator( random io.Reader, ) (*ThresholdKeyGenerator, error) { if publicKeyBitLength%2 == 1 { - // For an odd n-bit number, we can't find two n-1-bit numbers which - // multiplied gives an n-bit number. + // For an odd n-bit number, we can't find two n/2-bit numbers with two + // the most significant bits set on which multiplied gives an n-bit + // number. return nil, errors.New("Public key bit length must be an even number") } if publicKeyBitLength < 18 { - // We need to find two n-1-bit safe primes, P and Q which are not equal. + // We need to find two n/2-bit safe primes, P and Q which are not equal. // This is not possible for n<18. return nil, errors.New("Public key bit length must be at least 18 bits") } From 695e73602def6bab548107b88e71bdf8d9951795 Mon Sep 17 00:00:00 2001 From: Piotr Dyraga Date: Wed, 8 Aug 2018 14:03:35 +0200 Subject: [PATCH 12/14] Updated test to make sure all generator ctor values are set correctly --- thresholdkey_generator_test.go | 71 +++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 13 deletions(-) diff --git a/thresholdkey_generator_test.go b/thresholdkey_generator_test.go index f344e15..7c5ba74 100644 --- a/thresholdkey_generator_test.go +++ b/thresholdkey_generator_test.go @@ -14,32 +14,49 @@ var MockGenerateSafePrimes = func() (*big.Int, *big.Int, error) { func TestCreateThresholdKeyGenerator(t *testing.T) { var tests = map[string]struct { - keyLength int - expectedError error + keyLength int + totalNumberOfDecryptionServers int + threshold int + expectedError error }{ "generator successfully created for 20 bit key length": { - keyLength: 20, + keyLength: 20, + totalNumberOfDecryptionServers: 6, + threshold: 5, }, "generator can't be created for 19 bit key length": { - keyLength: 19, - expectedError: errors.New("Public key bit length must be an even number"), + keyLength: 19, + totalNumberOfDecryptionServers: 4, + threshold: 3, + expectedError: errors.New("Public key bit length must be an even number"), }, "generator successfully created for 18 bit key length": { - keyLength: 18, + keyLength: 18, + totalNumberOfDecryptionServers: 4, + threshold: 3, }, "generator can't be created for 17 bit key length": { - keyLength: 17, - expectedError: errors.New("Public key bit length must be an even number"), + keyLength: 17, + totalNumberOfDecryptionServers: 4, + threshold: 3, + expectedError: errors.New("Public key bit length must be an even number"), }, "generator can't be created for 16 bit key length": { - keyLength: 16, - expectedError: errors.New("Public key bit length must be at least 18 bits"), + keyLength: 16, + totalNumberOfDecryptionServers: 4, + threshold: 3, + expectedError: errors.New("Public key bit length must be at least 18 bits"), }, } for testName, test := range tests { t.Run(testName, func(t *testing.T) { - gen, err := GetThresholdKeyGenerator(test.keyLength, 4, 3, rand.Reader) + gen, err := GetThresholdKeyGenerator( + test.keyLength, + test.totalNumberOfDecryptionServers, + test.threshold, + rand.Reader, + ) if !reflect.DeepEqual(test.expectedError, err) { t.Fatalf( @@ -49,8 +66,36 @@ func TestCreateThresholdKeyGenerator(t *testing.T) { ) } - if test.expectedError == nil && gen == nil { - t.Fatal("Got nil generator, it should be successfully created") + if test.expectedError == nil { + if gen == nil { + t.Fatal( + "Got nil generator, it should be successfully created", + ) + } + + if test.keyLength != gen.PublicKeyBitLength { + t.Fatalf( + "Unexpected public key length\nExpected: %v\nActual: %v", + test.keyLength, + gen.PublicKeyBitLength, + ) + } + + if test.threshold != gen.Threshold { + t.Fatalf( + "Unexpected threshold\nExpected: %v\nActual: %v", + test.threshold, + gen.Threshold, + ) + } + + if test.totalNumberOfDecryptionServers != gen.TotalNumberOfDecryptionServers { + t.Fatalf( + "Unexpected number of decryption servers\nExpected: %v\nActual: %v", + test.totalNumberOfDecryptionServers, + gen.TotalNumberOfDecryptionServers, + ) + } } }) } From 1d535dc74069ee7ee421feae3b1a57eb02dad24c Mon Sep 17 00:00:00 2001 From: Piotr Dyraga Date: Wed, 8 Aug 2018 15:11:43 +0200 Subject: [PATCH 13/14] Simplified ThresholdKeyGenerator factory function tests --- thresholdkey_generator_test.go | 52 ++++++++++++++-------------------- 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/thresholdkey_generator_test.go b/thresholdkey_generator_test.go index 7c5ba74..754db8d 100644 --- a/thresholdkey_generator_test.go +++ b/thresholdkey_generator_test.go @@ -14,35 +14,35 @@ var MockGenerateSafePrimes = func() (*big.Int, *big.Int, error) { func TestCreateThresholdKeyGenerator(t *testing.T) { var tests = map[string]struct { - keyLength int + publicKeyBitLength int totalNumberOfDecryptionServers int threshold int expectedError error }{ "generator successfully created for 20 bit key length": { - keyLength: 20, + publicKeyBitLength: 20, totalNumberOfDecryptionServers: 6, threshold: 5, }, "generator can't be created for 19 bit key length": { - keyLength: 19, + publicKeyBitLength: 19, totalNumberOfDecryptionServers: 4, threshold: 3, expectedError: errors.New("Public key bit length must be an even number"), }, "generator successfully created for 18 bit key length": { - keyLength: 18, + publicKeyBitLength: 18, totalNumberOfDecryptionServers: 4, threshold: 3, }, "generator can't be created for 17 bit key length": { - keyLength: 17, + publicKeyBitLength: 17, totalNumberOfDecryptionServers: 4, threshold: 3, expectedError: errors.New("Public key bit length must be an even number"), }, "generator can't be created for 16 bit key length": { - keyLength: 16, + publicKeyBitLength: 16, totalNumberOfDecryptionServers: 4, threshold: 3, expectedError: errors.New("Public key bit length must be at least 18 bits"), @@ -52,7 +52,7 @@ func TestCreateThresholdKeyGenerator(t *testing.T) { for testName, test := range tests { t.Run(testName, func(t *testing.T) { gen, err := GetThresholdKeyGenerator( - test.keyLength, + test.publicKeyBitLength, test.totalNumberOfDecryptionServers, test.threshold, rand.Reader, @@ -73,10 +73,10 @@ func TestCreateThresholdKeyGenerator(t *testing.T) { ) } - if test.keyLength != gen.PublicKeyBitLength { + if test.publicKeyBitLength != gen.PublicKeyBitLength { t.Fatalf( "Unexpected public key length\nExpected: %v\nActual: %v", - test.keyLength, + test.publicKeyBitLength, gen.PublicKeyBitLength, ) } @@ -103,30 +103,22 @@ func TestCreateThresholdKeyGenerator(t *testing.T) { func TestGenerateNumbersOfCorrectBitLength(t *testing.T) { var tests = map[string]struct { - keyLength int - expectedPrimeLength int - expectedModulusLength int + publicKeyLength int + expectedPrimeLength int }{ - "key bit length = 32, prime bit length = 16, modulus bit length = 32": { - keyLength: 32, - expectedPrimeLength: 16, - expectedModulusLength: 32, + "public key bit length = 32, prime bit length = 16": { + publicKeyLength: 32, + expectedPrimeLength: 16, }, - "key bit length = 64, prime bit length = 32, modulus bit length = 64": { - keyLength: 64, - expectedPrimeLength: 32, - expectedModulusLength: 64, - }, - "key bit length = 128, prime bit length = 64, modulus bit length = 128": { - keyLength: 128, - expectedPrimeLength: 64, - expectedModulusLength: 128, + "public key bit length = 64, prime bit length = 32": { + publicKeyLength: 64, + expectedPrimeLength: 32, }, } for testName, test := range tests { t.Run(testName, func(t *testing.T) { - gen, err := GetThresholdKeyGenerator(test.keyLength, 10, 6, rand.Reader) + gen, err := GetThresholdKeyGenerator(test.publicKeyLength, 10, 6, rand.Reader) if err != nil { t.Fatal(err) } @@ -139,7 +131,7 @@ func TestGenerateNumbersOfCorrectBitLength(t *testing.T) { if gen.p.BitLen() != test.expectedPrimeLength { t.Fatalf( "Unexpected prime bit length\nExpected %v\n Actual %v", - test.expectedModulusLength, + test.expectedPrimeLength, gen.p.BitLen(), ) } @@ -147,15 +139,15 @@ func TestGenerateNumbersOfCorrectBitLength(t *testing.T) { if gen.q.BitLen() != test.expectedPrimeLength { t.Fatalf( "Unexpected prime bit length\nExpected %v\n Actual %v", - test.expectedModulusLength, + test.expectedPrimeLength, gen.q.BitLen(), ) } - if gen.n.BitLen() != test.expectedModulusLength { + if gen.n.BitLen() != test.publicKeyLength { t.Fatalf( "Unexpected modulus bit length\nExpected %v\n Actual %v", - test.expectedModulusLength, + test.publicKeyLength, gen.n.BitLen(), ) } From 88d1beaf509aa31303725484108dd4bf544edfab Mon Sep 17 00:00:00 2001 From: Jakub Nowakowski Date: Thu, 9 Aug 2018 14:04:41 +0200 Subject: [PATCH 14/14] Added IDEs specific directories to gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index daf913b..c3d7296 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,7 @@ _testmain.go *.exe *.test *.prof + +# IDEs +.vscode/ +.idea/