summaryrefslogtreecommitdiffstats
path: root/test/bn_test.js
blob: a669991380015f6d26ad091e430254875a241992 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
new sjcl.test.TestCase("Bignum modular reduction test", function (cb) {
  if (!sjcl.bn) {
    this.unimplemented();
    cb && cb();
    return;
  }

  var a, N, r;
  for (i=0; i < sjcl.test.vector.bn_mod.length; i++) {
    tv = sjcl.test.vector.bn_mod[i];
    try {
      a = new sjcl.bn(tv.a);
      N = new sjcl.bn(tv.N);
      r = a.mod(N);
      this.require(r.equals(new sjcl.bn(tv.r)));
    } catch(e) {
      this.fail(e);
    }
  }
  cb && cb();
});

new sjcl.test.TestCase("Bignum modular multiplication test", function (cb) {
  if (!sjcl.bn) {
    this.unimplemented();
    cb && cb();
    return;
  }

  var a, b, N, r;
  for(var j=0;j<10;j++)for (i=0; i < sjcl.test.vector.bn_mulmod.length; i++) {
      tv = sjcl.test.vector.bn_mulmod[i];
    try {
      a = new sjcl.bn(tv.a);
      b = new sjcl.bn(tv.b);
      N = new sjcl.bn(tv.N);
      r = a.mulmod(b, N);
      this.require(r.equals(new sjcl.bn(tv.r)));
    } catch(e) {
      this.fail(e);
    }
  }
  cb && cb();
});

new sjcl.test.TestCase("Bignum modular exponentiation test", function (cb) {
  if (!sjcl.bn) {
    this.unimplemented();
    cb && cb();
    return;
  }

  var i, tv, g, x, N, v;
  for (i=0; i < sjcl.test.vector.bn_powermod.length; i++) {
    tv = sjcl.test.vector.bn_powermod[i];
    try {
      g = new sjcl.bn(tv.g);
      x = new sjcl.bn(tv.x);
      N = new sjcl.bn(tv.N);
      v = g.powermod(x, N);
      this.require(v.equals(new sjcl.bn(tv.v)));
    } catch(e) {
      this.fail(e);
    }
  }
  cb && cb();
});

new sjcl.test.TestCase("Bignum toString test", function (cb) {
  if (!sjcl.bn) {
    this.unimplemented();
    cb && cb();
    return;
  }
  this.require((new sjcl.bn(12312434)).power(10).toString() ===
    '0xb99c06973dcc72429aa1dd41b0bc40a424289a05d3d72f066ee4e71c400');
  cb && cb();
});