Improved performance of the safe prime generator #9
No files matched your search
@@ -10,7 +10,6 @@ package paillier
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -63,6 +62,7 @@ func GenerateSafePrime(
|
||||
bitLen int,
|
||||
concurrencyLevel int,
|
||||
timeout time.Duration,
|
||||
random io.Reader,
|
||||
) (p *big.Int, q *big.Int, err error) {
|
||||
if bitLen < 6 {
|
||||
return nil, nil, errors.New("safe prime size must be at least 6 bits")
|
||||
@@ -82,7 +82,7 @@ func GenerateSafePrime(
|
||||
|
||||
for i := 0; i < concurrencyLevel; i++ {
|
||||
runGenPrimeRoutine(
|
||||
ctx, primeChan, errChan, mutex, waitGroup, rand.Reader, bitLen,
|
||||
ctx, primeChan, errChan, mutex, waitGroup, random, bitLen,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package paillier
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"math/big"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -44,6 +44,7 @@ func TestAsyncGenerator(t *testing.T) {
|
||||
test.bitLen,
|
||||
concurrencyLevel,
|
||||
test.timeout,
|
||||
rand.Reader,
|
||||
)
|
||||
|
||||
if test.expectedError != nil {
|
||||
@@ -59,38 +60,7 @@ func TestAsyncGenerator(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !p.ProbablyPrime(20) {
|
||||
t.Errorf("p is not prime; p = %v", p)
|
||||
}
|
||||
|
||||
if !q.ProbablyPrime(20) {
|
||||
t.Errorf("q is not prime; q = %v", q)
|
||||
}
|
||||
|
||||
// p = 2q + 1 ?
|
||||
expectedP := new(big.Int)
|
||||
expectedP.Mul(big.NewInt(2), q)
|
||||
expectedP.Add(expectedP, big.NewInt(1))
|
||||
|
||||
if expectedP.Cmp(p) != 0 {
|
||||
t.Errorf("2q + 1 != p")
|
||||
}
|
||||
|
||||
if p.BitLen() != test.bitLen {
|
||||
t.Fatalf(
|
||||
"Unexpected p bit length\nActual: %v\nExpected: %v",
|
||||
p.BitLen(),
|
||||
test.bitLen,
|
||||
)
|
||||
}
|
||||
|
||||
if q.BitLen() != test.bitLen-1 {
|
||||
t.Fatalf(
|
||||
"Unexpected q bit length\nActual: %v\nExpected: %v",
|
||||
q.BitLen(),
|
||||
test.bitLen-1,
|
||||
)
|
||||
}
|
||||
AreSafePrimes(p, q, test.bitLen, t)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"crypto/rand"
|
||||
"io"
|
||||
"math/big"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Generates a threshold Paillier key with an algorithm based on [DJN 10],
|
||||
@@ -58,7 +59,7 @@ func GetThresholdKeyGenerator(nbits, TotalNumberOfDecryptionServers, Threshold i
|
||||
}
|
||||
|
||||
func (tkg *ThresholdKeyGenerator) generateSafePrimes() (*big.Int, *big.Int, error) {
|
||||
return GenerateSafePrimes(tkg.nbits, tkg.Random)
|
||||
return GenerateSafePrime(tkg.nbits, 4, 120*time.Second, tkg.Random)
|
||||
}
|
||||
|
||||
func (tkg *ThresholdKeyGenerator) initPandP1() error {
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"crypto/rand"
|
||||
"io"
|
||||
"math/big"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ZERO = big.NewInt(0)
|
||||
@@ -21,15 +20,6 @@ func Factorial(n int) *big.Int {
|
||||
return ret
|
||||
}
|
||||
|
||||
// Returns 2 primes such that p = 2 * q + 1 and that the length of
|
||||
// p is nbits. `p` is called a safe prime
|
||||
//
|
||||
// Deprecated: This function has been left here just for backward compatibility.
|
||||
// Please use `GenerateSafePrime` from the `safe_prime_generator.go` directly.
|
||||
func GenerateSafePrimes(nbits int, random io.Reader) (p, q *big.Int, err error) {
|
||||
return GenerateSafePrime(nbits, 4, 120*time.Second)
|
||||
}
|
||||
|
||||
// Generate a random element in the group of all the elements in Z/nZ that
|
||||
// has a multiplicative inverse.
|
||||
func GetRandomNumberInMultiplicativeGroup(n *big.Int, random io.Reader) (*big.Int, error) {
|
||||
|
||||
@@ -87,16 +87,6 @@ func GetEntireRQn(n int) map[int]bool {
|
||||
return ret
|
||||
}
|
||||
|
||||
func TestGenerateSafePrimes(t *testing.T) {
|
||||
for i := 0; i < 10; i++ {
|
||||
p, q, err := GenerateSafePrimes(20, rand.Reader)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
AreSafePrimes(p, q, 20, t)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRandomGeneratorOfTheQuadraticResidue(t *testing.T) {
|
||||
tooSmallPrime1, tooSmallPrime2 := b(347), b(359)
|
||||
m := new(big.Int).Mul(tooSmallPrime1, tooSmallPrime2)
|
||||
|
||||
Reference in new issue
Block a user