removed dependency to old code

This commit is contained in:
didier amyot committed 2014-11-29 23:29:34 -08:00
1 parent 959098cd25
commit b2d17e0eb6
5 files changed
+100 -26

No files matched your search

+1 -3
View File
@@ -6,8 +6,6 @@ import (
"errors" "errors"
"io" "io"
"math/big" "math/big"
"bitbucket.org/ustraca/crypto"
) )
type ThresholdKey struct { type ThresholdKey struct {
@@ -26,7 +24,7 @@ func (this *ThresholdKey) CombineSharesConstant() *big.Int {
// returns the factorial of the number of TotalNumberOfDecryptionServers // returns the factorial of the number of TotalNumberOfDecryptionServers
func (this *ThresholdKey) Delta() *big.Int { func (this *ThresholdKey) Delta() *big.Int {
return crypto.Factorial(this.TotalNumberOfDecryptionServers) return Factorial(this.TotalNumberOfDecryptionServers)
} }
func (this *ThresholdKey) MakeVerificationBeforeCombiningPartialDecryptions(shares []*PartialDecryption) error { func (this *ThresholdKey) MakeVerificationBeforeCombiningPartialDecryptions(shares []*PartialDecryption) error {
+3 -5
View File
@@ -4,8 +4,6 @@ import (
"crypto/rand" "crypto/rand"
"io" "io"
"math/big" "math/big"
"bitbucket.org/ustraca/crypto"
) )
type ThresholdKeyGenerator struct { type ThresholdKeyGenerator struct {
@@ -48,7 +46,7 @@ func GetThresholdKeyGenerator(nbits, TotalNumberOfDecryptionServers, Threshold i
} }
func (this *ThresholdKeyGenerator) GenerateSafePrimes() (*big.Int, *big.Int, error) { func (this *ThresholdKeyGenerator) GenerateSafePrimes() (*big.Int, *big.Int, error) {
return crypto.GenerateSafePrimes(this.nbits, this.Random) return GenerateSafePrimes(this.nbits, this.Random)
} }
func (this *ThresholdKeyGenerator) InitPandP1() error { func (this *ThresholdKeyGenerator) InitPandP1() error {
@@ -99,7 +97,7 @@ func (this *ThresholdKeyGenerator) InitPsAndQs() error {
func (this *ThresholdKeyGenerator) ComputeV() error { func (this *ThresholdKeyGenerator) ComputeV() error {
var err error var err error
this.v, err = crypto.GetRandomGeneratorOfTheQuadraticResidue(this.nSquare, this.Random) this.v, err = GetRandomGeneratorOfTheQuadraticResidue(this.nSquare, this.Random)
return err return err
} }
@@ -152,7 +150,7 @@ func (this *ThresholdKeyGenerator) CreateShares() []*big.Int {
} }
func (this *ThresholdKeyGenerator) Delta() *big.Int { func (this *ThresholdKeyGenerator) Delta() *big.Int {
return crypto.Factorial(this.TotalNumberOfDecryptionServers) return Factorial(this.TotalNumberOfDecryptionServers)
} }
func (this *ThresholdKeyGenerator) CreateViArray(shares []*big.Int) (viArray []*big.Int) { func (this *ThresholdKeyGenerator) CreateViArray(shares []*big.Int) (viArray []*big.Int) {
-18
View File
@@ -11,24 +11,6 @@ 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 AreSafePrimes(p, q *big.Int, expectedLength int, t *testing.T) {
if l := p.BitLen(); l != expectedLength {
t.Error("p does not have the good length. ", l)
}
if l := q.BitLen(); l != expectedLength-1 {
t.Error("q does not have the good length. ", l)
}
if !p.ProbablyPrime(100) {
t.Error("p is not a probable prime :(")
}
if !q.ProbablyPrime(100) {
t.Error("q is not a probable prime :(")
}
if p.Int64() != 2*q.Int64()+1 {
t.Error("p does not equals 2 * q + 1")
}
}
func TestGenerateSafePrimesOfThresholdKeyGenerator(t *testing.T) { func TestGenerateSafePrimesOfThresholdKeyGenerator(t *testing.T) {
tkh := new(ThresholdKeyGenerator) tkh := new(ThresholdKeyGenerator)
tkh.nbits = 10 tkh.nbits = 10
+36
View File
@@ -11,6 +11,31 @@ var ONE = big.NewInt(1)
var TWO = big.NewInt(2) var TWO = big.NewInt(2)
var FOUR = big.NewInt(4) var FOUR = big.NewInt(4)
// returns n! = n*(n-1)*(n-2)...3*2*1
func Factorial(n int) *big.Int {
ret := big.NewInt(1)
for i := 1; i <= n; i++ {
ret = new(big.Int).Mul(ret, big.NewInt(int64(i)))
}
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
func GenerateSafePrimes(nbits int, random io.Reader) (p, q *big.Int, err error) {
for {
q, err = rand.Prime(random, nbits-1)
if err != nil {
return
}
p = (new(big.Int)).Mul(q, big.NewInt(2))
p = p.Add(p, big.NewInt(1))
if p.ProbablyPrime(50) { //a probability of 2**-100 of not being prime
return
}
}
}
// Generate a random element in the group of all the elements in Z/nZ that // Generate a random element in the group of all the elements in Z/nZ that
// has a multiplicative inverse. // has a multiplicative inverse.
func GetRandomNumberInMultiplicativeGroup(n *big.Int, random io.Reader) (*big.Int, error) { func GetRandomNumberInMultiplicativeGroup(n *big.Int, random io.Reader) (*big.Int, error) {
@@ -26,3 +51,14 @@ func GetRandomNumberInMultiplicativeGroup(n *big.Int, random io.Reader) (*big.In
return r, nil return r, nil
} }
// Return a random generator of RQn with high probability. THIS METHOD
// ONLY WORKS IF N IS THE PRODUCT OF TWO SAFE PRIMES! This heuristic is used
// threshold signature paper in the Victor Shoup
func GetRandomGeneratorOfTheQuadraticResidue(n *big.Int, rand io.Reader) (*big.Int, error) {
r, err := GetRandomNumberInMultiplicativeGroup(n, rand)
if err != nil {
return nil, err
}
return new(big.Int).Mod(new(big.Int).Mul(r, r), n), nil
}
+60
View File
@@ -51,5 +51,65 @@ func TestGetRandomNumberInMultiplicativeGroup(t *testing.T) {
t.Fail() t.Fail()
} }
} }
}
func TestFactorial(t *testing.T) {
if delta := Factorial(6); 720 != delta.Int64() {
t.Error("Delta is not 720 but", delta)
}
}
func AreSafePrimes(p, q *big.Int, expectedLength int, t *testing.T) {
if l := p.BitLen(); l != expectedLength {
t.Error("p does not have the good length. ", l)
}
if l := q.BitLen(); l != expectedLength-1 {
t.Error("q does not have the good length. ", l)
}
if !p.ProbablyPrime(100) {
t.Error("p is not a probable prime :(")
}
if !q.ProbablyPrime(100) {
t.Error("q is not a probable prime :(")
}
if p.Int64() != 2*q.Int64()+1 {
t.Error("p does not equals 2 * q + 1")
}
}
func GetEntireRQn(n int) map[int]bool {
ret := make(map[int]bool)
for i := 1; i < n; i++ {
if areRelativelyPrime(i, n) {
ret[i] = true
}
}
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)
RQn := GetEntireRQn(n(m))
for i := 0; i < 100; i++ {
elm, err := GetRandomGeneratorOfTheQuadraticResidue(m, rand.Reader)
if err != nil {
t.Error(err)
return
}
if _, ok := RQn[n(elm)]; !ok {
t.Fail()
}
}
} }