Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.10 KB | None | 0 0
  1. /*
  2.  * Alice wishes to sign a message. She generates her public and private keys as follows. Her private key is a random element x of Zr, and her corresponding public key is g^x.
  3.  * To sign a message, Alice hashes the message to some element h of G1, and then outputs the signature h^x.
  4.  * To verify a signature σ, Bob checks that e(h,g^x) = e(σ, g).
  5.  *
  6.  *
  7.  * */
  8. #include <pbc/pbc.h>
  9. #include <pbc/pbc_test.h>
  10.  
  11. int main(int argc, char **argv)
  12. {
  13.     // declare g and h random elements from G1 and G2 respectively
  14.     element_t g, h;
  15.  
  16.     // public and private key of alice
  17.     element_t public_key, private_key;
  18.     element_t sig;
  19.     element_t temp1, temp2;
  20.  
  21.     pairing_t pairing;
  22.     //char param[1024];
  23.     //size_t count = fread(param, 1, 1024, stdin);
  24.     //if (!count) pbc_die("input error");
  25.     //pairing_init_set_buf(pairing, param, count);
  26.     pbc_demo_pairing_init(pairing, argc, argv);
  27.  
  28.     // initialise all elements and keys
  29.     element_init_G2(g, pairing);
  30.     element_init_G2(public_key, pairing);
  31.     element_init_G1(h, pairing);
  32.     element_init_G1(sig, pairing);
  33.     element_init_GT(temp1, pairing);
  34.     element_init_GT(temp2, pairing);
  35.     element_init_Zr(private_key, pairing);
  36.  
  37.     // pick system parameter g
  38.     element_random(g);
  39.     element_printf(" g = %B\n", g);
  40.  
  41.     // Alice generates private key
  42.         element_random(private_key);
  43.         element_printf("Alices random private key = %B\n", private_key);
  44.  
  45.     // Alice generates public key, which is g^(private_key)
  46.     element_pow_zn(public_key, g, private_key);
  47.         element_printf("Alices random public key = %B\n", public_key);
  48.  
  49.     //Now Alice will sign a message
  50.     printf("=============Now Alice will hash the message==============\n");
  51.     element_from_hash(h, "Alice wants to kill you", 20);
  52.    
  53.         element_printf("Alices hash to element h = %B\n", h);
  54.    
  55.     //Sign the message
  56.     element_pow_zn(sig, h, private_key);
  57.  
  58.     // Bob verifies the signature
  59.     pairing_apply(temp1, sig, g, pairing);
  60.     pairing_apply(temp2, h, public_key, pairing);
  61.     if (!element_cmp(temp1, temp2)) {
  62.             printf("signature verifies\n");
  63.     } else {
  64.             printf("signature does not verify\n");
  65.     }
  66.    
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement