aboutsummaryrefslogtreecommitdiffstats
path: root/sample/pairing_c.c
blob: 95a8c351dccee83b0fce929b8bfd552097f335a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#define BN256_DEFINE_STRUCT
#include <mcl/bn256.h>
#include <stdio.h>

int g_err = 0;
#define ASSERT(x) { if (!(x)) { printf("err %s:%d\n", __FILE__, __LINE__); g_err++; } }

int main()
{
    char buf[1024];
    const char *aStr = "1234567788234243234234";
    const char *bStr = "239482098243";
    BN256_init();
    BN256_Fr a, b, ab;
    BN256_G1 P, aP;
    BN256_G2 Q, bQ;
    BN256_GT e, e1, e2;
    BN256_Fr_setStr(&a, aStr);
    BN256_Fr_setStr(&b, bStr);
    BN256_Fr_mul(&ab, &a, &b);
    BN256_Fr_getStr(buf, sizeof(buf), &ab);
    printf("%s x %s = %s\n", aStr, bStr, buf);

    ASSERT(!BN256_G1_setStr(&P, "1 -1 1")); // "1 <x> <y>"
    ASSERT(!BN256_G2_hashAndMapTo(&Q, "1"));
    BN256_G1_getStr(buf, sizeof(buf), &P);
    printf("P = %s\n", buf);
    BN256_G2_getStr(buf, sizeof(buf), &Q);
    printf("Q = %s\n", buf);

    BN256_G1_mul(&aP, &P, &a);
    BN256_G2_mul(&bQ, &Q, &b);

    BN256_pairing(&e, &P, &Q);
    BN256_GT_getStr(buf, sizeof(buf), &e);
    printf("e = %s\n", buf);
    BN256_GT_pow(&e1, &e, &a);
    BN256_pairing(&e2, &aP, &Q);
    ASSERT(BN256_GT_isSame(&e1, &e2));

    BN256_GT_pow(&e1, &e, &b);
    BN256_pairing(&e2, &P, &bQ);
    ASSERT(BN256_GT_isSame(&e1, &e2));
    ASSERT(BN256_setErrFile("") == 0);
    if (g_err) {
        printf("err %d\n", g_err);
        return 1;
    } else {
        printf("no err\n");
        return 0;
    }
}