正在熟悉PBC库,记录一下自己的练习
多方也依然适用
#include <pbc.h>
#include <pbc_test.h>
int main(int argc, char **argv) {
pairing_t pairing;
double time1, time2;
element_t P, a, b, c, Ka, Kb, Kc, pa, pb, pc, t4, t5, t6;
element_t zero;
element_t Q,qc,qb,qa,add1,add2,add3,sub1,sub2,sub3;
pbc_demo_pairing_init(pairing, argc, argv);
if (!pairing_is_symmetric(pairing)) pbc_die("pairing must be symmetric");
else printf("stage 1 succeeded.\n");
element_init_G1(P, pairing);
element_init_G1(Q, pairing);
element_init_G1(pa, pairing);
element_init_G1(pb, pairing);
element_init_G1(pc, pairing);
//
element_init_G1(qa, pairing);
element_init_G1(qb, pairing);
element_init_G1(qc, pairing);
element_init_G1(zero,pairing);
element_init_G1(add1, pairing);
element_init_G1(add2, pairing);
element_init_G1(add3, pairing);
element_init_G1(sub1, pairing);
element_init_G1(sub2, pairing);
element_init_G1(sub3, pairing);
//
element_init_Zr(a, pairing);
element_init_Zr(b, pairing);
element_init_Zr(c, pairing);
element_init_GT(t4, pairing);
element_init_GT(t5, pairing);
element_init_GT(t6, pairing);
element_init_GT(Ka, pairing);
element_init_GT(Kb, pairing);
element_init_GT(Kc, pairing);
time1 = pbc_get_time();
printf("Joux key agreement between A, B and C.\n");
element_random(P);
element_random(Q);
element_random(a);
element_random(b);
element_random(c);
//设zero为G1群的零元
element_set0(zero);
element_mul_zn(pa, P, a);
element_mul_zn(qa, Q, a);
printf("A sends B and C: (aP,aQ)\n");
element_printf("aP = %B\n,aQ=%B\n", pa,qa);
element_mul_zn(pb, P, b);
element_mul_zn(qb, Q, b);
printf("B sends A and C: (bP,bQ)\n");
element_printf("bP = %B\n,bQ=%B\n", pb,qb);
element_mul_zn(pc, P, c);
element_mul_zn(qc, Q, c);
printf("C sends A and B: (cP,cQ)\n");
element_printf("cP = %B\n,cQ=%B\n", pc,qc);
//秘钥交换部分
//加和减(add1=pc+qc-zero,sub1=pb-qb,其余同理)
element_add(add1,pc,qc);
element_sub(add1,add1,zero);
element_sub(sub1,pb,qb);
element_pairing(t4, add1, sub1);
element_pow_zn(Ka, t4, a);
element_printf("Ka = %B\n", Ka);
element_add(add2,pc,qc);
element_sub(add2,add2,zero);
element_sub(sub2,pa,qa);
element_pairing(t5, add2, sub2);
element_pow_zn(Kb, t5, b);
element_printf("Kb = %B\n", Kb);
element_add(add3,pa,qa);
element_sub(add3,add3,zero);
element_sub(sub3,pb,qb);
element_pairing(t6, add3, sub3);
element_pow_zn(Kc, t6, c);
element_printf("Kc = %B\n", Kc);
if(!(element_cmp(Ka,Kb)&&element_cmp(Kb,Kc)&&element_cmp(Ka,Kc)))
printf("Shared key K = Ka = Kb = Kc\n");
time2 = pbc_get_time();
printf("All time = %fs\n", time2 - time1);
element_clear(P);
element_clear(Q);
element_clear(zero);
element_clear(a);
element_clear(b);
element_clear(c);
element_clear(Ka);
element_clear(Kb);
element_clear(Kc);
element_clear(qa);
element_clear(qb);
element_clear(qc);
element_clear(pa);
element_clear(pb);
element_clear(pc);
element_clear(add1);
element_clear(add2);
element_clear(add3);
element_clear(sub1);
element_clear(sub2);
element_clear(sub3);
element_clear(t4);
element_clear(t5);
element_clear(t6);
pairing_clear(pairing);
return 0;
}```