From 3c6123c0e36eddb75a599f7976b1f4c30ae226d8 Mon Sep 17 00:00:00 2001 From: Piotr Dyraga Date: Tue, 7 Aug 2018 16:11:26 +0200 Subject: [PATCH] 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