I tried to use NTL library to implement my cryptographic algorithm. However, it showed me someting wromg about CRT
algorithm. CRT is short for Incremental Chinese Remaindering
and it is defined as following:
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
I try to print some information:
gp=66390828312651972973888361332364813613478546962956830307987136462065800330260786554389230936903970584093871405496526397214600259238471440684133633914352198492704004953513651583287557225250450851122687829965259605518687677556714602317993643325112503010372335796589795208660430772233273662752270218833954206912
p=98506238639786519141405322641812326504550334169475033833322673238957789026078361022953456930946799879629297087483487500221235712451981623924607546413344684294639373306430972887026133598448706655301440705253664704851471711197893571186626487501140015523931128287493592419972025673134065648304689552713586835457
gq=6667000274529267578982370009668139148348778725432024700386402060811738943238338877475151419711063751789939702415071471603295111504118310469114424237809527
q=11378762793182988817702088080680425474862067132459357853502156713056912474438574734825983540828752440930678737903819042152986539729875178831578851503505409
InvMod: inverse undefined
I called CRT
as below:
// ...
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);
From
ntl/ZZ.cpp:1307
, the following comments are given for theCRT
function:In your case, we need
gcd(p, q) = 1
, but with your values,gcd(p, q) = q
, i.e.p = r * q
, whereThe result was obtained using Big Number Calculator online.
In other words,
p
andq
must be relatively prime - but they are not.The test for relative primality is performed within
InvMod
:And
InvMod
is invoked fromCRT
as follows:Since
p
andq
are not relatively prime,rem(p, q) = 0
, and theInvMod(0, q)
invocation fails.