-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathco_helpers.go
More file actions
249 lines (201 loc) · 5.94 KB
/
Copy pathco_helpers.go
File metadata and controls
249 lines (201 loc) · 5.94 KB
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
package ot
import (
"crypto/elliptic"
crand "crypto/rand"
"crypto/sha256"
"errors"
"fmt"
"io"
"math/big"
)
// ErrNilCurve signals that a helper received a nil elliptic curve.
var ErrNilCurve = errors.New("ot: nil curve")
// ErrPointNotOnCurve signals that an input point is not on the active curve.
var ErrPointNotOnCurve = errors.New("ot: point not on curve")
// ECPoint describes an affine curve point.
type ECPoint struct {
// X is the affine x-coordinate.
X *big.Int
// Y is the affine y-coordinate.
Y *big.Int
}
// LabelCiphertext stores both encrypted labels for a single wire.
type LabelCiphertext struct {
// Zero holds the ciphertext for the zero label.
Zero LabelData
// One holds the ciphertext for the one label.
One LabelData
}
// COSenderSetup contains the immutable metadata sampled by the sender.
type COSenderSetup struct {
// CurveName identifies the elliptic curve in use.
CurveName string
// Scalar stores the secret exponent 'a'.
Scalar *big.Int
// Ax is the affine x-coordinate for A = g^a.
Ax *big.Int
// Ay is the affine y-coordinate for A = g^a.
Ay *big.Int
// AaInvX stores the affine x-coordinate of A^{-a}.
AaInvX *big.Int
// AaInvY stores the affine y-coordinate of A^{-a}.
AaInvY *big.Int
}
// COChoiceBundle preserves the receiver-side secrets for later decryption.
type COChoiceBundle struct {
// CurveName identifies the elliptic curve in use.
CurveName string
// Ax stores the sender's affine x-coordinate.
Ax *big.Int
// Ay stores the sender's affine y-coordinate.
Ay *big.Int
// Scalars contains the receiver's random scalars.
Scalars []*big.Int
// Bits mirrors the receiver's choice bits.
Bits []bool
}
// GenerateCOSenderSetup samples the sender randomness and curve points.
func GenerateCOSenderSetup(rand io.Reader, curve elliptic.Curve) (COSenderSetup, error) {
if curve == nil {
return COSenderSetup{}, ErrNilCurve
}
params := curve.Params()
a, err := crand.Int(rand, params.N)
if err != nil {
return COSenderSetup{}, err
}
Ax, Ay := curve.ScalarBaseMult(a.Bytes())
Aax, Aay := curve.ScalarMult(Ax, Ay, a.Bytes())
AaInvx := big.NewInt(0).Set(Aax)
AaInvy := big.NewInt(0).Sub(params.P, Aay)
return COSenderSetup{
CurveName: curve.Params().Name,
Scalar: a,
Ax: Ax,
Ay: Ay,
AaInvX: AaInvx,
AaInvY: AaInvy,
}, nil
}
// EncryptCOCiphertexts encrypts wire labels for every evaluator input bit.
func EncryptCOCiphertexts(curve elliptic.Curve, setup COSenderSetup, points []ECPoint, wires []Wire) ([]LabelCiphertext, error) {
if curve == nil {
return nil, ErrNilCurve
}
if err := ensureOnCurve(curve, setup.Ax, setup.Ay); err != nil {
return nil, err
}
if len(points) != len(wires) {
return nil, fmt.Errorf("OT point count mismatch: got %d want %d", len(points), len(wires))
}
aBytes := setup.Scalar.Bytes()
result := make([]LabelCiphertext, len(points))
for idx, point := range points {
if err := ensureOnCurve(curve, point.X, point.Y); err != nil {
return nil, err
}
Bx, By := curve.ScalarMult(point.X, point.Y, aBytes)
Bax, Bay := curve.Add(Bx, By, setup.AaInvX, setup.AaInvY)
mask0 := deriveMask(Bx, By, uint64(idx))
mask1 := deriveMask(Bax, Bay, uint64(idx))
var tmp LabelData
wires[idx].L0.GetData(&tmp)
copy(result[idx].Zero[:], xor(mask0[:], tmp[:]))
wires[idx].L1.GetData(&tmp)
copy(result[idx].One[:], xor(mask1[:], tmp[:]))
}
return result, nil
}
// BuildCOChoices constructs the receiver EC points for each choice bit.
func BuildCOChoices(rand io.Reader, curve elliptic.Curve, Ax, Ay *big.Int, bits []bool) (COChoiceBundle, []ECPoint, error) {
if curve == nil {
return COChoiceBundle{}, nil, ErrNilCurve
}
if err := ensureOnCurve(curve, Ax, Ay); err != nil {
return COChoiceBundle{}, nil, err
}
params := curve.Params()
points := make([]ECPoint, len(bits))
scalars := make([]*big.Int, len(bits))
for idx, bit := range bits {
b, err := crand.Int(rand, params.N)
if err != nil {
return COChoiceBundle{}, nil, err
}
scalars[idx] = b
Bx, By := curve.ScalarBaseMult(b.Bytes())
if bit {
Bx, By = curve.Add(Bx, By, Ax, Ay)
}
points[idx] = ECPoint{
X: Bx,
Y: By,
}
}
bundle := COChoiceBundle{
CurveName: curve.Params().Name,
Ax: new(big.Int).Set(Ax),
Ay: new(big.Int).Set(Ay),
Scalars: scalars,
Bits: append([]bool(nil), bits...),
}
return bundle, points, nil
}
// ensureOnCurve verifies that (x,y) is a valid affine point on the curve.
func ensureOnCurve(curve elliptic.Curve, x, y *big.Int) error {
if curve == nil {
return ErrNilCurve
}
if x == nil || y == nil || !curve.IsOnCurve(x, y) {
return ErrPointNotOnCurve
}
return nil
}
// DecryptCOCiphertexts decodes the chosen labels from ciphertexts.
func DecryptCOCiphertexts(curve elliptic.Curve, bundle COChoiceBundle, data []LabelCiphertext) ([]Label, error) {
if curve == nil {
return nil, ErrNilCurve
}
count := len(bundle.Bits)
if len(bundle.Scalars) != count || len(data) != count {
return nil, fmt.Errorf("invalid CO ciphertext bundle")
}
result := make([]Label, count)
for idx := 0; idx < count; idx++ {
Asx, Asy := curve.ScalarMult(bundle.Ax, bundle.Ay, bundle.Scalars[idx].Bytes())
mask := deriveMask(Asx, Asy, uint64(idx))
var cipher []byte
if bundle.Bits[idx] {
cipher = data[idx].One[:]
} else {
cipher = data[idx].Zero[:]
}
var tmp LabelData
copy(tmp[:], xor(mask[:], cipher))
result[idx].SetData(&tmp)
}
return result, nil
}
// deriveMask derives the XOR pad for a particular Diffie-Hellman output.
func deriveMask(x, y *big.Int, id uint64) [sha256.Size]byte {
hash := sha256.New()
hash.Write(x.Bytes())
hash.Write(y.Bytes())
var idBuf [8]byte
bo.PutUint64(idBuf[:], id)
hash.Write(idBuf[:])
var sum [sha256.Size]byte
hash.Sum(sum[:0])
return sum
}
// xor returns dst XOR src (truncating to the shorter slice).
func xor(dst, src []byte) []byte {
l := len(dst)
if len(src) < l {
l = len(src)
}
for i := 0; i < l; i++ {
dst[i] ^= src[i]
}
return dst[:l]
}