-
Notifications
You must be signed in to change notification settings - Fork 75
/
birkhoffinterpolation.go
282 lines (254 loc) · 8.59 KB
/
birkhoffinterpolation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright © 2020 AMIS Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package birkhoffinterpolation
import (
"errors"
fmt "fmt"
"math/big"
"sort"
pt "github.com/getamis/alice/crypto/ecpointgrouplaw"
"github.com/getamis/alice/crypto/matrix"
"github.com/getamis/alice/crypto/utils"
"gonum.org/v1/gonum/stat/combin"
)
var (
//ErrEqualOrLargerThreshold is returned if threshold is equal or larger than the length of Bk parameters
ErrEqualOrLargerThreshold = errors.New("equal or larger threshold")
//ErrNoValidBks is returned if there's no valid bk
ErrNoValidBks = errors.New("no valid bks")
//ErrInvalidBks is returned if it exists a pair of invalid bks
ErrInvalidBks = errors.New("invalid bks")
//ErrNoExistBk is returned if there does not exist bk
ErrNoExistBk = errors.New("no exist bk")
ErrInconsistentPubKey = errors.New("inconsistent public key")
)
type BkParameter struct {
x *big.Int
rank uint32
}
func NewBkParameter(x *big.Int, rank uint32) *BkParameter {
return &BkParameter{
x: x,
rank: rank,
}
}
func (p *BkParameter) GetX() *big.Int {
return p.x
}
func (p *BkParameter) GetRank() uint32 {
return p.rank
}
func (p *BkParameter) String() string {
return fmt.Sprintf("(x, rank) = (%s, %d)", p.x, p.rank)
}
func (p *BkParameter) GetLinearEquationCoefficient(fieldOrder *big.Int, degreePoly uint32) []*big.Int {
result := make([]*big.Int, degreePoly+1)
for i := uint32(0); i < uint32(len(result)); i++ {
result[i] = p.getDiffMonomialCoeff(fieldOrder, i)
}
return result
}
func (p *BkParameter) ToMessage() *BkParameterMessage {
return &BkParameterMessage{
X: p.x.Bytes(),
Rank: p.rank,
}
}
// Consider a monomial x^n where n is the degree. Then output is n*(n_1)*...*(n-diffTime+1)*x^{degree-diffTimes}|_{x}
// Example:x^5, diffTime = 2 and x =3 Then output is 3^(3)*5*4
func (p *BkParameter) getDiffMonomialCoeff(fieldOrder *big.Int, degree uint32) *big.Int {
if degree < p.rank {
return big.NewInt(0)
}
if degree == 0 {
return big.NewInt(1)
}
// Get extra coefficient
tempValue := uint32(1)
for j := uint32(0); j < p.rank; j++ {
tempValue *= (degree - j)
}
extraValue := new(big.Int).SetUint64(uint64(tempValue))
// x^{degree-diffTimes}
power := new(big.Int).SetUint64(uint64(degree - p.rank))
result := new(big.Int).Exp(p.x, power, fieldOrder)
return result.Mul(result, extraValue)
}
type BkParameters []*BkParameter
// Compare rank and then x
// Let bk := (rank, x). Then if (rank1, x1) > (rank2,x2) iff rank1<rank2 or ( rank1=rank2 and x1>x2)
func (bks BkParameters) Less(i, j int) bool {
if bks[i].rank < bks[j].rank {
return true
}
if bks[i].rank > bks[j].rank {
return false
}
return bks[i].x.Cmp(bks[j].x) < 0
}
func (bks BkParameters) Len() int {
return len(bks)
}
func (bks BkParameters) Swap(i, j int) {
bks[i], bks[j] = bks[j], bks[i]
}
// If there exists one bks such that we can recover the secret key, then this check will pass.
func (bks BkParameters) CheckValid(threshold uint32, fieldOrder *big.Int) error {
if err := bks.ensureRankAndOrder(threshold, fieldOrder); err != nil {
return err
}
bkMap := make(map[string]uint32)
for i := 0; i < bks.Len(); i++ {
xString := bks[i].x.String()
v, ok := bkMap[xString]
if ok {
if v == bks[i].rank {
return ErrInvalidBks
}
} else {
bkMap[xString] = bks[i].rank
}
}
// Deep copy and sort the pk slice
sortedBks := make(BkParameters, bks.Len())
copy(sortedBks, bks)
sort.Sort(sortedBks)
// Get all combinations of C(threshold, len(ps)).
combination := combin.Combinations(sortedBks.Len(), int(threshold))
for i := 0; i < len(combination); i++ {
tempBks := BkParameters{}
for j := 0; j < len(combination[i]); j++ {
tempBks = append(tempBks, sortedBks[combination[i][j]])
}
birkhoffMatrix, err := tempBks.getLinearEquationCoefficientMatrix(threshold, fieldOrder)
if err != nil {
return err
}
rankBirkhoffMatrix, err := birkhoffMatrix.GetMatrixRank(fieldOrder)
if err != nil {
return err
}
if rankBirkhoffMatrix >= uint64(threshold) {
return nil
}
}
return ErrNoValidBks
}
// ComputeBkCoefficient returns the bk coefficients from parameters
func (bks BkParameters) ComputeBkCoefficient(threshold uint32, fieldOrder *big.Int) ([]*big.Int, error) {
if err := bks.ensureRankAndOrder(threshold, fieldOrder); err != nil {
return nil, err
}
return bks.computeBkCoefficient(threshold, fieldOrder)
}
func (bks BkParameters) ensureRankAndOrder(threshold uint32, fieldOrder *big.Int) error {
if err := utils.EnsureFieldOrder(fieldOrder); err != nil {
return err
}
if uint32(bks.Len()) < threshold {
return ErrEqualOrLargerThreshold
}
return nil
}
func (bks BkParameters) computeBkCoefficient(threshold uint32, fieldOrder *big.Int) ([]*big.Int, error) {
birkhoffMatrix, err := bks.getLinearEquationCoefficientMatrix(threshold, fieldOrder)
if err != nil {
return nil, err
}
result, err := birkhoffMatrix.Pseudoinverse()
if err != nil {
return nil, err
}
return result.GetRow(0)
}
// Establish the coefficient of linear system of Birkhoff systems. The relation of Birkhoff matrix and LinearEquationCoefficientMatrix is
// LinearEquationCoefficientMatrix = the inverse of Birkhoff matrix.
// Assume: share1: diffTime=0, x=1 share2: x=2, diffTime = 1, share3: x =3, differTime=2
// Then output is:
// 1^(diffTime)|_{x} x^(diffTime)|_{x} (x^2)^(diffTime)|_{x}
// [ 1 1 1 ] diffTime = 0, x =1
// [ 0 1 4 ] diffTime = 1, x =2
// [ 0 0 2 ] diffTime = 2, x =3
// This matrix is called Birkhoff matrix
func (bks BkParameters) getLinearEquationCoefficientMatrix(nThreshold uint32, fieldOrder *big.Int) (*matrix.Matrix, error) {
lens := bks.Len()
result := make([][]*big.Int, lens)
degree := nThreshold - 1
for i := 0; i < lens; i++ {
result[i] = bks[i].GetLinearEquationCoefficient(fieldOrder, degree)
}
return matrix.NewMatrix(fieldOrder, result)
}
// GetAddShareCoefficient computes [sum_{k=newRank}^{t-1} k!/(k-newRank)!(x_new)^(k-newRank)*b_{ki}]*s_i, newRank is the rank of newBk, x_new is x-coordinate of newBk, and b_{ki} is
// the (k,i)-component of the pseudoinverse of Birkhoff matrix associated bks.
func (bks BkParameters) GetAddShareCoefficient(ownBk, newBk *BkParameter, fieldOrder *big.Int, threshold uint32) (*big.Int, error) {
birkhoffMatrix, err := bks.getLinearEquationCoefficientMatrix(threshold, fieldOrder)
if err != nil {
return nil, err
}
birkhoffMatrix, err = birkhoffMatrix.Pseudoinverse()
if err != nil {
return nil, err
}
ownIndex, err := bks.getIndexOfBK(ownBk)
if err != nil {
return nil, err
}
newrank := uint64(newBk.rank)
result := big.NewInt(0)
xPower := big.NewInt(1)
// Get newrank!
newRankFactorial := big.NewInt(1)
for i := uint64(2); i < newrank+1; i++ {
newRankFactorial = newRankFactorial.Mul(newRankFactorial, new(big.Int).SetUint64(i))
newRankFactorial = newRankFactorial.Mod(newRankFactorial, fieldOrder)
}
for i := newrank; i < uint64(threshold); i++ {
factorialCoe := new(big.Int).Binomial(int64(i), int64(i-newrank))
factorialCoe = factorialCoe.Mul(factorialCoe, newRankFactorial)
tempbki := birkhoffMatrix.Get(i, uint64(ownIndex))
tempResult := new(big.Int).Mul(factorialCoe, xPower)
tempResult = tempResult.Mul(tempResult, tempbki)
result = result.Add(tempResult, result)
result = result.Mod(result, fieldOrder)
xPower = xPower.Mul(xPower, newBk.GetX())
}
return result, nil
}
func (bks BkParameters) getIndexOfBK(ownBk *BkParameter) (int, error) {
for i := 0; i < len(bks); i++ {
if bks[i].GetX().Cmp(ownBk.GetX()) != 0 {
continue
}
if bks[i].GetRank() == ownBk.rank {
return i, nil
}
}
return 0, ErrNoExistBk
}
func (bks BkParameters) ValidatePublicKey(sgs []*pt.ECPoint, threshold uint32, pubkey *pt.ECPoint) error {
fieldOrder := pubkey.GetCurve().Params().N
scalars, err := bks.ComputeBkCoefficient(threshold, fieldOrder)
if err != nil {
return err
}
gotPub, err := pt.ComputeLinearCombinationPoint(scalars, sgs)
if err != nil {
return err
}
if !pubkey.Equal(gotPub) {
return ErrInconsistentPubKey
}
return nil
}