bigint-demo.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env lua
  2. -- Sorry for how ugly this script is! It's not really meant to be maintained.
  3. local bigint = require("bigint")
  4. local bigint_extra = require("bigint-extra")
  5. local comparisons = {">", "<", "==", "~=", ">=", "<="}
  6. math.randomseed(os.time())
  7. function printf(...)
  8. print(string.format(...))
  9. end
  10. function demo_serialize_unserialize(n)
  11. printf("bigint.unserialize(bigint.new(%s)) -> %s", tostring(n), tostring(bigint.unserialize(bigint.new(n), "string")))
  12. end
  13. function demo_compare(n1, n2, cmp)
  14. cmp = cmp or comparisons[math.random(#comparisons)]
  15. printf("bigint.compare(%s, %s, %s) -> %s", tostring(n1), tostring(n2), cmp, (bigint.compare(bigint.new(n1), bigint.new(n2), cmp) and "true" or "false"))
  16. end
  17. function demo_add(n1, n2)
  18. printf("bigint.add(%s, %s) -> %s", tostring(n1), tostring(n2), tostring(bigint.unserialize(bigint.add(bigint.new(n1), bigint.new(n2)), "string")))
  19. end
  20. function demo_subtract(n1, n2)
  21. printf("bigint.subtract(%s, %s) -> %s", tostring(n1), tostring(n2), tostring(bigint.unserialize(bigint.subtract(bigint.new(n1), bigint.new(n2)), "string")))
  22. end
  23. function demo_multiply(n1, n2)
  24. printf("bigint.multiply(%s, %s) -> %s", tostring(n1), tostring(n2), tostring(bigint.unserialize(bigint.multiply(bigint.new(n1), bigint.new(n2)), "string")))
  25. end
  26. function demo_exponentiate(n1, n2)
  27. printf("bigint.exponentiate(%s, %s) -> %s", tostring(n1), tostring(n2), tostring(bigint.unserialize(bigint.exponentiate(bigint.new(n1), bigint.new(n2)), "string")))
  28. end
  29. function demo_divide(n1, n2)
  30. local result, remainder = bigint.divide(bigint.new(n1), bigint.new(n2))
  31. printf("bigint.divide(%s, %s) -> %s R %s", tostring(n1), tostring(n2), tostring(bigint.unserialize(result), "string"), tostring(bigint.unserialize(remainder), "string"))
  32. end
  33. function demo_random(n1, n2)
  34. if (n2) then
  35. printf("bigint.random(%s, %s) -> %s", tostring(n1), tostring(n2), tostring(bigint.unserialize(bigint.random(bigint.new(n1), bigint.new(n2)), "string")))
  36. else
  37. printf("bigint.random(%s) -> %s", tostring(n1), tostring(bigint.unserialize(bigint.random(bigint.new(n1)), "string")))
  38. end
  39. end
  40. print("-- Serialize number")
  41. demo_serialize_unserialize(math.random(100))
  42. print("-- Serialize string")
  43. demo_serialize_unserialize("-" .. tostring(math.random(100)))
  44. print()
  45. print("-- Random comparison of two positives")
  46. demo_compare(math.random(100), math.random(100))
  47. print("-- Random comparison of two negatives")
  48. demo_compare(math.random(-100, 0), math.random(-100, 0))
  49. print("-- Random comparison of a positive and a negative")
  50. demo_compare(math.random(-100, 0), math.random(100))
  51. print()
  52. print("-- Add two positives")
  53. demo_add(math.random(100), math.random(100))
  54. print("-- Add two negatives")
  55. demo_add(math.random(-100, 0), math.random(-100, 0))
  56. print("-- Add a negative and a positive")
  57. demo_add(math.random(-100, 0), math.random(100))
  58. print()
  59. print("-- Raise a positive to a power")
  60. demo_exponentiate(math.random(100), math.random(100))
  61. print("-- Raise a negative to a power")
  62. demo_exponentiate(math.random(-100, 0), math.random(100))
  63. print()
  64. print("-- Subtract two positives")
  65. demo_subtract(math.random(100), math.random(100))
  66. print("-- Subtract two negatives")
  67. demo_subtract(math.random(-100, 0), math.random(-100, 0))
  68. print("-- Subtract a positive from a negative")
  69. demo_subtract(math.random(-100, 0), math.random(100))
  70. print("-- Subtract a negative from a positive")
  71. demo_subtract(math.random(100), math.random(-100, 0))
  72. print()
  73. print("-- Multiply two positives")
  74. demo_multiply(math.random(100), math.random(100))
  75. print("-- Multiply two negatives")
  76. demo_multiply(math.random(-100, 0), math.random(-100, 0))
  77. print("-- Multiply a negative and a positive")
  78. demo_multiply(math.random(-100, 0), math.random(100))
  79. print("-- Multiply by zero")
  80. demo_multiply(0, math.random(100))
  81. print("-- Multiply two very large numbers: RSA-1536 * RSA-2048")
  82. rsa_1536 = "1847699703211741474306835620200164403018549338663410171471785774910651696711161249859337684305435744585616061544571794052229717732524660960646946071249623720442022269756756687378427562389508764678440933285157496578843415088475528298186726451339863364931908084671990431874381283363502795470282653297802934916155811881049844908319545009848393775227257052578591944993870073695755688436933812779613089230392569695253261620823676490316036551371447913932347169566988069"
  83. rsa_2048 = "25195908475657893494027183240048398571429282126204032027777137836043662020707595556264018525880784406918290641249515082189298559149176184502808489120072844992687392807287776735971418347270261896375014971824691165077613379859095700097330459748808428401797429100642458691817195118746121515172654632282216869987549182422433637259085141865462043576798423387184774447920739934236584823824281198163815010674810451660377306056201619676256133844143603833904414952634432190114657544454178424020924616515723350778707749817125772467962926386356373289912154831438167899885040445364023527381951378636564391212010397122822120720357"
  84. rsa_mult = bigint.unserialize(bigint.multiply(bigint.new(rsa_1536), bigint.new(rsa_2048)), "string")
  85. os.execute("echo -n 'Checking answer with GNU arbitrary precision calculator... '; [ $(bc <<< " .. rsa_1536 .. "*" .. rsa_2048 .. " | tr -d '" .. '\n\\' .. "' 2> /dev/null) = " .. rsa_mult .. " ] && echo success || echo failure")
  86. print()
  87. print("-- Divide two positives")
  88. demo_divide(math.random(10000), math.random(100))
  89. print("-- Divide two negatives")
  90. demo_divide(math.random(-10000, 0), math.random(-100, 0))
  91. print("-- Divide a negative and a positive")
  92. demo_divide(math.random(-10000, 0), math.random(100))
  93. print("-- (Cannot demo divide by zero because of assertion failure)")
  94. print()
  95. print("-- Unserialize large random number to a number")
  96. print(bigint.unserialize(bigint_extra.random(bigint.new("100000000000000000000000000000000000000000000000000000000000000000000")), "number"))
  97. print("-- Unserialize large random number to a string")
  98. print(bigint.unserialize(bigint_extra.random(bigint.new("100000000000000000000000000000000000000000000000000000000000000000000")), "string"))
  99. print("-- Unserialize large random number to a human-readable string")
  100. print(bigint.unserialize(bigint_extra.random(bigint.new("100000000000000000000000000000000000000000000000000000000000000000000")), "human-readable"))
  101. print("-- Unserialize large random number to a scientific notation string")
  102. print(bigint.unserialize(bigint_extra.random(bigint.new("100000000000000000000000000000000000000000000000000000000000000000000")), "scientific"))
  103. --[[
  104. print("BUGGY RANDOM NUMBER GENERATOR")
  105. print("-- Generate a random positive with one argument")
  106. demo_random(10)
  107. print("-- Generate a random negative")
  108. demo_random(-100, -90)
  109. demo_subtract(-100, -90)
  110. print("-- Generate a random negative or positive")
  111. demo_random(-100, 100)
  112. print()
  113. --]]
  114. print("-- Unserialize 1000000000000 (1 Trillion) to a human-readable string")
  115. print(bigint.unserialize(bigint.new(1000000000000),"human-readable"))