Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. file = open('numtests.txt', mode = 'r', encoding = 'utf-8-sig')
  2. lines = file.readlines()
  3. file.close()
  4. commands = {"Sum", "Product", "LShift", "LShift1", "ModExp", "Remainder"}
  5. command = ""
  6. result = 0
  7. A = 0
  8. E = 0
  9. B = 0
  10. N = 0
  11. M = 0
  12. ctr = 0
  13.  
  14. def print_mpint(a, A):
  15.     print(a + " dd " + str((A.bit_length() + 7) // 8))
  16.     print("          db ", end='')
  17.     for byte in A.to_bytes(((A.bit_length() + 7) // 8),"little"):
  18.         print(hex(byte) + ", ", end='')
  19.     print("0x0")
  20.     print("          rb MPINT_MAX_LEN - " + str(max((((A.bit_length() + 7) // 8) + 1), 0)))    
  21.    
  22.  
  23. for line in lines:
  24.     words = line.split()
  25.     if line[0] == '#':
  26.         print(";" + line[1:], end='')
  27.     elif words[0] in commands:
  28.         command = words[0]
  29.         result = int(words[2], 16)
  30.         ctr+=1
  31.     elif words[0] == 'A':
  32.         A = int(words[2], 16)
  33.         if (command == 'LShift1'):
  34.             if (A >= 0):
  35.                 print("stdcall mpint_shl1, mpint_A" + str(ctr))
  36.                 print("stdcall mpint_cmp, mpint_A" + str(ctr) + ", mpint_result" + str(ctr))
  37.                 print("je @f")
  38.                 print("mov eax, " + str(ctr))                
  39.                 print("int3")
  40.                 print("@@:")
  41.                 print("iglobal")
  42.                 print_mpint(str("mpint_A" + str(ctr)), A)
  43.                 print_mpint(str("mpint_result" + str(ctr)), result)                
  44.                 print("endg")
  45.                 print()          
  46.     elif words[0] == 'B':
  47.         B = int(words[2], 16)
  48.         if (command == 'Sum'):
  49.             if (A >= 0) & (B >= 0):
  50.                 print("stdcall mpint_add, mpint_B" + str(ctr) + ", mpint_A" + str(ctr))
  51.                 print("stdcall mpint_cmp, mpint_B" + str(ctr) + ", mpint_result" + str(ctr))
  52.                 print("je @f")
  53.                 print("mov eax, " + str(ctr))                
  54.                 print("int3")
  55.                 print("@@:")
  56.                 print("iglobal")
  57.                 print_mpint(str("mpint_A" + str(ctr)), A)
  58.                 print_mpint(str("mpint_B" + str(ctr)), B)
  59.                 print_mpint(str("mpint_result" + str(ctr)), result)                
  60.                 print("endg")
  61.                 print()
  62.             elif (A <= 0) & (B >= 0) & (result >= 0):
  63.                 print("stdcall mpint_sub, mpint_B" + str(ctr) + ", mpint_A" + str(ctr))
  64.                 print("stdcall mpint_cmp, mpint_B" + str(ctr) + ", mpint_result" + str(ctr))
  65.                 print("je @f")
  66.                 print("mov eax, " + str(ctr))                
  67.                 print("int3")
  68.                 print("@@:")
  69.                 print("iglobal")
  70.                 print_mpint(str("mpint_A" + str(ctr)), -A)
  71.                 print_mpint(str("mpint_B" + str(ctr)), B)
  72.                 print_mpint(str("mpint_result" + str(ctr)), result)                
  73.                 print("endg")
  74.                 print()
  75.             elif (A >= 0) & (B <= 0) & (result >= 0):
  76.                 print("stdcall mpint_sub, mpint_A" + str(ctr) + ", mpint_B" + str(ctr))
  77.                 print("stdcall mpint_cmp, mpint_A" + str(ctr) + ", mpint_result" + str(ctr))
  78.                 print("je @f")
  79.                 print("mov eax, " + str(ctr))                
  80.                 print("int3")
  81.                 print("@@:")
  82.                 print("iglobal")
  83.                 print_mpint(str("mpint_A" + str(ctr)), A)
  84.                 print_mpint(str("mpint_B" + str(ctr)), -B)
  85.                 print_mpint(str("mpint_result" + str(ctr)), result)                
  86.                 print("endg")
  87.                 print()                  
  88.         if (command == 'Product'):
  89.             if (A >= 0) & (B >= 0):
  90.                 print("stdcall mpint_mul, mpint_tmp, mpint_B" + str(ctr) + ", mpint_A" + str(ctr))
  91.                 print("stdcall mpint_cmp, mpint_tmp, mpint_result" + str(ctr))
  92.                 print("je @f")
  93.                 print("mov eax, " + str(ctr))                
  94.                 print("int3")
  95.                 print("@@:")
  96.                 print("iglobal")
  97.                 print_mpint(str("mpint_A" + str(ctr)), A)
  98.                 print_mpint(str("mpint_B" + str(ctr)), B)
  99.                 print_mpint(str("mpint_result" + str(ctr)), result)                
  100.                 print("endg")
  101.                 print()
  102.         if (command == 'Remainder'):
  103.             if (A >= 0) & (B >= 0):
  104.                 print("stdcall mpint_mod, mpint_A" + str(ctr) + ", mpint_B" + str(ctr))
  105.                 print("stdcall mpint_cmp, mpint_A" + str(ctr) + ", mpint_result" + str(ctr))
  106.                 print("je @f")
  107.                 print("mov eax, " + str(ctr))                
  108.                 print("int3")
  109.                 print("@@:")
  110.                 print("iglobal")
  111.                 print_mpint(str("mpint_A" + str(ctr)), A)
  112.                 print_mpint(str("mpint_B" + str(ctr)), B)
  113.                 print_mpint(str("mpint_result" + str(ctr)), result)                
  114.                 print("endg")
  115.                 print()                  
  116.     elif words[0] == 'N':
  117.         N = int(words[2], 16)
  118.         if (command == 'LShift'):
  119.             if (A >= 0):
  120.                 print("stdcall mpint_shlmov, mpint_tmp, mpint_A" + str(ctr) + ", " + str(N))
  121.                 print("stdcall mpint_cmp, mpint_tmp, mpint_result" + str(ctr))
  122.                 print("je @f")
  123.                 print("mov eax, " + str(ctr))                
  124.                 print("int3")
  125.                 print("@@:")                
  126.                 print("stdcall mpint_shl, mpint_A" + str(ctr) + ", " + str(N))
  127.                 print("stdcall mpint_cmp, mpint_A" + str(ctr) + ", mpint_result" + str(ctr))
  128.                 print("je @f")
  129.                 print("mov eax, " + str(ctr))                
  130.                 print("int3")
  131.                 print("@@:")
  132.                 print("iglobal")
  133.                 print_mpint(str("mpint_A" + str(ctr)), A)
  134.                 print_mpint(str("mpint_result" + str(ctr)), result)                
  135.                 print("endg")
  136.                 print()
  137.     elif words[0] == 'E':
  138.         E = int(words[2], 16)
  139.     elif words[0] == 'M':
  140.         M = int(words[2], 16)
  141.         if (command == 'ModExp'):
  142.             if (A >= 0) & (B >= 0):
  143.                 print("stdcall mpint_modexp, mpint_tmp, mpint_A" + str(ctr) + ", mpint_E" + str(ctr) + ", mpint_M" + str(ctr))
  144.                 print("stdcall mpint_cmp, mpint_tmp, mpint_result" + str(ctr))
  145.                 print("je @f")
  146.                 print("mov eax, " + str(ctr))                
  147.                 print("int3")
  148.                 print("@@:")
  149.                 print("iglobal")
  150.                 print_mpint(str("mpint_A" + str(ctr)), A)
  151.                 print_mpint(str("mpint_E" + str(ctr)), E)
  152.                 print_mpint(str("mpint_M" + str(ctr)), M)                
  153.                 print_mpint(str("mpint_result" + str(ctr)), result)                
  154.                 print("endg")
  155.                 print()          
  156.     else:
  157.         command = ''
  158.