@li0ard/dstu 0.1.1 → 0.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.
package/dstu4145/index.js CHANGED
@@ -1,58 +1,53 @@
1
1
  import { concatBytes, randomBytes } from "@noble/hashes/utils.js";
2
- import { DSTU_163, DSTU_163_TEST, DSTU_167, DSTU_173, DSTU_173_ONB, DSTU_173_ONB_TEST, DSTU_179, DSTU_179_ONB, DSTU_191, DSTU_191_ONB, DSTU_233, DSTU_233_ONB, DSTU_257, DSTU_307, DSTU_367, DSTU_431, DSTU_431_ONB } from "./const.js";
2
+ import { DSTU_163, DSTU_167, DSTU_173, DSTU_179, DSTU_191, DSTU_233, DSTU_257, DSTU_307, DSTU_367, DSTU_431 } from "./const.js";
3
3
  import { binaryWeierstrass } from "./ec/index.js";
4
4
  import BN from "bn.js";
5
5
  /** Create DSTU 4145-2002 signer (Big-Endian) */
6
6
  export const dstu4145 = (parameters) => {
7
7
  const curve = binaryWeierstrass(parameters);
8
- const k = curve.ORDER.getLength() - 1;
9
- const getPublicKey = (secretKey, isCompressed = false) => curve.Point.BASE.mul(curve.Field.fromBytes(secretKey)).negate().toBytes(isCompressed);
10
- const randomPrivateKey = () => new Uint8Array(new BN(randomBytes(curve.lengths.scalarByteLength))
11
- .maskn(k)
12
- .toArray("be", curve.lengths.scalarByteLength));
8
+ const { Field, Point, truncate, lengths } = curve;
9
+ const getPublicKey = (secretKey, isCompressed = false) => Point.BASE.mul(Field.fromHexStringOrBytes(secretKey)).negate().toBytes(isCompressed);
10
+ const randomPrivateKey = () => Field.toBytes(truncate(new BN(randomBytes(lengths.scalarByteLength))), lengths.scalarByteLength);
13
11
  const computePresign = (rand) => {
14
- const e = (rand
12
+ const e = truncate(rand
15
13
  ? new BN(rand)
16
- : new BN(randomBytes(curve.lengths.scalarByteLength))).maskn(k);
14
+ : new BN(randomBytes(lengths.scalarByteLength)));
17
15
  if (rand && e.isZero())
18
16
  throw new Error("Invalid custom rand for presign (rand = 0)");
19
17
  if (e.isZero())
20
18
  return computePresign(rand);
21
- const xR = curve.Point.BASE.mul(new curve.Field(e)).x;
22
- if (xR.is0())
19
+ const Fe = Point.BASE.mul(e).x;
20
+ if (Fe.isZero())
23
21
  return computePresign(rand);
24
- return { Fe: xR, e };
22
+ return { Fe, e };
25
23
  };
26
24
  const prepareHash = (digest) => {
27
- let h = curve.toInternalField(curve.hashToField(digest));
28
- if (h.is0())
29
- h = curve.Field.get1();
25
+ let h = curve.toInternalField(Field.hashToField(digest));
26
+ if (h.isZero())
27
+ h = new BN(1);
30
28
  return h;
31
29
  };
32
30
  const sign = (secretKey, digest, rand) => {
33
- const d = new BN(secretKey).mod(curve.ORDER.value);
34
- const h = prepareHash(digest);
35
- const { Fe, e } = computePresign(rand);
36
- const r = curve.toExternalField(h.mul(Fe)).value.maskn(k);
31
+ const d = new BN(secretKey), h = prepareHash(digest), { Fe, e } = computePresign(rand);
32
+ const r = truncate(curve.toExternalField(Field.mul(h, Fe)));
37
33
  if (r.isZero())
38
34
  return sign(secretKey, digest, rand);
39
- const s = e.add(d.mul(r)).mod(curve.ORDER.value);
35
+ const s = e.add(d.mul(r)).mod(curve.ORDER);
40
36
  if (s.isZero())
41
37
  return sign(secretKey, digest, rand);
42
- return concatBytes(new Uint8Array(s.toArray("be", curve.lengths.scalarByteLength)), new Uint8Array(r.toArray("be", curve.lengths.scalarByteLength)));
38
+ return concatBytes(Field.toBytes(s, lengths.scalarByteLength), Field.toBytes(r, lengths.scalarByteLength));
43
39
  };
44
40
  const verify = (publicKey, digest, signature) => {
45
- if (signature.length != curve.lengths.signatureByteLength)
41
+ if (signature.length != lengths.signatureByteLength)
46
42
  throw new Error("Invalid signature length");
47
- const Q = curve.Point.fromBytes(publicKey);
48
- const _s = curve.Field.fromBytes(signature.subarray(0, curve.lengths.scalarByteLength)), _r = curve.Field.fromBytes(signature.subarray(curve.lengths.scalarByteLength));
49
- if (_s.value.isZero() || _s.value.gte(curve.ORDER.value)
50
- || _r.value.isZero() || _r.value.gte(curve.ORDER.value))
43
+ const Q = Point.fromBytes(publicKey);
44
+ const s = Field.fromHexStringOrBytes(signature.subarray(0, lengths.scalarByteLength)), r = Field.fromHexStringOrBytes(signature.subarray(lengths.scalarByteLength));
45
+ if (s.isZero() || s.gte(curve.ORDER) || r.isZero() || r.gte(curve.ORDER))
51
46
  return false;
52
47
  const h = prepareHash(digest);
53
- const R = curve.Point.BASE.mulWnaf(_s).add(Q.mulWnaf(_r));
54
- const y = curve.toExternalField(h.mul(R.x)).value.maskn(k);
55
- return y.eq(_r.value);
48
+ const R = Point.BASE.mulWnaf(s).add(Q.mulWnaf(r));
49
+ const y = truncate(curve.toExternalField(Field.mul(h, R.x)));
50
+ return y.eq(r);
56
51
  };
57
52
  const keygen = (isCompressed = false) => {
58
53
  const secretKey = randomPrivateKey();
@@ -64,13 +59,13 @@ export const dstu4145 = (parameters) => {
64
59
  sign,
65
60
  verify,
66
61
  keygen,
67
- lengths: curve.lengths
62
+ lengths
68
63
  });
69
64
  };
