use of com.github.zhenwei.core.math.ec.ECFieldElement in project LinLong-Java by zhenwei1108.
the class SecP256K1Point method add.
// B.3 pg 62
public ECPoint add(ECPoint b) {
if (this.isInfinity()) {
return b;
}
if (b.isInfinity()) {
return this;
}
if (this == b) {
return twice();
}
ECCurve curve = this.getCurve();
SecP256K1FieldElement X1 = (SecP256K1FieldElement) this.x, Y1 = (SecP256K1FieldElement) this.y;
SecP256K1FieldElement X2 = (SecP256K1FieldElement) b.getXCoord(), Y2 = (SecP256K1FieldElement) b.getYCoord();
SecP256K1FieldElement Z1 = (SecP256K1FieldElement) this.zs[0];
SecP256K1FieldElement Z2 = (SecP256K1FieldElement) b.getZCoord(0);
int c;
int[] tt1 = Nat256.createExt();
int[] t2 = Nat256.create();
int[] t3 = Nat256.create();
int[] t4 = Nat256.create();
boolean Z1IsOne = Z1.isOne();
int[] U2, S2;
if (Z1IsOne) {
U2 = X2.x;
S2 = Y2.x;
} else {
S2 = t3;
SecP256K1Field.square(Z1.x, S2);
U2 = t2;
SecP256K1Field.multiply(S2, X2.x, U2);
SecP256K1Field.multiply(S2, Z1.x, S2);
SecP256K1Field.multiply(S2, Y2.x, S2);
}
boolean Z2IsOne = Z2.isOne();
int[] U1, S1;
if (Z2IsOne) {
U1 = X1.x;
S1 = Y1.x;
} else {
S1 = t4;
SecP256K1Field.square(Z2.x, S1);
U1 = tt1;
SecP256K1Field.multiply(S1, X1.x, U1);
SecP256K1Field.multiply(S1, Z2.x, S1);
SecP256K1Field.multiply(S1, Y1.x, S1);
}
int[] H = Nat256.create();
SecP256K1Field.subtract(U1, U2, H);
int[] R = t2;
SecP256K1Field.subtract(S1, S2, R);
// Check if b == this or b == -this
if (Nat256.isZero(H)) {
if (Nat256.isZero(R)) {
// this == b, i.e. this must be doubled
return this.twice();
}
// this == -b, i.e. the result is the point at infinity
return curve.getInfinity();
}
int[] HSquared = t3;
SecP256K1Field.square(H, HSquared);
int[] G = Nat256.create();
SecP256K1Field.multiply(HSquared, H, G);
int[] V = t3;
SecP256K1Field.multiply(HSquared, U1, V);
SecP256K1Field.negate(G, G);
Nat256.mul(S1, G, tt1);
c = Nat256.addBothTo(V, V, G);
SecP256K1Field.reduce32(c, G);
SecP256K1FieldElement X3 = new SecP256K1FieldElement(t4);
SecP256K1Field.square(R, X3.x);
SecP256K1Field.subtract(X3.x, G, X3.x);
SecP256K1FieldElement Y3 = new SecP256K1FieldElement(G);
SecP256K1Field.subtract(V, X3.x, Y3.x);
SecP256K1Field.multiplyAddToExt(Y3.x, R, tt1);
SecP256K1Field.reduce(tt1, Y3.x);
SecP256K1FieldElement Z3 = new SecP256K1FieldElement(H);
if (!Z1IsOne) {
SecP256K1Field.multiply(Z3.x, Z1.x, Z3.x);
}
if (!Z2IsOne) {
SecP256K1Field.multiply(Z3.x, Z2.x, Z3.x);
}
ECFieldElement[] zs = new ECFieldElement[] { Z3 };
return new SecP256K1Point(curve, X3, Y3, zs);
}
use of com.github.zhenwei.core.math.ec.ECFieldElement in project LinLong-Java by zhenwei1108.
the class SecP160K1Point method add.
// B.3 pg 62
public ECPoint add(ECPoint b) {
if (this.isInfinity()) {
return b;
}
if (b.isInfinity()) {
return this;
}
if (this == b) {
return twice();
}
ECCurve curve = this.getCurve();
SecP160R2FieldElement X1 = (SecP160R2FieldElement) this.x, Y1 = (SecP160R2FieldElement) this.y;
SecP160R2FieldElement X2 = (SecP160R2FieldElement) b.getXCoord(), Y2 = (SecP160R2FieldElement) b.getYCoord();
SecP160R2FieldElement Z1 = (SecP160R2FieldElement) this.zs[0];
SecP160R2FieldElement Z2 = (SecP160R2FieldElement) b.getZCoord(0);
int c;
int[] tt1 = Nat160.createExt();
int[] t2 = Nat160.create();
int[] t3 = Nat160.create();
int[] t4 = Nat160.create();
boolean Z1IsOne = Z1.isOne();
int[] U2, S2;
if (Z1IsOne) {
U2 = X2.x;
S2 = Y2.x;
} else {
S2 = t3;
SecP160R2Field.square(Z1.x, S2);
U2 = t2;
SecP160R2Field.multiply(S2, X2.x, U2);
SecP160R2Field.multiply(S2, Z1.x, S2);
SecP160R2Field.multiply(S2, Y2.x, S2);
}
boolean Z2IsOne = Z2.isOne();
int[] U1, S1;
if (Z2IsOne) {
U1 = X1.x;
S1 = Y1.x;
} else {
S1 = t4;
SecP160R2Field.square(Z2.x, S1);
U1 = tt1;
SecP160R2Field.multiply(S1, X1.x, U1);
SecP160R2Field.multiply(S1, Z2.x, S1);
SecP160R2Field.multiply(S1, Y1.x, S1);
}
int[] H = Nat160.create();
SecP160R2Field.subtract(U1, U2, H);
int[] R = t2;
SecP160R2Field.subtract(S1, S2, R);
// Check if b == this or b == -this
if (Nat160.isZero(H)) {
if (Nat160.isZero(R)) {
// this == b, i.e. this must be doubled
return this.twice();
}
// this == -b, i.e. the result is the point at infinity
return curve.getInfinity();
}
int[] HSquared = t3;
SecP160R2Field.square(H, HSquared);
int[] G = Nat160.create();
SecP160R2Field.multiply(HSquared, H, G);
int[] V = t3;
SecP160R2Field.multiply(HSquared, U1, V);
SecP160R2Field.negate(G, G);
Nat160.mul(S1, G, tt1);
c = Nat160.addBothTo(V, V, G);
SecP160R2Field.reduce32(c, G);
SecP160R2FieldElement X3 = new SecP160R2FieldElement(t4);
SecP160R2Field.square(R, X3.x);
SecP160R2Field.subtract(X3.x, G, X3.x);
SecP160R2FieldElement Y3 = new SecP160R2FieldElement(G);
SecP160R2Field.subtract(V, X3.x, Y3.x);
SecP160R2Field.multiplyAddToExt(Y3.x, R, tt1);
SecP160R2Field.reduce(tt1, Y3.x);
SecP160R2FieldElement Z3 = new SecP160R2FieldElement(H);
if (!Z1IsOne) {
SecP160R2Field.multiply(Z3.x, Z1.x, Z3.x);
}
if (!Z2IsOne) {
SecP160R2Field.multiply(Z3.x, Z2.x, Z3.x);
}
ECFieldElement[] zs = new ECFieldElement[] { Z3 };
return new SecP160K1Point(curve, X3, Y3, zs);
}
use of com.github.zhenwei.core.math.ec.ECFieldElement in project LinLong-Java by zhenwei1108.
the class Curve25519Point method twice.
public ECPoint twice() {
if (this.isInfinity()) {
return this;
}
ECCurve curve = this.getCurve();
ECFieldElement Y1 = this.y;
if (Y1.isZero()) {
return curve.getInfinity();
}
return twiceJacobianModified(true);
}
use of com.github.zhenwei.core.math.ec.ECFieldElement in project LinLong-Java by zhenwei1108.
the class SecP128R1Point method add.
public ECPoint add(ECPoint b) {
if (this.isInfinity()) {
return b;
}
if (b.isInfinity()) {
return this;
}
if (this == b) {
return twice();
}
ECCurve curve = this.getCurve();
SecP128R1FieldElement X1 = (SecP128R1FieldElement) this.x, Y1 = (SecP128R1FieldElement) this.y;
SecP128R1FieldElement X2 = (SecP128R1FieldElement) b.getXCoord(), Y2 = (SecP128R1FieldElement) b.getYCoord();
SecP128R1FieldElement Z1 = (SecP128R1FieldElement) this.zs[0];
SecP128R1FieldElement Z2 = (SecP128R1FieldElement) b.getZCoord(0);
int c;
int[] tt1 = Nat128.createExt();
int[] t2 = Nat128.create();
int[] t3 = Nat128.create();
int[] t4 = Nat128.create();
boolean Z1IsOne = Z1.isOne();
int[] U2, S2;
if (Z1IsOne) {
U2 = X2.x;
S2 = Y2.x;
} else {
S2 = t3;
SecP128R1Field.square(Z1.x, S2);
U2 = t2;
SecP128R1Field.multiply(S2, X2.x, U2);
SecP128R1Field.multiply(S2, Z1.x, S2);
SecP128R1Field.multiply(S2, Y2.x, S2);
}
boolean Z2IsOne = Z2.isOne();
int[] U1, S1;
if (Z2IsOne) {
U1 = X1.x;
S1 = Y1.x;
} else {
S1 = t4;
SecP128R1Field.square(Z2.x, S1);
U1 = tt1;
SecP128R1Field.multiply(S1, X1.x, U1);
SecP128R1Field.multiply(S1, Z2.x, S1);
SecP128R1Field.multiply(S1, Y1.x, S1);
}
int[] H = Nat128.create();
SecP128R1Field.subtract(U1, U2, H);
int[] R = t2;
SecP128R1Field.subtract(S1, S2, R);
// Check if b == this or b == -this
if (Nat128.isZero(H)) {
if (Nat128.isZero(R)) {
// this == b, i.e. this must be doubled
return this.twice();
}
// this == -b, i.e. the result is the point at infinity
return curve.getInfinity();
}
int[] HSquared = t3;
SecP128R1Field.square(H, HSquared);
int[] G = Nat128.create();
SecP128R1Field.multiply(HSquared, H, G);
int[] V = t3;
SecP128R1Field.multiply(HSquared, U1, V);
SecP128R1Field.negate(G, G);
Nat128.mul(S1, G, tt1);
c = Nat128.addBothTo(V, V, G);
SecP128R1Field.reduce32(c, G);
SecP128R1FieldElement X3 = new SecP128R1FieldElement(t4);
SecP128R1Field.square(R, X3.x);
SecP128R1Field.subtract(X3.x, G, X3.x);
SecP128R1FieldElement Y3 = new SecP128R1FieldElement(G);
SecP128R1Field.subtract(V, X3.x, Y3.x);
SecP128R1Field.multiplyAddToExt(Y3.x, R, tt1);
SecP128R1Field.reduce(tt1, Y3.x);
SecP128R1FieldElement Z3 = new SecP128R1FieldElement(H);
if (!Z1IsOne) {
SecP128R1Field.multiply(Z3.x, Z1.x, Z3.x);
}
if (!Z2IsOne) {
SecP128R1Field.multiply(Z3.x, Z2.x, Z3.x);
}
ECFieldElement[] zs = new ECFieldElement[] { Z3 };
return new SecP128R1Point(curve, X3, Y3, zs);
}
use of com.github.zhenwei.core.math.ec.ECFieldElement in project LinLong-Java by zhenwei1108.
the class SecP224K1Point method add.
// B.3 pg 62
public ECPoint add(ECPoint b) {
if (this.isInfinity()) {
return b;
}
if (b.isInfinity()) {
return this;
}
if (this == b) {
return twice();
}
ECCurve curve = this.getCurve();
SecP224K1FieldElement X1 = (SecP224K1FieldElement) this.x, Y1 = (SecP224K1FieldElement) this.y;
SecP224K1FieldElement X2 = (SecP224K1FieldElement) b.getXCoord(), Y2 = (SecP224K1FieldElement) b.getYCoord();
SecP224K1FieldElement Z1 = (SecP224K1FieldElement) this.zs[0];
SecP224K1FieldElement Z2 = (SecP224K1FieldElement) b.getZCoord(0);
int c;
int[] tt1 = Nat224.createExt();
int[] t2 = Nat224.create();
int[] t3 = Nat224.create();
int[] t4 = Nat224.create();
boolean Z1IsOne = Z1.isOne();
int[] U2, S2;
if (Z1IsOne) {
U2 = X2.x;
S2 = Y2.x;
} else {
S2 = t3;
SecP224K1Field.square(Z1.x, S2);
U2 = t2;
SecP224K1Field.multiply(S2, X2.x, U2);
SecP224K1Field.multiply(S2, Z1.x, S2);
SecP224K1Field.multiply(S2, Y2.x, S2);
}
boolean Z2IsOne = Z2.isOne();
int[] U1, S1;
if (Z2IsOne) {
U1 = X1.x;
S1 = Y1.x;
} else {
S1 = t4;
SecP224K1Field.square(Z2.x, S1);
U1 = tt1;
SecP224K1Field.multiply(S1, X1.x, U1);
SecP224K1Field.multiply(S1, Z2.x, S1);
SecP224K1Field.multiply(S1, Y1.x, S1);
}
int[] H = Nat224.create();
SecP224K1Field.subtract(U1, U2, H);
int[] R = t2;
SecP224K1Field.subtract(S1, S2, R);
// Check if b == this or b == -this
if (Nat224.isZero(H)) {
if (Nat224.isZero(R)) {
// this == b, i.e. this must be doubled
return this.twice();
}
// this == -b, i.e. the result is the point at infinity
return curve.getInfinity();
}
int[] HSquared = t3;
SecP224K1Field.square(H, HSquared);
int[] G = Nat224.create();
SecP224K1Field.multiply(HSquared, H, G);
int[] V = t3;
SecP224K1Field.multiply(HSquared, U1, V);
SecP224K1Field.negate(G, G);
Nat224.mul(S1, G, tt1);
c = Nat224.addBothTo(V, V, G);
SecP224K1Field.reduce32(c, G);
SecP224K1FieldElement X3 = new SecP224K1FieldElement(t4);
SecP224K1Field.square(R, X3.x);
SecP224K1Field.subtract(X3.x, G, X3.x);
SecP224K1FieldElement Y3 = new SecP224K1FieldElement(G);
SecP224K1Field.subtract(V, X3.x, Y3.x);
SecP224K1Field.multiplyAddToExt(Y3.x, R, tt1);
SecP224K1Field.reduce(tt1, Y3.x);
SecP224K1FieldElement Z3 = new SecP224K1FieldElement(H);
if (!Z1IsOne) {
SecP224K1Field.multiply(Z3.x, Z1.x, Z3.x);
}
if (!Z2IsOne) {
SecP224K1Field.multiply(Z3.x, Z2.x, Z3.x);
}
ECFieldElement[] zs = new ECFieldElement[] { Z3 };
return new SecP224K1Point(curve, X3, Y3, zs);
}
Aggregations