Coverage Summary for Class: JavaAltBN128 (co.rsk.pcc.altBN128.impls)

Class Class, % Method, % Line, %
JavaAltBN128 0% (0/1) 0% (0/7) 0% (0/58)


1 package co.rsk.pcc.altBN128.impls; 2  3 import co.rsk.crypto.altbn128java.*; 4 import org.ethereum.util.BIUtil; 5 import org.ethereum.vm.DataWord; 6  7 import static org.ethereum.util.ByteUtil.parseWord; 8 import static org.ethereum.util.ByteUtil.stripLeadingZeroes; 9  10 public class JavaAltBN128 extends AbstractAltBN128 { 11  12  private static byte[] encodeRes(byte[] w1, byte[] w2) { 13  14  byte[] res = new byte[64]; 15  16  w1 = stripLeadingZeroes(w1); 17  w2 = stripLeadingZeroes(w2); 18  19  System.arraycopy(w1, 0, res, 32 - w1.length, w1.length); 20  System.arraycopy(w2, 0, res, 64 - w2.length, w2.length); 21  22  return res; 23  } 24  25  private static BN128Pair decodePair(byte[] in, int offset) { 26  27  byte[] x = parseWord(in, offset, 0); 28  byte[] y = parseWord(in, offset, 1); 29  30  BN128G1 p1 = BN128G1.create(x, y); 31  32  // fail if point is invalid 33  if (p1 == null) { 34  return null; 35  } 36  37  // (b, a) 38  byte[] b = parseWord(in, offset, 2); 39  byte[] a = parseWord(in, offset, 3); 40  41  // (d, c) 42  byte[] d = parseWord(in, offset, 4); 43  byte[] c = parseWord(in, offset, 5); 44  45  BN128G2 p2 = BN128G2.create(a, b, c, d); 46  47  // fail if point is invalid 48  if (p2 == null) { 49  return null; 50  } 51  52  return BN128Pair.of(p1, p2); 53  } 54  55  56  @Override 57  public int add(byte[] data, int length) { 58  output = new byte[64]; 59  60  byte[] x1 = parseWord(data, 0); 61  byte[] y1 = parseWord(data, 1); 62  63  byte[] x2 = parseWord(data, 2); 64  byte[] y2 = parseWord(data, 3); 65  66  BN128<Fp> p1 = BN128Fp.create(x1, y1); 67  68  if (p1 == null) { 69  return returnError(); 70  } 71  72  BN128<Fp> p2 = BN128Fp.create(x2, y2); 73  if (p2 == null) { 74  return returnError(); 75  } 76  77  BN128<Fp> res = p1.add(p2).toEthNotation(); 78  79  output = encodeRes(res.x().bytes(), res.y().bytes()); 80  return 1; 81  } 82  83  @Override 84  public int mul(byte[] data, int length) { 85  output = new byte[64]; 86  87  byte[] x = parseWord(data, 0); 88  byte[] y = parseWord(data, 1); 89  90  byte[] s = parseWord(data, 2); 91  92  BN128<Fp> p = BN128Fp.create(x, y); 93  94  if (p == null) { 95  return returnError(); 96  } 97  98  BN128<Fp> res = p.mul(BIUtil.toBI(s)).toEthNotation(); 99  100  output = encodeRes(res.x().bytes(), res.y().bytes()); 101  return 1; 102  } 103  104  @Override 105  public int pairing(byte[] data, int length) { 106  107  output = new byte[32]; 108  109  // fail if input len is not a multiple of PAIR_SIZE 110  if (data.length % PAIR_SIZE > 0) { 111  return returnError(); 112  } 113  114  PairingCheck check = PairingCheck.create(); 115  116  // iterating over all pairs 117  for (int offset = 0; offset < data.length; offset += PAIR_SIZE) { 118  119  BN128Pair pair = decodePair(data, offset); 120  121  // fail if decoding has failed 122  if (pair == null) { 123  return returnError(); 124  } 125  126  check.addPair(pair.getG1(), pair.getG2()); 127  } 128  129  check.run(); 130  int result = check.result(); 131  132  output = DataWord.valueOf(result).getData(); 133  return 1; 134  } 135  136  protected int returnError() { 137  return -1; 138  } 139 }