70
65
  export * from "./const.js";
71
66
  export * from "./ec/expand.js";
72
67
  // ONB curves don't preinitialized because `multiplyOnb` is slow O(m^3) function
73
- // You need to initialize it manually via `dstu4145`
68
+ // You need to initialize them manually via `dstu4145`
74
69
  /** DSTU 4145-2002 163 bit curve */
75
70
  export const dstu163 = dstu4145(DSTU_163);
76
71
  /** DSTU 4145-2002 167 bit curve */
package/kupyna/index.d.ts CHANGED
@@ -61,28 +61,28 @@ export declare class Kupyna512 extends Kupyna<Kupyna512> {
61
61
  export declare class Kupyna48 extends KupynaDerived<Kupyna256> {
62
62
  /** Kupyna 48 bit version */
63
63
  constructor();
64
- /** Create hash instance */
65
- static create(): Kupyna48;
66
64
  _cloneInto(to?: Kupyna48): Kupyna48;
67
65
  clone(): Kupyna48;
66
+ /** Create hash instance */
67
+ static create(): Kupyna48;
68
68
  }
69
69
  /** Kupyna 304 bit version */
70
70
  export declare class Kupyna304 extends KupynaDerived<Kupyna512> {
71
71
  /** Kupyna 304 bit version */
72
72
  constructor();
73
- /** Create hash instance */
74
- static create(): Kupyna304;
75
73
  _cloneInto(to?: Kupyna304): Kupyna304;
76
74
  clone(): Kupyna304;
75
+ /** Create hash instance */
76
+ static create(): Kupyna304;
77
77
  }
78
78
  /** Kupyna 384 bit version */
79
79
  export declare class Kupyna384 extends KupynaDerived<Kupyna512> {
80
80
  /** Kupyna 384 bit version */
81
81
  constructor();
82
- /** Create hash instance */
83
- static create(): Kupyna384;
84
82
  _cloneInto(to?: Kupyna384): Kupyna384;
85
83
  clone(): Kupyna384;
84
+ /** Create hash instance */
85
+ static create(): Kupyna384;
86
86
  }
87
87
  /** Kupyna 48 bit version */
88
88
  export declare const kupyna48: {
package/kupyna/index.js CHANGED
@@ -185,40 +185,40 @@ export class Kupyna512 extends Kupyna {
185
185
  export class Kupyna48 extends KupynaDerived {
186
186
  /** Kupyna 48 bit version */
187
187
  constructor() { super(Kupyna256.create, -6); }
188
- /** Create hash instance */
189
- static create() { return new Kupyna48(); }
190
188
  _cloneInto(to) {
191
189
  to ||= new Kupyna48();
192
190
  to.buffer = new Uint8Array(this.buffer);
193
191
  return to;
194
192
  }
195
193
  clone() { return this._cloneInto(); }
194
+ /** Create hash instance */
195
+ static create() { return new Kupyna48(); }
196
196
  }
197
197
  /** Kupyna 304 bit version */
198
198
  export class Kupyna304 extends KupynaDerived {
199
199
  /** Kupyna 304 bit version */
200
200
  constructor() { super(Kupyna512.create, -38); }
201
- /** Create hash instance */
202
- static create() { return new Kupyna304(); }
203
201
  _cloneInto(to) {
204
202
  to ||= new Kupyna304();
205
203
  to.buffer = new Uint8Array(this.buffer);
206
204
  return to;
207
205
  }
208
206
  clone() { return this._cloneInto(); }
207
+ /** Create hash instance */
208
+ static create() { return new Kupyna304(); }
209
209
  }
210
210
  /** Kupyna 384 bit version */
211
211
  export class Kupyna384 extends KupynaDerived {
212
212
  /** Kupyna 384 bit version */
213
213
  constructor() { super(Kupyna512.create, -48); }
214
- /** Create hash instance */
215
- static create() { return new Kupyna384(); }
216
214
  _cloneInto(to) {
217
215
  to ||= new Kupyna384();
218
216
  to.buffer = new Uint8Array(this.buffer);
219
217
  return to;
220
218
  }
221
219
  clone() { return this._cloneInto(); }
220
+ /** Create hash instance */
221
+ static create() { return new Kupyna384(); }
222
222
  }
223
223
  /** Kupyna 48 bit version */
224
224
  export const kupyna48 = createHasher(Kupyna48.create);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@li0ard/dstu",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "type": "module",
@@ -30,7 +30,7 @@
30
30
  "docs": "typedoc --options .config/typedoc/config.cjs"
31
31
  },
32
32
  "devDependencies": {
33
- "@li0ard/gost": "^0.2.0",
33
+ "@li0ard/gost": "^0.2.2",
34
34
  "@peculiar/asn1-schema": "^2.8.0",
35
35
  "@peculiar/asn1-x509": "^2.8.0",
36
36
  "@types/bn.js": "^5.2.0",