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.
This commit is contained in:
Piotr Dyraga committed 2018-08-08 10:44:02 +02:00
1 parent 3a27db63e5
commit 3c6123c0e3
3 files changed
+32 -25

No files matched your search

+13 -6
View File
@@ -19,7 +19,7 @@ import (
// with Applications to Electronic Voting // with Applications to Electronic Voting
// Aarhus University, Dept. of Computer Science, BRICS // Aarhus University, Dept. of Computer Science, BRICS
type ThresholdKeyGenerator struct { type ThresholdKeyGenerator struct {
nbits int publicKeyBitLength int
TotalNumberOfDecryptionServers int TotalNumberOfDecryptionServers int
Threshold int Threshold int
Random io.Reader 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 // is done on the input values. You need to be sure that nbits is big enough
// and that Threshold > TotalNumberOfDecryptionServers / 2. // and that Threshold > TotalNumberOfDecryptionServers / 2.
// The plaintext space for the key will be Z_n. // 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 := new(ThresholdKeyGenerator)
ret.nbits = nbits ret.publicKeyBitLength = publicKeyBitLength // TODO: at least 32
ret.TotalNumberOfDecryptionServers = TotalNumberOfDecryptionServers ret.TotalNumberOfDecryptionServers = totalNumberOfDecryptionServers
ret.Threshold = Threshold ret.Threshold = threshold
ret.Random = random ret.Random = random
return ret return ret
} }
@@ -61,7 +66,9 @@ func GetThresholdKeyGenerator(nbits, TotalNumberOfDecryptionServers, Threshold i
func (tkg *ThresholdKeyGenerator) generateSafePrimes() (*big.Int, *big.Int, error) { func (tkg *ThresholdKeyGenerator) generateSafePrimes() (*big.Int, *big.Int, error) {
concurrencyLevel := 4 concurrencyLevel := 4
timeout := 120 * time.Second 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 { func (tkg *ThresholdKeyGenerator) initPandP1() error {
+13 -13
View File
@@ -13,42 +13,42 @@ var MockGenerateSafePrimes = func() (*big.Int, *big.Int, error) {
func TestGenerateSafePrimesOfThresholdKeyGenerator(t *testing.T) { func TestGenerateSafePrimesOfThresholdKeyGenerator(t *testing.T) {
tkh := new(ThresholdKeyGenerator) tkh := new(ThresholdKeyGenerator)
tkh.nbits = 10 tkh.publicKeyBitLength = 32
tkh.Random = rand.Reader tkh.Random = rand.Reader
p, q, err := tkh.generateSafePrimes() p, q, err := tkh.generateSafePrimes()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
AreSafePrimes(p, q, 10, t) AreSafePrimes(p, q, 16, t)
} }
func TestInitPandP1(t *testing.T) { func TestInitPandP1(t *testing.T) {
tkh := new(ThresholdKeyGenerator) tkh := new(ThresholdKeyGenerator)
tkh.nbits = 10 tkh.publicKeyBitLength = 32
tkh.Random = rand.Reader tkh.Random = rand.Reader
tkh.initPandP1() tkh.initPandP1()
AreSafePrimes(tkh.p, tkh.p1, 10, t) AreSafePrimes(tkh.p, tkh.p1, 16, t)
} }
func TestInitQandQ1(t *testing.T) { func TestInitQandQ1(t *testing.T) {
tkh := new(ThresholdKeyGenerator) tkh := new(ThresholdKeyGenerator)
tkh.nbits = 10 tkh.publicKeyBitLength = 32
tkh.Random = rand.Reader tkh.Random = rand.Reader
tkh.initQandQ1() tkh.initQandQ1()
AreSafePrimes(tkh.q, tkh.q1, 10, t) AreSafePrimes(tkh.q, tkh.q1, 16, t)
} }
func TestInitPsAndQs(t *testing.T) { func TestInitPsAndQs(t *testing.T) {
tkh := new(ThresholdKeyGenerator) tkh := new(ThresholdKeyGenerator)
tkh.nbits = 10 tkh.publicKeyBitLength = 32
tkh.Random = rand.Reader tkh.Random = rand.Reader
tkh.initPsAndQs() tkh.initPsAndQs()
AreSafePrimes(tkh.p, tkh.p1, 10, t) AreSafePrimes(tkh.p, tkh.p1, 16, t)
AreSafePrimes(tkh.q, tkh.q1, 10, t) AreSafePrimes(tkh.q, tkh.q1, 16, t)
} }
func TestArePsAndQsGood(t *testing.T) { func TestArePsAndQsGood(t *testing.T) {
@@ -103,7 +103,7 @@ func TestInitD(t *testing.T) {
func TestInitNumerialValues(t *testing.T) { func TestInitNumerialValues(t *testing.T) {
tkh := new(ThresholdKeyGenerator) tkh := new(ThresholdKeyGenerator)
tkh.nbits = 10 tkh.publicKeyBitLength = 32
tkh.Random = rand.Reader tkh.Random = rand.Reader
if err := tkh.initNumerialValues(); err != nil { if err := tkh.initNumerialValues(); err != nil {
@@ -113,7 +113,7 @@ func TestInitNumerialValues(t *testing.T) {
func TestGenerateHidingPolynomial(t *testing.T) { func TestGenerateHidingPolynomial(t *testing.T) {
tkh := new(ThresholdKeyGenerator) tkh := new(ThresholdKeyGenerator)
tkh.nbits = 10 tkh.publicKeyBitLength = 32
tkh.Threshold = 10 tkh.Threshold = 10
tkh.Random = rand.Reader tkh.Random = rand.Reader
if err := tkh.initNumerialValues(); err != nil { if err := tkh.initNumerialValues(); err != nil {
@@ -138,7 +138,7 @@ func TestGenerateHidingPolynomial(t *testing.T) {
func TestComputeShare(t *testing.T) { func TestComputeShare(t *testing.T) {
tkh := new(ThresholdKeyGenerator) tkh := new(ThresholdKeyGenerator)
tkh.nbits = 10 tkh.publicKeyBitLength = 32
tkh.Threshold = 3 tkh.Threshold = 3
tkh.TotalNumberOfDecryptionServers = 5 tkh.TotalNumberOfDecryptionServers = 5
tkh.nm = b(103) tkh.nm = b(103)
@@ -151,7 +151,7 @@ func TestComputeShare(t *testing.T) {
func TestCreateShares(t *testing.T) { func TestCreateShares(t *testing.T) {
tkh := new(ThresholdKeyGenerator) tkh := new(ThresholdKeyGenerator)
tkh.nbits = 10 tkh.publicKeyBitLength = 32
tkh.Threshold = 10 tkh.Threshold = 10
tkh.TotalNumberOfDecryptionServers = 100 tkh.TotalNumberOfDecryptionServers = 100
tkh.Random = rand.Reader tkh.Random = rand.Reader
+6 -6
View File
@@ -184,7 +184,7 @@ func TestupdateCprime(t *testing.T) {
} }
func TestEncryptingDecryptingSimple(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() tpks, err := tkh.Generate()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@@ -205,7 +205,7 @@ func TestEncryptingDecryptingSimple(t *testing.T) {
} }
func TestEncryptingDecrypting(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() tpks, err := tkh.Generate()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@@ -227,7 +227,7 @@ func TestEncryptingDecrypting(t *testing.T) {
} }
func TestHomomorphicThresholdEncryption(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() tpks, _ := tkh.Generate()
plainText1 := b(13) plainText1 := b(13)
@@ -278,7 +278,7 @@ func TestValidate(t *testing.T) {
} }
func TestCombinePartialDecryptionsZKP(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() tpks, err := tkh.Generate()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@@ -311,7 +311,7 @@ func TestCombinePartialDecryptionsZKP(t *testing.T) {
} }
func TestCombinePartialDecryptionsWith100Shares(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() tpks, err := tkh.Generate()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@@ -338,7 +338,7 @@ func TestCombinePartialDecryptionsWith100Shares(t *testing.T) {
} }
func TestVerifyDecryption(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() tpks, err := tkh.Generate()
pk := &tpks[0].ThresholdPublicKey pk := &tpks[0].ThresholdPublicKey