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.
This commit is contained in:
Piotr Dyraga committed 2018-08-08 11:11:05 +02:00
1 parent d5973f86a7
commit 12b3dadef7
4 files changed
+26 -10

No files matched your search

+1 -1
View File
@@ -60,7 +60,7 @@ func TestAsyncGenerator(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
AreSafePrimes(p, q, test.bitLen, t) IsSafePrime(p, q, test.bitLen, t)
} }
}) })
} }
+9 -1
View File
@@ -49,7 +49,8 @@ type ThresholdKeyGenerator struct {
// GetThresholdKeyGenerator is a preferable way to construct the // GetThresholdKeyGenerator is a preferable way to construct the
// ThresholdKeyGenerator. // ThresholdKeyGenerator.
// Due to the various properties that must be met for the threshold key to be // 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`. // The plaintext space for the key will be `Z_N`.
func GetThresholdKeyGenerator( func GetThresholdKeyGenerator(
publicKeyBitLength int, publicKeyBitLength int,
@@ -57,7 +58,14 @@ func GetThresholdKeyGenerator(
threshold int, threshold int,
random io.Reader, random io.Reader,
) (*ThresholdKeyGenerator, error) { ) (*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 { 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") return nil, errors.New("Public key bit length must be at least 18 bits")
} }
+13 -7
View File
@@ -20,11 +20,17 @@ func TestCreateThresholdKeyGenerator(t *testing.T) {
"generator successfully created for 20 bit key length": { "generator successfully created for 20 bit key length": {
keyLength: 20, 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": { "generator successfully created for 18 bit key length": {
keyLength: 18, 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": { "generator can't be created for 16 bit key length": {
keyLength: 16, keyLength: 16,
expectedError: errors.New("Public key bit length must be at least 18 bits"), 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 { if err != nil {
t.Error(err) t.Error(err)
} }
AreSafePrimes(p, q, 16, t) IsSafePrime(p, q, 16, t)
} }
func TestInitPandP1(t *testing.T) { func TestInitPandP1(t *testing.T) {
@@ -70,7 +76,7 @@ func TestInitPandP1(t *testing.T) {
} }
tkh.initPandP1() tkh.initPandP1()
AreSafePrimes(tkh.p, tkh.p1, 16, t) IsSafePrime(tkh.p, tkh.p1, 16, t)
} }
func TestInitQandQ1(t *testing.T) { func TestInitQandQ1(t *testing.T) {
@@ -80,7 +86,7 @@ func TestInitQandQ1(t *testing.T) {
} }
tkh.initQandQ1() tkh.initQandQ1()
AreSafePrimes(tkh.q, tkh.q1, 16, t) IsSafePrime(tkh.q, tkh.q1, 16, t)
} }
func TestInitPsAndQs(t *testing.T) { func TestInitPsAndQs(t *testing.T) {
@@ -91,8 +97,8 @@ func TestInitPsAndQs(t *testing.T) {
tkh.initPsAndQs() tkh.initPsAndQs()
AreSafePrimes(tkh.p, tkh.p1, 16, t) IsSafePrime(tkh.p, tkh.p1, 16, t)
AreSafePrimes(tkh.q, tkh.q1, 16, t) IsSafePrime(tkh.q, tkh.q1, 16, t)
} }
func TestArePsAndQsGood(t *testing.T) { func TestArePsAndQsGood(t *testing.T) {
+3 -1
View File
@@ -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 { if l := p.BitLen(); l != expectedLength {
t.Error("p does not have the good length. ", l) t.Error("p does not have the good length. ", l)
} }