-
Notifications
You must be signed in to change notification settings - Fork 15
/
indcpa.go
235 lines (220 loc) · 7.59 KB
/
indcpa.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
/* SPDX-FileCopyrightText: © 2020-2024 Nadim Kobeissi <[email protected]>
* SPDX-License-Identifier: MIT */
package kyberk2so
import (
"crypto/rand"
"golang.org/x/crypto/sha3"
)
// indcpaPackPublicKey serializes the public key as a concatenation of the
// serialized vector of polynomials of the public key, and the public seed
// used to generate the matrix `A`.
func indcpaPackPublicKey(publicKey polyvec, seed []byte, paramsK int) []byte {
return append(polyvecToBytes(publicKey, paramsK), seed...)
}
// indcpaUnpackPublicKey de-serializes the public key from a byte array
// and represents the approximate inverse of indcpaPackPublicKey.
func indcpaUnpackPublicKey(packedPublicKey []byte, paramsK int) (polyvec, []byte) {
switch paramsK {
case 2:
publicKeyPolyvec := polyvecFromBytes(packedPublicKey[:paramsPolyvecBytesK512], paramsK)
seed := packedPublicKey[paramsPolyvecBytesK512:]
return publicKeyPolyvec, seed
case 3:
publicKeyPolyvec := polyvecFromBytes(packedPublicKey[:paramsPolyvecBytesK768], paramsK)
seed := packedPublicKey[paramsPolyvecBytesK768:]
return publicKeyPolyvec, seed
default:
publicKeyPolyvec := polyvecFromBytes(packedPublicKey[:paramsPolyvecBytesK1024], paramsK)
seed := packedPublicKey[paramsPolyvecBytesK1024:]
return publicKeyPolyvec, seed
}
}
// indcpaPackPrivateKey serializes the private key.
func indcpaPackPrivateKey(privateKey polyvec, paramsK int) []byte {
return polyvecToBytes(privateKey, paramsK)
}
// indcpaUnpackPrivateKey de-serializes the private key and represents
// the inverse of indcpaPackPrivateKey.
func indcpaUnpackPrivateKey(packedPrivateKey []byte, paramsK int) polyvec {
return polyvecFromBytes(packedPrivateKey, paramsK)
}
// indcpaPackCiphertext serializes the ciphertext as a concatenation of
// the compressed and serialized vector of polynomials `b` and the
// compressed and serialized polynomial `v`.
func indcpaPackCiphertext(b polyvec, v poly, paramsK int) []byte {
return append(polyvecCompress(b, paramsK), polyCompress(v, paramsK)...)
}
// indcpaUnpackCiphertext de-serializes and decompresses the ciphertext
// from a byte array, and represents the approximate inverse of
// indcpaPackCiphertext.
func indcpaUnpackCiphertext(c []byte, paramsK int) (polyvec, poly) {
switch paramsK {
case 2:
b := polyvecDecompress(c[:paramsPolyvecCompressedBytesK512], paramsK)
v := polyDecompress(c[paramsPolyvecCompressedBytesK512:], paramsK)
return b, v
case 3:
b := polyvecDecompress(c[:paramsPolyvecCompressedBytesK768], paramsK)
v := polyDecompress(c[paramsPolyvecCompressedBytesK768:], paramsK)
return b, v
default:
b := polyvecDecompress(c[:paramsPolyvecCompressedBytesK1024], paramsK)
v := polyDecompress(c[paramsPolyvecCompressedBytesK1024:], paramsK)
return b, v
}
}
// indcpaRejUniform runs rejection sampling on uniform random bytes
// to generate uniform random integers modulo `Q`.
func indcpaRejUniform(buf []byte, bufl int, l int) (poly, int) {
var r poly
var d1 uint16
var d2 uint16
i := 0
j := 0
for i < l && j+3 <= bufl {
d1 = (uint16((buf[j])>>0) | (uint16(buf[j+1]) << 8)) & 0xFFF
d2 = (uint16((buf[j+1])>>4) | (uint16(buf[j+2]) << 4)) & 0xFFF
j = j + 3
if d1 < uint16(paramsQ) {
r[i] = int16(d1)
i = i + 1
}
if i < l && d2 < uint16(paramsQ) {
r[i] = int16(d2)
i = i + 1
}
}
return r, i
}
// indcpaGenMatrix deterministically generates a matrix `A` (or the transpose of `A`)
// from a seed. Entries of the matrix are polynomials that look uniformly random.
// Performs rejection sampling on the output of an extendable-output function (XOF).
func indcpaGenMatrix(seed []byte, transposed bool, paramsK int) ([]polyvec, error) {
r := make([]polyvec, paramsK)
buf := make([]byte, 672)
xof := sha3.NewShake128()
ctr := 0
for i := 0; i < paramsK; i++ {
r[i] = polyvecNew(paramsK)
for j := 0; j < paramsK; j++ {
xof.Reset()
var err error
if transposed {
_, err = xof.Write(append(seed, byte(i), byte(j)))
} else {
_, err = xof.Write(append(seed, byte(j), byte(i)))
}
if err != nil {
return []polyvec{}, err
}
_, err = xof.Read(buf)
if err != nil {
return []polyvec{}, err
}
r[i][j], ctr = indcpaRejUniform(buf[:504], 504, paramsN)
for ctr < paramsN {
missing, ctrn := indcpaRejUniform(buf[504:672], 168, paramsN-ctr)
for k := ctr; k < paramsN; k++ {
r[i][j][k] = missing[k-ctr]
}
ctr = ctr + ctrn
}
}
}
return r, nil
}
// indcpaPrf provides a pseudo-random function (PRF) which returns
// a byte array of length `l`, using the provided key and nonce
// to instantiate the PRF's underlying hash function.
func indcpaPrf(l int, key []byte, nonce byte) []byte {
hash := make([]byte, l)
sha3.ShakeSum256(hash, append(key, nonce))
return hash
}
// indcpaKeypair generates public and private keys for the CPA-secure
// public-key encryption scheme underlying Kyber.
func indcpaKeypair(paramsK int) ([]byte, []byte, error) {
skpv := polyvecNew(paramsK)
pkpv := polyvecNew(paramsK)
e := polyvecNew(paramsK)
buf := make([]byte, 2*paramsSymBytes)
h := sha3.New512()
_, err := rand.Read(buf[:paramsSymBytes])
if err != nil {
return []byte{}, []byte{}, err
}
_, err = h.Write(buf[:paramsSymBytes])
if err != nil {
return []byte{}, []byte{}, err
}
buf = buf[:0]
buf = h.Sum(buf)
publicSeed := make([]byte, paramsSymBytes)
noiseSeed := make([]byte, paramsSymBytes)
copy(publicSeed, buf[:paramsSymBytes])
copy(noiseSeed, buf[paramsSymBytes:])
a, err := indcpaGenMatrix(publicSeed, false, paramsK)
if err != nil {
return []byte{}, []byte{}, err
}
var nonce byte
for i := 0; i < paramsK; i++ {
skpv[i] = polyGetNoise(noiseSeed, nonce, paramsK)
nonce = nonce + 1
}
for i := 0; i < paramsK; i++ {
e[i] = polyGetNoise(noiseSeed, nonce, paramsK)
nonce = nonce + 1
}
polyvecNtt(skpv, paramsK)
polyvecReduce(skpv, paramsK)
polyvecNtt(e, paramsK)
for i := 0; i < paramsK; i++ {
pkpv[i] = polyToMont(polyvecPointWiseAccMontgomery(a[i], skpv, paramsK))
}
polyvecAdd(pkpv, e, paramsK)
polyvecReduce(pkpv, paramsK)
return indcpaPackPrivateKey(skpv, paramsK), indcpaPackPublicKey(pkpv, publicSeed, paramsK), nil
}
// indcpaEncrypt is the encryption function of the CPA-secure
// public-key encryption scheme underlying Kyber.
func indcpaEncrypt(m []byte, publicKey []byte, coins []byte, paramsK int) ([]byte, error) {
sp := polyvecNew(paramsK)
ep := polyvecNew(paramsK)
bp := polyvecNew(paramsK)
publicKeyPolyvec, seed := indcpaUnpackPublicKey(publicKey, paramsK)
k := polyFromMsg(m)
at, err := indcpaGenMatrix(seed[:paramsSymBytes], true, paramsK)
if err != nil {
return []byte{}, err
}
for i := 0; i < paramsK; i++ {
sp[i] = polyGetNoise(coins, byte(i), paramsK)
ep[i] = polyGetNoise(coins, byte(i+paramsK), 3)
}
epp := polyGetNoise(coins, byte(paramsK*2), 3)
polyvecNtt(sp, paramsK)
polyvecReduce(sp, paramsK)
for i := 0; i < paramsK; i++ {
bp[i] = polyvecPointWiseAccMontgomery(at[i], sp, paramsK)
}
v := polyvecPointWiseAccMontgomery(publicKeyPolyvec, sp, paramsK)
polyvecInvNttToMont(bp, paramsK)
v = polyInvNttToMont(v)
polyvecAdd(bp, ep, paramsK)
v = polyAdd(polyAdd(v, epp), k)
polyvecReduce(bp, paramsK)
return indcpaPackCiphertext(bp, polyReduce(v), paramsK), nil
}
// indcpaDecrypt is the decryption function of the CPA-secure
// public-key encryption scheme underlying Kyber.
func indcpaDecrypt(c []byte, privateKey []byte, paramsK int) []byte {
bp, v := indcpaUnpackCiphertext(c, paramsK)
privateKeyPolyvec := indcpaUnpackPrivateKey(privateKey, paramsK)
polyvecNtt(bp, paramsK)
mp := polyvecPointWiseAccMontgomery(privateKeyPolyvec, bp, paramsK)
mp = polyInvNttToMont(mp)
mp = polySub(v, mp)
mp = polyReduce(mp)
return polyToMsg(mp)
}