-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdppk.go
More file actions
452 lines (374 loc) · 12 KB
/
dppk.go
File metadata and controls
452 lines (374 loc) · 12 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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// # Copyright (c) 2024 xtaci
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package dppk
import (
"crypto/rand"
"errors"
"math/big"
)
// DefaultPrime is the default prime number used in the DPPK protocol.
const DefaultPrime = "0x1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003d5"
const (
ERR_MSG_ORDER = "order must be at least 5"
ERR_MSG_NULL_ENCRYPT = "encrypted values cannot be null"
ERR_MSG_DATA_EXCEEDED = "the secret to encrypt is not in the GF(p)"
ERR_MSG_VU_PUBLICKEY = "VU in public key is not equal"
)
const secretMarker = "\x5f\x37\x59\xdf"
// defaultPrime is the prime number used in cryptographic operations.
var defaultPrime *big.Int
var (
errInvalidPrime = errors.New("Invalid Prime")
errNoQuadraticResidue = errors.New("ciphertext is not a quadratic residue")
errSingularQuadratic = errors.New("no modular inverse for quadratic coefficient")
errInvalidSecretFormat = errors.New("invalid secret encoding")
)
func init() {
defaultPrime, _ = new(big.Int).SetString(DefaultPrime, 0)
}
// PrivateKey represents a private key in the DPPK protocol.
type PrivateKey struct {
S0 *big.Int // Initial secret value
A0, A1, B0, B1 *big.Int // Coefficients for the polynomials
PublicKey
}
// PublicKey represents a public key in the DPPK protocol.
type PublicKey struct {
Prime *big.Int
VectorU []*big.Int // Coefficients for polynomial U
VectorV []*big.Int // Coefficients for polynomial V
}
// KEM represents a Key Encapsulation Mechanism in the DPPK protocol.
type KEM struct {
Ps *big.Int
Qs *big.Int
}
// Equal checks if two public keys are equal.
func (pub *PublicKey) Equal(other *PublicKey) bool {
if pub == nil || other == nil {
return false
}
if (pub.Prime == nil) != (other.Prime == nil) {
return false
}
if pub.Prime != nil && pub.Prime.Cmp(other.Prime) != 0 {
return false
}
if len(pub.VectorU) != len(other.VectorU) {
return false
}
if len(pub.VectorV) != len(other.VectorV) {
return false
}
for i := range pub.VectorU {
if pub.VectorU[i].Cmp(other.VectorU[i]) != 0 {
return false
}
}
for i := range pub.VectorV {
if pub.VectorV[i].Cmp(other.VectorV[i]) != 0 {
return false
}
}
return true
}
// Order returns the order of the public key.
func (pub *PublicKey) Order() int {
return len(pub.VectorU) - 1
}
// GenerateKey generates a new DPPK private key with the given order and prime number
// the prime number is a string formatted in base 10
func GenerateKeyWithPrime(order int, strPrime string) (*PrivateKey, error) {
customPrime, ok := big.NewInt(0).SetString(strPrime, 0)
if !ok {
return nil, errInvalidPrime
}
return generateKey(order, customPrime)
}
// GenerateKey generates a new DPPK private key with the given order and default prime number
func GenerateKey(order int) (*PrivateKey, error) {
return generateKey(order, defaultPrime)
}
// GenerateKey generates a new DPPK private key with the given order.
func generateKey(order int, prime *big.Int) (*PrivateKey, error) {
// Ensure the order is at least 5
if order < 5 {
return nil, errors.New(ERR_MSG_ORDER)
}
RETRY:
// Generate random coefficients for the polynomials
a0, err := rand.Int(rand.Reader, prime)
if err != nil {
return nil, err
}
a1, err := rand.Int(rand.Reader, prime)
if err != nil {
return nil, err
}
b0, err := rand.Int(rand.Reader, prime)
if err != nil {
return nil, err
}
b1, err := rand.Int(rand.Reader, prime)
if err != nil {
return nil, err
}
// Ensure all coefficients are distinct
if a0.Cmp(a1) == 0 || a0.Cmp(b0) == 0 || a0.Cmp(b1) == 0 || a1.Cmp(b0) == 0 || a1.Cmp(b1) == 0 || b0.Cmp(b1) == 0 {
goto RETRY
}
// Generate random coefficients for the polynomial Bn(x)
Bn := make([]*big.Int, order)
for i := 0; i < len(Bn); i++ {
r, err := rand.Int(rand.Reader, prime)
if err != nil {
return nil, err
}
Bn[i] = r
}
// Ensure the coefficient of x^n is 1
Bn = append(Bn, big.NewInt(1))
// Initialize vectors for polynomials U(x) and V(x)
vecU := make([]*big.Int, order+3)
vecV := make([]*big.Int, order+3)
for i := 0; i < order+3; i++ {
vecU[i] = big.NewInt(0)
vecV[i] = big.NewInt(0)
}
bigInt := new(big.Int)
// Compute the coefficients for the polynomials U(x) and V(x) using Vieta's formulas
for i := 0; i < order+1; i++ {
// Vector U
vecU[i].Add(vecU[i], bigInt.Mul(a0, Bn[i]))
vecU[i].Mod(vecU[i], prime)
vecU[i+1].Add(vecU[i+1], bigInt.Mul(a1, Bn[i]))
vecU[i+1].Mod(vecU[i+1], prime)
vecU[i+2].Add(vecU[i+2], Bn[i])
vecU[i+2].Mod(vecU[i+2], prime)
// Vector V
vecV[i].Add(vecV[i], bigInt.Mul(b0, Bn[i]))
vecV[i].Mod(vecV[i], prime)
vecV[i+1].Add(vecV[i+1], bigInt.Mul(b1, Bn[i]))
vecV[i+1].Mod(vecV[i+1], prime)
vecV[i+2].Add(vecV[i+2], Bn[i])
vecV[i+2].Mod(vecV[i+2], prime)
}
// Create the private key
priv := &PrivateKey{
S0: Bn[0],
A0: a0,
A1: a1,
B0: b0,
B1: b1,
}
// Set the public key vectors, excluding the first and last elements
priv.Prime = prime
priv.PublicKey.VectorU = vecU[1 : order+2]
priv.PublicKey.VectorV = vecV[1 : order+2]
return priv, nil
}
// encrypt encrypts a message with the given public key and the prime specified in public key
func encodeSecret(msg []byte) []byte {
encoded := make([]byte, len(msg)+len(secretMarker))
copy(encoded, secretMarker)
copy(encoded[len(secretMarker):], msg)
return encoded
}
func Encrypt(pub *PublicKey, msg []byte) (kem *KEM, err error) {
return encrypt(pub, msg, pub.Prime)
}
// encrypt encrypts a message with the given public key.
func encrypt(pub *PublicKey, msg []byte, prime *big.Int) (kem *KEM, err error) {
// Convert the message to a big integer
secret := new(big.Int).SetBytes(encodeSecret(msg))
if secret.Cmp(prime) >= 0 {
return nil, errors.New(ERR_MSG_DATA_EXCEEDED)
}
if len(pub.VectorU) != len(pub.VectorV) {
return nil, errors.New(ERR_MSG_VU_PUBLICKEY)
}
// Ensure the values in the public key are not nil
for i := range pub.VectorU {
if pub.VectorU[i] == nil {
return nil, errors.New(ERR_MSG_VU_PUBLICKEY)
}
if pub.VectorV[i] == nil {
return nil, errors.New(ERR_MSG_VU_PUBLICKEY)
}
}
// Extend the vectors U and Q with a constant term of 1
vecUExt := make([]*big.Int, len(pub.VectorU)+1)
vecVExt := make([]*big.Int, len(pub.VectorV)+1)
copy(vecUExt, pub.VectorU)
copy(vecVExt, pub.VectorV)
vecUExt[len(vecUExt)-1] = big.NewInt(1)
vecVExt[len(vecVExt)-1] = big.NewInt(1)
// Initialize variables for the encryption process
Ps := big.NewInt(0)
Qs := big.NewInt(0)
Si := new(big.Int).Set(secret)
UiSi := new(big.Int)
ViSi := new(big.Int)
// Compute the encrypted values Ps and Qs
for i := range vecUExt {
UiSi.Mul(Si, vecUExt[i])
UiSi.Mod(UiSi, prime)
Ps.Add(Ps, UiSi)
Ps.Mod(Ps, prime)
ViSi.Mul(Si, vecVExt[i])
ViSi.Mod(ViSi, prime)
Qs.Add(Qs, ViSi)
Qs.Mod(Qs, prime)
Si.Mul(Si, secret)
Si.Mod(Si, prime)
}
return &KEM{Ps: Ps, Qs: Qs}, nil
}
// Decrypt decrypts the encrypted values Ps and Qs using the private key.
func (priv *PrivateKey) Decrypt(kem *KEM) (x1, x2 *big.Int, err error) {
if kem == nil {
return nil, nil, errors.New(ERR_MSG_NULL_ENCRYPT)
}
Ps := kem.Ps
Qs := kem.Qs
if Ps == nil || Qs == nil {
return nil, nil, errors.New(ERR_MSG_NULL_ENCRYPT)
}
prime := priv.Prime
// Add constant term to get full Ps and Qs polynomial
polyP := new(big.Int).Set(Ps)
polyQ := new(big.Int).Set(Qs)
s0a0 := new(big.Int)
s0b0 := new(big.Int)
s0a0.Mul(priv.S0, priv.A0)
s0a0.Mod(s0a0, prime)
s0b0.Mul(priv.S0, priv.B0)
s0b0.Mod(s0b0, prime)
polyP.Add(polyP, s0a0)
polyP.Mod(polyP, prime)
polyQ.Add(polyQ, s0b0)
polyQ.Mod(polyQ, prime)
// Explanation:
// As:
// Ps := Bn * (x^2 + a1x + a0) mod p
// Qs := Bn * (x^2 + b1x + b0) mod p
//
// multiply the reverse of Bn on the both side of the equation, we have:
// Ps*revBn(s):= (x^2 + a1x + a0) mod p
// Qs*revBn(s):= (x^2 + b1x + b0) mod p
//
// to align the left and right side of the equation, we have:
// Ps* Qs * revBn(s):= (x^2 + a1x + a0) * Qs mod p
// Ps* Qs * revBn(s):= (x^2 + b1x + b0) * Ps mod p
//
// and evidently:
// (x^2 + a1x + a0) * Qs == (x^2 + b1x + b0) * Ps modp
//
// Solve this equation to get x
// the following procedure will be formalized to :
// ax^2 + bx + c = 0
a := new(big.Int)
revPs := new(big.Int).Sub(prime, polyP)
a.Add(polyQ, revPs)
a.Mod(a, priv.Prime)
b := new(big.Int)
a1Qs := new(big.Int).Mul(polyQ, priv.A1)
b1Ps := new(big.Int).Mul(polyP, priv.B1)
b1Ps.Mod(b1Ps, priv.Prime)
revb1Ps := new(big.Int).Sub(prime, b1Ps)
b.Add(a1Qs, revb1Ps)
b.Mod(b, priv.Prime)
c := new(big.Int)
a0Qs := new(big.Int).Mul(polyQ, priv.A0)
b0Ps := new(big.Int).Mul(polyP, priv.B0)
b0Ps.Mod(b0Ps, priv.Prime)
revb0Ps := new(big.Int).Sub(prime, b0Ps)
c.Add(a0Qs, revb0Ps)
c.Mod(c, priv.Prime)
// Solve the quadratic equation derived from Ps and Qs
// Compute the discriminant of the quadratic equation
bsquared := new(big.Int).Mul(b, b)
bsquared.Mod(bsquared, prime)
fourac := new(big.Int).Mul(big.NewInt(4), big.NewInt(0).Mul(a, c))
fourac.Mod(fourac, prime)
invFourac := new(big.Int).Sub(prime, fourac)
squared := big.NewInt(0).Add(bsquared, invFourac)
squared.Mod(squared, prime)
// Solve the quadratic equation
root := new(big.Int).ModSqrt(squared, prime)
if root == nil {
return nil, nil, errNoQuadraticResidue
}
// Calculate the roots of the equation
doubleA := new(big.Int).Mul(big.NewInt(2), a)
doubleA.Mod(doubleA, prime)
inv2a := new(big.Int).ModInverse(doubleA, prime)
if inv2a == nil {
return nil, nil, errSingularQuadratic
}
negb := new(big.Int).Sub(prime, b)
// Solve the quadratic equation:
//
// -b + sqrt(b^2 - 4ac)
// x1 = ---------------------
// 2a
//
// -b - sqrt(b^2 - 4ac)
// x2 = ---------------------
// 2a
revRoot := new(big.Int).Sub(prime, root)
x1 = big.NewInt(0).Add(negb, revRoot)
x1.Mod(x1, prime)
x1.Mul(x1, inv2a)
x1.Mod(x1, prime)
x2 = big.NewInt(0).Add(negb, root)
x2.Mod(x2, prime)
x2.Mul(x2, inv2a)
x2.Mod(x2, prime)
return x1, x2, nil
}
// DecryptMessage returns the plaintext message embedded in the ciphertext.
// It tries both candidate roots and returns the first one that matches the
// expected secret encoding marker.
func (priv *PrivateKey) DecryptMessage(kem *KEM) ([]byte, error) {
x1, x2, err := priv.Decrypt(kem)
if err != nil {
return nil, err
}
if msg, err := RecoverMessage(x1); err == nil {
return msg, nil
}
if msg, err := RecoverMessage(x2); err == nil {
return msg, nil
}
return nil, errInvalidSecretFormat
}
// RecoverMessage converts a decrypted root into the original plaintext.
func RecoverMessage(candidate *big.Int) ([]byte, error) {
if candidate == nil {
return nil, errInvalidSecretFormat
}
raw := candidate.Bytes()
if len(raw) < len(secretMarker) || string(raw[:len(secretMarker)]) != secretMarker {
return nil, errInvalidSecretFormat
}
msg := make([]byte, len(raw)-len(secretMarker))
copy(msg, raw[len(secretMarker):])
return msg, nil
}
// Public returns the public key of the private key.
func (priv *PrivateKey) Public() *PublicKey {
return &priv.PublicKey
}