簡體   English   中英

Java Pow BigInteger實現

[英]Java pow BigInteger implementation

我正在研究密碼學實現,部分設計包括以下內容:

((y ^ a)^ b /(y ^ c)^ b)mod p

我有以下片段:

BigInteger yab = y.pow(ab.intValue());
BigInteger ycb = y.pow(cb.intValue());

BigInteger ans = (yab.divide(ycb)).mod(p);

對於小整數,它可以正常工作。 一旦用生成的鍵替換了它,指數就變得如此巨大,並且我將遇到“ BigInteger int range out”錯誤。 我嘗試了modPow函數,但結果不同。

我知道將其強制轉換為int有其局限性。 這是否意味着我的實施不可行?

似乎您正在分組中進行模塊化算術 在此處輸入圖片說明 其中n是素數(在您的情況下為n = p )。 這意味着

x / y

是不是一個分裂但x的與y -1(Y模塊化倒數)相乘。

好東西是BigInteger類提供了這樣一種方法:

BigInteger ans = yab.multiply(ycb.modInverse(p)).mod(p);

其中yabycb可以有效地計算而不會溢出(假設abab的乘積):

BigInteger yab = y.modPow(ab, p);
BigInteger ycb = y.modPow(cb, p);

您可以簡化代碼,這也可以使其更快

x^y / x^z = x^(y - z)

所以

BigInteger yab = y.pow(ab.intValue());
BigInteger ycb = y.pow(cb.intValue());

BigInteger ans = (yab.divide(ycb)).mod(p);

可以簡化為

BigInteger yabc = y.pow((int) (ab.longValue() - cb.longValue()));
BigInteger ans = yabc.mod(p);

要么

BigInteger and = y.modPow(ab.minus(cb), p);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM