valuate.sage 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # $ sage -sh
  2. # $ pip install tabulate
  3. from tabulate import tabulate
  4. # P = (2, 4)
  5. # ord_P(y - 2x) = 2
  6. # from Washington example 11.4 page 345
  7. K.<x, y> = Integers(11)[]
  8. Px, Py = K(2), K(4)
  9. assert (3*Px^2 + 4) / (2*Py) == 2
  10. basis = [(x - Px), (y - Py), 1]
  11. # Return components for basis
  12. def decomp(f, basis):
  13. comps = []
  14. r = f
  15. for b in basis:
  16. a, r = r.quo_rem(b)
  17. comps.append(a)
  18. assert r == 0
  19. return comps
  20. def comp(comps, basis):
  21. return sum(a*b for a, b in zip(comps, basis))
  22. f = y - 2*x
  23. assert comp(decomp(f, basis), basis) == f
  24. # P = (a, b)
  25. # y² = x³ + Ax + B
  26. # (y - b)(y + b) = (x - a)³ + C(3,2)a(x - a)² + (3a² + A)(x - a)
  27. #
  28. # sage: ((x - a)^3 + binomial(3,2)*a*(x - a)^2 + (3*a^2 + A)*(x - a)).expand()
  29. # -a^3 + x^3 - A*a + A*x
  30. # But since (a, b) ∈ E(K) => b² = a³ + Aa + B
  31. # => B = b² - (a³ + Aa)
  32. #
  33. # So at every step we replace the component for (y - Py)
  34. # with the reduction to the component for (x - Px)
  35. EC_A = 4
  36. EC_B = 0
  37. EC = y^2 - x^3 - A*x - B
  38. # so we can replace (y - Py) with this
  39. sub_poly_f = b0^2 + binomial(3,2)*Px*b0^1 + (3*Px^2 + EC_A)
  40. sub_poly_g = (y + Py)
  41. assert EC == b1*sub_poly_g - b0*sub_poly_f
  42. # f / g
  43. # Technically we don't need g but we keep track of it anyway
  44. def apply_reduction(comp_f, comp_g, basis):
  45. #a1 = comp_f[1]
  46. #comp_f[1] = 0
  47. b0, b1, _ = basis
  48. # b1 == b0 * f / g
  49. # so we can replace c b1 with (cf/g) b0
  50. # a2 = 0
  51. assert comp_f[2] == 0
  52. # note that
  53. # b1 = (f/g) b0
  54. # so
  55. # x = a0 b0 + a1 b1 + 0 b2
  56. # = (a0 + a1 f/g) b0
  57. # let a0 = p/q
  58. # x = (pg + a1 f)
  59. # ----------- b0
  60. # qg
  61. comp_f[0] = comp_f[0]*sub_poly_g + comp_f[1]*sub_poly_f
  62. comp_g[2] *= sub_poly_g
  63. k = 1
  64. table = []
  65. table.append(("", "f", "g", "k"))
  66. def log(step_name, comp_f, comp_g, k):
  67. table.append((step_name, str(comp_f), str(comp_g), k))
  68. comp_f = decomp(f, basis)
  69. comp_g = [0, 0, 1]
  70. log("start", comp_f, comp_g, k)
  71. # Reduce
  72. apply_reduction(comp_f, comp_g, basis)
  73. log("reduce", comp_f, comp_g, k)
  74. f = comp_f[0]
  75. # Decompose
  76. comp_f = decomp(f, basis)
  77. comp_g = [0, 0, 1]
  78. log("decomp", comp_f, comp_g, k)
  79. assert comp(comp_f, basis) == (x - 2)^2 - 5*(x - 2) - 2*(y - 4)
  80. assert comp_f[2] == 0
  81. k += 1
  82. # Reduce
  83. apply_reduction(comp_f, comp_g, basis)
  84. log("reduce", comp_f, comp_g, k)
  85. f = comp_f[0]
  86. # Decompose
  87. comp_f = decomp(f, basis)
  88. comp_g = [0, 0, 1]
  89. log("decomp", comp_f, comp_g, k)
  90. # Program terminates because remainder is nonzero
  91. assert comp_f[2] != 0
  92. print(f"basis = {basis}")
  93. print(tabulate(table))
  94. print(f"k = {k}")