/
pdp.go
320 lines (272 loc) · 9.37 KB
/
pdp.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright (c) 2019 lambdastorage.com
// --------
// This file is part of The proofDP library.
//
// The proofDP is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The proofDP is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the proofDP. If not, see <http://www.gnu.org/licenses/>.
package proofDP
import (
"bytes"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"fmt"
"io"
"strconv"
"strings"
"github.com/LambdaIM/proofDP/math"
"golang.org/x/crypto/scrypt"
)
// constant
const (
errParsePublicParamsFmt = "Failed to restore PublicParams: %s"
errGeneratePrivateParamFmt = "Failed to generate PrivateParams: %s"
errGenerateDataTagFmt = "Failed to generate tag for given data (index:%s): %s"
errGenerateDataChalFmt = "Failed to generate challenge for given data (index:%s): %s"
errProveFmt = "Failed to prove against challenge (index:%s): %s"
errParseChalFmt = "Failed to restore Chal: %s"
errParseProofFmt = "Failed to restore Proof: %s"
intStrRadix = 10
)
// PublicParams holds the public paramters of a specific PDP proof.
// Note that there may be multiple PublicParams instance coresponding
// to the same PrivateParams.
type PublicParams struct {
v math.EllipticPoint
u math.EllipticPoint
e math.QuadraticElem
}
// Marshal works as the serialization routine
func (pp *PublicParams) Marshal() string {
return fmt.Sprintf("%s,%s,%s", pp.v.Marshal(), pp.u.Marshal(), pp.e.Marshal())
}
// ParsePublicParams trys to restore a PublicParams instance from a given string
func ParsePublicParams(s string) (*PublicParams, error) {
parts := strings.Split(s, ",")
if len(parts) != 3 {
return nil, fmt.Errorf(errParsePublicParamsFmt, "unmatched parts num")
}
v, err := math.ParseEllipticPt(parts[0])
if err != nil {
return nil, fmt.Errorf(errParsePublicParamsFmt, err.Error())
}
u, err := math.ParseEllipticPt(parts[1])
if err != nil {
return nil, fmt.Errorf(errParsePublicParamsFmt, err.Error())
}
e, err := math.ParseQuadraticElem(parts[2])
if err != nil {
return nil, fmt.Errorf(errParsePublicParamsFmt, err.Error())
}
return &PublicParams{
v: v,
u: u,
e: e,
}, nil
}
// PrivateParams holds the private parameters of a specific PDP proof.
// Note a PrivateParams instance can be used to validate multiple
// PublicParams's proof.
type PrivateParams struct {
x math.GaloisElem
}
// Marshal works as a serialization
func (sp *PrivateParams) Marshal() string {
return sp.x.Marshal()
}
// ParsePrivateParams try to restore a PrivateParams instance
func ParsePrivateParams(s string) (*PrivateParams, error) {
x, err := math.ParseGaloisElem(s)
return &PrivateParams{x: x}, err
}
// Tag is the product of GenTag & a param of the VerifyProof
type Tag = math.EllipticPoint
// ParseTag try to restore a Tag instance
func ParseTag(s string) (Tag, error) {
return math.ParseEllipticPt(s)
}
// Chal wraps a validator created random value & corespoding idx
type Chal struct {
idx []byte
nu math.GaloisElem
}
// Marshal works as a serialization routine
func (c *Chal) Marshal() string {
return fmt.Sprintf("%s,%s", base64.StdEncoding.EncodeToString(c.idx), c.nu.Marshal())
}
// Equal works
func (c *Chal) Equal(a Chal) bool {
return bytes.Equal(c.idx, a.idx) && c.nu.Equal(a.nu)
}
func (c *Chal) GetNum() math.GaloisElem {
return c.nu
}
// ParseChal trys to restore a Chal instance
func ParseChal(s string) (Chal, error) {
parts := strings.Split(s, ",")
if len(parts) != 2 {
return Chal{}, fmt.Errorf(errParseChalFmt, "unmatch parts num")
}
idx, err := base64.StdEncoding.DecodeString(parts[0])
if err != nil {
return Chal{}, fmt.Errorf(errParseChalFmt, err.Error())
}
nu, err := math.ParseGaloisElem(parts[1])
if err != nil {
return Chal{}, fmt.Errorf(errParseChalFmt, err.Error())
}
return Chal{
idx: idx,
nu: nu,
}, nil
}
// Proof is the product of Prove
type Proof struct {
miu math.GaloisElem
sigma math.EllipticPoint
r math.QuadraticElem
}
// Marshal works as a serialization routine
func (p *Proof) Marshal() string {
return fmt.Sprintf("%s,%s,%s", p.miu.Marshal(), p.sigma.Marshal(), p.r.Marshal())
}
// ParseProof trys to restore a Proof instance by parsing given string
func ParseProof(s string) (Proof, error) {
parts := strings.Split(s, ",")
if len(parts) != 3 {
return Proof{}, fmt.Errorf(errParseProofFmt, "unmatched parts num")
}
miu, err := math.ParseGaloisElem(parts[0])
if err != nil {
return Proof{}, fmt.Errorf(errParseProofFmt, err.Error())
}
sigma, err := math.ParseEllipticPt(parts[1])
if err != nil {
return Proof{}, fmt.Errorf(errParseProofFmt, err.Error())
}
r, err := math.ParseQuadraticElem(parts[2])
if err != nil {
return Proof{}, fmt.Errorf(errParseProofFmt, err.Error())
}
return Proof{
miu: miu,
sigma: sigma,
r: r,
}, nil
}
// GeneratePrivateParams returns the PrivateParams instance created using
// given crypto.PrivKey
func GeneratePrivateParams(sk []byte) (*PrivateParams, error) {
salt := make([]byte, scryptR)
_, err := rand.Read(salt)
if err != nil {
return nil, fmt.Errorf(errGeneratePrivateParamFmt, err.Error())
}
saltedKey, err := scrypt.Key(sk, salt, scryptN, scryptR, scryptP, scryptL)
if err != nil {
return nil, fmt.Errorf(errGeneratePrivateParamFmt, err.Error())
}
return &PrivateParams{
x: math.HashToGaloisElem(saltedKey),
}, nil
}
// GeneratePublicParams returns a PublicParams instance generated using
// the given elliptic curve point 'u'
func (sp *PrivateParams) GeneratePublicParams(u math.EllipticPoint) *PublicParams {
v := math.EllipticPow(math.GetGenerator(), sp.x)
return &PublicParams{
v: v,
u: u,
e: math.BiLinearMap(u, v),
}
}
// GenTag calculates the tag for given 'data' & 'idx'. Since the 'data' block may
// be too huge to load into memory, a SHA256 digest is applied here.
// Note that 'idx' here is actually refers to the (Fid||index) parameter in PDP paper.
// And we actually apply a different way of 'idx'-related calculating.
func GenTag(sp *PrivateParams, pp *PublicParams, idx int64, data io.Reader) (Tag, error) {
idxStr := strconv.FormatInt(idx, intStrRadix)
hasher := sha256.New() // a singleton hasher maybe?
if _, err := io.Copy(hasher, data); err != nil {
return Tag{}, fmt.Errorf(errGenerateDataTagFmt, idxStr, err.Error())
}
m := math.BytesToGaloisElem(hasher.Sum(nil))
t := math.HashToEllipticPt([]byte(idxStr))
t = math.EllipticMul(t, math.EllipticPow(pp.u, m))
return math.EllipticPow(t, sp.x), nil
}
// GenChal created a challenge instance for given 'idx'.
// Note that 'idx' here is actually refers to the (Fid||index) parameter in PDP paper.
// Here the 'idx' works just as in GenTag() implementation.
// Also, GenChal creates just *ONE* challenge against the given 'idx'. According to
// the original paper, there should be a set of challenge against *ONE* file, which,
// however, is not going well with Lambda's system design.
func GenChal(idx int64) (Chal, error) {
idxStr := strconv.FormatInt(idx, intStrRadix)
nu, err := math.RandGaloisElem()
if err != nil {
return Chal{}, fmt.Errorf(errGenerateDataChalFmt, idxStr, err.Error())
}
return Chal{
idx: []byte(idxStr),
nu: nu,
}, nil
}
// GenChalWithSeed created a challenge instatnce for given 'idx'.
// This routine works in the same way that GenChal does, except that the
// GenChalWithSeed requires an external entropy input 'rand'.
func GenChalWithSeed(idx int64, rand []byte) (Chal, error) {
idxStr := strconv.FormatInt(idx, intStrRadix)
nu := math.BytesToGaloisElem(rand)
return Chal{
idx: []byte(idxStr),
nu: nu,
}, nil
}
// Prove created a Proof instance against the given challenge & the local storage.
// Note that in this implementation a Chal instance contains only *ONE* pair of
// challenge target index & coresponding random value.
func Prove(pp *PublicParams, c Chal, t Tag, data io.Reader) (Proof, error) {
rand, err := math.RandGaloisElem()
if err != nil {
return Proof{}, fmt.Errorf(errProveFmt, string(c.idx), err.Error())
}
r := math.QuadraticPow(pp.e, rand)
hasher := sha256.New()
if _, err := io.Copy(hasher, data); err != nil {
return Proof{}, fmt.Errorf(errProveFmt, string(c.idx), err.Error())
}
m := math.BytesToGaloisElem(hasher.Sum(nil))
miu := math.GaloisMul(c.nu, m)
miu = math.GaloisMul(miu, math.HashQuadraticToGalois(r))
miu = math.GaloisAdd(miu, rand)
sigma := math.EllipticPow(t, c.nu)
return Proof{
miu: miu,
sigma: sigma,
r: r,
}, nil
}
// VerifyProof validates if the given 'p' is exactly a sound
// proof against the given challenge 'c'
func VerifyProof(pp *PublicParams, c Chal, p Proof) bool {
gamma := math.HashQuadraticToGalois(p.r)
lhsParam := math.EllipticPow(p.sigma, gamma)
lhs := math.BiLinearMap(lhsParam, math.GetGenerator())
lhs = math.QuadraticMul(p.r, lhs)
rhsParam := math.EllipticPow(math.HashToEllipticPt(c.idx), c.nu)
rhsParam = math.EllipticPow(rhsParam, gamma)
rhsParam = math.EllipticMul(rhsParam, math.EllipticPow(pp.u, p.miu))
rhs := math.BiLinearMap(rhsParam, pp.v)
return math.QuadraticEqual(lhs, rhs)
}