Coverage Summary for Class: BN128G2 (co.rsk.crypto.altbn128java)
Class |
Class, %
|
Method, %
|
Line, %
|
BN128G2 |
0%
(0/1)
|
0%
(0/7)
|
0%
(0/15)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2019 RSK Labs Ltd.
4 * (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>)
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package co.rsk.crypto.altbn128java;
21
22 import java.math.BigInteger;
23
24 import static co.rsk.crypto.altbn128java.Params.R;
25 import static co.rsk.crypto.altbn128java.Params.TWIST_MUL_BY_P_X;
26 import static co.rsk.crypto.altbn128java.Params.TWIST_MUL_BY_P_Y;
27
28 /**
29 * Implementation of specific cyclic subgroup of points belonging to {@link BN128Fp2} <br/>
30 * Members of this subgroup are passed as a second param to pairing input {@link PairingCheck#addPair(BN128G1, BN128G2)} <br/>
31 * <br/>
32 *
33 * The order of subgroup is {@link Params#R} <br/>
34 * Generator of subgroup G = <br/>
35 * (11559732032986387107991004021392285783925812861821192530917403151452391805634 * i + <br/>
36 * 10857046999023057135944570762232829481370756359578518086990519993285655852781, <br/>
37 * 4082367875863433681332203403145435568316851327593401208105741076214120093531 * i + <br/>
38 * 8495653923123431417604973247489272438418190587263600148770280649306958101930) <br/>
39 * <br/>
40 *
41 * @author Mikhail Kalinin
42 * @since 31.08.2017
43 */
44 public class BN128G2 extends BN128Fp2 {
45
46 BN128G2(BN128<Fp2> p) {
47 super(p.x, p.y, p.z);
48 }
49
50 BN128G2(Fp2 x, Fp2 y, Fp2 z) {
51 super(x, y, z);
52 }
53
54 @Override
55 public BN128G2 toAffine() {
56 return new BN128G2(super.toAffine());
57 }
58
59 /**
60 * Checks whether provided data are coordinates of a point belonging to subgroup,
61 * if check has been passed it returns a point, otherwise returns null
62 */
63 public static BN128G2 create(byte[] a, byte[] b, byte[] c, byte[] d) {
64
65 BN128<Fp2> p = BN128Fp2.create(a, b, c, d);
66
67 // fails if point is invalid
68 if (p == null) {
69 return null;
70 }
71
72 // check whether point is a subgroup member
73 if (!isGroupMember(p)) {return null;}
74
75 return new BN128G2(p);
76 }
77
78 private static boolean isGroupMember(BN128<Fp2> p) {
79 BN128<Fp2> left = p.mul(FR_NEG_ONE).add(p);
80 return left.isZero(); // should satisfy condition: -1 * p + p == 0, where -1 belongs to F_r
81 }
82 private static final BigInteger FR_NEG_ONE = BigInteger.ONE.negate().mod(R);
83
84 BN128G2 mulByP() {
85
86 Fp2 rx = TWIST_MUL_BY_P_X.mul(x.frobeniusMap(1));
87 Fp2 ry = TWIST_MUL_BY_P_Y.mul(y.frobeniusMap(1));
88 Fp2 rz = z.frobeniusMap(1);
89
90 return new BN128G2(rx, ry, rz);
91 }
92 }