如何计算 java 中 curve1174 的倍增?
How to calculate doubling in curve1174 in java?
很难找到关于它的文档,所以我来了。
我正在尝试在 java 中应用 Curve1174 加倍,但出现问题。
当我使用 BigInteger 时,它给了我 (0,0)
当我使用 BigDecimal 时,它给出
Exception in thread “main” java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
我知道我必须使用 divide(blabla , length , RoundingMode) 但我不知道标准的确切长度是多少。
我的代码在下面,你能帮帮我吗?我思考了3天,我快疯了。
大整数代码
public class ECCprocess {
// C1174
// COSTRUCTOR
public static ECPoint operate(ECPoint point1,ECPoint point2){
ECPoint resultPoint;
BigInteger resultX;
BigInteger resultY;
CONSTANTlist constantList = new CONSTANTlist();
BigInteger calculating1 = point1.getAffineX().multiply(point2.getAffineY());
BigInteger calculating2 = point2.getAffineX().multiply(point1.getAffineY());
BigInteger calculating3 = ((BigInteger)constantList.getConstant("dConstant"));
calculating3 = (calculating3.multiply(point1.getAffineX().multiply(point2.getAffineX()).multiply(point1.getAffineY()).multiply(point2.getAffineY()))).add(new BigInteger("1"));
resultX = (calculating1.add(calculating2)).divide(calculating3);
calculating1 = point1.getAffineY().multiply(point2.getAffineY());
calculating2 = point1.getAffineX().multiply(point2.getAffineX());
calculating3 = ((BigInteger)constantList.getConstant("dConstant")).multiply(point1.getAffineX()).multiply(point1.getAffineY()).multiply(point2.getAffineX()).multiply(point2.getAffineY());
calculating3 = (new BigInteger("1")).subtract(calculating3);
resultY = (calculating1.subtract(calculating2)).divide(calculating3);
resultX = resultX.remainder((BigInteger)constantList.getConstant("pConstant"));
resultY = resultY.remainder((BigInteger)constantList.getConstant("pConstant"));
resultPoint = new ECPoint(resultX, resultY);
return resultPoint;
}
}
BigDecimal 代码
public class ECCprocess2 {
// C1174
public static ECCpoint operate(ECCpoint point1,ECCpoint point2){
ECCpoint resultPoint;
BigDecimal resultX;
BigDecimal resultY;
CONSTANTlist constantList = new CONSTANTlist();
point1.setX(point1.getX().remainder((BigDecimal) constantList.getConstant("pConstant")));
point1.setY(point1.getY().remainder((BigDecimal) constantList.getConstant("pConstant")));
point2.setX(point2.getX().remainder((BigDecimal) constantList.getConstant("pConstant")));
point2.setY(point2.getY().remainder((BigDecimal) constantList.getConstant("pConstant")));
BigDecimal calculating1 = point1.getX().multiply(point2.getY());
BigDecimal calculating2 = point2.getX().multiply(point1.getY());
BigDecimal calculating3 = ((BigDecimal)constantList.getConstant("dConstant")).multiply(point1.getX().multiply(point1.getY()).multiply(point2.getX()).multiply(point2.getY()));
resultX = (calculating1.add(calculating2)).divide(calculating3.add(new BigDecimal("1")),100,RoundingMode.HALF_EVEN);
calculating1 = point1.getY().multiply(point2.getY());
calculating2 = point1.getX().multiply(point2.getX());
calculating3 = ((BigDecimal) constantList.getConstant("dConstant")).multiply(point1.getX().multiply(point1.getY()).multiply(point2.getX()).multiply(point2.getY()));
calculating3 = (new BigDecimal("1")).subtract(calculating3);
resultY = (calculating1.subtract(calculating2)).divide(calculating3,100,RoundingMode.HALF_EVEN);
resultX = resultX.remainder((BigDecimal)constantList.getConstant("pConstant"));
resultY = resultY.remainder((BigDecimal)constantList.getConstant("pConstant"));
resultPoint = new ECCpoint(resultX, resultY);
return resultPoint;
}
}
顺便说一句,这里是 class 我用 BigDecimal
public class ECCpoint {
private BigDecimal x;
private BigDecimal y;
private ECPoint point;
// CONSTRUCTOR
public ECCpoint(BigDecimal x,BigDecimal y){
setX(x);
setY(y);
}
// GETTERS
public BigDecimal getX(){
return this.x;
}
public BigDecimal getY(){
return this.y;
}
// SETTERS
public void setX(BigDecimal x){
this.x = x;
}
public void setY(BigDecimal y){
this.y = y;
}
}
这是我的主要功能及其确切输出
public static void main(String[] args) throws Exception {
ECCpoint result;
ECCpoint point = new ECCpoint(new BigDecimal("2025"), new BigDecimal("588747530266665079407582947937120321357732884331117971504880828350684014295"));
result = ECCprocess2.operate(point, point);
System.out.println(result.getX());
System.out.println(result.getY());
}
run:
-1.4289195326809714896E-81
2.077216903248689341047078303547341201311658613556384884404177594774968601568246831530179235E-10
BUILD SUCCESSFUL (total time: 0 seconds)
您应该为此使用 BigInteger
,而不是 BigDecimal
。但是记住这里的划分是领域内的模块化划分,不是正常的划分。这与模逆相乘相同。
在您的代码中,更改执行除法的两行应该可以满足您的需求。
改变
resultX = (calculating1.add(calculating2)).divide(calculating3);
类似于
resultX = (calculating1.add(calculating2)).multiply(calculating3.modInverse(p));
其中 p
定义为(即它是场质数)
BigInteger p = (BigDecimal) constantList.getConstant("pConstant");
同样改变
resultY = (calculating1.subtract(calculating2)).divide(calculating3);
到
resultY = (calculating1.subtract(calculating2)).multiply(calculating3.modInverse(p));
很难找到关于它的文档,所以我来了。
我正在尝试在 java 中应用 Curve1174 加倍,但出现问题。
当我使用 BigInteger 时,它给了我 (0,0)
当我使用 BigDecimal 时,它给出
Exception in thread “main” java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
我知道我必须使用 divide(blabla , length , RoundingMode) 但我不知道标准的确切长度是多少。
我的代码在下面,你能帮帮我吗?我思考了3天,我快疯了。
大整数代码
public class ECCprocess {
// C1174
// COSTRUCTOR
public static ECPoint operate(ECPoint point1,ECPoint point2){
ECPoint resultPoint;
BigInteger resultX;
BigInteger resultY;
CONSTANTlist constantList = new CONSTANTlist();
BigInteger calculating1 = point1.getAffineX().multiply(point2.getAffineY());
BigInteger calculating2 = point2.getAffineX().multiply(point1.getAffineY());
BigInteger calculating3 = ((BigInteger)constantList.getConstant("dConstant"));
calculating3 = (calculating3.multiply(point1.getAffineX().multiply(point2.getAffineX()).multiply(point1.getAffineY()).multiply(point2.getAffineY()))).add(new BigInteger("1"));
resultX = (calculating1.add(calculating2)).divide(calculating3);
calculating1 = point1.getAffineY().multiply(point2.getAffineY());
calculating2 = point1.getAffineX().multiply(point2.getAffineX());
calculating3 = ((BigInteger)constantList.getConstant("dConstant")).multiply(point1.getAffineX()).multiply(point1.getAffineY()).multiply(point2.getAffineX()).multiply(point2.getAffineY());
calculating3 = (new BigInteger("1")).subtract(calculating3);
resultY = (calculating1.subtract(calculating2)).divide(calculating3);
resultX = resultX.remainder((BigInteger)constantList.getConstant("pConstant"));
resultY = resultY.remainder((BigInteger)constantList.getConstant("pConstant"));
resultPoint = new ECPoint(resultX, resultY);
return resultPoint;
}
}
BigDecimal 代码
public class ECCprocess2 {
// C1174
public static ECCpoint operate(ECCpoint point1,ECCpoint point2){
ECCpoint resultPoint;
BigDecimal resultX;
BigDecimal resultY;
CONSTANTlist constantList = new CONSTANTlist();
point1.setX(point1.getX().remainder((BigDecimal) constantList.getConstant("pConstant")));
point1.setY(point1.getY().remainder((BigDecimal) constantList.getConstant("pConstant")));
point2.setX(point2.getX().remainder((BigDecimal) constantList.getConstant("pConstant")));
point2.setY(point2.getY().remainder((BigDecimal) constantList.getConstant("pConstant")));
BigDecimal calculating1 = point1.getX().multiply(point2.getY());
BigDecimal calculating2 = point2.getX().multiply(point1.getY());
BigDecimal calculating3 = ((BigDecimal)constantList.getConstant("dConstant")).multiply(point1.getX().multiply(point1.getY()).multiply(point2.getX()).multiply(point2.getY()));
resultX = (calculating1.add(calculating2)).divide(calculating3.add(new BigDecimal("1")),100,RoundingMode.HALF_EVEN);
calculating1 = point1.getY().multiply(point2.getY());
calculating2 = point1.getX().multiply(point2.getX());
calculating3 = ((BigDecimal) constantList.getConstant("dConstant")).multiply(point1.getX().multiply(point1.getY()).multiply(point2.getX()).multiply(point2.getY()));
calculating3 = (new BigDecimal("1")).subtract(calculating3);
resultY = (calculating1.subtract(calculating2)).divide(calculating3,100,RoundingMode.HALF_EVEN);
resultX = resultX.remainder((BigDecimal)constantList.getConstant("pConstant"));
resultY = resultY.remainder((BigDecimal)constantList.getConstant("pConstant"));
resultPoint = new ECCpoint(resultX, resultY);
return resultPoint;
}
}
顺便说一句,这里是 class 我用 BigDecimal
public class ECCpoint {
private BigDecimal x;
private BigDecimal y;
private ECPoint point;
// CONSTRUCTOR
public ECCpoint(BigDecimal x,BigDecimal y){
setX(x);
setY(y);
}
// GETTERS
public BigDecimal getX(){
return this.x;
}
public BigDecimal getY(){
return this.y;
}
// SETTERS
public void setX(BigDecimal x){
this.x = x;
}
public void setY(BigDecimal y){
this.y = y;
}
}
这是我的主要功能及其确切输出
public static void main(String[] args) throws Exception {
ECCpoint result;
ECCpoint point = new ECCpoint(new BigDecimal("2025"), new BigDecimal("588747530266665079407582947937120321357732884331117971504880828350684014295"));
result = ECCprocess2.operate(point, point);
System.out.println(result.getX());
System.out.println(result.getY());
}
run:
-1.4289195326809714896E-81
2.077216903248689341047078303547341201311658613556384884404177594774968601568246831530179235E-10
BUILD SUCCESSFUL (total time: 0 seconds)
您应该为此使用 BigInteger
,而不是 BigDecimal
。但是记住这里的划分是领域内的模块化划分,不是正常的划分。这与模逆相乘相同。
在您的代码中,更改执行除法的两行应该可以满足您的需求。
改变
resultX = (calculating1.add(calculating2)).divide(calculating3);
类似于
resultX = (calculating1.add(calculating2)).multiply(calculating3.modInverse(p));
其中 p
定义为(即它是场质数)
BigInteger p = (BigDecimal) constantList.getConstant("pConstant");
同样改变
resultY = (calculating1.subtract(calculating2)).divide(calculating3);
到
resultY = (calculating1.subtract(calculating2)).multiply(calculating3.modInverse(p));