Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. ackage week8ex1;
  2.  
  3. import java.math.BigInteger;
  4.  
  5. /**
  6. *
  7. * @author student
  8. */
  9. public class Week8ex1 {
  10.  
  11. private static BigInteger NaiveCubicRootSearch(BigInteger a, BigInteger left, BigInteger right) {
  12. // fix root as the arithmetic mean of left and right
  13. BigInteger root = left.add(right).shiftRight(1);
  14. // if the root is not between [root, root+1],
  15. //is not an integer and root is our best integer approximation
  16. if(!((root.pow(3).compareTo(a) == -1)&&(root.add(BigInteger.ONE).pow(3).compareTo(a) == 1))){
  17. if (root.pow(3).compareTo(a) == -1) root =
  18. NaiveCubicRootSearch(a, root, right);
  19. if (root.pow(3).compareTo(a) == 1)
  20. root = NaiveCubicRootSearch(a, left, root);
  21. }
  22. return root;
  23. }
  24.  
  25. public static BigInteger CubicRoot(BigInteger a){
  26. return NaiveCubicRootSearch(a, BigInteger.ZERO, a);
  27. }
  28. private static BigInteger NaiveSquareRootSearch(BigInteger a, BigInteger left, BigInteger right) {
  29. // fix root as the arithmetic mean of left and right
  30. BigInteger root = left.add(right).shiftRight(1);
  31. // if the root is not between [root, root+1],
  32. //is not an integer and root is our best integer approximation
  33. if(!((root.pow(2).compareTo(a) == -1)&&(root.add(BigInteger.ONE).pow(2).compareTo(a) == 1))){
  34. if (root.pow(2).compareTo(a) == -1) root =
  35. NaiveSquareRootSearch(a, root, right);
  36. if (root.pow(2).compareTo(a) == 1)
  37. root = NaiveSquareRootSearch(a, left, root);
  38. }
  39. return root;
  40. }
  41.  
  42. public static BigInteger SquareRoot(BigInteger a){
  43. return NaiveSquareRootSearch(a, BigInteger.ZERO, a);
  44. }
  45.  
  46. public static void ex1(){
  47. BigInteger c = new BigInteger("1375865583010982618632308529423371271821438577980922927124130396877925863587827122886875024570556859122064458153631");
  48. System.out.println("Message:\n" + CubicRoot(c));
  49. }
  50.  
  51. public static void ex2(){
  52. BigInteger four = new BigInteger("4");
  53. BigInteger two = new BigInteger("2");
  54.  
  55. BigInteger n = new BigInteger("5076313634899413540120536350051034312987619378778911504647420938544746517711031490115528420427319479274407389058253897498557110913160302801741874277608327");
  56. BigInteger e = new BigInteger("3");
  57. BigInteger d = new BigInteger("3384209089932942360080357566700689541991746252519274336431613959029831011807259226655786125050887727921274719751986104162037800807641522348207376583379547");
  58. //k = (d*e - 1) / n
  59. BigInteger de = d.multiply(e);
  60. BigInteger k = (de.subtract(BigInteger.ONE)).divide(n);
  61.  
  62. BigInteger mymod = (de.subtract(BigInteger.ONE)).mod(n);
  63. if(mymod.compareTo(BigInteger.ZERO) != 0)
  64. k = k.add(BigInteger.ONE);
  65. //s = (k*(n+1)+1-de)/k
  66. BigInteger S = (((k.multiply(n.add(BigInteger.ONE))).add(BigInteger.ONE)).subtract(de)).divide(k);
  67. //x^2 - S*x + n = 0
  68. //delta = S^2 - 4 * n
  69. BigInteger delta = (S.pow(2)).subtract(n.multiply(four));
  70. //fact1,2 = (S +- sqr(delta)/2
  71. BigInteger fact1 = (S.add(SquareRoot(delta))).divide(two);
  72. BigInteger fact2 = (S.subtract(SquareRoot(delta))).divide(two);
  73.  
  74. System.out.println("The factors of the modulus are:\n" + fact1 + "\nand\n" + fact2);
  75. }
  76.  
  77. public static void ex3(){
  78. BigInteger n = new BigInteger("10700646585680885848520503735299852478865837438709815138992859883249955498916287857233627498606657866763592788339595921943627412052904161935201780928478603");
  79. BigInteger x = new BigInteger("7133764390453923899013669156866568319243891625806543425995239922166636999277387253194048505767340924598064169304136210581809906511216168762318630818311867");
  80. System.out.println(x.mod(n));
  81. }
  82.  
  83. /**
  84. * @param args the command line arguments
  85. */
  86. public static void main(String[] args) {
  87. ex1();
  88. //ex2();
  89. //p*q = n
  90. //x^(p+q) mod (p*q)
  91. // x ^ (p+q) = x mod (p+q)
  92. }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement