From 12b3dadef7ea3bf92dd9ee0add814b9cf672980e Mon Sep 17 00:00:00 2001 From: Piotr Dyraga Date: Wed, 8 Aug 2018 11:11:05 +0200 Subject: [PATCH] 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) }