Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
java_api.cpp
Go to the documentation of this file.
1/*
2 a new api of pairing for Java
3*/
5#include <iostream>
6
7#undef PUT
8#define PUT(x) std::cout << #x "\t=" << (x).toString() << std::endl
9
10static int errNum = 0;
11
12void assertBool(const char *msg, bool b)
13{
14 if (b) {
15 printf("%s : ok\n", msg);
16 } else {
17 printf("%s : ng\n", msg);
18 errNum++;
19 }
20}
21
22template<class T, class S>
23void assertEqual(const char *msg, const T& a, const S& b)
24{
25 if (a.equals(b)) {
26 printf("%s : ok\n", msg);
27 } else {
28 PUT(a);
29 PUT(b);
30 errNum++;
31 }
32}
33
34struct G2 {
35 const char *aa;
36 const char *ab;
37 const char *ba;
38 const char *bb;
39} g2c = {
40 "12723517038133731887338407189719511622662176727675373276651903807414909099441",
41 "4168783608814932154536427934509895782246573715297911553964171371032945126671",
42 "13891744915211034074451795021214165905772212241412891944830863846330766296736",
43 "7937318970632701341203597196594272556916396164729705624521405069090520231616",
44};
45
46int main()
47{
48 SystemInit();
49 const Ec1 g1(-1, 1);
50 const Ec2 g2(
51 Fp2(Fp(g2c.aa), Fp(g2c.ab)),
52 Fp2(Fp(g2c.ba), Fp(g2c.bb))
53 );
54 // assertBool g2 and g1 on curve
55 assertBool("g1 is on EC", g1.isValid());
56 assertBool("g2 is on twist EC", g2.isValid());
57 puts("order of group");
58 const Mpz& r = GetParamR();
59 PUT(r);
60 {
61 Ec1 t = g1;
62 t.mul(r);
63 assertBool("orgder of g1 == r", t.isZero());
64 }
65 {
66 Ec2 t = g2;
67 t.mul(r);
68 assertBool("order of g2 == r", t.isZero());
69 }
70 const Mpz a("123456789012345");
71 const Mpz b("998752342342342342424242421");
72
73 // scalar-multiplication sample
74 {
75 Mpz c = a;
76 c.add(b);
77 Ec1 Pa = g1; Pa.mul(a);
78 Ec1 Pb = g1; Pb.mul(b);
79 Ec1 Pc = g1; Pc.mul(c);
80 Ec1 out = Pa;
81 out.add(Pb);
82
83 assertEqual("check g1 * c = g1 * a + g1 * b", Pc, out);
84 }
85
86 Fp12 e;
87 // calc e : G2 x G1 -> G3 pairing
88 e.pairing(g2, g1); // e = e(g2, g1)
89 PUT(e);
90 {
91 Fp12 t = e;
92 t.power(r);
93 assertEqual("order of e == r", t, Fp12(1));
94 }
95 Ec2 g2a = g2;
96 g2a.mul(a);
97 Fp12 ea1;
98 ea1.pairing(g2a, g1);
99 Fp12 ea2 = e;
100 ea2.power(a); // ea2 = e^a
101 assertEqual("e(g2 * a, g1) = e(g2, g1)^a", ea1, ea2);
102
103 Ec1 g1b = g1;
104 g1b.mul(b);
105 Fp12 eb1;
106 eb1.pairing(g2, g1b); // eb1 = e(g2, g1b)
107 Fp12 eb2 = e;
108 eb2.power(b); // eb2 = e^b
109 assertEqual("e(g2a, g1 * b) = e(g2, g1)^b", eb1, eb2);
110
111 Ec1 q1 = g1;
112 q1.mul(12345);
113 assertBool("q1 is on EC", q1.isValid());
114 Fp12 e1, e2;
115 e1.pairing(g2, g1); // e1 = e(g2, g1)
116 e2.pairing(g2, q1); // e2 = e(g2, q1)
117 Ec1 q2 = g1;
118 q2.add(q1);
119 e.pairing(g2, q2); // e = e(g2, q2)
120 e1.mul(e2);
121 assertEqual("e = e1 * e2", e, e1);
122 /*
123 reduce one copy as the following
124 */
125 g2a = g2;
126 g2a.mul(a);
127 g1b = g1;
128 g1b.mul(b);
129 Ec2 g2at = g2; g2at.mul(a);
130 Ec1 g1bt = g1; g1bt.mul(b);
131 assertEqual("g2a == g2 * a", g2a, g2at);
132 assertEqual("g1b == g1 * b", g1b, g1bt);
133 printf("errNum = %d\n", errNum);
134}
api for Java
const Mpz & GetParamR()
Definition bn254_if.hpp:250
void SystemInit()
Definition bn254_if.hpp:14
const mie::Vuint & r
Definition bn.cpp:28
bool isZero() const
Definition bn254_if.hpp:186
bool isValid() const
Definition bn254_if.hpp:165
void mul(const Mpz &rhs)
Definition bn254_if.hpp:192
void add(const Ec1 &rhs)
Definition bn254_if.hpp:190
bool isValid() const
Definition bn254_if.hpp:212
void mul(const Mpz &rhs)
Definition bn254_if.hpp:239
bool isZero() const
Definition bn254_if.hpp:233
void mul(const Fp12 &rhs)
Definition bn254_if.hpp:143
void pairing(const Ec2 &ec2, const Ec1 &ec1)
Definition bn254_if.hpp:245
void power(const Mpz &x)
Definition bn254_if.hpp:145
void add(const Mpz &rhs)
Definition bn254_if.hpp:50
void assertBool(const char *msg, bool b)
Definition java_api.cpp:12
void assertEqual(const char *msg, const T &a, const S &b)
Definition java_api.cpp:23
struct G2 g2c
int main()
Definition java_api.cpp:46
#define PUT(x)
Definition java_api.cpp:8
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
#define T(meth, val, expected)
const char * aa
Definition java_api.cpp:35
const char * ab
Definition java_api.cpp:36
const char * bb
Definition java_api.cpp:38
const char * ba
Definition java_api.cpp:37