You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
926 B

  1. package ownrsa
  2. import "math/rand"
  3. func randInt(min int, max int) int {
  4. r := rand.Intn(max-min) + min
  5. return r
  6. }
  7. func randPrime(min int, max int) int {
  8. primes := sieveOfEratosthenes(max)
  9. randN := rand.Intn(len(primes)-0) + 0
  10. return primes[randN]
  11. }
  12. // return list of primes less than N
  13. func sieveOfEratosthenes(N int) (primes []int) {
  14. b := make([]bool, N)
  15. for i := 2; i < N; i++ {
  16. if b[i] == true {
  17. continue
  18. }
  19. primes = append(primes, i)
  20. for k := i * i; k < N; k += i {
  21. b[k] = true
  22. }
  23. }
  24. return
  25. }
  26. func gcd(a, b int) int {
  27. var bgcd func(a, b, res int) int
  28. bgcd = func(a, b, res int) int {
  29. switch {
  30. case a == b:
  31. return res * a
  32. case a%2 == 0 && b%2 == 0:
  33. return bgcd(a/2, b/2, 2*res)
  34. case a%2 == 0:
  35. return bgcd(a/2, b, res)
  36. case b%2 == 0:
  37. return bgcd(a, b/2, res)
  38. case a > b:
  39. return bgcd(a-b, b, res)
  40. default:
  41. return bgcd(a, b-a, res)
  42. }
  43. }
  44. return bgcd(a, b, 1)
  45. }