Pass a public key bit length when creating a threshold key generator #11

Merged
pdyraga merged 16 commits from master into master 2018-08-10 06:50:47 -04:00
3 changed files with 136 additions and 26 deletions
Showing only changes of commit 3394a6136a - Show all commits

No files matched your search

+12 -7
View File
@@ -2,6 +2,7 @@ package paillier
import ( import (
"crypto/rand" "crypto/rand"
"errors"
"io" "io"
"math/big" "math/big"
"time" "time"
@@ -54,13 +55,17 @@ func GetThresholdKeyGenerator(
totalNumberOfDecryptionServers int, totalNumberOfDecryptionServers int,
threshold int, threshold int,
random io.Reader, random io.Reader,
) *ThresholdKeyGenerator { ) (*ThresholdKeyGenerator, error) {
ret := new(ThresholdKeyGenerator) if publicKeyBitLength < 18 {
ret.publicKeyBitLength = publicKeyBitLength // TODO: at least 32 return nil, errors.New("Public key bit length must be at least 18 bits")
ret.TotalNumberOfDecryptionServers = totalNumberOfDecryptionServers }
ret.Threshold = threshold
ret.Random = random generator := new(ThresholdKeyGenerator)
return ret generator.publicKeyBitLength = publicKeyBitLength
generator.TotalNumberOfDecryptionServers = totalNumberOfDecryptionServers
generator.Threshold = threshold
generator.Random = random
return generator, nil
} }
func (tkg *ThresholdKeyGenerator) generateSafePrimes() (*big.Int, *big.Int, error) { func (tkg *ThresholdKeyGenerator) generateSafePrimes() (*big.Int, *big.Int, error) {
+89 -12
View File
@@ -2,6 +2,7 @@ package paillier
import ( import (
"crypto/rand" "crypto/rand"
"errors"
"math/big" "math/big"
"reflect" "reflect"
"testing" "testing"
@@ -11,8 +12,49 @@ var MockGenerateSafePrimes = func() (*big.Int, *big.Int, error) {
return big.NewInt(887), big.NewInt(443), nil 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) { 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() p, q, err := tkh.generateSafePrimes()
if err != nil { if err != nil {
@@ -22,21 +64,30 @@ func TestGenerateSafePrimesOfThresholdKeyGenerator(t *testing.T) {
} }
func TestInitPandP1(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() tkh.initPandP1()
AreSafePrimes(tkh.p, tkh.p1, 16, t) AreSafePrimes(tkh.p, tkh.p1, 16, t)
} }
func TestInitQandQ1(t *testing.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() tkh.initQandQ1()
AreSafePrimes(tkh.q, tkh.q1, 16, t) AreSafePrimes(tkh.q, tkh.q1, 16, t)
} }
func TestInitPsAndQs(t *testing.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() tkh.initPsAndQs()
@@ -95,7 +146,10 @@ func TestInitD(t *testing.T) {
} }
func TestInitNumerialValues(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 { if err := tkh.initNumerialValues(); err != nil {
t.Error(err) t.Error(err)
@@ -103,7 +157,11 @@ func TestInitNumerialValues(t *testing.T) {
} }
func TestGenerateHidingPolynomial(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 { if err := tkh.initNumerialValues(); err != nil {
t.Error(err) t.Error(err)
} }
@@ -125,7 +183,11 @@ func TestGenerateHidingPolynomial(t *testing.T) {
} }
func TestComputeShare(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.nm = b(103)
tkh.polynomialCoefficients = []*big.Int{b(29), b(88), b(51)} tkh.polynomialCoefficients = []*big.Int{b(29), b(88), b(51)}
share := tkh.computeShare(2) share := tkh.computeShare(2)
@@ -135,8 +197,11 @@ func TestComputeShare(t *testing.T) {
} }
func TestCreateShares(t *testing.T) { func TestCreateShares(t *testing.T) {
tkh := GetThresholdKeyGenerator(32, 100, 10, rand.Reader) tkh, err := GetThresholdKeyGenerator(32, 100, 10, rand.Reader)
tkh.Random = rand.Reader if err != nil {
t.Fatal(err)
}
if err := tkh.initNumerialValues(); err != nil { if err := tkh.initNumerialValues(); err != nil {
t.Error(err) t.Error(err)
} }
@@ -162,14 +227,22 @@ func TestCreateViArray(t *testing.T) {
} }
func TestGetThresholdKeyGenerator(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 { if err := tkh.initNumerialValues(); err != nil {
t.Error(nil) t.Error(nil)
} }
} }
func TestGenerate(t *testing.T) { 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() tpks, err := tkh.Generate()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@@ -195,7 +268,11 @@ func TestGenerate(t *testing.T) {
} }
func TestComputeV(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.n = b(1907 * 1823)
tkh.nSquare = new(big.Int).Mul(tkh.n, tkh.n) tkh.nSquare = new(big.Int).Mul(tkh.n, tkh.n)
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
+35 -7
View File
@@ -8,7 +8,11 @@ import (
) )
func getThresholdPrivateKey() *ThresholdPrivateKey { 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() tpks, err := tkh.Generate()
if err != nil { if err != nil {
panic(err) panic(err)
@@ -184,7 +188,11 @@ func TestupdateCprime(t *testing.T) {
} }
func TestEncryptingDecryptingSimple(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() tpks, err := tkh.Generate()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@@ -205,7 +213,11 @@ func TestEncryptingDecryptingSimple(t *testing.T) {
} }
func TestEncryptingDecrypting(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() tpks, err := tkh.Generate()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@@ -227,7 +239,11 @@ func TestEncryptingDecrypting(t *testing.T) {
} }
func TestHomomorphicThresholdEncryption(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() tpks, _ := tkh.Generate()
plainText1 := b(13) plainText1 := b(13)
@@ -278,7 +294,11 @@ func TestValidate(t *testing.T) {
} }
func TestCombinePartialDecryptionsZKP(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() tpks, err := tkh.Generate()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@@ -311,7 +331,11 @@ func TestCombinePartialDecryptionsZKP(t *testing.T) {
} }
func TestCombinePartialDecryptionsWith100Shares(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() tpks, err := tkh.Generate()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@@ -338,7 +362,11 @@ func TestCombinePartialDecryptionsWith100Shares(t *testing.T) {
} }
func TestVerifyDecryption(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() tpks, err := tkh.Generate()
pk := &tpks[0].ThresholdPublicKey pk := &tpks[0].ThresholdPublicKey