-
Notifications
You must be signed in to change notification settings - Fork 145
/
oprf.go
289 lines (247 loc) · 7.49 KB
/
oprf.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
// Package oprf provides Verifiable, Oblivious Pseudo-Random Functions.
//
// An Oblivious Pseudorandom Function (OPRFs) is a two-party protocol for
// computing the output of a PRF. One party (the server) holds the PRF secret
// key, and the other (the client) holds the PRF input.
//
// This package is compatible with the OPRF specification at RFC-9497 [1].
//
// # Protocol Overview
//
// This diagram shows the steps of the protocol that are common for all operation modes.
//
// Client(info*) Server(sk, pk, info*)
// =================================================================
// finData, evalReq = Blind(input)
//
// evalReq
// ---------->
//
// evaluation = Evaluate(evalReq, info*)
//
// evaluation
// <----------
//
// output = Finalize(finData, evaluation, info*)
//
// # Operation Modes
//
// Each operation mode provides different properties to the PRF.
//
// Base Mode: Provides obliviousness to the PRF evaluation, i.e., it ensures
// that the server does not learn anything about the client's input and output
// during the Evaluation step.
//
// Verifiable Mode: Extends the Base mode allowing the client to verify that
// Server used the private key that corresponds to the public key.
//
// Partial Oblivious Mode: Extends the Verifiable mode by including shared
// public information to the PRF input.
//
// All three modes can perform batches of PRF evaluations, so passing an array
// of inputs will produce an array of outputs.
//
// # References
//
// [1] RFC-9497: https://www.rfc-editor.org/info/rfc9497
package oprf
import (
"crypto"
"encoding/binary"
"errors"
"hash"
"io"
"math"
"github.com/cloudflare/circl/group"
"github.com/cloudflare/circl/zk/dleq"
)
const (
version = "OPRFV1-"
finalizeDST = "Finalize"
hashToGroupDST = "HashToGroup-"
hashToScalarDST = "HashToScalar-"
deriveKeyPairDST = "DeriveKeyPair"
infoLabel = "Info"
)
type Mode = uint8
const (
BaseMode Mode = 0x00
VerifiableMode Mode = 0x01
PartialObliviousMode Mode = 0x02
)
func isValidMode(m Mode) bool {
return m == BaseMode || m == VerifiableMode || m == PartialObliviousMode
}
type Suite interface {
Identifier() string
Group() group.Group
Hash() crypto.Hash
cannotBeImplementedExternally()
}
var (
// SuiteRistretto255 represents the OPRF with Ristretto255 and SHA-512
SuiteRistretto255 Suite = params{identifier: "ristretto255-SHA512", group: group.Ristretto255, hash: crypto.SHA512}
// SuiteP256 represents the OPRF with P-256 and SHA-256.
SuiteP256 Suite = params{identifier: "P256-SHA256", group: group.P256, hash: crypto.SHA256}
// SuiteP384 represents the OPRF with P-384 and SHA-384.
SuiteP384 Suite = params{identifier: "P384-SHA384", group: group.P384, hash: crypto.SHA384}
// SuiteP521 represents the OPRF with P-521 and SHA-512.
SuiteP521 Suite = params{identifier: "P521-SHA512", group: group.P521, hash: crypto.SHA512}
)
func GetSuite(identifier string) (Suite, error) {
for _, suite := range []Suite{SuiteRistretto255, SuiteP256, SuiteP384, SuiteP521} {
if suite.Identifier() == identifier {
return suite, nil
}
}
return nil, ErrInvalidSuite
}
func NewClient(s Suite) Client {
p := s.(params)
p.m = BaseMode
return Client{client{p}}
}
func NewVerifiableClient(s Suite, server *PublicKey) VerifiableClient {
p, ok := s.(params)
if !ok || server == nil {
panic(ErrNoKey)
}
p.m = VerifiableMode
return VerifiableClient{client{p}, server}
}
func NewPartialObliviousClient(s Suite, server *PublicKey) PartialObliviousClient {
p, ok := s.(params)
if !ok || server == nil {
panic(ErrNoKey)
}
p.m = PartialObliviousMode
return PartialObliviousClient{client{p}, server}
}
func NewServer(s Suite, key *PrivateKey) Server {
p, ok := s.(params)
if !ok || key == nil {
panic(ErrNoKey)
}
p.m = BaseMode
return Server{server{p, key}}
}
func NewVerifiableServer(s Suite, key *PrivateKey) VerifiableServer {
p, ok := s.(params)
if !ok || key == nil {
panic(ErrNoKey)
}
p.m = VerifiableMode
return VerifiableServer{server{p, key}}
}
func NewPartialObliviousServer(s Suite, key *PrivateKey) PartialObliviousServer {
p, ok := s.(params)
if !ok || key == nil {
panic(ErrNoKey)
}
p.m = PartialObliviousMode
return PartialObliviousServer{server{p, key}}
}
type params struct {
m Mode
group group.Group
hash crypto.Hash
identifier string
}
func (p params) cannotBeImplementedExternally() {}
func (p params) String() string { return p.Identifier() }
func (p params) Group() group.Group { return p.group }
func (p params) Hash() crypto.Hash { return p.hash }
func (p params) Identifier() string { return p.identifier }
func (p params) getDST(name string) []byte {
return append(append(append(append(
[]byte{},
[]byte(name)...),
[]byte(version)...),
[]byte{p.m, byte('-')}...),
[]byte(p.identifier)...)
}
func (p params) scalarFromInfo(info []byte) (group.Scalar, error) {
if len(info) > math.MaxUint16 {
return nil, ErrInvalidInfo
}
lenInfo := []byte{0, 0}
binary.BigEndian.PutUint16(lenInfo, uint16(len(info)))
framedInfo := append(append(append([]byte{},
[]byte(infoLabel)...),
lenInfo...),
info...)
return p.group.HashToScalar(framedInfo, p.getDST(hashToScalarDST)), nil
}
func (p params) finalizeHash(h hash.Hash, input, info, element []byte) []byte {
h.Reset()
lenBuf := []byte{0, 0}
binary.BigEndian.PutUint16(lenBuf, uint16(len(input)))
mustWrite(h, lenBuf)
mustWrite(h, input)
if p.m == PartialObliviousMode {
binary.BigEndian.PutUint16(lenBuf, uint16(len(info)))
mustWrite(h, lenBuf)
mustWrite(h, info)
}
binary.BigEndian.PutUint16(lenBuf, uint16(len(element)))
mustWrite(h, lenBuf)
mustWrite(h, element)
mustWrite(h, []byte(finalizeDST))
return h.Sum(nil)
}
func (p params) getDLEQParams() (out dleq.Params) {
out.G = p.group
out.H = p.hash
out.DST = p.getDST("")
return
}
func mustWrite(h io.Writer, bytes []byte) {
bytesLen, err := h.Write(bytes)
if err != nil {
panic(err)
}
if len(bytes) != bytesLen {
panic("oprf: failed to write")
}
}
var (
ErrInvalidSuite = errors.New("oprf: invalid suite")
ErrInvalidMode = errors.New("oprf: invalid mode")
ErrDeriveKeyPairError = errors.New("oprf: key pair derivation failed")
ErrInvalidInput = errors.New("oprf: invalid input")
ErrInvalidSeed = errors.New("oprf: invalid seed size")
ErrInvalidInfo = errors.New("oprf: invalid info")
ErrInvalidProof = errors.New("oprf: proof verification failed")
ErrInverseZero = errors.New("oprf: inverting a zero value")
ErrNoKey = errors.New("oprf: must provide a key")
)
type (
Blind = group.Scalar
Blinded = group.Element
Evaluated = group.Element
)
// FinalizeData encapsulates data needed for Finalize step.
type FinalizeData struct {
inputs [][]byte
blinds []Blind
evalReq *EvaluationRequest
}
// CopyBlinds copies the serialized blinds to use when deterministically
// invoking DeterministicBlind.
func (f FinalizeData) CopyBlinds() []Blind {
out := make([]Blind, len(f.blinds))
for i, b := range f.blinds {
out[i] = b.Copy()
}
return out
}
// EvaluationRequest contains the blinded elements to be evaluated by the Server.
type EvaluationRequest struct {
Elements []Blinded
}
// Evaluation contains a list of elements produced during server's evaluation, and
// for verifiable modes it also includes a proof.
type Evaluation struct {
Elements []Evaluated
Proof *dleq.Proof
}