Documentation improvements, code cleaned up #2
+63
-64
@@ -57,60 +57,59 @@ func GetThresholdKeyGenerator(nbits, TotalNumberOfDecryptionServers, Threshold i
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ThresholdKeyGenerator) generateSafePrimes() (*big.Int, *big.Int, error) {
|
func (tkg *ThresholdKeyGenerator) generateSafePrimes() (*big.Int, *big.Int, error) {
|
||||||
return GenerateSafePrimes(this.nbits, this.Random)
|
return GenerateSafePrimes(tkg.nbits, tkg.Random)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ThresholdKeyGenerator) initPandP1() error {
|
func (tkg *ThresholdKeyGenerator) initPandP1() error {
|
||||||
var err error
|
var err error
|
||||||
this.p, this.p1, err = this.generateSafePrimes()
|
tkg.p, tkg.p1, err = tkg.generateSafePrimes()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ThresholdKeyGenerator) initQandQ1() error {
|
func (tkg *ThresholdKeyGenerator) initQandQ1() error {
|
||||||
var err error
|
var err error
|
||||||
this.q, this.q1, err = this.generateSafePrimes()
|
tkg.q, tkg.q1, err = tkg.generateSafePrimes()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ThresholdKeyGenerator) initShortcuts() {
|
func (tkg *ThresholdKeyGenerator) initShortcuts() {
|
||||||
this.n = new(big.Int).Mul(this.p, this.q)
|
tkg.n = new(big.Int).Mul(tkg.p, tkg.q)
|
||||||
this.m = new(big.Int).Mul(this.p1, this.q1)
|
tkg.m = new(big.Int).Mul(tkg.p1, tkg.q1)
|
||||||
this.nSquare = new(big.Int).Mul(this.n, this.n)
|
tkg.nSquare = new(big.Int).Mul(tkg.n, tkg.n)
|
||||||
this.nm = new(big.Int).Mul(this.n, this.m)
|
tkg.nm = new(big.Int).Mul(tkg.n, tkg.m)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ThresholdKeyGenerator) arePsAndQsGood() bool {
|
func (tkg *ThresholdKeyGenerator) arePsAndQsGood() bool {
|
||||||
if this.p.Cmp(this.q) == 0 {
|
if tkg.p.Cmp(tkg.q) == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if this.p.Cmp(this.q1) == 0 {
|
if tkg.p.Cmp(tkg.q1) == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if this.p1.Cmp(this.q) == 0 {
|
if tkg.p1.Cmp(tkg.q) == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ThresholdKeyGenerator) initPsAndQs() error {
|
func (tkg *ThresholdKeyGenerator) initPsAndQs() error {
|
||||||
if err := this.initPandP1(); err != nil {
|
if err := tkg.initPandP1(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := this.initQandQ1(); err != nil {
|
if err := tkg.initQandQ1(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !this.arePsAndQsGood() {
|
if !tkg.arePsAndQsGood() {
|
||||||
return this.initPsAndQs()
|
return tkg.initPsAndQs()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// v generates a cyclic group of squares in Zn^2.
|
// v generates a cyclic group of squares in Zn^2.
|
||||||
func (this *ThresholdKeyGenerator) computeV() error {
|
func (tkg *ThresholdKeyGenerator) computeV() error {
|
||||||
var err error
|
var err error
|
||||||
this.v, err = GetRandomGeneratorOfTheQuadraticResidue(this.nSquare, this.Random)
|
tkg.v, err = GetRandomGeneratorOfTheQuadraticResidue(tkg.nSquare, tkg.Random)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,18 +137,18 @@ func (this *ThresholdKeyGenerator) computeV() error {
|
|||||||
// z2 = m^-1 mod n
|
// z2 = m^-1 mod n
|
||||||
//
|
//
|
||||||
// x = a2*y2*z2 = 1 * m * [m^-1 mod n]
|
// x = a2*y2*z2 = 1 * m * [m^-1 mod n]
|
||||||
func (this *ThresholdKeyGenerator) initD() {
|
func (tkg *ThresholdKeyGenerator) initD() {
|
||||||
mInverse := new(big.Int).ModInverse(this.m, this.n)
|
mInverse := new(big.Int).ModInverse(tkg.m, tkg.n)
|
||||||
this.d = new(big.Int).Mul(mInverse, this.m)
|
tkg.d = new(big.Int).Mul(mInverse, tkg.m)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ThresholdKeyGenerator) initNumerialValues() error {
|
func (tkg *ThresholdKeyGenerator) initNumerialValues() error {
|
||||||
if err := this.initPsAndQs(); err != nil {
|
if err := tkg.initPsAndQs(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
this.initShortcuts()
|
tkg.initShortcuts()
|
||||||
this.initD()
|
tkg.initD()
|
||||||
return this.computeV()
|
return tkg.computeV()
|
||||||
}
|
}
|
||||||
|
|
||||||
// f(X) = a_0 X^0 + a_1 X^1 + ... + a_(w-1) X^(w-1)
|
// f(X) = a_0 X^0 + a_1 X^1 + ... + a_(w-1) X^(w-1)
|
||||||
@@ -158,12 +157,12 @@ func (this *ThresholdKeyGenerator) initNumerialValues() error {
|
|||||||
// `w` - threshold
|
// `w` - threshold
|
||||||
// `a_i` - random value from {0, ... nm - 1} for 0<i<w
|
// `a_i` - random value from {0, ... nm - 1} for 0<i<w
|
||||||
// `a_0` is always equal `d`
|
// `a_0` is always equal `d`
|
||||||
func (this *ThresholdKeyGenerator) generateHidingPolynomial() error {
|
func (tkg *ThresholdKeyGenerator) generateHidingPolynomial() error {
|
||||||
this.polynomialCoefficients = make([]*big.Int, this.Threshold)
|
tkg.polynomialCoefficients = make([]*big.Int, tkg.Threshold)
|
||||||
this.polynomialCoefficients[0] = this.d
|
tkg.polynomialCoefficients[0] = tkg.d
|
||||||
var err error
|
var err error
|
||||||
for i := 1; i < this.Threshold; i++ {
|
for i := 1; i < tkg.Threshold; i++ {
|
||||||
this.polynomialCoefficients[i], err = rand.Int(this.Random, this.nm)
|
tkg.polynomialCoefficients[i], err = rand.Int(tkg.Random, tkg.nm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -173,28 +172,28 @@ func (this *ThresholdKeyGenerator) generateHidingPolynomial() error {
|
|||||||
|
|
||||||
// The secred share of the i'th authority is `f(i) mod nm`, where `f` is
|
// The secred share of the i'th authority is `f(i) mod nm`, where `f` is
|
||||||
// the polynomial we generated in `GenerateHidingPolynomial` function.
|
// the polynomial we generated in `GenerateHidingPolynomial` function.
|
||||||
func (this *ThresholdKeyGenerator) computeShare(index int) *big.Int {
|
func (tkg *ThresholdKeyGenerator) computeShare(index int) *big.Int {
|
||||||
share := big.NewInt(0)
|
share := big.NewInt(0)
|
||||||
for i := 0; i < this.Threshold; i++ {
|
for i := 0; i < tkg.Threshold; i++ {
|
||||||
a := this.polynomialCoefficients[i]
|
a := tkg.polynomialCoefficients[i]
|
||||||
// we index authorities from 1, that's why we do index+1 here
|
// we index authorities from 1, that's why we do index+1 here
|
||||||
b := new(big.Int).Exp(big.NewInt(int64(index+1)), big.NewInt(int64(i)), nil)
|
b := new(big.Int).Exp(big.NewInt(int64(index+1)), big.NewInt(int64(i)), nil)
|
||||||
tmp := new(big.Int).Mul(a, b)
|
tmp := new(big.Int).Mul(a, b)
|
||||||
share = new(big.Int).Add(share, tmp)
|
share = new(big.Int).Add(share, tmp)
|
||||||
}
|
}
|
||||||
return new(big.Int).Mod(share, this.nm)
|
return new(big.Int).Mod(share, tkg.nm)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ThresholdKeyGenerator) createShares() []*big.Int {
|
func (tkg *ThresholdKeyGenerator) createShares() []*big.Int {
|
||||||
shares := make([]*big.Int, this.TotalNumberOfDecryptionServers)
|
shares := make([]*big.Int, tkg.TotalNumberOfDecryptionServers)
|
||||||
for i := 0; i < this.TotalNumberOfDecryptionServers; i++ {
|
for i := 0; i < tkg.TotalNumberOfDecryptionServers; i++ {
|
||||||
shares[i] = this.computeShare(i)
|
shares[i] = tkg.computeShare(i)
|
||||||
}
|
}
|
||||||
return shares
|
return shares
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ThresholdKeyGenerator) delta() *big.Int {
|
func (tkg *ThresholdKeyGenerator) delta() *big.Int {
|
||||||
return Factorial(this.TotalNumberOfDecryptionServers)
|
return Factorial(tkg.TotalNumberOfDecryptionServers)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generates verification keys for actions of decryption servers.
|
// Generates verification keys for actions of decryption servers.
|
||||||
@@ -206,45 +205,45 @@ func (this *ThresholdKeyGenerator) delta() *big.Int {
|
|||||||
// `l` is the number of decryption servers
|
// `l` is the number of decryption servers
|
||||||
// `s_i` is a secret share for server `i`.
|
// `s_i` is a secret share for server `i`.
|
||||||
// Secret shares were previously generated in the `CrateShares` function.
|
// Secret shares were previously generated in the `CrateShares` function.
|
||||||
func (this *ThresholdKeyGenerator) createViArray(shares []*big.Int) (viArray []*big.Int) {
|
func (tkg *ThresholdKeyGenerator) createViArray(shares []*big.Int) (viArray []*big.Int) {
|
||||||
viArray = make([]*big.Int, len(shares))
|
viArray = make([]*big.Int, len(shares))
|
||||||
delta := this.delta()
|
delta := tkg.delta()
|
||||||
for i, share := range shares {
|
for i, share := range shares {
|
||||||
tmp := new(big.Int).Mul(share, delta)
|
tmp := new(big.Int).Mul(share, delta)
|
||||||
viArray[i] = new(big.Int).Exp(this.v, tmp, this.nSquare)
|
viArray[i] = new(big.Int).Exp(tkg.v, tmp, tkg.nSquare)
|
||||||
}
|
}
|
||||||
return viArray
|
return viArray
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ThresholdKeyGenerator) createPrivateKey(i int, share *big.Int, viArray []*big.Int) *ThresholdPrivateKey {
|
func (tkg *ThresholdKeyGenerator) createPrivateKey(i int, share *big.Int, viArray []*big.Int) *ThresholdPrivateKey {
|
||||||
ret := new(ThresholdPrivateKey)
|
ret := new(ThresholdPrivateKey)
|
||||||
ret.N = this.n
|
ret.N = tkg.n
|
||||||
ret.V = this.v
|
ret.V = tkg.v
|
||||||
|
|
||||||
ret.TotalNumberOfDecryptionServers = this.TotalNumberOfDecryptionServers
|
ret.TotalNumberOfDecryptionServers = tkg.TotalNumberOfDecryptionServers
|
||||||
ret.Threshold = this.Threshold
|
ret.Threshold = tkg.Threshold
|
||||||
ret.Share = share
|
ret.Share = share
|
||||||
ret.Id = i + 1
|
ret.Id = i + 1
|
||||||
ret.Vi = viArray
|
ret.Vi = viArray
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ThresholdKeyGenerator) createPrivateKeys() []*ThresholdPrivateKey {
|
func (tkg *ThresholdKeyGenerator) createPrivateKeys() []*ThresholdPrivateKey {
|
||||||
shares := this.createShares()
|
shares := tkg.createShares()
|
||||||
viArray := this.createViArray(shares)
|
viArray := tkg.createViArray(shares)
|
||||||
ret := make([]*ThresholdPrivateKey, this.TotalNumberOfDecryptionServers)
|
ret := make([]*ThresholdPrivateKey, tkg.TotalNumberOfDecryptionServers)
|
||||||
for i := 0; i < this.TotalNumberOfDecryptionServers; i++ {
|
for i := 0; i < tkg.TotalNumberOfDecryptionServers; i++ {
|
||||||
ret[i] = this.createPrivateKey(i, shares[i], viArray)
|
ret[i] = tkg.createPrivateKey(i, shares[i], viArray)
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ThresholdKeyGenerator) Generate() ([]*ThresholdPrivateKey, error) {
|
func (tkg *ThresholdKeyGenerator) Generate() ([]*ThresholdPrivateKey, error) {
|
||||||
if err := this.initNumerialValues(); err != nil {
|
if err := tkg.initNumerialValues(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := this.generateHidingPolynomial(); err != nil {
|
if err := tkg.generateHidingPolynomial(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return this.createPrivateKeys(), nil
|
return tkg.createPrivateKeys(), nil
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user