@ivujs/i-utils 1.1.1 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/README.md +17 -19
  2. package/dist/cjs/id/index.cjs +26 -43
  3. package/dist/cjs/index.cjs +8 -398
  4. package/dist/es/id/index.d.ts +16 -0
  5. package/dist/es/id/index.mjs +28 -42
  6. package/dist/es/index.d.ts +2 -0
  7. package/dist/es/index.mjs +8 -58
  8. package/dist/index.d.ts +21 -0
  9. package/dist/lib/index.full.cjs.js +38 -0
  10. package/dist/lib/index.full.cjs.min.js +2 -0
  11. package/dist/lib/index.full.cjs.min.js.map +1 -0
  12. package/dist/lib/index.full.esm.js +35 -15822
  13. package/dist/lib/index.full.esm.min.js +2 -58
  14. package/dist/lib/index.full.esm.min.js.map +1 -0
  15. package/dist/lib/index.full.umd.js +39 -16176
  16. package/dist/lib/index.full.umd.min.js +2 -0
  17. package/dist/lib/index.full.umd.min.js.map +1 -0
  18. package/package.json +84 -54
  19. package/dist/cjs/ID-card/index.cjs +0 -122
  20. package/dist/cjs/array/index.cjs +0 -465
  21. package/dist/cjs/clipboard/index.cjs +0 -135
  22. package/dist/cjs/color/index.cjs +0 -210
  23. package/dist/cjs/constants/date.cjs +0 -186
  24. package/dist/cjs/constants/lang.cjs +0 -13
  25. package/dist/cjs/constants/math.cjs +0 -13
  26. package/dist/cjs/constants/regexp.cjs +0 -57
  27. package/dist/cjs/constants/sort.cjs +0 -15
  28. package/dist/cjs/cookie/index.cjs +0 -71
  29. package/dist/cjs/crypto/aes/aes.cjs +0 -533
  30. package/dist/cjs/crypto/aes/index.cjs +0 -28
  31. package/dist/cjs/crypto/base32/base32.cjs +0 -385
  32. package/dist/cjs/crypto/base32/index.cjs +0 -44
  33. package/dist/cjs/crypto/base64/base64.cjs +0 -433
  34. package/dist/cjs/crypto/base64/index.cjs +0 -64
  35. package/dist/cjs/crypto/des/des.cjs +0 -1054
  36. package/dist/cjs/crypto/des/index.cjs +0 -30
  37. package/dist/cjs/crypto/md5/index.cjs +0 -25
  38. package/dist/cjs/crypto/md5/md5.cjs +0 -944
  39. package/dist/cjs/crypto/sha/sha1/index.cjs +0 -25
  40. package/dist/cjs/crypto/sha/sha1/sha1.cjs +0 -602
  41. package/dist/cjs/crypto/sha/sha256/index.cjs +0 -46
  42. package/dist/cjs/crypto/sha/sha256/sha256.cjs +0 -654
  43. package/dist/cjs/crypto/sha/sha3/index.cjs +0 -44
  44. package/dist/cjs/crypto/sha/sha3/sha3.cjs +0 -747
  45. package/dist/cjs/crypto/sha/sha512/index.cjs +0 -88
  46. package/dist/cjs/crypto/sha/sha512/sha512.cjs +0 -1186
  47. package/dist/cjs/crypto/sm/lib/asn1.cjs +0 -167
  48. package/dist/cjs/crypto/sm/lib/ec.cjs +0 -352
  49. package/dist/cjs/crypto/sm/lib/jsbn.cjs +0 -1633
  50. package/dist/cjs/crypto/sm/lib/sm3.cjs +0 -173
  51. package/dist/cjs/crypto/sm/lib/utils.cjs +0 -198
  52. package/dist/cjs/crypto/sm/sm2/index.cjs +0 -122
  53. package/dist/cjs/crypto/sm/sm2/sm2.cjs +0 -269
  54. package/dist/cjs/crypto/sm/sm3/index.cjs +0 -15
  55. package/dist/cjs/crypto/sm/sm3/sm3.cjs +0 -103
  56. package/dist/cjs/crypto/sm/sm4/index.cjs +0 -28
  57. package/dist/cjs/crypto/sm/sm4/sm4.cjs +0 -362
  58. package/dist/cjs/crypto/tea/index.cjs +0 -26
  59. package/dist/cjs/crypto/tea/tea.cjs +0 -215
  60. package/dist/cjs/date/index.cjs +0 -1349
  61. package/dist/cjs/desensitized/index.cjs +0 -74
  62. package/dist/cjs/device/index.cjs +0 -159
  63. package/dist/cjs/dom/index.cjs +0 -176
  64. package/dist/cjs/file/index.cjs +0 -339
  65. package/dist/cjs/function/index.cjs +0 -67
  66. package/dist/cjs/keycode/index.cjs +0 -142
  67. package/dist/cjs/math/index.cjs +0 -284
  68. package/dist/cjs/number/index.cjs +0 -14
  69. package/dist/cjs/object/index.cjs +0 -216
  70. package/dist/cjs/pagination/index.cjs +0 -140
  71. package/dist/cjs/random/index.cjs +0 -25
  72. package/dist/cjs/regexp/index.cjs +0 -109
  73. package/dist/cjs/storage/index.cjs +0 -11
  74. package/dist/cjs/storage/localStorage.cjs +0 -40
  75. package/dist/cjs/storage/sessionStorage.cjs +0 -40
  76. package/dist/cjs/string/index.cjs +0 -338
  77. package/dist/cjs/url/index.cjs +0 -279
  78. package/dist/cjs/validate/index.cjs +0 -487
  79. package/dist/cjs/weapp/index.cjs +0 -151
  80. package/dist/es/ID-card/index.mjs +0 -116
  81. package/dist/es/array/index.mjs +0 -437
  82. package/dist/es/clipboard/index.mjs +0 -129
  83. package/dist/es/color/index.mjs +0 -200
  84. package/dist/es/constants/date.mjs +0 -184
  85. package/dist/es/constants/lang.mjs +0 -11
  86. package/dist/es/constants/math.mjs +0 -11
  87. package/dist/es/constants/regexp.mjs +0 -55
  88. package/dist/es/constants/sort.mjs +0 -13
  89. package/dist/es/cookie/index.mjs +0 -65
  90. package/dist/es/crypto/aes/aes.mjs +0 -529
  91. package/dist/es/crypto/aes/index.mjs +0 -25
  92. package/dist/es/crypto/base32/base32.mjs +0 -381
  93. package/dist/es/crypto/base32/index.mjs +0 -39
  94. package/dist/es/crypto/base64/base64.mjs +0 -427
  95. package/dist/es/crypto/base64/index.mjs +0 -57
  96. package/dist/es/crypto/des/des.mjs +0 -1050
  97. package/dist/es/crypto/des/index.mjs +0 -27
  98. package/dist/es/crypto/md5/index.mjs +0 -22
  99. package/dist/es/crypto/md5/md5.mjs +0 -941
  100. package/dist/es/crypto/sha/sha1/index.mjs +0 -22
  101. package/dist/es/crypto/sha/sha1/sha1.mjs +0 -599
  102. package/dist/es/crypto/sha/sha256/index.mjs +0 -41
  103. package/dist/es/crypto/sha/sha256/sha256.mjs +0 -649
  104. package/dist/es/crypto/sha/sha3/index.mjs +0 -39
  105. package/dist/es/crypto/sha/sha3/sha3.mjs +0 -742
  106. package/dist/es/crypto/sha/sha512/index.mjs +0 -79
  107. package/dist/es/crypto/sha/sha512/sha512.mjs +0 -1177
  108. package/dist/es/crypto/sm/lib/asn1.mjs +0 -164
  109. package/dist/es/crypto/sm/lib/ec.mjs +0 -349
  110. package/dist/es/crypto/sm/lib/jsbn.mjs +0 -1630
  111. package/dist/es/crypto/sm/lib/sm3.mjs +0 -170
  112. package/dist/es/crypto/sm/lib/utils.mjs +0 -186
  113. package/dist/es/crypto/sm/sm2/index.mjs +0 -111
  114. package/dist/es/crypto/sm/sm2/sm2.mjs +0 -258
  115. package/dist/es/crypto/sm/sm3/index.mjs +0 -13
  116. package/dist/es/crypto/sm/sm3/sm3.mjs +0 -99
  117. package/dist/es/crypto/sm/sm4/index.mjs +0 -25
  118. package/dist/es/crypto/sm/sm4/sm4.mjs +0 -359
  119. package/dist/es/crypto/tea/index.mjs +0 -23
  120. package/dist/es/crypto/tea/tea.mjs +0 -211
  121. package/dist/es/date/index.mjs +0 -1262
  122. package/dist/es/desensitized/index.mjs +0 -69
  123. package/dist/es/device/index.mjs +0 -145
  124. package/dist/es/dom/index.mjs +0 -164
  125. package/dist/es/file/index.mjs +0 -323
  126. package/dist/es/function/index.mjs +0 -63
  127. package/dist/es/keycode/index.mjs +0 -139
  128. package/dist/es/math/index.mjs +0 -274
  129. package/dist/es/number/index.mjs +0 -12
  130. package/dist/es/object/index.mjs +0 -203
  131. package/dist/es/pagination/index.mjs +0 -134
  132. package/dist/es/random/index.mjs +0 -22
  133. package/dist/es/regexp/index.mjs +0 -98
  134. package/dist/es/storage/index.mjs +0 -9
  135. package/dist/es/storage/localStorage.mjs +0 -35
  136. package/dist/es/storage/sessionStorage.mjs +0 -35
  137. package/dist/es/string/index.mjs +0 -321
  138. package/dist/es/url/index.mjs +0 -261
  139. package/dist/es/validate/index.mjs +0 -451
  140. package/dist/es/weapp/index.mjs +0 -140
