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

Class Class, % Method, % Line, %
BN128Pairing 0% (0/1) 0% (0/3) 0% (0/8)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2019 RSK Labs Ltd. 4  * 5  * This program is free software: you can redistribute it and/or modify 6  * it under the terms of the GNU Lesser General Public License as published by 7  * the Free Software Foundation, either version 3 of the License, or 8  * (at your option) any later version. 9  * 10  * This program is distributed in the hope that it will be useful, 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13  * GNU Lesser General Public License for more details. 14  * 15  * You should have received a copy of the GNU Lesser General Public License 16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 17  */ 18  19 package co.rsk.pcc.altBN128; 20  21 import co.rsk.pcc.altBN128.impls.AbstractAltBN128; 22 import org.ethereum.config.blockchain.upgrades.ActivationConfig; 23 import org.ethereum.vm.GasCost; 24  25 /** 26  * Computes pairing check. <br/> 27  * See {@link PairingCheck} for details.<br/> 28  * <br/> 29  * 30  * Input data[]: <br/> 31  * an array of points (a1, b1, ... , ak, bk), <br/> 32  * where "ai" is a point of {@link BN128Fp} curve and encoded as two 32-byte left-padded integers (x; y) <br/> 33  * "bi" is a point of {@link BN128G2} curve and encoded as four 32-byte left-padded integers {@code (ai + b; ci + d)}, 34  * each coordinate of the point is a big-endian {@link Fp2} number, so {@code b} precedes {@code a} in the encoding: 35  * {@code (b, a; d, c)} <br/> 36  * thus each pair (ai, bi) has 192 bytes length, if 192 is not a multiple of {@code data.length} then execution fails <br/> 37  * the number of pairs is derived from input length by dividing it by 192 (the length of a pair) <br/> 38  * <br/> 39  * 40  * output: <br/> 41  * pairing product which is either 0 or 1, encoded as 32-byte left-padded integer <br/> 42  * 43  */ 44  45 /** 46  * @author Sebastian Sicardi 47  * @since 10.09.2019 48  */ 49 public class BN128Pairing extends BN128PrecompiledContract { 50  51  public BN128Pairing(ActivationConfig.ForBlock activations, AbstractAltBN128 altBN128) { 52  super(activations, altBN128); 53  } 54  55  @Override 56  public long getGasForData(byte[] data) { 57  long baseCost = GasCost.toGas(45_000); 58  long perPairCost = GasCost.toGas(34_000L); 59  60  if (data == null) { 61  return baseCost; 62  } 63  64  return GasCost.add(GasCost.multiply(perPairCost, (data.length / AbstractAltBN128.PAIR_SIZE)) , baseCost); 65  } 66  67  @Override 68  protected int concreteExecute(byte[] data) { 69  return altBN128Lib.pairing(data, data.length); 70  } 71 }