Skip to content

Commit ab64600

Browse files
committed
IBBE clean-up
1 parent 24ce617 commit ab64600

File tree

3 files changed

+16
-249
lines changed

3 files changed

+16
-249
lines changed

pbc_ibbe/ibbe.c

+13-245
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,6 @@ int setup_sgx_safe(PublicKey *puk, ShortPublicKey *spuk, MasterSecretKey *msk, i
6666
}
6767
mpz_clear(n);
6868

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-
7769
puk->h = hRec;
7870
element_init_G1(puk->w, pairing);
7971
element_init_GT(puk->v, pairing);
@@ -397,11 +389,9 @@ struct exp_part_struct {
397389
element_t* polyA;
398390
};
399391

400-
void *exponentiate_by_partition(void *pargs)
392+
void *decrypt_exponentiate_by_partition(void *pargs)
401393
{
402394
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);
405395

406396
/* old style exponentiation */
407397
element_t thread_temp2;
@@ -411,36 +401,31 @@ void *exponentiate_by_partition(void *pargs)
411401
element_set1(*ptemp);
412402

413403
element_init_G1(thread_temp2, pairing);
414-
//element_set1(thread_temp1);
415404

416405
int i;
417-
int max = 0;
418406
for (i = args->start; i <= args->end - 3; i+=3)
419407
{
420408
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]);
424412
element_mul(*ptemp, *ptemp, thread_temp2);
425-
max = i > max ? i : max;
426413
}
427414
// exponentiate and multiply any leftovers
428415
if (i <= args->end)
429416
{
430417
for (int j = i; j <= args->end; j++)
431418
{
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]);
433420
element_mul(*ptemp, *ptemp, thread_temp2);
434-
max = i > max ? i : max;
435421
}
436422
}
437423

438-
printf("Max %d\n", max);
439424
pthread_exit(ptemp);
440425
return (void*) ptemp;
441426
}
442427

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)
444429
{
445430
int i, j;
446431
int mark = 1;
@@ -511,26 +496,25 @@ int decrypt_user(int sw, BroadcastKey* bKey, Ciphertext cipher, PublicKey key, U
511496
element_set1(htemp1);
512497

513498
// multithreading
514-
if (sw == 1)
515499
{
516500
int idNum = idCount - 2;
517-
int exp_per_partition = idNum / THREADS_COUNT;
501+
int exp_per_partition = (idNum / THREADS_COUNT) + 1;
518502

519503
pthread_t tid[THREADS_COUNT];
520504
for(int thread_idx = 0; thread_idx < THREADS_COUNT; thread_idx++)
521505
{
522506
// 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));
526508
args->key = key;
527509
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)
529513
{
530-
args->end += 2;
514+
args->end = idNum;
531515
}
532516

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);
534518
}
535519

536520
element_t result;
@@ -543,17 +527,6 @@ int decrypt_user(int sw, BroadcastKey* bKey, Ciphertext cipher, PublicKey key, U
543527
pthread_join(tid[thread_idx], &partition_result);
544528
element_mul(htemp1, htemp1, *(element_t*)partition_result);
545529
}
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-
}
557530
}
558531
}
559532
/*
@@ -592,208 +565,3 @@ int decrypt_user(int sw, BroadcastKey* bKey, Ciphertext cipher, PublicKey key, U
592565

593566
return 0;
594567
}
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-
}

pbc_ibbe/ibbe.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <time.h>
66
#include <string.h>
77

8-
#define MAX_RECEIVER 100
8+
#define MAX_RECEIVER 1000
99
#define MAX_STRING_LENGTH 20
1010
#define THREADS_COUNT 8
1111

@@ -46,7 +46,7 @@ int decrypt_sgx_safe(BroadcastKey* bKey, Ciphertext cipher,
4646
char* id, char idSet[][MAX_STRING_LENGTH], int idCount);
4747

4848
/* NON-SGX METHODS - FOR USER USE */
49-
int decrypt_user(int sw, BroadcastKey* bKey,
49+
int decrypt_user(BroadcastKey* bKey,
5050
Ciphertext cipher, PublicKey key, UserPrivateKey ikey,
5151
char* id, char idSet[][MAX_STRING_LENGTH], int idCount);
5252

pbc_ibbe/test.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ int main(int argc, char** argv)
2929
PublicKey pubkey;
3030
MasterSecretKey prvkey;
3131
ShortPublicKey shortPubKey;
32-
//UserPrivateKey idkey[MAX_RECEIVER+1];
3332

3433
setup_sgx_safe(&pubkey, &shortPubKey, &prvkey, argc, argv);
3534

@@ -58,7 +57,7 @@ int main(int argc, char** argv)
5857
print_key(oldStyleBroadcastKey);
5958

6059
BroadcastKey userBroadcastKey;
61-
decrypt_user(1, &userBroadcastKey, cipher, pubkey,
60+
decrypt_user(&userBroadcastKey, cipher, pubkey,
6261
usr13PriKey, "[email protected]", S, MAX_RECEIVER);
6362
printf("USR DECR. KEY : ");
6463
print_key(userBroadcastKey);

0 commit comments

Comments
 (0)