-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDigitalSignature.cc
743 lines (570 loc) · 17.1 KB
/
DigitalSignature.cc
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
////
// Digital Signature Module
// Lattice-Based Post-Quantum Authenticated Key Exchange
// Designed by Del Pino, Lyubasgevsky, Pointcheval
// Implemented by Pedro Miguel Sosa
// FFT, GPV, Sampling code repurpose from Thomas Prest's IBE scheme
////
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <complex.h>
#include <time.h>
#include <NTL/ZZ.h>
#include <NTL/ZZX.h>
#include <NTL/mat_ZZ.h>
#include <gmp.h>
#include <openssl/sha.h>
#include "Sampling.h"
#include "params.h"
#include "FFT.h"
#include "Random.h"
#include "Algebra.h"
#include "DigitalSignature.h"
using namespace std;
using namespace NTL;
const ZZX phi = Cyclo();
const bool dtime = false; //Print Timing Info
const bool debug = false; //Print Debug Info
///DIGITAL SIGNATURE - Pedro M. Sosa///
void SigKeyGen(ZZX Ks[2],ZZ_pX& Kv, MSK_Data* MSKD){
ZZX MSK[4];
ZZ_pX MPK;
//GENERATE MATRIX B
Keygen(MPK, MSK);
CompleteMSK(MSKD, MSK);
//ZZX f = MSKD -> PrK[0]; //Notice both paper invert f <==> g
//ZZX g = MSKD -> PrK[1]; //So for simplicity think of them as nom/denom
Ks[0] = MSKD -> PrK[0]; // denom
Ks[1] = MSKD -> PrK[1]; // nom
Kv = Quotient(Ks[0],Ks[1]); //h
return;
}
/////////////////////////////////////////////////////////////////////
// Scheme with message recovery!
/////////////////////////////////////////////////////////////////////
void Sign(ZZX s[2],vec_ZZ& msg, vec_ZZ& r, MSK_Data* MSKD){
//Hash Function
r = conv<vec_ZZ>(RandomPoly(50));
vec_ZZ hashed;
Hash(hashed, r, msg);
IBE_Extract(s, hashed, MSKD);
return;
}
bool Verify(ZZX Kv,ZZX s[2], vec_ZZ& msg, vec_ZZ& r){
//Hash id
const ZZX phi = Cyclo();
vec_ZZ hashed = vec_ZZ();
Hash(hashed, r, msg);
ZZX c;
FFTmultiply(c,Kv,s[1]);
ZZ_pX aux = conv<ZZ_pX>((conv<ZZX>(hashed) - (c))%phi);
s[0] = conv<ZZX>(aux);
modCoeffs(s[0],q1);
double norm1 = 0;
double norm2 = 0;
double norm3 = 0;
for(int i=0; i < deg(s[0]);i++){
norm1 += conv<double>(s[0][i]*s[0][i]);
norm2 += conv<double>(s[1][i]*s[1][i]);
}
norm1 = sqrt(norm1);
norm2 = sqrt(norm2);
norm3 = sqrt(norm1*norm1+norm2*norm2);
if (debug){
cout << norm3 << " |" << conv<ZZ>(DS_SIGMA*sqrt(q0/2*N0)*sqrt(2*N0));
}
return (norm3 < conv<ZZ>(DS_SIGMA*sqrt(q0/2*N0)*sqrt(2*N0)));//conv<ZZ>(1.36*q0/2*sqrt(2*N0)));
}
void Hash(vec_ZZ& hashed, vec_ZZ& r, vec_ZZ& msg){
SHA256_CTX ctx;
unsigned char digest[SHA256_DIGEST_LENGTH];
SHA256_Init(&ctx);
//vec_ZZ msg = RandomVector();
hashed = msg;
// if (debug){
// cout << "msg:" << msg << "\n";
// cout << " r:" << r << "\n";
// cout << "m+r:" << hashed << "\n";
// }
for(int i = 0; i < msg.length(); i++) {
char f = (conv<int>(msg[i])%255);
SHA256_Update(&ctx, &f, 1);
}
for(int i = 0; i < r.length(); i++) {
char f = (conv<int>(r[i])%255);
SHA256_Update(&ctx, &f, 1);
}
SHA256_Final(digest, &ctx);
for(int i=0; i < SHA256_DIGEST_LENGTH and i < N0; i++){
//cout << int(digest[i]) << " ";
hashed[i] = conv<ZZ>(digest[i])%q0;
}
if (debug){
cout << "Hashed: " << hashed << "\n";
}
}
/////////////////////////////////////////////////////////////////////
// Scheme without message recovery!
/////////////////////////////////////////////////////////////////////
void HashF(vec_ZZ& hashed, vec_ZZ& msg){
SHA256_CTX ctx;
unsigned char digest[SHA256_DIGEST_LENGTH];
SHA256_Init(&ctx);
//SHA256_Update(&ctx, "F", 1);
//vec_ZZ msg = RandomVector();
hashed.SetLength(32);
//cout << SHA256_DIGEST_LENGTH <<"\n";
//cout << "hashed: ------ "<< hashed <<"\n\n";
//cout << "MSG" << msg << "\n";
// if (debug){
// cout << "msg:" << msg << "\n";
// cout << " r:" << r << "\n";
// cout << "m+r:" << hashed << "\n";
// }
//cout << "Elem: ";
for(int i = 0; i < msg.length(); i++) {
char f = (conv<int>(msg[i])%255);
SHA256_Update(&ctx, &f, 1);
//cout << int(f) << ",";
}
//cout << "\nHASH:";
SHA256_Final(digest, &ctx);
for(int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
//cout << int(digest[i]) <<",";
}
for(int i=0; i < 32 and i < N0; i++){
//cout << int(digest[i]) << " ";
hashed[i] = conv<ZZ>(digest[i])%q0;
}
//cout << "\n\nh:" <<hashed <<"\n";
if (debug){
cout << "Hashed F: " << hashed << "\n";
}
}
void HashH(vec_ZZ& hashed, vec_ZZ& msg){
SHA256_CTX ctx;
unsigned char digest[SHA256_DIGEST_LENGTH];
SHA256_Init(&ctx);
SHA256_Update(&ctx, "H", 1);
//vec_ZZ msg = RandomVector();
hashed.SetLength(32);
// if (debug){
// cout << "msg:" << msg << "\n";
// cout << " r:" << r << "\n";
// cout << "m+r:" << hashed << "\n";
// }
//cout << "\n----------HASH-----------------\n";
//cout << "INPUT:" << msg <<"\n";
for(int i = 0; i < msg.length(); i++) {
char f = (conv<int>(msg[i])%255);
SHA256_Update(&ctx, &f, 1);
}
SHA256_Final(digest, &ctx);
for(int i=0; i < 32 and i < N0; i++){
//cout << int(digest[i]) << " ";
hashed[i] = conv<ZZ>(digest[i])%255;
}
//cout << "OUTPUT:" << hashed << "\n";
if (debug){
cout << "Hashed H: " << hashed << "\n";
}
}
void Sign2(ZZX s[2],vec_ZZ& m2, vec_ZZ& msg, MSK_Data* MSKD){
vec_ZZ m1;
vec_ZZ h1,f1,t;
// 512: |m1|=496, 1024: |m1|= 1008
modCoeffs(msg,q1);
int m1_size = N0-32;
m1.SetLength(m1_size);
m2.SetLength(msg.length()-m1_size);
for (int i=0; i < m1_size; i++){
m1[i] = msg[i];
if (i+m1_size < msg.length()){
m2[i] = msg[i+m1_size];
}
}
if (debug){
cout << "M:" << msg << "\n";
cout << "M1:" << m1 << "\n";
cout << "M2:" << m2 << "\n";
}
//cout << "M1:" << m1 << "\n";
// t = (m1 + F(H(m))) mod q || H(m))
HashH(h1,msg);
HashF(f1,h1);
//cout << "f1:"<<f1 <<"\n";
//cout << "h1:"<<h1 <<"\n";
//cout << "f1:"<<f1 << "\n";
f1.SetLength(m1_size);
for (int i=0; i < f1.length(); i++){
f1[i] = (f1[i]+m1[i])%q0;
}
//cout << "Message:"<<msg <<"\n";
t = f1;
t.append(h1);
//cout << "t:"<<t <<"\n";
//cout << "\nt:"<< t.length()<<"\n";
//cout << "\nt:"<< t.length()<<"\n";
// s1,s2 DS such that hs1 +s2 = t mod q
IBE_Extract(s, t, MSKD);
// This returns s[1] and s[2] and m2 (last part of message)
}
bool Verify2(ZZX Kv,ZZX s[2], vec_ZZ& m2, vec_ZZ& m1){
//Hash id
vec_ZZ t,t1,h1,f1,t2;
const ZZX phi = Cyclo();
vec_ZZ hashed = vec_ZZ();
ZZX c;
FFTmultiply(c,Kv,s[1]);
c = c + s[0];
t = conv<vec_ZZ>(c);
t1.SetLength(N0-32);
h1.SetLength(32);
for (int i=0; i < N0-32; i++){
t1[i] = t[i]%q0;
if (i < 32){
h1[i] = t[i+N0-32];
}
}
modCoeffs(h1,q1);
//cout << "t:" <<t <<"\n";
//cout << "t1:"<<t1 <<"\n";
//cout << "h1:"<<h1<<"\n";
//cout << "m2o:"<<m2_o<<"\n";
HashF(f1,h1);
//cout << "f1:"<<f1 <<"\n";
m1.SetLength(t1.length());
f1.SetLength(t1.length());
for (int i=0; i < t1.length(); i++){
m1[i] = (t1[i] - f1[i]);
}
modCoeffs(m1,q1);
m1.append(m2);
//cout << "m: " << m1 << "\n";
HashH(t2,m1);
//cout << "t2"<<t2 <<"\n";
//cout << "h1"<<h1 <<"\n";
for (int i=0; i < h1.length(); i++){
if (h1[i] != t2[i]){
return false;
}
}
// m1 = conv<vec_ZZ>(t);
// modCoeffs(s[0],q1);
double norm1 = 0;
double norm2 = 0;
double norm3 = 0;
for(int i=0; i < deg(s[0]);i++){
norm1 += conv<double>(s[0][i]*s[0][i]);
norm2 += conv<double>(s[1][i]*s[1][i]);
}
norm1 = sqrt(norm1);
norm2 = sqrt(norm2);
norm3 = sqrt(norm1*norm1+norm2*norm2);
if (debug){
cout << norm3 << " |" << conv<ZZ>(DS_SIGMA*sqrt(q0/2*N0)*sqrt(2*N0));
}
return (norm3 < conv<ZZ>(DS_SIGMA*sqrt(q0/2*N0)*sqrt(2*N0)));//conv<ZZ>(1.36*q0/2*sqrt(2*N0)));
}
void run_DS_exampleMR(){
clock_t t1, t2;
float t_keygen, t_sig, t_ver;
/// RUNNING THE KEYGEN ALGORITHM
ZZX Ks[2];
ZZ_pX Kv;
MSK_Data * MSKD = new MSK_Data;
t1 = clock();
SigKeyGen(Ks,Kv,MSKD);
t2 = clock();
t_keygen = ((float)t2 - (float)t1)/1000000.0F;
//ZZX Kv2 = conv<ZZX>(Kv);
/// RUNNING THE SIGN ALGORITHM
vec_ZZ r,m2;
vec_ZZ msg = RandomVector();
ZZX s[2];
t1 = clock();
Sign2(s,m2,msg,MSKD);
t2 = clock();
t_sig = ((float)t2 - (float)t1)/1000000.0F;
//PRINTIN DEBUG INFO
if (debug){
cout << "\n---Sign---\n";
cout << "\n (rand) message: ";
for(int i = 0; i < N0;i++){
cout << msg[i] << ",";
}
cout <<"\n";
cout << " s1: ";
for(int i = 0; i < N0;i++){
cout << s[0][i] << ",";
}
cout <<"\n";
cout << " (debug) s2: ";
for(int i = 0; i < N0;i++){
cout << s[1][i] << ",";
}
cout <<"\n";
}
/// RUNNING THE VERIFY ALGORITHM
//Notice
//1. Turn Kv into ZZX(Kv)
//2. We will pretend that s[0] is empty since we didn't get that info
vec_ZZ recovery;
t1 = clock();
bool valid = Verify2(conv<ZZX>(Kv),s, m2,recovery);
t2 = clock();
t_ver = ((float)t2 - (float)t1)/1000000.0F;
cout << "VALID:"<<valid <<"\n";
//cout << recovery;
//Not Certain of this part.
// double norm1 = 0;
// double norm2 = 0;
// double norm3 = 0;
// for(int i=0; i < deg(s[0]);i++){
// norm1 += conv<double>(s[0][i]*s[0][i]);
// norm2 += conv<double>(s[1][i]*s[1][i]);
// }
// norm1 = sqrt(norm1);
// norm2 = sqrt(norm2);
// norm3 = sqrt(norm1*norm1+norm2*norm2);
// cout << norm3 << " | " << conv<ZZ>(1.36*q0/2*sqrt(2*N0));
//PRINTIN DEBUG INFO
if (debug){
cout << "\n--Verify---\n";
cout << " derived s1: ";
for (int i=0; i < N0; i++){
cout << s[0][i] << ",";
}
cout <<"\n";
cout << " Valid: " << valid <<"\n" ;
}
if (dtime){
cout << "\nTiming\n";
cout << "DSKeyGen : " << t_keygen << "\n";
cout << "DSSign : " << t_sig << "\n";
cout << "DSVerif : " << t_ver << "\n";
}
return;
}
void run_DS_example(){
clock_t t1, t2;
float t_keygen, t_sig, t_ver;
/// RUNNING THE KEYGEN ALGORITHM
ZZX Ks[2];
ZZ_pX Kv;
MSK_Data * MSKD = new MSK_Data;
t1 = clock();
SigKeyGen(Ks,Kv,MSKD);
t2 = clock();
t_keygen = ((float)t2 - (float)t1)/1000000.0F;
//ZZX Kv2 = conv<ZZX>(Kv);
//PRINTIN DEBUG INFO
if (debug){
cout << "\n---Keygen---\n";
cout << " Private:\n";
cout << " denom: ";
for( int i = 0; i< N0;i++){
cout << Ks[0][i] << ",";
}
cout <<"\n";
cout <<" nom: ";
for (int i = 0; i< N0;i++){
cout << Ks[1][i] << ",";
}
cout <<"\n";
cout << "\n Public:\n";
cout << " h: ";
for(int i = 0; i < N0;i++){
cout << Kv[i] << ","; //"( "<< Kv2[i] <<" ) " << ",";
}
cout <<"\n";
}
/// RUNNING THE SIGN ALGORITHM
vec_ZZ r;
vec_ZZ msg = RandomVector();
ZZX s[2];
t1 = clock();
Sign(s,msg,r,MSKD);
t2 = clock();
t_sig = ((float)t2 - (float)t1)/1000000.0F;
//PRINTIN DEBUG INFO
if (debug){
cout << "\n---Sign---\n";
cout << "\n (rand) message: ";
for(int i = 0; i < N0;i++){
cout << msg[i] << ",";
}
cout <<"\n";
cout << " s1: ";
for(int i = 0; i < N0;i++){
cout << s[0][i] << ",";
}
cout <<"\n";
cout << " (debug) s2: ";
for(int i = 0; i < N0;i++){
cout << s[1][i] << ",";
}
cout <<"\n";
}
/// RUNNING THE VERIFY ALGORITHM
//Notice
//1. Turn Kv into ZZX(Kv)
//2. We will pretend that s[0] is empty since we didn't get that info
t1 = clock();
bool valid = Verify(conv<ZZX>(Kv),s, msg,r);
t2 = clock();
t_ver = ((float)t2 - (float)t1)/1000000.0F;
//Not Certain of this part.
// double norm1 = 0;
// double norm2 = 0;
// double norm3 = 0;
// for(int i=0; i < deg(s[0]);i++){
// norm1 += conv<double>(s[0][i]*s[0][i]);
// norm2 += conv<double>(s[1][i]*s[1][i]);
// }
// norm1 = sqrt(norm1);
// norm2 = sqrt(norm2);
// norm3 = sqrt(norm1*norm1+norm2*norm2);
// cout << norm3 << " | " << conv<ZZ>(1.36*q0/2*sqrt(2*N0));
//PRINTIN DEBUG INFO
if (debug){
cout << "\n--Verify---\n";
cout << " derived s1: ";
for (int i=0; i < N0; i++){
cout << s[0][i] << ",";
}
cout <<"\n";
cout << " Valid: " << valid <<"\n" ;
}
if (dtime){
cout << "\nTiming\n";
cout << "DSKeyGen : " << t_keygen << "\n";
cout << "DSSign : " << t_sig << "\n";
cout << "DSVerif : " << t_ver << "\n";
}
return;
}
///Repurposed IBE Code - Peikart///
//==============================================================================
//Generates from parameters N and q :
// - a public key : polynomial h
// - a private key : polynomials f,g,F,G
//==============================================================================
void Keygen(ZZ_pX& PublicKey, ZZX* PrivateKey)
{
ZZ SqNorm;
ZZX f,g,F,G;
SqNorm = conv<ZZ>(1.36*q0/2);
GenerateBasis(f, g, F, G, SqNorm);
PrivateKey[0] = f;
PrivateKey[1] = g;
PrivateKey[2] = F;
PrivateKey[3] = G;
for(unsigned int i=0; i<4; i++)
{
PrivateKey[i].SetLength(N0);
}
PublicKey = Quotient(f, g);
}
//==============================================================================
//Computes the private basis B from private key PrivateKey and parameter N
//==============================================================================
void CompletePrivateKey(mat_ZZ& B, const ZZX * const PrivateKey)
{
ZZX f,g,F,G;
f = PrivateKey[0];
g = PrivateKey[1];
F = PrivateKey[2];
G = PrivateKey[3];
f = -f;
F = -F;
B = BasisFromPolynomials(g, f, G, F);
}
void GPV(RR_t * v, const RR_t * const c, const RR_t s, const MSK_Data * const MSKD)
{
int i;
unsigned j;
RR_t ci[2*N0], zi, cip, sip, aux;
for(j=0; j<2*N0;j++)
{
ci[j] = c[j];
}
for(j=0; j<2*N0; j++)
{
}
for(i=2*N0-1; i>=0; i--)
{
aux = (MSKD->GS_Norms)[i];
cip = DotProduct(ci, MSKD->Bstar[i])/(aux*aux);
sip = s/aux;
zi = Sample4(cip, sip*PiPrime);
for(j=0; j<2*N0; j++)
{
ci[j] -= zi*(MSKD->B)[i][j];
}
}
for(j=0; j<2*N0; j++)
{
v[j] = c[j] - ci[j];
}
}
//==============================================================================
//==============================================================================
// MAIN PROGRAMS
//==============================================================================
//==============================================================================
void CompleteMSK(MSK_Data * MSKD, ZZX * MSK)
{
unsigned int i, j;
mat_ZZ B0;
for(i=0; i<4; i++)
{
MSKD->PrK[i] = MSK[i];
ZZXToFFT(MSKD->PrK_fft[i], MSK[i]);
}
CompletePrivateKey(B0, MSK);
for(i=0; i<2*N0; i++)
{
for(j=0; j<2*N0; j++)
{
MSKD->B[i][j] = ( (RR_t) conv<double>(B0[i][j]) );
}
}
for(i=0; i<1; i++)
{
FastMGS(MSKD->Bstar, MSKD->B);
}
for(i=0; i<2*N0; i++)
{
MSKD->GS_Norms[i] = sqrt( DotProduct(MSKD->Bstar[i], MSKD->Bstar[i]) );
}
MSKD->sigma = 2*MSKD->GS_Norms[0];
}
void IBE_Extract(ZZX SK_id[2], vec_ZZ id, const MSK_Data * const MSKD)
{
unsigned int i;
RR_t c[2*N0], sk[2*N0], sigma;
ZZX f,g,aux;
f = MSKD -> PrK[0];
g = MSKD -> PrK[1];
sigma = MSKD->sigma;
SK_id[0].SetLength(N0);
SK_id[1].SetLength(N0);
for(i=0;i<N0;i++)
{
c[i] = ((RR_t) conv<double>(id[i])) ;
c[i+N0] = 0;
}
GPV(sk, c, sigma, MSKD);
for(i=0; i<N0; i++)
{
sk[i] = c[i] - sk[i];
sk[i+N0] = - sk[i+N0];
}
for(i=0; i<N0; i++)
{
SK_id[0][i] = sk[i];
SK_id[1][i] = sk[i+N0];
}
}