a portable and fast pairing-based cryptography library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mcl/sample/pairing_c.c

54 lines
1.3 KiB

8 years ago
#include <stdio.h>
8 years ago
#include <string.h>
#define MCLBN_FP_UNIT_SIZE 4
#include <mcl/bn.h>
8 years ago
int g_err = 0;
#define ASSERT(x) { if (!(x)) { printf("err %s:%d\n", __FILE__, __LINE__); g_err++; } }
int main()
{
char buf[1024];
8 years ago
const char *aStr = "123";
const char *bStr = "456";
mclBn_init(MCL_BN254, MCLBN_FP_UNIT_SIZE);
8 years ago
mclBnFr a, b, ab;
mclBnG1 P, aP;
mclBnG2 Q, bQ;
mclBnGT e, e1, e2;
mclBnFr_setStr(&a, aStr, strlen(aStr), 10);
mclBnFr_setStr(&b, bStr, strlen(bStr), 10);
mclBnFr_mul(&ab, &a, &b);
mclBnFr_getStr(buf, sizeof(buf), &ab, 10);
8 years ago
printf("%s x %s = %s\n", aStr, bStr, buf);
8 years ago
ASSERT(!mclBnG1_hashAndMapTo(&P, "this", 4));
ASSERT(!mclBnG2_hashAndMapTo(&Q, "that", 4));
mclBnG1_getStr(buf, sizeof(buf), &P, 16);
8 years ago
printf("P = %s\n", buf);
8 years ago
mclBnG2_getStr(buf, sizeof(buf), &Q, 16);
8 years ago
printf("Q = %s\n", buf);
8 years ago
mclBnG1_mul(&aP, &P, &a);
mclBnG2_mul(&bQ, &Q, &b);
8 years ago
8 years ago
mclBn_pairing(&e, &P, &Q);
mclBnGT_getStr(buf, sizeof(buf), &e, 16);
8 years ago
printf("e = %s\n", buf);
8 years ago
mclBnGT_pow(&e1, &e, &a);
mclBn_pairing(&e2, &aP, &Q);
ASSERT(mclBnGT_isEqual(&e1, &e2));
8 years ago
8 years ago
mclBnGT_pow(&e1, &e, &b);
mclBn_pairing(&e2, &P, &bQ);
ASSERT(mclBnGT_isEqual(&e1, &e2));
ASSERT(mclBn_setErrFile("") == 0);
8 years ago
if (g_err) {
printf("err %d\n", g_err);
return 1;
} else {
printf("no err\n");
return 0;
}
}