summaryrefslogtreecommitdiffstats
path: root/moon-abe/pbc-0.5.14/guru/eta_T_3_test.c
blob: 69cce7debdc6b02681fca6227a784f43db67183c (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* Test eta_T pairing over ternary extension fields.
   Outputing nothing if everything is good. */

#include <stddef.h>
#include <stdarg.h>
#include <stdio.h>
#include <gmp.h>
#include "pbc.h"
#include "pbc_ternary_extension_field.h"
#include "pbc_test.h"

static pairing_t pairing;
static element_t a1, a2, b1, b2, c1, c2;
static mpz_t order;

static void setup(void) {
    mpz_init(order);
    mpz_set_str(order, "2726865189058261010774960798134976187171462721", 10);
    const char *param = "type i\n" "m 97\n" "t 12\n" "n2 7\n"
                        "n 2726865189058261010774960798134976187171462721\n";
    EXPECT(pairing_init_set_str(pairing, param) == 0);
    element_init_G1(a1, pairing);
    element_init_G1(a2, pairing);
    element_init_G2(b1, pairing);
    element_init_G2(b2, pairing);
    element_init_GT(c1, pairing);
    element_init_GT(c2, pairing);
}

static void test_set_mpz(void) {
    mpz_t a;
    mpz_init(a);
    int i;
    for(i = 0; i < 2; i ++) {
        mpz_set_si(a, i);
        element_set_mpz(a1, a);
        EXPECT(element_is0(a1) && element_is1(a1));
        element_set_mpz(b1, a);
        EXPECT(element_is0(b1) && element_is1(b1));
        element_set_mpz(c1, a);
        EXPECT(element_is0(c1) && element_is1(c1));
    }
    mpz_clear(a);
}

static void test_order(void) {
    EXPECT(mpz_cmp(pairing->G1->order, order) == 0);
    EXPECT(mpz_cmp(pairing->G2->order, order) == 0);
    EXPECT(mpz_cmp(pairing->GT->order, order) == 0);
    int i;
    for (i=0; i<10; i++) {
        element_random(a1);
        EXPECT(element_is0(a1) == 0);
        element_pow_mpz(a1, a1, order);
        EXPECT(element_is0(a1));
        element_random(c1);
        EXPECT(element_is0(c1) == 0);
        element_pow_mpz(c1, c1, order);
        EXPECT(element_is0(c1));
    }
}

static void test_bilinear_with_zero(void) {
    element_set0(a1);
    element_random(b1);
    element_pairing(c1, a1, b1);
    EXPECT(element_is0(c1) && element_is1(c1));
    element_random(a1);
    element_set0(b1);
    element_pairing(c1, a1, b1);
    EXPECT(element_is0(c1) && element_is1(c1));
    element_set0(a1);
    element_set0(b1);
    element_pairing(c1, a1, b1);
    EXPECT(element_is0(c1) && element_is1(c1));
}

static void test_bilinear(void) {
    element_random(a1);
    element_mul_si(a2, a1, 33);
    element_random(b1);
    element_mul_si(b2, b1, 33);
    element_pairing(c1, a1, b2);
    element_pairing(c2, a2, b1);
    EXPECT(element_cmp(c1, c2) == 0);
    element_mul_mpz(c1, c1, order);
    EXPECT(element_is0(c1));
}

static void test_gen_param(void) {
    typedef struct {
        unsigned int len;
        int m;
        int t;
        element_ptr p;
        mpz_t n;
        mpz_t n2;
    } params;

    pbc_param_t par;
    pbc_param_init_i_gen(par, 150);
    params *p = par->data;
    EXPECT(p->m == 97);
    EXPECT(p->t == 12);
    EXPECT(!mpz_cmp(p->n, order));
    EXPECT(!mpz_cmp_ui(p->n2, 7));
    pbc_param_clear(par);
}

static void tear_down(void) {
    element_clear(a1);
    element_clear(a2);
    element_clear(b1);
    element_clear(b2);
    element_clear(c1);
    element_clear(c2);
    pairing_clear(pairing);
    mpz_clear(order);
}

int main(void) {
    setup();
    test_set_mpz();
    test_order();
    test_bilinear_with_zero();
    test_bilinear();
    test_gen_param();
    tear_down();
    return 0;
}