bn_test.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. new sjcl.test.TestCase("Bignum modular reduction test", function (cb) {
  2. if (!sjcl.bn) {
  3. this.unimplemented();
  4. cb && cb();
  5. return;
  6. }
  7. var a, N, r;
  8. for (i=0; i < sjcl.test.vector.bn_mod.length; i++) {
  9. tv = sjcl.test.vector.bn_mod[i];
  10. try {
  11. a = new sjcl.bn(tv.a);
  12. N = new sjcl.bn(tv.N);
  13. r = a.mod(N);
  14. this.require(r.equals(new sjcl.bn(tv.r)));
  15. } catch(e) {
  16. this.fail(e);
  17. }
  18. }
  19. cb && cb();
  20. });
  21. new sjcl.test.TestCase("Bignum modular multiplication test", function (cb) {
  22. if (!sjcl.bn) {
  23. this.unimplemented();
  24. cb && cb();
  25. return;
  26. }
  27. var a, b, N, r;
  28. for(var j=0;j<10;j++)for (i=0; i < sjcl.test.vector.bn_mulmod.length; i++) {
  29. tv = sjcl.test.vector.bn_mulmod[i];
  30. try {
  31. a = new sjcl.bn(tv.a);
  32. b = new sjcl.bn(tv.b);
  33. N = new sjcl.bn(tv.N);
  34. r = a.mulmod(b, N);
  35. this.require(r.equals(new sjcl.bn(tv.r)));
  36. } catch(e) {
  37. this.fail(e);
  38. }
  39. }
  40. cb && cb();
  41. });
  42. new sjcl.test.TestCase("Bignum modular exponentiation test", function (cb) {
  43. if (!sjcl.bn) {
  44. this.unimplemented();
  45. cb && cb();
  46. return;
  47. }
  48. var i, tv, g, x, N, v;
  49. for (i=0; i < sjcl.test.vector.bn_powermod.length; i++) {
  50. tv = sjcl.test.vector.bn_powermod[i];
  51. try {
  52. g = new sjcl.bn(tv.g);
  53. x = new sjcl.bn(tv.x);
  54. N = new sjcl.bn(tv.N);
  55. v = g.powermod(x, N);
  56. this.require(v.equals(new sjcl.bn(tv.v)));
  57. } catch(e) {
  58. this.fail(e);
  59. }
  60. }
  61. cb && cb();
  62. });