@@ -66,14 +66,6 @@ int setup_sgx_safe(PublicKey *puk, ShortPublicKey *spuk, MasterSecretKey *msk, i
66
66
}
67
67
mpz_clear (n );
68
68
69
- // Most likely these commented lines are useless, will keep them until
70
- // all tests w/ and w/out SGX are finalized, then remove!
71
- /*h ^ r ^ (-1)*/
72
- //element_init_G1(hRec[i], pairing);
73
- //element_invert(temp1, r);
74
- //element_pow_zn(hRec[i], h, temp1);
75
- // ---- end commented lines
76
-
77
69
puk -> h = hRec ;
78
70
element_init_G1 (puk -> w , pairing );
79
71
element_init_GT (puk -> v , pairing );
@@ -397,11 +389,9 @@ struct exp_part_struct {
397
389
element_t * polyA ;
398
390
};
399
391
400
- void * exponentiate_by_partition (void * pargs )
392
+ void * decrypt_exponentiate_by_partition (void * pargs )
401
393
{
402
394
struct exp_part_struct * args = (struct exp_part_struct * )pargs ;
403
- // printf("PART START : %d\n", args->start);
404
- // printf("PART END : %d\n", args->end);
405
395
406
396
/* old style exponentiation */
407
397
element_t thread_temp2 ;
@@ -411,36 +401,31 @@ void *exponentiate_by_partition(void *pargs)
411
401
element_set1 (* ptemp );
412
402
413
403
element_init_G1 (thread_temp2 , pairing );
414
- //element_set1(thread_temp1);
415
404
416
405
int i ;
417
- int max = 0 ;
418
406
for (i = args -> start ; i <= args -> end - 3 ; i += 3 )
419
407
{
420
408
element_pow3_zn (thread_temp2 ,
421
- args -> key .h [i ], args -> polyA [i ],
422
- args -> key .h [i + 1 ], args -> polyA [i + 1 ],
423
- args -> key .h [i + 2 ], args -> polyA [i + 2 ]);
409
+ args -> key .h [i ], args -> polyA [i + 1 ],
410
+ args -> key .h [i + 1 ], args -> polyA [i + 2 ],
411
+ args -> key .h [i + 2 ], args -> polyA [i + 3 ]);
424
412
element_mul (* ptemp , * ptemp , thread_temp2 );
425
- max = i > max ? i : max ;
426
413
}
427
414
// exponentiate and multiply any leftovers
428
415
if (i <= args -> end )
429
416
{
430
417
for (int j = i ; j <= args -> end ; j ++ )
431
418
{
432
- element_pow_zn (thread_temp2 , args -> key .h [j ], args -> polyA [j ]);
419
+ element_pow_zn (thread_temp2 , args -> key .h [j ], args -> polyA [j + 1 ]);
433
420
element_mul (* ptemp , * ptemp , thread_temp2 );
434
- max = i > max ? i : max ;
435
421
}
436
422
}
437
423
438
- printf ("Max %d\n" , max );
439
424
pthread_exit (ptemp );
440
425
return (void * ) ptemp ;
441
426
}
442
427
443
- int decrypt_user (int sw , BroadcastKey * bKey , Ciphertext cipher , PublicKey key , UserPrivateKey ikey , char * id , char idSet [][MAX_STRING_LENGTH ], int idCount )
428
+ int decrypt_user (BroadcastKey * bKey , Ciphertext cipher , PublicKey key , UserPrivateKey ikey , char * id , char idSet [][MAX_STRING_LENGTH ], int idCount )
444
429
{
445
430
int i , j ;
446
431
int mark = 1 ;
@@ -511,26 +496,25 @@ int decrypt_user(int sw, BroadcastKey* bKey, Ciphertext cipher, PublicKey key, U
511
496
element_set1 (htemp1 );
512
497
513
498
// multithreading
514
- if (sw == 1 )
515
499
{
516
500
int idNum = idCount - 2 ;
517
- int exp_per_partition = idNum / THREADS_COUNT ;
501
+ int exp_per_partition = ( idNum / THREADS_COUNT ) + 1 ;
518
502
519
503
pthread_t tid [THREADS_COUNT ];
520
504
for (int thread_idx = 0 ; thread_idx < THREADS_COUNT ; thread_idx ++ )
521
505
{
522
506
// compute the partition of exponentiations
523
- struct exp_part_struct * args = malloc (sizeof (struct exp_part_struct ));
524
- args -> start = thread_idx * exp_per_partition ;
525
- args -> end = args -> start + exp_per_partition - 1 ;
507
+ struct exp_part_struct * args = malloc (sizeof (struct exp_part_struct ));
526
508
args -> key = key ;
527
509
args -> polyA = polyA ;
528
- if (thread_idx == THREADS_COUNT - 1 )
510
+ args -> start = thread_idx * exp_per_partition ;
511
+ args -> end = args -> start + exp_per_partition - 1 ;
512
+ if (args -> end > idNum )
529
513
{
530
- args -> end += 2 ;
514
+ args -> end = idNum ;
531
515
}
532
516
533
- pthread_create (& tid [thread_idx ], NULL , exponentiate_by_partition , (void * )args );
517
+ pthread_create (& tid [thread_idx ], NULL , decrypt_exponentiate_by_partition , (void * )args );
534
518
}
535
519
536
520
element_t result ;
@@ -543,17 +527,6 @@ int decrypt_user(int sw, BroadcastKey* bKey, Ciphertext cipher, PublicKey key, U
543
527
pthread_join (tid [thread_idx ], & partition_result );
544
528
element_mul (htemp1 , htemp1 , * (element_t * )partition_result );
545
529
}
546
-
547
- //element_printf("THR TEMP1 : %B\n", result);
548
- }
549
- else
550
- {
551
- for (i = 0 ; i < idCount - 1 ; i ++ )
552
- {
553
- element_pow_zn (htemp2 , key .h [i ], polyA [i + 1 ]);
554
- element_mul (htemp1 , htemp1 , htemp2 );
555
- printf ("USING %d\n" , i );
556
- }
557
530
}
558
531
}
559
532
/*
@@ -592,208 +565,3 @@ int decrypt_user(int sw, BroadcastKey* bKey, Ciphertext cipher, PublicKey key, U
592
565
593
566
return 0 ;
594
567
}
595
-
596
-
597
- int Encrypt (Ciphertext * cipher , PublicKey key , char idSet [][MAX_STRING_LENGTH ], int idNum )
598
- {
599
- element_t k ;
600
- element_t m ;
601
- element_t c1 , c2 , c3 ;
602
- element_t temp1 , temp2 ;
603
-
604
- element_init_GT (m , pairing );
605
- element_init_G1 (c1 , pairing );
606
- element_init_G1 (c2 , pairing );
607
- element_init_GT (c3 , pairing );
608
- element_init_G1 (temp1 , pairing );
609
- element_init_G1 (temp2 , pairing );
610
-
611
- //element_random(k);
612
- //element_set_mpz(m, message);
613
-
614
- /*compute c1 = w ^ -k*/
615
- {
616
- element_pow_zn (c1 , key .w , k );
617
- element_invert (c1 , c1 );
618
- }
619
-
620
- /*compute c2 = h ^ (k * (r+H(ID)...)*/
621
- {
622
- /*polynominal multiplication */
623
- /***************************************/
624
- int i , j ;
625
- /*
626
- element_t hid[idNum + 1];
627
- element_t polyA[idNum + 1], polyB[idNum + 1];
628
- */
629
- element_t * hid ;
630
- element_t * polyA , * polyB ;
631
- hid = (element_t * )malloc (sizeof (element_t ) * idNum );
632
- polyA = (element_t * )malloc (sizeof (element_t ) * (idNum + 1 ));
633
- polyB = (element_t * )malloc (sizeof (element_t ) * (idNum + 1 ));
634
-
635
- clock_t time1 , time2 ;
636
-
637
- /*initialization*/
638
- time1 = clock ();
639
- for (i = 0 ; i < idNum + 1 ; i ++ )
640
- {
641
- element_init_Zr (polyA [i ], pairing );
642
- element_set0 (polyA [i ]);
643
- element_init_Zr (polyB [i ], pairing );
644
- element_set0 (polyB [i ]);
645
- element_init_Zr (hid [i ], pairing );
646
- if (i < idNum )
647
- {
648
- //printf("Receiver %d: %s\n", i+1, idSet+i*5);
649
-
650
-
651
- //unsigned char ibuf[] = "compute sha1";
652
- unsigned char obuf [20 ];
653
-
654
- //SHA1(ibuf, strlen(ibuf), obuf);
655
- //SHA1(idSet[i], strlen(idSet[i]), obuf);
656
-
657
- element_from_hash (hid [i ], idSet [i ], strlen (idSet [i ]));
658
- //element_from_hash(hid[i], obuf, strlen(obuf));
659
- //element_random(hid[i]);
660
- }
661
- }
662
- time2 = clock ();
663
- printf ("@Computing hashes : %lfms\n\n " , 1000.0 * (time2 - time1 )/CLOCKS_PER_SEC );
664
-
665
- /*calculation*/
666
- time1 = clock ();
667
- element_set (polyA [0 ], hid [0 ]);
668
- element_set1 (polyA [1 ]);
669
- for (i = 1 ; i < idNum ; i ++ )
670
- {
671
- /*i-th polynomial*/
672
- /*polyA * (r + H(ID))*/
673
- element_set1 (polyA [i + 1 ]);
674
- for (j = i ; j >= 1 ; j -- )
675
- {
676
- element_mul (polyB [j ], polyA [j ], hid [i ]);
677
- element_add (polyA [j ], polyA [j - 1 ], polyB [j ]);
678
- //element_printf("%B\n", polyA[j]);
679
- }
680
- element_mul (polyA [0 ], polyA [0 ], hid [i ]);
681
- }
682
- time2 = clock ();
683
- printf ("@Polynomial expansion : %lfms\n\n " , 1000.0 * (time2 - time1 )/CLOCKS_PER_SEC );
684
-
685
- printf ("START COUNTING ... \n" );
686
- clock_t time_exp = clock ();
687
- /* old style exponentiation */
688
- /*
689
- element_t old_temp1, old_temp2;
690
- element_init_G1(old_temp1, pairing);
691
- element_init_G1(old_temp2, pairing);
692
- element_set1(old_temp1);
693
- for (i = 0; i < idNum+1; i++)
694
- {
695
- element_pow_zn(old_temp2, key.h[i], polyA[i]);
696
- element_mul(old_temp1, old_temp1, old_temp2);
697
- }
698
- element_printf("OLD TEMP1 : %B\n", old_temp1);
699
- */
700
- /* finished old style exponentation */
701
-
702
- /* 3-Batch Exponentation */
703
- /* element_set1(temp1);
704
- for (i = 0; i < idNum-1; i+=3)
705
- {
706
- element_pow3_zn(temp2,
707
- key.h[i], polyA[i],
708
- key.h[i+1], polyA[i+1],
709
- key.h[i+2], polyA[i+2]);
710
- element_mul(temp1, temp1, temp2);
711
- }
712
- // exponentiate and multiply any leftovers
713
- if (i < idNum + 1)
714
- {
715
- for (j = i; j < idNum + 1; j++)
716
- {
717
- element_pow_zn(temp2, key.h[j], polyA[j]);
718
- element_mul(temp1, temp1, temp2);
719
- }
720
- }
721
- element_printf("NEW TEMP1 : %B\n", temp1);
722
- */
723
-
724
- /* Multithreaded exponentation */
725
- /*
726
- if (idNum > 299)
727
- {
728
- // it's worth doing parallel once groups get some users
729
- int THREADS_COUNT = 8;
730
- int exp_per_partition = idNum / THREADS_COUNT;
731
-
732
- pthread_t tid[THREADS_COUNT];
733
- for(int thread_idx = 0; thread_idx < THREADS_COUNT; thread_idx++)
734
- {
735
- // compute the partition of exponentiations
736
- struct exp_part_struct* args= malloc (sizeof (struct exp_part_struct));
737
- args->start = thread_idx * exp_per_partition;
738
- args->end = args->start + exp_per_partition - 1;
739
- args->key = key;
740
- args->polyA = polyA;
741
- if (thread_idx == THREADS_COUNT - 1)
742
- {
743
- args->end++;
744
- }
745
-
746
- pthread_create(&tid[thread_idx], NULL, exponentiate_by_partition, (void *)args);
747
- }
748
-
749
- element_t result;
750
- element_init_G1(result, pairing);
751
- element_set1(result);
752
- // wait that threads are complete
753
- for(int thread_idx = 0; thread_idx < THREADS_COUNT; thread_idx++)
754
- {
755
- void* partition_result;
756
- pthread_join(tid[thread_idx], &partition_result);
757
- if (partition_result == NULL)
758
- {
759
- printf("NULL PASSED !!!!\n");
760
- }
761
- element_mul(temp1, temp1, *(element_t*)partition_result);
762
- }
763
-
764
- //element_printf("THR TEMP1 : %B\n", result);
765
- }
766
-
767
- clock_t time_exp_end = clock();
768
- printf("@linear exponentiations : %lu\n ", time_exp_end - time_exp);
769
- */
770
- element_pow_zn (c2 , temp1 , k );
771
- // printf("c2 second half computed !!!!\n");
772
-
773
- /*
774
- free(hid);
775
- free(polyA);
776
- free(polyB);
777
- */
778
- }
779
-
780
- /*calculate c3 = v^k * m*/
781
- {
782
- element_pow_zn (c3 , key .v , k );
783
- element_mul (c3 , c3 , m );
784
- }
785
-
786
- element_init_G1 (cipher -> c1 , pairing );
787
- element_init_G1 (cipher -> c2 , pairing );
788
- //element_init_GT(cipher->c3, pairing);
789
- element_set (cipher -> c1 , c1 );
790
- element_set (cipher -> c2 , c2 );
791
- //element_set(cipher->c3, c3);
792
-
793
- element_clear (k );
794
- element_clear (m );
795
- element_clear (temp1 );
796
- element_clear (temp2 );
797
-
798
- return 0 ;
799
- }
0 commit comments