为什么 Java 在 BigInteger 的 mag 字段中不使用 long[]?

huangapple go评论49阅读模式
英文:

Why doesn't java use long[] in BigInteger's mag field?

问题

64位JVM中,我相信long[]比int[]更有效率,可以显著加快RSA操作速度。

在long[]中存在Carry问题,但我们可以使用一些本地方法将long强制为无符号,例如long z = u64add(x, y, cr),这里的cr是一个boolean[],可以替代long z = x + y

英文:

I believe in 64-bit JVM long[] is much more efficient than int[], and it can significantly speed up RSA operations.

Carry is a problem in long[], but we can use some native methods to force long as unsigned, e.g. long z = u64add(x, y, cr), here cr is a boolean[], can replace long z = x + y.

答案1

得分: 5

BigInteger 支持 multiply(),而不仅仅是 add()。由于 multiply() 是更昂贵的操作,因此对算法来说,它的速度也是最重要的。

将两个 64 位整数相乘会得到一个 128 位结果。在 Java 中没有 128 位整数类型,所以要获得正确的结果不仅仅是“执行操作并移动一些位”。您需要将这两个数字拆分成 4 个 32 位部分,将它们相乘,然后组合结果。或者添加一个 JVM 内部函数,以便可以使用本机的 128 位操作。并不是说它不能做到,而更多是成本效益的问题。

英文:

BigInteger supports multiply(), not just add(). Since multiply() is the more expensive operation, its speed is also what matters most for algorithms.

Multiplying two 64-bit integers gives a 128-bit result. There is no 128-bit integer type in Java, so getting the correct result isn't just "do the operation and shift some bits." You would have to split the two numbers into 4 32-bit pieces, multiply those, and then compose the result. Or add a JVM intrinsic so that a native 128-bit operation could be used. It's not that it can't be done, it's more of a cost-benefit thing.

huangapple
  • 本文由 发表于 2020年8月2日 21:22:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63216502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定