aboutsummaryrefslogtreecommitdiff
path: root/javascript/BigInteger.js
blob: f495fd38e4cd8a2dc2b527223a7be480f1223b88 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
 * @license
 * Copyright 2017 Google Inc. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @fileoverview Defines interfaces for big numbers.
 * All arithmetic operations are inherits from e2e.BigNum.
 */
goog.provide('wycheproof.BigInteger');
goog.provide('wycheproof.BigPrimeInteger');
goog.require('e2e.BigNum');
goog.require('e2e.BigPrimeNum');
goog.require('goog.crypt');
goog.require('goog.crypt.base64');
goog.require('goog.testing.asserts');
goog.require('wycheproof.TestUtil');

var TestUtil = wycheproof.TestUtil;



/**
 * Non-negative arbitrary-precision integers.
 * @param {(!Array<number>|!Uint8Array)=} optValue
 *     The value of the BigInteger in big endian.
 * @constructor
 * @extends {e2e.BigNum}
 */
wycheproof.BigInteger = function(optValue) {
   wycheproof.BigInteger.base(this, 'constructor', optValue);
};
goog.inherits(wycheproof.BigInteger, e2e.BigNum);


/**
 * A factory method to create a BigInteger from a string that is
 * in big-endian, two's complement and hex representation.
 * @param {!string} s The string representation of the BigInteger
 *
 * @return {!BigInteger}
 */
wycheproof.BigInteger.fromHex = function(s) {
  assertTrue('Input is not a hex string', TestUtil.isHex(s));
  if (s.length % 2 == 1) {
    s = '0' + s;
  }
  var bytes = goog.crypt.hexToByteArray(s);
  var bigInt = new wycheproof.BigInteger(bytes);
  return bigInt.dropLeadingZeros();
};

/**
 * Converts this to a big-endian Base64URL string.
 * @param {!number} optValue An optional value that specifies the length in
 * bytes of the result. If it is bigger than the original length of this number,
 * zeros are added to its beginning.
 *
 * @return {!string}
 */
wycheproof.BigInteger.prototype.toBase64Url = function(optValue) {
  var bytes = this.toByteArray();
  if (optValue !== undefined && optValue > bytes.length) {
    var addingZeros = new Array(optValue-bytes.length).fill(0);
    bytes = addingZeros.concat(bytes);
  }
  var b64Str = goog.crypt.base64.encodeByteArray(bytes);
  return TestUtil.base64ToBase64Url(b64Str);
};


/**
 * Converts this to an ArrayBuffer of big-endian bytes
 * @param {!number} optValue An optional value that specifies the length in
 * bytes of the result. If it is bigger than the original length of this number,
 * zeros are added to its beginning.
 *
 * @return {!ArrayBuffer}
 */
wycheproof.BigInteger.prototype.toArrayBuffer = function(optValue) {
  var bytes = this.toByteArray();
  if (optValue !== undefined && optValue > bytes.length) {
    var addingZeros = new Array(optValue-bytes.length).fill(0);
    bytes = addingZeros.concat(bytes);
  }
  return new Uint8Array(bytes).buffer;
};


/**
 * Odd prime big integer that could be use as the modulus in modular arithmetic
 * operations in crypto schemes such as ECDSA or ECDH.
 * @param {!Array<number>|!Uint8Array} modulus The modulus to use.
 * @constructor
 * @extends {e2e.BigPrimeNum}
 */
wycheproof.BigPrimeInteger = function(modulus) {
   wycheproof.BigPrimeInteger.base(this, 'constructor', modulus);
};
goog.inherits(wycheproof.BigPrimeInteger, e2e.BigPrimeNum);