为什么在NTL库中使用CRT算法时显示"InvMod: inverse undefined"?
Why did it show "InvMod: inverse undefined" when used CRT algorithm in NTL library?
我尝试使用 NTL 库来实现我的密码算法。但是,它向我展示了一些关于 CRT
算法的问题。 CRT是Incremental Chinese Remaindering
的缩写,定义如下:
long CRT(ZZ& a, ZZ& p, const ZZ& A, const ZZ& P);
long CRT(ZZ& a, ZZ& p, long A, long P);
// 0 <= A < P, (p, P) = 1; computes a' such that a' = a mod p,
// a' = A mod P, and -p*P/2 < a' <= p*P/2; sets a := a', p := p*P, and
// returns 1 if a's value has changed, otherwise 0
我尝试打印一些信息:
gp=66390828312651972973888361332364813613478546962956830307987136462065800330260786554389230936903970584093871405496526397214600259238471440684133633914352198492704004953513651583287557225250450851122687829965259605518687677556714602317993643325112503010372335796589795208660430772233273662752270218833954206912
p=98506238639786519141405322641812326504550334169475033833322673238957789026078361022953456930946799879629297087483487500221235712451981623924607546413344684294639373306430972887026133598448706655301440705253664704851471711197893571186626487501140015523931128287493592419972025673134065648304689552713586835457
gq=6667000274529267578982370009668139148348778725432024700386402060811738943238338877475151419711063751789939702415071471603295111504118310469114424237809527
q=11378762793182988817702088080680425474862067132459357853502156713056912474438574734825983540828752440930678737903819042152986539729875178831578851503505409
InvMod: inverse undefined
我调用 CRT
如下:
// ...
std::cout << "gp=" << gp << std::endl;
std::cout << "p=" << p << std::endl;
std::cout << "gq=" << gq << std::endl;
std::cout << "q=" << q << std::endl;
NTL::CRT(gp, p, gq, q);
来自ntl/ZZ.cpp:1307
,针对CRT
函数给出了以下评论:
// Chinese Remaindering.
[...]
// This function takes as input g, a, G, p,
// such that a > 0, 0 <= G < p, and gcd(a, p) = 1.
[...]
long CRT(ZZ& gg, ZZ& a, long G, long p)
在您的情况下,我们需要 gcd(p, q) = 1
,但使用您的值 gcd(p, q) = q
,即 p = r * q
,其中
r = 8657025410425249234452045797441790045805303296607662\
201360892817104202207969817487169683905119008751440550350704\
462002980862924347497458107362187486953473
结果是使用Big Number Calculator online获得的。
换句话说,p
和 q
必须互质 - 但它们不是。
相对素数测试在 InvMod
:
内执行
long InvMod(long a, long n)
{
long d, s, t;
XGCD(d, s, t, a, n);
if (d != 1) {
InvModError("InvMod: inverse undefined");
}
if (s < 0)
return s + n;
else
return s;
}
而InvMod
是invoked from CRT
as follows:
long CRT(ZZ& gg, ZZ& a, long G, long p)
{
if (p >= NTL_SP_BOUND) {
ZZ GG, pp;
conv(GG, G);
conv(pp, p);
return CRT(gg, a, GG, pp);
}
[...]
long a_inv;
a_inv = rem(a, p);
a_inv = InvMod(a_inv, p);
[...]
}
由于 p
和 q
不互质,rem(p, q) = 0
,InvMod(0, q)
调用失败。
我尝试使用 NTL 库来实现我的密码算法。但是,它向我展示了一些关于 CRT
算法的问题。 CRT是Incremental Chinese Remaindering
的缩写,定义如下:
long CRT(ZZ& a, ZZ& p, const ZZ& A, const ZZ& P);
long CRT(ZZ& a, ZZ& p, long A, long P);
// 0 <= A < P, (p, P) = 1; computes a' such that a' = a mod p,
// a' = A mod P, and -p*P/2 < a' <= p*P/2; sets a := a', p := p*P, and
// returns 1 if a's value has changed, otherwise 0
我尝试打印一些信息:
gp=66390828312651972973888361332364813613478546962956830307987136462065800330260786554389230936903970584093871405496526397214600259238471440684133633914352198492704004953513651583287557225250450851122687829965259605518687677556714602317993643325112503010372335796589795208660430772233273662752270218833954206912
p=98506238639786519141405322641812326504550334169475033833322673238957789026078361022953456930946799879629297087483487500221235712451981623924607546413344684294639373306430972887026133598448706655301440705253664704851471711197893571186626487501140015523931128287493592419972025673134065648304689552713586835457
gq=6667000274529267578982370009668139148348778725432024700386402060811738943238338877475151419711063751789939702415071471603295111504118310469114424237809527
q=11378762793182988817702088080680425474862067132459357853502156713056912474438574734825983540828752440930678737903819042152986539729875178831578851503505409
InvMod: inverse undefined
我调用 CRT
如下:
// ...
std::cout << "gp=" << gp << std::endl;
std::cout << "p=" << p << std::endl;
std::cout << "gq=" << gq << std::endl;
std::cout << "q=" << q << std::endl;
NTL::CRT(gp, p, gq, q);
来自ntl/ZZ.cpp:1307
,针对CRT
函数给出了以下评论:
// Chinese Remaindering. [...] // This function takes as input g, a, G, p, // such that a > 0, 0 <= G < p, and gcd(a, p) = 1. [...] long CRT(ZZ& gg, ZZ& a, long G, long p)
在您的情况下,我们需要 gcd(p, q) = 1
,但使用您的值 gcd(p, q) = q
,即 p = r * q
,其中
r = 8657025410425249234452045797441790045805303296607662\
201360892817104202207969817487169683905119008751440550350704\
462002980862924347497458107362187486953473
结果是使用Big Number Calculator online获得的。
换句话说,p
和 q
必须互质 - 但它们不是。
相对素数测试在 InvMod
:
long InvMod(long a, long n)
{
long d, s, t;
XGCD(d, s, t, a, n);
if (d != 1) {
InvModError("InvMod: inverse undefined");
}
if (s < 0)
return s + n;
else
return s;
}
而InvMod
是invoked from CRT
as follows:
long CRT(ZZ& gg, ZZ& a, long G, long p)
{
if (p >= NTL_SP_BOUND) {
ZZ GG, pp;
conv(GG, G);
conv(pp, p);
return CRT(gg, a, GG, pp);
}
[...]
long a_inv;
a_inv = rem(a, p);
a_inv = InvMod(a_inv, p);
[...]
}
由于 p
和 q
不互质,rem(p, q) = 0
,InvMod(0, q)
调用失败。