Coverage Summary for Class: Fp (co.rsk.crypto.altbn128java)

Class Class, % Method, % Line, %
Fp 0% (0/1) 0% (0/18) 0% (0/24)


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.P; 25  26 /** 27  * Arithmetic in F_p, p = 21888242871839275222246405745257275088696311157297823662689037894645226208583 28  * 29  * @author Mikhail Kalinin 30  * @since 01.09.2017 31  */ 32 public class Fp implements Field<Fp> { 33  34  static public final Fp ZERO = new Fp(BigInteger.ZERO); 35  static public final Fp _1 = new Fp(BigInteger.ONE); 36  static public final Fp NON_RESIDUE = new Fp(new BigInteger("21888242871839275222246405745257275088696311157297823662689037894645226208582")); 37  38  static public final Fp _2_INV = new Fp(BigInteger.valueOf(2).modInverse(P)); 39  40  private BigInteger v; 41  42  Fp(BigInteger v) { this.v = v; } 43  44  @Override public Fp add(Fp o) { return new Fp(this.v.add(o.v).mod(P)); } 45  @Override public Fp mul(Fp o) { return new Fp(this.v.multiply(o.v).mod(P)); } 46  @Override public Fp sub(Fp o) { return new Fp(this.v.subtract(o.v).mod(P)); } 47  @Override public Fp squared() { return new Fp(v.multiply(v).mod(P)); } 48  @Override public Fp dbl() { return new Fp(v.add(v).mod(P)); } 49  @Override public Fp inverse() { return new Fp(v.modInverse(P)); } 50  @Override public Fp negate() { return new Fp(v.negate().mod(P)); } 51  @Override public boolean isZero() { return v.compareTo(BigInteger.ZERO) == 0; } 52  53  /** 54  * Checks if provided value is a valid Fp member 55  */ 56  @Override 57  public boolean isValid() { 58  return v.compareTo(P) < 0; 59  } 60  61  Fp2 mul(Fp2 o) { return new Fp2(o.a().mul(this), o.b().mul(this)); } 62  63  static Fp create(byte[] v) { 64  return new Fp(new BigInteger(1, v)); 65  } 66  67  static Fp create(BigInteger v) { 68  return new Fp(v); 69  } 70  71  public byte[] bytes() { 72  return v.toByteArray(); 73  } 74  75  @Override 76  public boolean equals(Object o) { 77  if (this == o) {return true;} 78  if (o == null || getClass() != o.getClass()) {return false;} 79  80  Fp fp = (Fp) o; 81  82  return !(v != null ? v.compareTo(fp.v) != 0 : fp.v != null); 83  } 84  85  @Override 86  public int hashCode() { 87  return v.hashCode(); 88  } 89  90  @Override 91  public String toString() { 92  return v.toString(); 93  } 94 }