177 lines
6.4 KiB
Java
177 lines
6.4 KiB
Java
|
package org.bouncycastle.math.ec.custom.sec;
|
||
|
|
||
|
import java.math.BigInteger;
|
||
|
import org.bouncycastle.math.ec.ECFieldElement;
|
||
|
import org.bouncycastle.math.raw.Mod;
|
||
|
import org.bouncycastle.math.raw.Nat256;
|
||
|
import org.bouncycastle.util.Arrays;
|
||
|
|
||
|
/* loaded from: classes6.dex */
|
||
|
public class SecP256K1FieldElement extends ECFieldElement {
|
||
|
public static final BigInteger Q = SecP256K1Curve.q;
|
||
|
protected int[] x;
|
||
|
|
||
|
@Override // org.bouncycastle.math.ec.ECFieldElement
|
||
|
public BigInteger toBigInteger() {
|
||
|
return Nat256.toBigInteger(this.x);
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.math.ec.ECFieldElement
|
||
|
public boolean testBitZero() {
|
||
|
return Nat256.getBit(this.x, 0) == 1;
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.math.ec.ECFieldElement
|
||
|
public ECFieldElement subtract(ECFieldElement eCFieldElement) {
|
||
|
int[] create = Nat256.create();
|
||
|
SecP256K1Field.subtract(this.x, ((SecP256K1FieldElement) eCFieldElement).x, create);
|
||
|
return new SecP256K1FieldElement(create);
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.math.ec.ECFieldElement
|
||
|
public ECFieldElement square() {
|
||
|
int[] create = Nat256.create();
|
||
|
SecP256K1Field.square(this.x, create);
|
||
|
return new SecP256K1FieldElement(create);
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.math.ec.ECFieldElement
|
||
|
public ECFieldElement sqrt() {
|
||
|
int[] iArr = this.x;
|
||
|
if (Nat256.isZero(iArr) || Nat256.isOne(iArr)) {
|
||
|
return this;
|
||
|
}
|
||
|
int[] create = Nat256.create();
|
||
|
SecP256K1Field.square(iArr, create);
|
||
|
SecP256K1Field.multiply(create, iArr, create);
|
||
|
int[] create2 = Nat256.create();
|
||
|
SecP256K1Field.square(create, create2);
|
||
|
SecP256K1Field.multiply(create2, iArr, create2);
|
||
|
int[] create3 = Nat256.create();
|
||
|
SecP256K1Field.squareN(create2, 3, create3);
|
||
|
SecP256K1Field.multiply(create3, create2, create3);
|
||
|
SecP256K1Field.squareN(create3, 3, create3);
|
||
|
SecP256K1Field.multiply(create3, create2, create3);
|
||
|
SecP256K1Field.squareN(create3, 2, create3);
|
||
|
SecP256K1Field.multiply(create3, create, create3);
|
||
|
int[] create4 = Nat256.create();
|
||
|
SecP256K1Field.squareN(create3, 11, create4);
|
||
|
SecP256K1Field.multiply(create4, create3, create4);
|
||
|
SecP256K1Field.squareN(create4, 22, create3);
|
||
|
SecP256K1Field.multiply(create3, create4, create3);
|
||
|
int[] create5 = Nat256.create();
|
||
|
SecP256K1Field.squareN(create3, 44, create5);
|
||
|
SecP256K1Field.multiply(create5, create3, create5);
|
||
|
int[] create6 = Nat256.create();
|
||
|
SecP256K1Field.squareN(create5, 88, create6);
|
||
|
SecP256K1Field.multiply(create6, create5, create6);
|
||
|
SecP256K1Field.squareN(create6, 44, create5);
|
||
|
SecP256K1Field.multiply(create5, create3, create5);
|
||
|
SecP256K1Field.squareN(create5, 3, create3);
|
||
|
SecP256K1Field.multiply(create3, create2, create3);
|
||
|
SecP256K1Field.squareN(create3, 23, create3);
|
||
|
SecP256K1Field.multiply(create3, create4, create3);
|
||
|
SecP256K1Field.squareN(create3, 6, create3);
|
||
|
SecP256K1Field.multiply(create3, create, create3);
|
||
|
SecP256K1Field.squareN(create3, 2, create3);
|
||
|
SecP256K1Field.square(create3, create);
|
||
|
if (Nat256.eq(iArr, create)) {
|
||
|
return new SecP256K1FieldElement(create3);
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.math.ec.ECFieldElement
|
||
|
public ECFieldElement negate() {
|
||
|
int[] create = Nat256.create();
|
||
|
SecP256K1Field.negate(this.x, create);
|
||
|
return new SecP256K1FieldElement(create);
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.math.ec.ECFieldElement
|
||
|
public ECFieldElement multiply(ECFieldElement eCFieldElement) {
|
||
|
int[] create = Nat256.create();
|
||
|
SecP256K1Field.multiply(this.x, ((SecP256K1FieldElement) eCFieldElement).x, create);
|
||
|
return new SecP256K1FieldElement(create);
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.math.ec.ECFieldElement
|
||
|
public boolean isZero() {
|
||
|
return Nat256.isZero(this.x);
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.math.ec.ECFieldElement
|
||
|
public boolean isOne() {
|
||
|
return Nat256.isOne(this.x);
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.math.ec.ECFieldElement
|
||
|
public ECFieldElement invert() {
|
||
|
int[] create = Nat256.create();
|
||
|
Mod.invert(SecP256K1Field.P, this.x, create);
|
||
|
return new SecP256K1FieldElement(create);
|
||
|
}
|
||
|
|
||
|
public int hashCode() {
|
||
|
return Q.hashCode() ^ Arrays.hashCode(this.x, 0, 8);
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.math.ec.ECFieldElement
|
||
|
public int getFieldSize() {
|
||
|
return Q.bitLength();
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.math.ec.ECFieldElement
|
||
|
public String getFieldName() {
|
||
|
return "SecP256K1Field";
|
||
|
}
|
||
|
|
||
|
public boolean equals(Object obj) {
|
||
|
if (obj == this) {
|
||
|
return true;
|
||
|
}
|
||
|
if (obj instanceof SecP256K1FieldElement) {
|
||
|
return Nat256.eq(this.x, ((SecP256K1FieldElement) obj).x);
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.math.ec.ECFieldElement
|
||
|
public ECFieldElement divide(ECFieldElement eCFieldElement) {
|
||
|
int[] create = Nat256.create();
|
||
|
Mod.invert(SecP256K1Field.P, ((SecP256K1FieldElement) eCFieldElement).x, create);
|
||
|
SecP256K1Field.multiply(create, this.x, create);
|
||
|
return new SecP256K1FieldElement(create);
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.math.ec.ECFieldElement
|
||
|
public ECFieldElement addOne() {
|
||
|
int[] create = Nat256.create();
|
||
|
SecP256K1Field.addOne(this.x, create);
|
||
|
return new SecP256K1FieldElement(create);
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.math.ec.ECFieldElement
|
||
|
public ECFieldElement add(ECFieldElement eCFieldElement) {
|
||
|
int[] create = Nat256.create();
|
||
|
SecP256K1Field.add(this.x, ((SecP256K1FieldElement) eCFieldElement).x, create);
|
||
|
return new SecP256K1FieldElement(create);
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: protected */
|
||
|
public SecP256K1FieldElement(int[] iArr) {
|
||
|
this.x = iArr;
|
||
|
}
|
||
|
|
||
|
public SecP256K1FieldElement(BigInteger bigInteger) {
|
||
|
if (bigInteger == null || bigInteger.signum() < 0 || bigInteger.compareTo(Q) >= 0) {
|
||
|
throw new IllegalArgumentException("x value invalid for SecP256K1FieldElement");
|
||
|
}
|
||
|
this.x = SecP256K1Field.fromBigInteger(bigInteger);
|
||
|
}
|
||
|
|
||
|
public SecP256K1FieldElement() {
|
||
|
this.x = Nat256.create();
|
||
|
}
|
||
|
}
|