rabin_encryption_method.sf 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/ruby
  2. /*
  3. Rabin's protocol is equivalent to factoring: Suppose you have a procedure P
  4. which, given a quadratic residue, gives one of its square roots mod pq. The
  5. four nsquare roots of a quadratic residue y=x^2 mod pq is -x, x, -gamma x,
  6. gamma x, where gamma is the nontrivial square root of unity mod pq.
  7. Aside: you can find gamma if you know p and q by using the Chinese
  8. Remainder Theorem (CRT) and solving the system of equations
  9. x = -1 mod p
  10. x = 1 mod q
  11. [ You can see where the other square roots of unity comes from: they are the
  12. other possible patterns of signs on the 1's in the system of eqns for CRT. ]
  13. Now, given P, you choose a random r between 1 and pq-1 inclusive and compute
  14. y = P(r^2). With 1/2 probability, y = +/- gamma r. Since you knew r, you
  15. can find g = y/r = +/- gamma. Now, since g-1 is either 0 mod q or 0 mod p,
  16. so GCD(g-1,pq) will give you p or q.
  17. [ To find 1/r mod pq, use EGCD: The extended Euclidean algorithm, given
  18. m,n, will find GCD(m,n) as well as the pair a,b such that am+bn=GCD(m,n).
  19. When GCD(m,n)=1, we have a=1/m mod n. ]
  20. Note that this can be simplfied a little, since with very high probability r
  21. does not divide pq: r(g-1) = r(y/r - 1) = y - r, so GCD(y-r,pq) will work
  22. just as well. If r divides pq, you've already (accidentally) factored the
  23. modulus.
  24. */
  25. # See also:
  26. # https://ratthing.com/files/textfiles.com/programming/CRYPTOGRAPHY/rabin-al.txt
  27. # TODO: implement it