sm-crypto-v2 1.6.0 → 1.7.0

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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [1.7.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.6.0...v1.7.0) (2023-07-17)
6
+
7
+
8
+ ### Features
9
+
10
+ * **sm2:** support precompute sm2 point ([ae347bf](https://github.com/Cubelrti/sm-crypto-v2/commit/ae347bfff6c306318276a31f5958290ac7f07b9c))
11
+
5
12
  ## [1.6.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.5.1...v1.6.0) (2023-07-11)
6
13
 
7
14
 
package/README.md CHANGED
@@ -127,6 +127,21 @@ import { sm2 } from 'sm-crypto-v2'
127
127
  let point = sm2.getPoint() // 获取一个椭圆曲线点,可在sm2签名时传入
128
128
  ```
129
129
 
130
+ ### 预计算公钥
131
+
132
+ ```js
133
+ import { sm2 } from 'sm-crypto-v2'
134
+ let keypair = sm2.generateKeyPairHex()
135
+
136
+ const precomputedPublicKey = sm2.precomputePublicKey(keypair.publicKey)
137
+ // 加密和验签可以传入预计算后的点
138
+ let encryptData = sm2.doEncrypt(msgString, precomputedPublicKey, cipherMode) // 加密结果
139
+ let verifyResult4 = sm2.doVerifySignature(msg, sigValueHex4, precomputedPublicKey, {
140
+ hash: true,
141
+ }) // 验签结果
142
+
143
+ ```
144
+
130
145
  ## sm3
131
146
 
132
147
  ```js
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { ProjPointType } from '@noble/curves/abstract/weierstrass';
2
+
1
3
  interface KeyPair {
2
4
  privateKey: string;
3
5
  publicKey: string;
@@ -47,7 +49,7 @@ declare const EmptyArray: Uint8Array;
47
49
  /**
48
50
  * 加密
49
51
  */
50
- declare function doEncrypt(msg: string | Uint8Array, publicKey: string, cipherMode?: number): string;
52
+ declare function doEncrypt(msg: string | Uint8Array, publicKey: string | ProjPointType<bigint>, cipherMode?: number): string;
51
53
  /**
52
54
  * 解密
53
55
  */
@@ -74,7 +76,7 @@ declare function doSignature(msg: Uint8Array | string, privateKey: string, optio
74
76
  /**
75
77
  * 验签
76
78
  */
77
- declare function doVerifySignature(msg: string | Uint8Array, signHex: string, publicKey: string, options?: {
79
+ declare function doVerifySignature(msg: string | Uint8Array, signHex: string, publicKey: string | ProjPointType<bigint>, options?: {
78
80
  der?: boolean;
79
81
  hash?: boolean;
80
82
  userId?: string;
@@ -84,6 +86,14 @@ declare function getZ(publicKey: string, userId?: string): Uint8Array;
84
86
  * sm3杂凑算法
85
87
  */
86
88
  declare function getHash(hashHex: string | Uint8Array, publicKey: string, userId?: string): string;
89
+ /**
90
+ * 预计算公钥点,可用于提升加密性能
91
+ * @export
92
+ * @param {string} publicKey 公钥
93
+ * @param windowSize 计算窗口大小,默认为 8
94
+ * @returns {ProjPointType<bigint>} 预计算的点
95
+ */
96
+ declare function precomputePublicKey(publicKey: string, windowSize?: number): ProjPointType<bigint>;
87
97
  /**
88
98
  * 计算公钥
89
99
  */
@@ -106,6 +116,7 @@ declare const index$1_doSignature: typeof doSignature;
106
116
  declare const index$1_doVerifySignature: typeof doVerifySignature;
107
117
  declare const index$1_getZ: typeof getZ;
108
118
  declare const index$1_getHash: typeof getHash;
119
+ declare const index$1_precomputePublicKey: typeof precomputePublicKey;
109
120
  declare const index$1_getPublicKeyFromPrivateKey: typeof getPublicKeyFromPrivateKey;
110
121
  declare const index$1_getPoint: typeof getPoint;
111
122
  declare const index$1_initRNGPool: typeof initRNGPool;
@@ -130,6 +141,7 @@ declare namespace index$1 {
130
141
  index$1_doVerifySignature as doVerifySignature,
131
142
  index$1_getZ as getZ,
132
143
  index$1_getHash as getHash,
144
+ index$1_precomputePublicKey as precomputePublicKey,
133
145
  index$1_getPublicKeyFromPrivateKey as getPublicKeyFromPrivateKey,
134
146
  index$1_getPoint as getPoint,
135
147
  index$1_initRNGPool as initRNGPool,
package/dist/index.js CHANGED
@@ -53,6 +53,7 @@ __export(sm2_exports, {
53
53
  hexToArray: () => hexToArray,
54
54
  initRNGPool: () => initRNGPool,
55
55
  leftPad: () => leftPad,
56
+ precomputePublicKey: () => precomputePublicKey,
56
57
  utf8ToHex: () => utf8ToHex,
57
58
  verifyPublicKey: () => verifyPublicKey
58
59
  });
@@ -640,9 +641,12 @@ function verifyPublicKey(publicKey) {
640
641
  const point = sm2Curve.ProjectivePoint.fromHex(publicKey);
641
642
  if (!point)
642
643
  return false;
643
- const x2 = point.x;
644
- const y = point.y;
645
- return sm2Fp.sqr(y) === sm2Fp.add(sm2Fp.addN(sm2Fp.mulN(x2, sm2Fp.sqrN(x2)), sm2Fp.mulN(x2, sm2Curve.CURVE.a)), sm2Curve.CURVE.b);
644
+ try {
645
+ point.assertValidity();
646
+ return true;
647
+ } catch (error) {
648
+ return false;
649
+ }
646
650
  }
647
651
  function comparePublicKeyHex(publicKey1, publicKey2) {
648
652
  const point1 = sm2Curve.ProjectivePoint.fromHex(publicKey1);
@@ -712,7 +716,7 @@ var C1C2C3 = 0;
712
716
  var EmptyArray = new Uint8Array();
713
717
  function doEncrypt(msg, publicKey, cipherMode = 1) {
714
718
  const msgArr = typeof msg === "string" ? hexToArray(utf8ToHex(msg)) : Uint8Array.from(msg);
715
- const publicKeyPoint = sm2Curve.ProjectivePoint.fromHex(publicKey);
719
+ const publicKeyPoint = typeof publicKey === "string" ? sm2Curve.ProjectivePoint.fromHex(publicKey) : publicKey;
716
720
  const keypair = generateKeyPairHex();
717
721
  const k = utils4.hexToNumber(keypair.privateKey);
718
722
  let c1 = keypair.publicKey;
@@ -812,8 +816,9 @@ function doVerifySignature(msg, signHex, publicKey, options = {}) {
812
816
  der,
813
817
  userId
814
818
  } = options;
819
+ const publicKeyHex = typeof publicKey === "string" ? publicKey : publicKey.toHex(false);
815
820
  if (hash) {
816
- hashHex = getHash(typeof msg === "string" ? utf8ToHex(msg) : msg, publicKey, userId);
821
+ hashHex = getHash(typeof msg === "string" ? utf8ToHex(msg) : msg, publicKeyHex, userId);
817
822
  } else {
818
823
  hashHex = typeof msg === "string" ? utf8ToHex(msg) : arrayToHex(Array.from(msg));
819
824
  }
@@ -827,7 +832,7 @@ function doVerifySignature(msg, signHex, publicKey, options = {}) {
827
832
  r = utils4.hexToNumber(signHex.substring(0, 64));
828
833
  s = utils4.hexToNumber(signHex.substring(64));
829
834
  }
830
- const PA = sm2Curve.ProjectivePoint.fromHex(publicKey);
835
+ const PA = typeof publicKey === "string" ? sm2Curve.ProjectivePoint.fromHex(publicKey) : publicKey;
831
836
  const e = utils4.hexToNumber(hashHex);
832
837
  const t = field.add(r, s);
833
838
  if (t === ZERO)
@@ -861,6 +866,10 @@ function getHash(hashHex, publicKey, userId = "1234567812345678") {
861
866
  const z = getZ(publicKey, userId);
862
867
  return bytesToHex(sm3(utils4.concatBytes(z, typeof hashHex === "string" ? hexToArray(hashHex) : hashHex)));
863
868
  }
869
+ function precomputePublicKey(publicKey, windowSize) {
870
+ const point = sm2Curve.ProjectivePoint.fromHex(publicKey);
871
+ return sm2Curve.utils.precompute(windowSize, point);
872
+ }
864
873
  function getPublicKeyFromPrivateKey(privateKey) {
865
874
  const pubKey = sm2Curve.getPublicKey(privateKey, false);
866
875
  const pubPad = leftPad(utils4.bytesToHex(pubKey), 64);
package/dist/index.mjs CHANGED
@@ -25,6 +25,7 @@ __export(sm2_exports, {
25
25
  hexToArray: () => hexToArray,
26
26
  initRNGPool: () => initRNGPool,
27
27
  leftPad: () => leftPad,
28
+ precomputePublicKey: () => precomputePublicKey,
28
29
  utf8ToHex: () => utf8ToHex,
29
30
  verifyPublicKey: () => verifyPublicKey
30
31
  });
@@ -612,9 +613,12 @@ function verifyPublicKey(publicKey) {
612
613
  const point = sm2Curve.ProjectivePoint.fromHex(publicKey);
613
614
  if (!point)
614
615
  return false;
615
- const x2 = point.x;
616
- const y = point.y;
617
- return sm2Fp.sqr(y) === sm2Fp.add(sm2Fp.addN(sm2Fp.mulN(x2, sm2Fp.sqrN(x2)), sm2Fp.mulN(x2, sm2Curve.CURVE.a)), sm2Curve.CURVE.b);
616
+ try {
617
+ point.assertValidity();
618
+ return true;
619
+ } catch (error) {
620
+ return false;
621
+ }
618
622
  }
619
623
  function comparePublicKeyHex(publicKey1, publicKey2) {
620
624
  const point1 = sm2Curve.ProjectivePoint.fromHex(publicKey1);
@@ -684,7 +688,7 @@ var C1C2C3 = 0;
684
688
  var EmptyArray = new Uint8Array();
685
689
  function doEncrypt(msg, publicKey, cipherMode = 1) {
686
690
  const msgArr = typeof msg === "string" ? hexToArray(utf8ToHex(msg)) : Uint8Array.from(msg);
687
- const publicKeyPoint = sm2Curve.ProjectivePoint.fromHex(publicKey);
691
+ const publicKeyPoint = typeof publicKey === "string" ? sm2Curve.ProjectivePoint.fromHex(publicKey) : publicKey;
688
692
  const keypair = generateKeyPairHex();
689
693
  const k = utils4.hexToNumber(keypair.privateKey);
690
694
  let c1 = keypair.publicKey;
@@ -784,8 +788,9 @@ function doVerifySignature(msg, signHex, publicKey, options = {}) {
784
788
  der,
785
789
  userId
786
790
  } = options;
791
+ const publicKeyHex = typeof publicKey === "string" ? publicKey : publicKey.toHex(false);
787
792
  if (hash) {
788
- hashHex = getHash(typeof msg === "string" ? utf8ToHex(msg) : msg, publicKey, userId);
793
+ hashHex = getHash(typeof msg === "string" ? utf8ToHex(msg) : msg, publicKeyHex, userId);
789
794
  } else {
790
795
  hashHex = typeof msg === "string" ? utf8ToHex(msg) : arrayToHex(Array.from(msg));
791
796
  }
@@ -799,7 +804,7 @@ function doVerifySignature(msg, signHex, publicKey, options = {}) {
799
804
  r = utils4.hexToNumber(signHex.substring(0, 64));
800
805
  s = utils4.hexToNumber(signHex.substring(64));
801
806
  }
802
- const PA = sm2Curve.ProjectivePoint.fromHex(publicKey);
807
+ const PA = typeof publicKey === "string" ? sm2Curve.ProjectivePoint.fromHex(publicKey) : publicKey;
803
808
  const e = utils4.hexToNumber(hashHex);
804
809
  const t = field.add(r, s);
805
810
  if (t === ZERO)
@@ -833,6 +838,10 @@ function getHash(hashHex, publicKey, userId = "1234567812345678") {
833
838
  const z = getZ(publicKey, userId);
834
839
  return bytesToHex(sm3(utils4.concatBytes(z, typeof hashHex === "string" ? hexToArray(hashHex) : hashHex)));
835
840
  }
841
+ function precomputePublicKey(publicKey, windowSize) {
842
+ const point = sm2Curve.ProjectivePoint.fromHex(publicKey);
843
+ return sm2Curve.utils.precompute(windowSize, point);
844
+ }
836
845
  function getPublicKeyFromPrivateKey(privateKey) {
837
846
  const pubKey = sm2Curve.getPublicKey(privateKey, false);
838
847
  const pubPad = leftPad(utils4.bytesToHex(pubKey), 64);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sm-crypto-v2",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "sm-crypto-v2",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -33,6 +33,7 @@
33
33
  "@noble/curves": "^1.1.0"
34
34
  },
35
35
  "devDependencies": {
36
+ "@swc-node/register": "^1.6.6",
36
37
  "@swc/core": "^1.3.62",
37
38
  "@types/node": "^16",
38
39
  "@typescript-eslint/eslint-plugin": "^5.23.0",
@@ -40,6 +41,7 @@
40
41
  "@vitest/coverage-c8": "^0.31.0",
41
42
  "@vitest/coverage-istanbul": "^0.31.0",
42
43
  "@vitest/expect": "^0.31.4",
44
+ "@vitest/runner": "^0.33.0",
43
45
  "@vitest/ui": "^0.31.0",
44
46
  "conventional-changelog-cli": "^2.2.2",
45
47
  "eslint": "^8.15.0",
package/src/sm2/index.ts CHANGED
@@ -6,6 +6,7 @@ import * as utils from '@noble/curves/abstract/utils';
6
6
  import { field, sm2Curve } from './ec';
7
7
  import { ONE, ZERO } from './bn';
8
8
  import { bytesToHex } from '@/sm3/utils';
9
+ import { ProjPointType } from '@noble/curves/abstract/weierstrass';
9
10
 
10
11
  export * from './utils'
11
12
  export { initRNGPool } from './rng'
@@ -17,10 +18,11 @@ export const EmptyArray = new Uint8Array()
17
18
  /**
18
19
  * 加密
19
20
  */
20
- export function doEncrypt(msg: string | Uint8Array, publicKey: string, cipherMode = 1) {
21
+ export function doEncrypt(msg: string | Uint8Array, publicKey: string | ProjPointType<bigint>, cipherMode = 1) {
21
22
 
22
23
  const msgArr = typeof msg === 'string' ? hexToArray(utf8ToHex(msg)) : Uint8Array.from(msg)
23
- const publicKeyPoint = sm2Curve.ProjectivePoint.fromHex(publicKey)
24
+ const publicKeyPoint = typeof publicKey === 'string' ? sm2Curve.ProjectivePoint.fromHex(publicKey) :
25
+ publicKey
24
26
 
25
27
  const keypair = generateKeyPairHex()
26
28
  const k = utils.hexToNumber(keypair.privateKey)
@@ -28,7 +30,7 @@ export function doEncrypt(msg: string | Uint8Array, publicKey: string, cipherMod
28
30
  // c1 = k * G
29
31
  let c1 = keypair.publicKey
30
32
  if (c1.length > 128) c1 = c1.substring(c1.length - 128)
31
- const p = publicKeyPoint!.multiply(k)
33
+ const p = publicKeyPoint.multiply(k)
32
34
 
33
35
  // (x2, y2) = k * publicKey
34
36
  const x2 = hexToArray(leftPad(utils.numberToHexUnpadded(p.x), 64))
@@ -165,16 +167,17 @@ export function doSignature(msg: Uint8Array | string, privateKey: string, option
165
167
  /**
166
168
  * 验签
167
169
  */
168
- export function doVerifySignature(msg: string | Uint8Array, signHex: string, publicKey: string, options: { der?: boolean, hash?: boolean, userId?: string } = {}) {
170
+ export function doVerifySignature(msg: string | Uint8Array, signHex: string, publicKey: string | ProjPointType<bigint>, options: { der?: boolean, hash?: boolean, userId?: string } = {}) {
169
171
  let hashHex: string
170
172
  const {
171
173
  hash,
172
174
  der,
173
175
  userId,
174
176
  } = options
177
+ const publicKeyHex = typeof publicKey === 'string' ? publicKey : publicKey.toHex(false)
175
178
  if (hash) {
176
179
  // sm3杂凑
177
- hashHex = getHash(typeof msg === 'string' ? utf8ToHex(msg) : msg, publicKey, userId)
180
+ hashHex = getHash(typeof msg === 'string' ? utf8ToHex(msg) : msg, publicKeyHex, userId)
178
181
  } else {
179
182
  hashHex = typeof msg === 'string' ? utf8ToHex(msg) : arrayToHex(Array.from(msg))
180
183
  }
@@ -190,7 +193,7 @@ export function doVerifySignature(msg: string | Uint8Array, signHex: string, pub
190
193
  s = utils.hexToNumber(signHex.substring(64))
191
194
  }
192
195
 
193
- const PA = sm2Curve.ProjectivePoint.fromHex(publicKey)!
196
+ const PA = typeof publicKey === 'string' ? sm2Curve.ProjectivePoint.fromHex(publicKey) : publicKey
194
197
  const e = utils.hexToNumber(hashHex)
195
198
 
196
199
  // t = (r + s) mod n
@@ -250,6 +253,18 @@ export function getHash(hashHex: string | Uint8Array, publicKey: string, userId
250
253
  return bytesToHex(sm3(utils.concatBytes(z, typeof hashHex === 'string' ? hexToArray(hashHex) : hashHex)))
251
254
  }
252
255
 
256
+ /**
257
+ * 预计算公钥点,可用于提升加密性能
258
+ * @export
259
+ * @param {string} publicKey 公钥
260
+ * @param windowSize 计算窗口大小,默认为 8
261
+ * @returns {ProjPointType<bigint>} 预计算的点
262
+ */
263
+ export function precomputePublicKey(publicKey: string, windowSize?: number) {
264
+ const point = sm2Curve.ProjectivePoint.fromHex(publicKey)
265
+ return sm2Curve.utils.precompute(windowSize, point)
266
+ }
267
+
253
268
  /**
254
269
  * 计算公钥
255
270
  */
package/src/sm2/utils.ts CHANGED
@@ -142,11 +142,12 @@ export function hexToArray(hexStr: string) {
142
142
  export function verifyPublicKey(publicKey: string) {
143
143
  const point = sm2Curve.ProjectivePoint.fromHex(publicKey)
144
144
  if (!point) return false
145
-
146
- const x = point.x
147
- const y = point.y
148
- // 验证 y^2 是否等于 x^3 + ax + b
149
- return sm2Fp.sqr(y) === sm2Fp.add(sm2Fp.addN(sm2Fp.mulN(x, sm2Fp.sqrN(x)), sm2Fp.mulN(x, sm2Curve.CURVE.a)), sm2Curve.CURVE.b)
145
+ try {
146
+ point.assertValidity()
147
+ return true
148
+ } catch (error) {
149
+ return false
150
+ }
150
151
  }
151
152
 
152
153
  /**
package/src/sm3/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { hmac } from '@/sm2/hmac'
2
2
  import { sm3 as sm2sm3 } from '../sm2/sm3'
3
- import { arrayToHex, hexToArray, leftPad } from '../sm2/utils'
3
+ import { hexToArray } from '../sm2/utils'
4
4
  import { bytesToHex } from './utils'
5
5
 
6
6
  /**