@@ -1,167 +0,0 @@
1
- 'use strict';
2
-
3
- var jsbn = require('./jsbn.cjs');
4
-
5
- const { BigInteger } = jsbn;
6
-
7
- function bigintToValue(bigint) {
8
- let h = bigint.toString(16);
9
- if (h[0] !== "-") {
10
- // 正数
11
- if (h.length % 2 === 1)
12
- h = "0" + h; // 补齐到整字节
13
- else if (!h.match(/^[0-7]/)) h = "00" + h; // 非0开头,则补一个全0字节
14
- } else {
15
- // 负数
16
- h = h.substr(1);
17
-
18
- let len = h.length;
19
- if (len % 2 === 1)
20
- len += 1; // 补齐到整字节
21
- else if (!h.match(/^[0-7]/)) len += 2; // 非0开头,则补一个全0字节
22
-
23
- let mask = "";
24
- for (let i = 0; i < len; i++) mask += "f";
25
- mask = new BigInteger(mask, 16);
26
-
27
- // 对绝对值取反,加1
28
- h = mask.xor(bigint).add(BigInteger.ONE);
29
- h = h.toString(16).replace(/^-/, "");
30
- }
31
- return h;
32
- }
33
-
34
- class ASN1Object {
35
- constructor() {
36
- this.tlv = null;
37
- this.t = "00";
38
- this.l = "00";
39
- this.v = "";
40
- }
41
-
42
- /**
43
- * 获取 der 编码比特流16进制串
44
- */
45
- getEncodedHex() {
46
- if (!this.tlv) {
47
- this.v = this.getValue();
48
- this.l = this.getLength();
49
- this.tlv = this.t + this.l + this.v;
50
- }
51
- return this.tlv;
52
- }
53
-
54
- getLength() {
55
- const n = this.v.length / 2; // 字节数
56
- let nHex = n.toString(16);
57
- if (nHex.length % 2 === 1) nHex = "0" + nHex; // 补齐到整字节
58
-
59
- if (n < 128) {
60
- // 短格式,以 0 开头
61
- return nHex;
62
- } else {
63
- // 长格式,以 1 开头
64
- const head = 128 + nHex.length / 2; // 1(1位) + 真正的长度占用字节数(7位) + 真正的长度
65
- return head.toString(16) + nHex;
66
- }
67
- }
68
-
69
- getValue() {
70
- return "";
71
- }
72
- }
73
-
74
- class DERInteger extends ASN1Object {
75
- constructor(bigint) {
76
- super();
77
-
78
- this.t = "02"; // 整型标签说明
79
- if (bigint) this.v = bigintToValue(bigint);
80
- }
81
-
82
- getValue() {
83
- return this.v;
84
- }
85
- }
86
-
87
- class DERSequence extends ASN1Object {
88
- constructor(asn1Array) {
89
- super();
90
-
91
- this.t = "30"; // 序列标签说明
92
- this.asn1Array = asn1Array;
93
- }
94
-
95
- getValue() {
96
- this.v = this.asn1Array.map((asn1Object) => asn1Object.getEncodedHex()).join("");
97
- return this.v;
98
- }
99
- }
100
-
101
- /**
102
- * 获取 l 占用字节数
103
- */
104
- function getLenOfL(str, start) {
105
- if (+str[start + 2] < 8) return 1; // l 以0开头,则表示短格式,只占一个字节
106
- return +str.substr(start + 2, 2) & (0x7f + 1); // 长格式,取第一个字节后7位作为长度真正占用字节数,再加上本身
107
- }
108
-
109
- /**
110
- * 获取 l
111
- */
112
- function getL(str, start) {
113
- // 获取 l
114
- const len = getLenOfL(str, start);
115
- const l = str.substr(start + 2, len * 2);
116
-
117
- if (!l) return -1;
118
- const bigint = +l[0] < 8 ? new BigInteger(l, 16) : new BigInteger(l.substr(2), 16);
119
-
120
- return bigint.intValue();
121
- }
122
-
123
- /**
124
- * 获取 v 的位置
125
- */
126
- function getStartOfV(str, start) {
127
- const len = getLenOfL(str, start);
128
- return start + (len + 1) * 2;
129
- }
130
-
131
- /**
132
- * ASN.1 der 编码,针对 sm2 签名
133
- */
134
- function encodeDer(r, s) {
135
- const derR = new DERInteger(r);
136
- const derS = new DERInteger(s);
137
- const derSeq = new DERSequence([derR, derS]);
138
-
139
- return derSeq.getEncodedHex();
140
- }
141
-
142
- /**
143
- * 解析 ASN.1 der,针对 sm2 验签
144
- */
145
- function decodeDer(input) {
146
- // 结构:
147
- // input = | tSeq | lSeq | vSeq |
148
- // vSeq = | tR | lR | vR | tS | lS | vS |
149
- const start = getStartOfV(input, 0);
150
-
151
- const vIndexR = getStartOfV(input, start);
152
- const lR = getL(input, start);
153
- const vR = input.substr(vIndexR, lR * 2);
154
-
155
- const nextStart = vIndexR + vR.length;
156
- const vIndexS = getStartOfV(input, nextStart);
157
- const lS = getL(input, nextStart);
158
- const vS = input.substr(vIndexS, lS * 2);
159
-
160
- const r = new BigInteger(vR, 16);
161
- const s = new BigInteger(vS, 16);
162
-
163
- return { r, s };
164
- }
165
-
166
- exports.decodeDer = decodeDer;
167
- exports.encodeDer = encodeDer;
@@ -1,352 +0,0 @@
1
- 'use strict';
2
-
3
- var jsbn = require('./jsbn.cjs');
4
-
5
- const { BigInteger } = jsbn;
6
-
7
- /**
8
- * thanks for Tom Wu : http://www-cs-students.stanford.edu/~tjw/jsbn/
9
- *
10
- * Basic Javascript Elliptic Curve implementation
11
- * Ported loosely from BouncyCastle's Java EC code
12
- * Only Fp curves implemented for now
13
- */
14
-
15
- const TWO = new BigInteger("2");
16
- const THREE = new BigInteger("3");
17
-
18
- /**
19
- * 椭圆曲线域元素
20
- */
21
- class ECFieldElementFp {
22
- constructor(q, x) {
23
- this.x = x;
24
- this.q = q;
25
- // TODO if (x.compareTo(q) >= 0) error
26
- }
27
-
28
- /**
29
- * 判断相等
30
- */
31
- equals(other) {
32
- if (other === this) return true;
33
- return this.q.equals(other.q) && this.x.equals(other.x);
34
- }
35
-
36
- /**
37
- * 返回具体数值
38
- */
39
- toBigInteger() {
40
- return this.x;
41
- }
42
-
43
- /**
44
- * 取反
45
- */
46
- negate() {
47
- return new ECFieldElementFp(this.q, this.x.negate().mod(this.q));
48
- }
49
-
50
- /**
51
- * 相加
52
- */
53
- add(b) {
54
- return new ECFieldElementFp(this.q, this.x.add(b.toBigInteger()).mod(this.q));
55
- }
56
-
57
- /**
58
- * 相减
59
- */
60
- subtract(b) {
61
- return new ECFieldElementFp(this.q, this.x.subtract(b.toBigInteger()).mod(this.q));
62
- }
63
-
64
- /**
65
- * 相乘
66
- */
67
- multiply(b) {
68
- return new ECFieldElementFp(this.q, this.x.multiply(b.toBigInteger()).mod(this.q));
69
- }
70
-
71
- /**
72
- * 相除
73
- */
74
- divide(b) {
75
- return new ECFieldElementFp(this.q, this.x.multiply(b.toBigInteger().modInverse(this.q)).mod(this.q));
76
- }
77
-
78
- /**
79
- * 平方
80
- */
81
- square() {
82
- return new ECFieldElementFp(this.q, this.x.square().mod(this.q));
83
- }
84
- }
85
-
86
- class ECPointFp {
87
- constructor(curve, x, y, z) {
88
- this.curve = curve;
89
- this.x = x;
90
- this.y = y;
91
- // 标准射影坐标系:zinv == null 或 z * zinv == 1
92
- this.z = z == null ? BigInteger.ONE : z;
93
- this.zinv = null;
94
- // TODO: compression flag
95
- }
96
-
97
- getX() {
98
- if (this.zinv === null) this.zinv = this.z.modInverse(this.curve.q);
99
-
100
- return this.curve.fromBigInteger(this.x.toBigInteger().multiply(this.zinv).mod(this.curve.q));
101
- }
102
-
103
- getY() {
104
- if (this.zinv === null) this.zinv = this.z.modInverse(this.curve.q);
105
-
106
- return this.curve.fromBigInteger(this.y.toBigInteger().multiply(this.zinv).mod(this.curve.q));
107
- }
108
-
109
- /**
110
- * 判断相等
111
- */
112
- equals(other) {
113
- if (other === this) return true;
114
- if (this.isInfinity()) return other.isInfinity();
115
- if (other.isInfinity()) return this.isInfinity();
116
-
117
- // u = y2 * z1 - y1 * z2
118
- const u = other.y
119
- .toBigInteger()
120
- .multiply(this.z)
121
- .subtract(this.y.toBigInteger().multiply(other.z))
122
- .mod(this.curve.q);
123
- if (!u.equals(BigInteger.ZERO)) return false;
124
-
125
- // v = x2 * z1 - x1 * z2
126
- const v = other.x
127
- .toBigInteger()
128
- .multiply(this.z)
129
- .subtract(this.x.toBigInteger().multiply(other.z))
130
- .mod(this.curve.q);
131
- return v.equals(BigInteger.ZERO);
132
- }
133
-
134
- /**
135
- * 是否是无穷远点
136
- */
137
- isInfinity() {
138
- if (this.x === null && this.y === null) return true;
139
- return this.z.equals(BigInteger.ZERO) && !this.y.toBigInteger().equals(BigInteger.ZERO);
140
- }
141
-
142
- /**
143
- * 取反,x 轴对称点
144
- */
145
- negate() {
146
- return new ECPointFp(this.curve, this.x, this.y.negate(), this.z);
147
- }
148
-
149
- /**
150
- * 相加
151
- *
152
- * 标准射影坐标系:
153
- *
154
- * λ1 = x1 * z2
155
- * λ2 = x2 * z1
156
- * λ3 = λ1 − λ2
157
- * λ4 = y1 * z2
158
- * λ5 = y2 * z1
159
- * λ6 = λ4 − λ5
160
- * λ7 = λ1 + λ2
161
- * λ8 = z1 * z2
162
- * λ9 = λ3^2
163
- * λ10 = λ3 * λ9
164
- * λ11 = λ8 * λ6^2 − λ7 * λ9
165
- * x3 = λ3 * λ11
166
- * y3 = λ6 * (λ9 * λ1 − λ11) − λ4 * λ10
167
- * z3 = λ10 * λ8
168
- */
169
- add(b) {
170
- if (this.isInfinity()) return b;
171
- if (b.isInfinity()) return this;
172
-
173
- const x1 = this.x.toBigInteger();
174
- const y1 = this.y.toBigInteger();
175
- const z1 = this.z;
176
- const x2 = b.x.toBigInteger();
177
- const y2 = b.y.toBigInteger();
178
- const z2 = b.z;
179
- const q = this.curve.q;
180
-
181
- const w1 = x1.multiply(z2).mod(q);
182
- const w2 = x2.multiply(z1).mod(q);
183
- const w3 = w1.subtract(w2);
184
- const w4 = y1.multiply(z2).mod(q);
185
- const w5 = y2.multiply(z1).mod(q);
186
- const w6 = w4.subtract(w5);
187
-
188
- if (BigInteger.ZERO.equals(w3)) {
189
- if (BigInteger.ZERO.equals(w6)) {
190
- return this.twice(); // this == b,计算自加
191
- }
192
- return this.curve.infinity; // this == -b,则返回无穷远点
193
- }
194
-
195
- const w7 = w1.add(w2);
196
- const w8 = z1.multiply(z2).mod(q);
197
- const w9 = w3.square().mod(q);
198
- const w10 = w3.multiply(w9).mod(q);
199
- const w11 = w8.multiply(w6.square()).subtract(w7.multiply(w9)).mod(q);
200
-
201
- const x3 = w3.multiply(w11).mod(q);
202
- const y3 = w6.multiply(w9.multiply(w1).subtract(w11)).subtract(w4.multiply(w10)).mod(q);
203
- const z3 = w10.multiply(w8).mod(q);
204
-
205
- return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3);
206
- }
207
-
208
- /**
209
- * 自加
210
- *
211
- * 标准射影坐标系:
212
- *
213
- * λ1 = 3 * x1^2 + a * z1^2
214
- * λ2 = 2 * y1 * z1
215
- * λ3 = y1^2
216
- * λ4 = λ3 * x1 * z1
217
- * λ5 = λ2^2
218
- * λ6 = λ1^2 − 8 * λ4
219
- * x3 = λ2 * λ6
220
- * y3 = λ1 * (4 * λ4 − λ6) − 2 * λ5 * λ3
221
- * z3 = λ2 * λ5
222
- */
223
- twice() {
224
- if (this.isInfinity()) return this;
225
- if (!this.y.toBigInteger().signum()) return this.curve.infinity;
226
-
227
- const x1 = this.x.toBigInteger();
228
- const y1 = this.y.toBigInteger();
229
- const z1 = this.z;
230
- const q = this.curve.q;
231
- const a = this.curve.a.toBigInteger();
232
-
233
- const w1 = x1.square().multiply(THREE).add(a.multiply(z1.square())).mod(q);
234
- const w2 = y1.shiftLeft(1).multiply(z1).mod(q);
235
- const w3 = y1.square().mod(q);
236
- const w4 = w3.multiply(x1).multiply(z1).mod(q);
237
- const w5 = w2.square().mod(q);
238
- const w6 = w1.square().subtract(w4.shiftLeft(3)).mod(q);
239
-
240
- const x3 = w2.multiply(w6).mod(q);
241
- const y3 = w1.multiply(w4.shiftLeft(2).subtract(w6)).subtract(w5.shiftLeft(1).multiply(w3)).mod(q);
242
- const z3 = w2.multiply(w5).mod(q);
243
-
244
- return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3);
245
- }
246
-
247
- /**
248
- * 倍点计算
249
- */
250
- multiply(k) {
251
- if (this.isInfinity()) return this;
252
- if (!k.signum()) return this.curve.infinity;
253
-
254
- // 使用加减法
255
- const k3 = k.multiply(THREE);
256
- const neg = this.negate();
257
- let Q = this;
258
-
259
- for (let i = k3.bitLength() - 2; i > 0; i--) {
260
- Q = Q.twice();
261
-
262
- const k3Bit = k3.testBit(i);
263
- const kBit = k.testBit(i);
264
-
265
- if (k3Bit !== kBit) {
266
- Q = Q.add(k3Bit ? this : neg);
267
- }
268
- }
269
-
270
- return Q;
271
- }
272
- }
273
-
274
- /**
275
- * 椭圆曲线 y^2 = x^3 + ax + b
276
- */
277
- class ECCurveFp {
278
- constructor(q, a, b) {
279
- this.q = q;
280
- this.a = this.fromBigInteger(a);
281
- this.b = this.fromBigInteger(b);
282
- this.infinity = new ECPointFp(this, null, null); // 无穷远点
283
- }
284
-
285
- /**
286
- * 判断两个椭圆曲线是否相等
287
- */
288
- equals(other) {
289
- if (other === this) return true;
290
- return this.q.equals(other.q) && this.a.equals(other.a) && this.b.equals(other.b);
291
- }
292
-
293
- /**
294
- * 生成椭圆曲线域元素
295
- */
296
- fromBigInteger(x) {
297
- return new ECFieldElementFp(this.q, x);
298
- }
299
-
300
- /**
301
- * 解析 16 进制串为椭圆曲线点
302
- */
303
- decodePointHex(s) {
304
- switch (parseInt(s.substr(0, 2), 16)) {
305
- // 第一个字节
306
- case 0:
307
- return this.infinity;
308
- case 2:
309
- case 3:
310
- // 压缩
311
- const x = this.fromBigInteger(new BigInteger(s.substr(2), 16));
312
- // 对 p ≡ 3 (mod4),即存在正整数 u,使得 p = 4u + 3
313
- // 计算 y = (√ (x^3 + ax + b) % p)^(u + 1) modp
314
- let y = this.fromBigInteger(
315
- x
316
- .multiply(x.square())
317
- .add(x.multiply(this.a))
318
- .add(this.b)
319
- .toBigInteger()
320
- .modPow(this.q.divide(new BigInteger("4")).add(BigInteger.ONE), this.q),
321
- );
322
- // 算出结果 2 进制最后 1 位不等于第 1 个字节减 2 则取反
323
- if (
324
- !y
325
- .toBigInteger()
326
- .mod(TWO)
327
- .equals(new BigInteger(s.substr(0, 2), 16).subtract(TWO))
328
- ) {
329
- y = y.negate();
330
- }
331
- return new ECPointFp(this, x, y);
332
- case 4:
333
- case 6:
334
- case 7:
335
- const len = (s.length - 2) / 2;
336
- const xHex = s.substr(2, len);
337
- const yHex = s.substr(len + 2, len);
338
-
339
- return new ECPointFp(
340
- this,
341
- this.fromBigInteger(new BigInteger(xHex, 16)),
342
- this.fromBigInteger(new BigInteger(yHex, 16)),
343
- );
344
- default:
345
- // 不支持
346
- return null;
347
- }
348
- }
349
- }
350
-
351
- exports.ECCurveFp = ECCurveFp;
352
- exports.ECPointFp = ECPointFp;