Here we go !
One more test code. no over 256bits bug, returns also symmetric by module result. a little bit fuzzy conditions checking logic, I can call it quasi-recursive. it's binary in it's nature, just realized in a kind strange way.
Part of results:
test# calls# in [out, mod symm out] 0 364 75316636100529471829340583998430204208230490280793297630878645139432980850156 [-21481456315583653538655726848489351884800251925024345069131289520822185938657, 33025674478099610959444009195219596039238050737410647384245105885135854051132] 1 385 39888426508246177183753123701414070167668113673592482698911298409940354217536 [-29960760575314177163053332470999754258247890690156906035102057524026276070769, 86973073792154943490949046208733472044349727771710623997020383424312036814343] 2 360 14620752759159978489146343667071343355650196237127974340021988638599091328303 [12174829735737702474779493260773244294171916983485771075179095797017886235155, -96421093662667652633700306227539987869225167103772496913502848083575395070788] 3 383 102729399071985721675035906741175076856965109198477353566672326776001633036975 [70580439539010164434200376999720640409016804834521829709981617744376762996302, -79555186999421925612988503851178736579078677622324526318628793408380057609471] 4 370 46339676628294076970974548809934900743231066044015694600462844368554430445143 [2539235054366037579706180091206398081627721978267432321188130108233551671292, -6344958648894604413478372293328991555118887139277093896810498521216296850165] 5 368 26660698059602402176427980216008131458879504029132938908230058389025515567933 [16540677529517895161096648910563987919480254183872798943295671897534551358308, -71839064538438794363947864942753805375404185837480918551631678838489697347191] 6 354 594851748071041868996674850995031382743985475622021247651263573480083167890 [-64638328581828718437634047594178337042202536377421456333765449858808997403, 12582306659716172481129154954429736186019811569145338237994747628567568503971] 7 359 80681079014327548209785902873606497791641955222184391014151038138305717861451 [44686311015859290531071619784617096434102136509364594238047222505744964738512, -64133020728639186296951506401894836855168854511276042070060900652427696988205] 8 372 82669520055979716581712547816646165489596227017635063706256738823534854476311 [45051940896140100306048968032786700593661378537963560664532177163878090793890, -63102560133743198571360833063775568154007385207992635050991710591879861205579] 9 360 55943743540139384302837049042449505019549115662377158567347079861108763368228 [-21482989564914934180757589310789560187172090496876602635626422561634142769637, 44465387680038776841839359752521363942810960966395512063353292864302945775019] 10 349 66724607627136654255694803441709862788832479174235937080362394192249729343898 [29069806528358354934610693165080779089541395635574791746246411666180327081765, -50446960294663850907746139715414905993368124572672804919153802516270964965753]
|
#
#
#
As in previous code iterations count close to 384number.
But it's not good iterations indicator. I have idea how to count real true unrolled iterations, but need some time to implement.
#code by hakabit supraspecially for gmaxwell
#algorithm source: https://dspace.cvut.cz/bitstream/handle/10467/77227/F3-DP-2018-Zhumaniezov-Alisher-priloha-Testing.py
import random
def bin_ext_gcd(a, b):
global cc
cc = cc + 1
if a == 0:
#print(a, b, 0, 1)
return [0, 1]
if b == 0:
#print(a, b, 1, 0)
return [1, 0]
if a == b:
#print(a, b, 1, 0)
return [1, 0]
if (a % 2 == 0) & (b % 2 == 0):
x, y = bin_ext_gcd(a // 2, b // 2)
#print(a, b, x, y)
return [x, y]
if a % 2 == 0:
x, y = bin_ext_gcd(a // 2, b)
if x % 2 == 0:
x, y = x // 2, y
else:
x, y = (x + b) // 2, y - a // 2
#print(a, b, x, y)
return [x, y]
if b % 2 == 0:
x, y = bin_ext_gcd(a, b // 2)
if y % 2 == 0:
x, y = x, y // 2
else:
x, y = x - b // 2, (y + a) // 2
#print(a, b, x, y)
return [x, y]
if a > b:
x, y = bin_ext_gcd((a - b) // 2, b)
if x % 2 == 0:
x, y = x // 2, y - x // 2
else:
x, y = (x + b) // 2, y - (a - b) // 2 - (x + b) // 2
#print(a, b, x, y)
return [x, y]
if a < b:
x, y = bin_ext_gcd(a, (b - a) // 2)
if y % 2 == 0:
x, y = x - y // 2, y // 2
else:
x, y = x - (b - a) // 2 - (y + a) // 2, (y + a) // 2
#print(a, b, x, y)
return [x, y]
cc = 0
p = 2**256 - 2**32 - 977
#cc=0
#a=12
#res=bin_ext_gcd(p,a)
#print ("initial test, should be in=12, out=48246703848881748093154577086953294938862493610683568349773993336628681113193")
#print (res,cc)
print ("test# calls# in [out, mod symm out]")
for i in range(100):
#print(i)
ii = random.getrandbits(256)
ii = ii % p
if (ii < 2):
ii = ii + 2
cc = 0
res = bin_ext_gcd(p, ii)
print(i,cc,ii,res)
#end