114 lines
3.1 KiB
C
114 lines
3.1 KiB
C
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
#include <time.h>
|
||
|
#include "blst/blst.h"
|
||
|
|
||
|
const byte dst[] = "MY-DST";
|
||
|
double time_taken;
|
||
|
clock_t t;
|
||
|
|
||
|
void printbytes(byte *toprint, int length){
|
||
|
for(int i=0;i<length;i++){
|
||
|
printf("%.2x ", toprint[i]);
|
||
|
}
|
||
|
printf("\n");
|
||
|
}
|
||
|
|
||
|
void signer(byte *compressed_signature, byte *compressed_public_key, byte *msg){
|
||
|
blst_scalar sk;
|
||
|
blst_p2 pk;
|
||
|
blst_p1 hash, signature;
|
||
|
byte debug_print_buf[256];
|
||
|
byte myikm[32] = {'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'};
|
||
|
|
||
|
// On signer's side:
|
||
|
printf("IKM: ");
|
||
|
printbytes(myikm, 32);
|
||
|
|
||
|
blst_keygen(&sk, myikm, 32, 0, 0);
|
||
|
|
||
|
blst_bendian_from_scalar(debug_print_buf, &sk);
|
||
|
printf("Secret Key: ");
|
||
|
printbytes(debug_print_buf, 32);
|
||
|
|
||
|
blst_sk_to_pk_in_g2(&pk, &sk);
|
||
|
|
||
|
blst_p2_compress(compressed_public_key, &pk);
|
||
|
printf("Compressed Public Key: ");
|
||
|
printbytes(compressed_public_key, 96);
|
||
|
|
||
|
t = clock();
|
||
|
blst_hash_to_g1(&hash, msg, strlen((char *) msg), dst, strlen((char *) dst), 0, 0);
|
||
|
t = clock() - t;
|
||
|
|
||
|
time_taken = ((double)t)/CLOCKS_PER_SEC*1000.0;
|
||
|
printf("blst_hash_to_g1 took %f ms\n", time_taken);
|
||
|
|
||
|
blst_p1_serialize(debug_print_buf, &hash);
|
||
|
printf("Message Hash: ");
|
||
|
printbytes(debug_print_buf, 96);
|
||
|
|
||
|
t = clock();
|
||
|
blst_sign_pk_in_g2(&signature, &hash, &sk);
|
||
|
t = clock() - t;
|
||
|
|
||
|
time_taken = ((double)t)/CLOCKS_PER_SEC*1000.0;
|
||
|
printf("blst_sign_pk_in_g2 took %f ms\n", time_taken);
|
||
|
|
||
|
blst_p1_serialize(debug_print_buf, &signature);
|
||
|
printf("Signature: ");
|
||
|
printbytes(debug_print_buf, 96);
|
||
|
|
||
|
blst_p1_compress(compressed_signature, &signature);
|
||
|
printf("Compressed Signature: ");
|
||
|
printbytes(compressed_signature, 48);
|
||
|
}
|
||
|
|
||
|
void verifier(byte *compressed_signature, byte *compressed_public_key, byte *msg){
|
||
|
blst_p1_affine sig;
|
||
|
blst_p2_affine pk;
|
||
|
|
||
|
blst_p1_uncompress(&sig, compressed_signature);
|
||
|
blst_p2_uncompress(&pk, compressed_public_key);
|
||
|
|
||
|
BLST_ERROR returned;
|
||
|
|
||
|
// TODO: check if in g2 group
|
||
|
|
||
|
t = clock();
|
||
|
returned = blst_core_verify_pk_in_g2(&pk, &sig, 1, msg, strlen((char *) msg), dst, strlen((char *) dst), 0, 0);
|
||
|
t = clock() - t;
|
||
|
|
||
|
time_taken = ((double)t)/CLOCKS_PER_SEC*1000.0;
|
||
|
printf("blst_core_verify_pk_in_g2 took %f ms\n", time_taken);
|
||
|
|
||
|
if(returned == BLST_SUCCESS){
|
||
|
printf("Verified!\n");
|
||
|
}else{
|
||
|
printf("Not verified!\n");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main(){
|
||
|
byte compressed_signature[48];
|
||
|
byte compressed_public_key[96];
|
||
|
byte msg[] = "assertion";
|
||
|
|
||
|
t = clock();
|
||
|
t = clock() - t;
|
||
|
|
||
|
time_taken = ((double)t)/CLOCKS_PER_SEC*1000.0;
|
||
|
printf("Doing nothing took %f ms\n", time_taken);
|
||
|
|
||
|
printf("msg is now %s\n", msg);
|
||
|
|
||
|
// Sign the message and get the results back
|
||
|
signer(compressed_signature, compressed_public_key, msg);
|
||
|
|
||
|
//msg[8] = 'A';
|
||
|
|
||
|
printf("msg is now %s\n", msg);
|
||
|
|
||
|
// Now on verifier's side (after compressed_signature, serialized_public_key, and msg are passed over the network)
|
||
|
verifier(compressed_signature, compressed_public_key, msg);
|
||
|
}
|