/js/lib/Socket.IO-node/support/socket.io-client/lib/vendor/web-socket-js/flash-src/com/hurlant/math/BarrettReduction.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs · ActionScript · 90 lines · 63 code · 8 blank · 19 comment · 8 complexity · 1d39861de5cc32c15f8467c6dccb319d MD5 · raw file

  1. package com.hurlant.math
  2. {
  3. use namespace bi_internal;
  4. internal class BarrettReduction implements IReduction
  5. {
  6. private var m:BigInteger;
  7. private var r2:BigInteger;
  8. private var q3:BigInteger;
  9. private var mu:BigInteger;
  10. public function BarrettReduction(m:BigInteger) {
  11. // setup Barrett
  12. r2 = new BigInteger;
  13. q3 = new BigInteger;
  14. BigInteger.ONE.dlShiftTo(2*m.t, r2);
  15. mu = r2.divide(m);
  16. this.m = m;
  17. }
  18. public function revert(x:BigInteger):BigInteger
  19. {
  20. return x;
  21. }
  22. /**
  23. *
  24. * @param x
  25. * @param y
  26. * @param r = x*y mod m; x != r
  27. *
  28. */
  29. public function mulTo(x:BigInteger, y:BigInteger, r:BigInteger):void
  30. {
  31. x.multiplyTo(y, r);
  32. reduce(r);
  33. }
  34. /**
  35. *
  36. * @param x
  37. * @param r = x^2 mod m; x != r
  38. *
  39. */
  40. public function sqrTo(x:BigInteger, r:BigInteger):void
  41. {
  42. x.squareTo(r);
  43. reduce(r);
  44. }
  45. public function convert(x:BigInteger):BigInteger
  46. {
  47. if (x.s<0 || x.t>2*m.t) {
  48. return x.mod(m);
  49. } else if (x.compareTo(m)<0) {
  50. return x;
  51. } else {
  52. var r:BigInteger = new BigInteger;
  53. x.copyTo(r);
  54. reduce(r);
  55. return r;
  56. }
  57. }
  58. /**
  59. *
  60. * @param x = x mod m (HAC 14.42)
  61. *
  62. */
  63. public function reduce(lx:BigInteger):void
  64. {
  65. var x:BigInteger = lx as BigInteger;
  66. x.drShiftTo(m.t-1,r2);
  67. if (x.t>m.t+1) {
  68. x.t = m.t+1;
  69. x.clamp();
  70. }
  71. mu.multiplyUpperTo(r2, m.t+1, q3);
  72. m.multiplyLowerTo(q3, m.t+1, r2);
  73. while (x.compareTo(r2)<0) {
  74. x.dAddOffset(1, m.t+1);
  75. }
  76. x.subTo(r2,x);
  77. while (x.compareTo(m)>=0) {
  78. x.subTo(m,x);
  79. }
  80. }
  81. }
  82. }