Skip to content

Commit

Permalink
Merge pull request #2 from perun-network/refactor-code
Browse files Browse the repository at this point in the history
Refactor code base
  • Loading branch information
NhoxxKienn authored Sep 4, 2023
2 parents 6c32847 + 6526a2b commit 776be9c
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 86 deletions.
41 changes: 22 additions & 19 deletions fhks_bbs_plus/precomputation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@ package fhks_bbs_plus

import (
bls12381 "github.com/kilic/bls12-381"

"github.com/perun-network/bbs-plus-threshold-wallet/helper"
)

type PerPartyPreSignature struct {
AShare *bls12381.Fr
EShare *bls12381.Fr
SShare *bls12381.Fr
AeTermOwn *bls12381.Fr
AsTermOwn *bls12381.Fr
AskTermOwn *bls12381.Fr
AeTermsA []*bls12381.Fr
AeTermsE []*bls12381.Fr
AsTermsA []*bls12381.Fr
AsTermsS []*bls12381.Fr
AskTermsA []*bls12381.Fr
AskTermsSK []*bls12381.Fr
AShare *bls12381.Fr // a^k_i for k in [t].
EShare *bls12381.Fr // e^k_i for k in [t].
SShare *bls12381.Fr // s^k_i for k in [t].
AeTermOwn *bls12381.Fr // a^k_i * e^k_i for k in [t]. // Might not be necessary.
AsTermOwn *bls12381.Fr // a^k_i * s^k_i for k in [t] // Might not be necessary.
AskTermOwn *bls12381.Fr // a^k_i * sk_i for k in [t] // Might not be necessary.
AeTermsA []*bls12381.Fr // Share of a^k_i * e^k_j for k in [t], j in [n] (j can also be i).
AeTermsE []*bls12381.Fr // Share of a^k_j * e^k_i for k in [t], j in [n] (j can also be i -- this time other share).
AsTermsA []*bls12381.Fr // Share of a^k_i * s^k_j for k in [t], j in [n] (j can also be i).
AsTermsS []*bls12381.Fr // Share of a^k_j * s^k_i for k in [t], j in [n] (j can also be i -- this time other share).
AskTermsA []*bls12381.Fr // Share of a^k_i * sk_j for k in [t], j in [n] (j can also be i).
AskTermsSK []*bls12381.Fr // Share of a^k_j * sk_i for k in [t], j in [n] (j can also be i -- this time other share).
}

type PerPartyPrecomputations struct {
Index int
Index int // Position at which sk-polynomial for own secret key share is evaluated.
SkShare *bls12381.Fr
PreSignatures []*PerPartyPreSignature
}
Expand Down Expand Up @@ -56,36 +57,38 @@ func (lps *LivePreSignature) FromPresignatureWithCoefficients(
preSignature *PerPartyPreSignature,
lagrangeCoefficients []*bls12381.Fr) *LivePreSignature {

//For (ae,as = alpha)-shares start with the multiplication of both own shares
// For (ae,as = alpha)-shares start with the multiplication of both own shares
alphaShare := bls12381.NewFr().Set(preSignature.AsTermOwn)
aeShare := bls12381.NewFr().Set(preSignature.AeTermOwn)

//ASK-Share is split into a part which is to multiplied with own-index-lagrange and one which directly gets other-index-lagrange
// ASK-Share is split into a part which is to multiplied with own-index-lagrange and one which directly gets
// other-index-lagrange.
askShare := bls12381.NewFr().Zero()
tmpAskOwnCoefficient := bls12381.NewFr().Set(preSignature.AskTermOwn)

indI := 0
for indJ, elJ := range indices {
if elJ != ownIndex {
//Add shares of a_i * e/s_j (ae/s_terms_a), a_j * e_i (ae/s_terms_a/s)
// Add shares of a_i * e/s_j (ae/s_terms_a), a_j * e_i (ae/s_terms_a/s)
aeShare.Add(aeShare, preSignature.AeTermsA[elJ-1])
aeShare.Add(aeShare, preSignature.AeTermsE[elJ-1])
alphaShare.Add(alphaShare, preSignature.AsTermsA[elJ-1])
alphaShare.Add(alphaShare, preSignature.AsTermsS[elJ-1])

//Share of a_i * sk_j (using j's lagrange coefficient) is added to share_of_ask
// Share of a_i * sk_j (using j's lagrange coefficient) is added to share_of_ask
tmp := bls12381.NewFr().Set(preSignature.AskTermsA[elJ-1])
tmp.Mul(tmp, lagrangeCoefficients[indJ])

askShare.Add(askShare, tmp)

//Share of a_j * sk_i (using i's lagrange coefficeint) is added to tmp_ask_own_lagrange (coefficient is applied later for all at once)
// Share of a_j * sk_i (using i's lagrange coefficient) is added to tmp_ask_own_lagrange (coefficient is
// applied later for all at once).
tmpAskOwnCoefficient.Add(tmpAskOwnCoefficient, preSignature.AskTermsSK[elJ-1])
} else {
indI = indJ
}
}
//Apply i's lagrange coefficient to sum of share of all cross-terms incoperating sk_i and add result to share of ask
// Apply i's lagrange coefficient to sum of share of all cross-terms incorporating sk_i and add result to share of ask.
tmpAskOwnCoefficient.Mul(tmpAskOwnCoefficient, lagrangeCoefficients[indI])
askShare.Add(askShare, tmpAskOwnCoefficient)

Expand Down
File renamed without changes.
45 changes: 25 additions & 20 deletions helper/ole_correlation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import (
bls12381 "github.com/kilic/bls12-381"
)

// Gets twice t elements of each party and creates the following ole correlation:
// It holds that res[i_s][i_x][i_y][0] + res[i_s][i_x][i_y][1] = x[i_s][i_x] * y[i_s][i_y] (for each i_s in k, i_x in n, i_y in n)
// Party i is supposed to own all res[i_s][i][j][0] and res[i_s][j][i][1] for all j in [n]#
func MakeAllPartiesOLE(rng *rand.Rand, n, k int, x, y [][]*bls12381.Fr) [][][][2]*bls12381.Fr {
// OLECorrelation represents the correlation for Oblivious Linear Evaluation (OLE).
type OLECorrelation struct {
U *bls12381.Fr
V *bls12381.Fr
}

// MakeAllPartiesOLE generates OLE correlations for all parties based on input data.
func MakeAllPartiesOLE(rng *rand.Rand, k, n int, x, y [][]*bls12381.Fr) [][][]*OLECorrelation {
if k != len(x) {
panic("make_all_parties_vole got ill-structured input format x.len() != k")
}
Expand All @@ -18,55 +22,56 @@ func MakeAllPartiesOLE(rng *rand.Rand, n, k int, x, y [][]*bls12381.Fr) [][][][2
panic("make_all_parties_vole got ill-structured input format y.len() != k")
}

voleCorrelation := make([][][][2]*bls12381.Fr, k)
voleCorrelation := make([][][]*OLECorrelation, k)
for i := 0; i < k; i++ {
if n != len(x[i]) {
panic("make_all_parties_vole got ill-structured input format x[i_k].len() != n")
panic("MakeAllPartiesOLE got ill-structured input format x[i_k].len() != n")
}
if n != len(y[i]) {
panic("make_all_parties_vole got ill-structured input format y[i].len() != n")
panic("MakeAllPartiesOLE got ill-structured input format y[i].len() != n")
}
voleCorrelation[i] = make([][][2]*bls12381.Fr, n)

voleCorrelation[i] = make([][]*OLECorrelation, n)
for j := 0; j < n; j++ {
voleCorrelation[i][j] = make([][2]*bls12381.Fr, n)
voleCorrelation[i][j] = make([]*OLECorrelation, n)
for l := 0; l < n; l++ {
voleCorrelation[i][j][l][0], voleCorrelation[i][j][l][1] = makeOLESingle(rng, x[i][j], y[i][l])
voleCorrelation[i][j][l] = makeOLESingle(rng, x[i][j], y[i][l])
}
}
}
return voleCorrelation
}

// Gets t elements and one scalar of each party (x[i_k][i]: element i_k of party i, y[i]: scalar of party i)
func MakeAllPartiesVOLE(rng *rand.Rand, n, k int, x [][]*bls12381.Fr, y []*bls12381.Fr) [][][][2]*bls12381.Fr {
// MakeAllPartiesVOLE Gets t elements and one scalar of each party (x[i_k][i]: element i_k of party i, y[i]: scalar of party i)
func MakeAllPartiesVOLE(rng *rand.Rand, k, n int, x [][]*bls12381.Fr, y []*bls12381.Fr) [][][]*OLECorrelation {
if k != len(x) {
panic("make_all_parties_vole got ill-structured input format x.len() != k")
}
if n != len(y) {
panic("make_all_parties_vole got ill-structured input format y.len() != n")
}
voleCorrelation := make([][][][2]*bls12381.Fr, k)
voleCorrelation := make([][][]*OLECorrelation, k)
for i := 0; i < k; i++ {
if n != len(x[i]) {
panic("make_all_parties_vole got ill-structured input format x[i_k].len() != n")
}
voleCorrelation[i] = make([][][2]*bls12381.Fr, n)
voleCorrelation[i] = make([][]*OLECorrelation, n)
for j := 0; j < n; j++ {
if n != len(y) {
panic("make_all_parties_vole got ill-structured input format y[i].len() != n")
}
voleCorrelation[i][j] = make([][2]*bls12381.Fr, n)
voleCorrelation[i][j] = make([]*OLECorrelation, n)
for l := 0; l < n; l++ {
voleCorrelation[i][j][l][0], voleCorrelation[i][j][l][1] = makeOLESingle(rng, x[i][j], y[l])
voleCorrelation[i][j][l] = makeOLESingle(rng, x[i][j], y[l])
}
}
}
return voleCorrelation
}

// Function to compute the OLE correlation for a single pair of field elements
// Gets inputs x and y and generates u,v such that x*y = u+ v
func makeOLESingle(rng *rand.Rand, x, y *bls12381.Fr) (*bls12381.Fr, *bls12381.Fr) {
// makeOLESingle computes the OLE correlation for a single pair of field elements.
// For inputs x and y, it generates u,v such that x*y = u+v.
func makeOLESingle(rng *rand.Rand, x, y *bls12381.Fr) *OLECorrelation {
u := bls12381.NewFr()
_, err := u.Rand(rng)
if err != nil {
Expand All @@ -75,5 +80,5 @@ func makeOLESingle(rng *rand.Rand, x, y *bls12381.Fr) (*bls12381.Fr, *bls12381.F
v := bls12381.NewFr().Set(x)
v.Mul(v, y)
v.Sub(v, u)
return u, v
return &OLECorrelation{u, v}
}
8 changes: 5 additions & 3 deletions helper/rng.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
bls12381 "github.com/kilic/bls12-381"
)

func GetRandomElements(rng *rand.Rand, n, k int) [][]*bls12381.Fr {
// GetRandomElements creates a k-size vector of n-size vectors of random field elements
func GetRandomElements(rng *rand.Rand, k, n int) [][]*bls12381.Fr {
result := make([][]*bls12381.Fr, k)
for i := 0; i < k; i++ {
result[i] = make([]*bls12381.Fr, n)
Expand All @@ -23,9 +24,10 @@ func GetRandomElements(rng *rand.Rand, n, k int) [][]*bls12381.Fr {
return result
}

func GetRandomMessagesFromSeed(seedArray [16]uint8, c1 int, c2 int) [][]*bls12381.Fr {
// GetRandomMessagesFromSeed creates a k-size vector of n-size vectors of random messages (field elements).
func GetRandomMessagesFromSeed(seedArray [16]uint8, k int, n int) [][]*bls12381.Fr {
seed := int64(binary.BigEndian.Uint64(seedArray[:]))
rng := rand.New(rand.NewSource(seed))

return GetRandomElements(rng, c1, c2)
return GetRandomElements(rng, k, n)
}
12 changes: 8 additions & 4 deletions helper/secret_sharing.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ func uint64ToFr(val uint64) *bls12381.Fr {
return fr
}

// Computes the lagrange coefficient that is to be applied to the evaluation of the polynomial at position evaluation_x for an interpolation to position interpolation_x if the available evaluated positions are defined by indices
// GetLagrangeCoefficientFr computes the lagrange coefficient that is to be applied to the evaluation of the polynomial
// at position evaluation_x for an interpolation to position interpolation_x if the available evaluated positions are
// defined by indices
func GetLagrangeCoefficientFr(indices []int, evaluationX int, interpolationX int) *bls12381.Fr {
top := bls12381.NewFr().One()
bot := bls12381.NewFr().One()
Expand All @@ -38,12 +40,14 @@ func GetLagrangeCoefficientFr(indices []int, evaluationX int, interpolationX int
return top
}

// Computes the lagrange coefficient that is to be applied to the evaluation of the polynomial at position evaluation_x for an interpolation to position 0 if the available evaluated positions are defined by indices
// Get0LagrangeCoefficientFr computes the lagrange coefficient that is to be applied to the evaluation of the polynomial
// at position evaluation_x for an interpolation to position 0 if the available evaluated positions are defined by indices
func Get0LagrangeCoefficientFr(indices []int, evaluationX int) *bls12381.Fr {
return GetLagrangeCoefficientFr(indices, evaluationX, 0)
}

// Computes all lagrange coefficients for an interpolation to position 0 if the available evaluated positions are defined by indices
// Get0LagrangeCoefficientSetFr computes all lagrange coefficients for an interpolation to position 0 if the available
// evaluated positions are defined by indices
func Get0LagrangeCoefficientSetFr(indices []int) []*bls12381.Fr {
coefficients := make([]*bls12381.Fr, len(indices))
for i, idx := range indices {
Expand All @@ -52,7 +56,7 @@ func Get0LagrangeCoefficientSetFr(indices []int) []*bls12381.Fr {
return coefficients
}

// Generates a t-out-of-n shamir secret sharing of a random element
// GetShamirSharedRandomElement generates a t-out-of-n shamir secret sharing of a random element
func GetShamirSharedRandomElement(rng *rand.Rand, t, n int) (*bls12381.Fr, []*bls12381.Fr) {
// Generate the secret key element
secretKeyElement := bls12381.NewFr()
Expand Down
10 changes: 5 additions & 5 deletions measurements/simple_measurement.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/perun-network/bbs-plus-threshold-wallet/fhks_bbs_plus"
"github.com/perun-network/bbs-plus-threshold-wallet/helper"
"github.com/perun-network/bbs-plus-threshold-wallet/precomputation_mockup"
"github.com/perun-network/bbs-plus-threshold-wallet/precomputation"
)

var (
Expand Down Expand Up @@ -37,12 +37,12 @@ func SimpleMeasurementWithCoefficientComputation() {
var verifyDurations []time.Duration
var directSignDurations []time.Duration

messages := helper.GetRandomMessagesFromSeed(seedMessages, messageCount, k)
messages := helper.GetRandomMessagesFromSeed(seedMessages, k, messageCount)

directES := helper.GetRandomMessagesFromSeed(seedKeys, 2, k)
directES := helper.GetRandomMessagesFromSeed(seedKeys, k, 2)

sk, preComputation := precomputation_mockup.GeneratePPPrecomputation(
seedPresignatures, t, n, k)
sk, preComputation := precomputation.GeneratePPPrecomputation(
seedPresignatures, t, k, n)

pk := fhks_bbs_plus.GeneratePublicKey(seedKeys, sk, messageCount)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package precomputation_mockup
package precomputation

import (
"encoding/binary"
"math/rand"

bls12381 "github.com/kilic/bls12-381"

fhksbbsplus "github.com/perun-network/bbs-plus-threshold-wallet/fhks_bbs_plus"
"github.com/perun-network/bbs-plus-threshold-wallet/helper"
)
Expand All @@ -15,14 +16,14 @@ type PCFPCGOutput struct {
AShares [][]*bls12381.Fr
EShares [][]*bls12381.Fr
SShares [][]*bls12381.Fr
AeTerms [][][][2]*bls12381.Fr
AsTerms [][][][2]*bls12381.Fr
AskTerms [][][][2]*bls12381.Fr
AeTerms [][][]*helper.OLECorrelation
AsTerms [][][]*helper.OLECorrelation
AskTerms [][][]*helper.OLECorrelation
}

func GeneratePPPrecomputation(seedArray [16]uint8, t, n, k int) (*bls12381.Fr, []*fhksbbsplus.PerPartyPrecomputations) {
output := GeneratePCFPCGOutput(seedArray, t, n, k)
return output.Sk, CreatePPPrecomputationFromALLVOLEEvaluation(k, n,
func GeneratePPPrecomputation(seedArray [16]uint8, t, k, n int) (*bls12381.Fr, []*fhksbbsplus.PerPartyPrecomputations) {
output := GeneratePCFPCGOutput(seedArray, t, k, n)
return output.Sk, CreatePPPrecomputationFromVOLEEvaluation(k, n,
output.SkShares,
output.AShares,
output.EShares,
Expand All @@ -34,26 +35,26 @@ func GeneratePPPrecomputation(seedArray [16]uint8, t, n, k int) (*bls12381.Fr, [

}

func GeneratePCFPCGOutput(seedArray [16]uint8, t int, n int, k int) PCFPCGOutput {
func GeneratePCFPCGOutput(seedArray [16]uint8, t int, k int, n int) PCFPCGOutput {
seed := int64(binary.BigEndian.Uint64(seedArray[:]))
rng := rand.New(rand.NewSource(seed))
sk, skShares := helper.GetShamirSharedRandomElement(rng, t, n)
aShares := helper.GetRandomElements(rng, n, k)
eShares := helper.GetRandomElements(rng, n, k)
sShares := helper.GetRandomElements(rng, n, k)
aeTerms := helper.MakeAllPartiesOLE(rng, n, k, aShares, eShares)
asTerms := helper.MakeAllPartiesOLE(rng, n, k, aShares, sShares)
askTerms := helper.MakeAllPartiesVOLE(rng, n, k, aShares, skShares)
aShares := helper.GetRandomElements(rng, k, n)
eShares := helper.GetRandomElements(rng, k, n)
sShares := helper.GetRandomElements(rng, k, n)
aeTerms := helper.MakeAllPartiesOLE(rng, k, n, aShares, eShares)
asTerms := helper.MakeAllPartiesOLE(rng, k, n, aShares, sShares)
askTerms := helper.MakeAllPartiesVOLE(rng, k, n, aShares, skShares)

return PCFPCGOutput{sk, skShares, aShares, eShares, sShares, aeTerms, asTerms, askTerms}
}

func CreatePPPrecomputationFromALLVOLEEvaluation(
func CreatePPPrecomputationFromVOLEEvaluation(
k int,
n int,
skShares []*bls12381.Fr,
aShares, eShares, sShares [][]*bls12381.Fr,
aeTerms, asTerms, askTerms [][][][2]*bls12381.Fr,
aeTerms, asTerms, askTerms [][][]*helper.OLECorrelation,
) []*fhksbbsplus.PerPartyPrecomputations {
precomputations := make([]*fhksbbsplus.PerPartyPrecomputations, n)
for iN := 0; iN < n; iN++ {
Expand All @@ -76,17 +77,17 @@ func CreatePPPrecomputationFromALLVOLEEvaluation(

for jN := 0; jN < n; jN++ {
aeTermsA[jN] = bls12381.NewFr()
aeTermsA[jN].Set(aeTerms[iK][iN][jN][0])
aeTermsA[jN].Set(aeTerms[iK][iN][jN].U)
aeTermsE[jN] = bls12381.NewFr()
aeTermsE[jN].Set(aeTerms[iK][jN][iN][1])
aeTermsE[jN].Set(aeTerms[iK][jN][iN].V)
asTermsA[jN] = bls12381.NewFr()
asTermsA[jN].Set(asTerms[iK][iN][jN][0])
asTermsA[jN].Set(asTerms[iK][iN][jN].U)
asTermsS[jN] = bls12381.NewFr()
asTermsS[jN].Set(asTerms[iK][jN][iN][1])
asTermsS[jN].Set(asTerms[iK][jN][iN].V)
askTermsA[jN] = bls12381.NewFr()
askTermsA[jN].Set(askTerms[iK][iN][jN][0])
askTermsA[jN].Set(askTerms[iK][iN][jN].U)
askTermsSK[jN] = bls12381.NewFr()
askTermsSK[jN].Set(askTerms[iK][jN][iN][1])
askTermsSK[jN].Set(askTerms[iK][jN][iN].V)
}

preSignatureList[iK] = &fhksbbsplus.PerPartyPreSignature{
Expand Down
Loading

0 comments on commit 776be9c

Please sign in to comment.