@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.
@@ -8,9 +8,13 @@ export interface DSTUShortParameters {
8
8
  m: number;
9
9
  /** Polynomial coefficients */
10
10
  ks: number[];
11
+ /** Parameters for Optimal normal basis (ONB) */
11
12
  onb?: {
13
+ /** Compressed matrix for ONB multiplication */
12
14
  matrix: string;
15
+ /** Roots in ONB for PB-irreducible */
13
16
  root1: string;
17
+ /** Roots in PB for ONB-irreducible */
14
18
  root2: string;
15
19
  };
16
20
  }
@@ -1,129 +1,39 @@
1
- import BN from "bn.js";
2
- import { computeMod } from "./index.js";
3
1
  import { init_onb_parameters } from "./onb.js";
4
- class Field {
5
- value;
6
- constructor(value = new BN(0)) {
7
- this.value = value;
8
- }
9
- clone() { return new Field(this.value.clone()); }
10
- testBit(i) { return this.value.testn(i) ? 1 : 0; }
11
- setBit(i, v) { this.value = this.value.setn(i, v == 1); }
12
- add(v) { return new Field(this.value.xor(v.value)); }
13
- shiftLeft(n) {
14
- if (n < 0)
15
- throw new Error("Shift amount cannot be negative.");
16
- return new Field(this.value.ushln(n));
17
- }
18
- static get0() { return new Field(new BN(0)); }
19
- static get1() { return new Field(new BN(1)); }
20
- }
2
+ import { createField } from "./math.js";
21
3
  /** Uncompress point without creating full API curve */
22
4
  export const expandPoint = (xBytes, params) => {
23
- const { m, ks, a } = params;
5
+ const { m, ks, a, onb } = params;
6
+ const field = createField(m, ks);
24
7
  let converter;
25
- if (params.onb)
8
+ if (onb)
26
9
  converter = init_onb_parameters(params);
27
- const bInt = new BN(params.b, 16);
28
- const b = new Field(params.onb
29
- ? converter.onbToPb(bInt)
30
- : bInt);
31
- const modulo = new Field(computeMod(m, ks));
32
- const fieldByteLength = Math.ceil(m / 8);
33
- const mod = (f) => {
34
- const cmp = f.value.cmp(modulo.value);
35
- if (cmp === 0)
36
- return Field.get0();
37
- if (cmp < 0)
38
- return f.clone();
39
- let bag = f;
40
- const vl = modulo.value.bitLength();
41
- while (true) {
42
- bag = bag.add(modulo.shiftLeft(bag.value.bitLength() - vl));
43
- if (bag.value.bitLength() < vl)
44
- return bag;
45
- }
46
- };
47
- const mul = (x, v) => {
48
- let bag = Field.get0();
49
- let shift = x;
50
- const vLen = v.value.bitLength();
51
- for (let i = 0; i < vLen; i++) {
52
- if (v.testBit(i) == 1)
53
- bag = bag.add(shift);
54
- shift = shift.shiftLeft(1);
55
- }
56
- return mod(bag);
57
- };
58
- const trace = (x) => {
59
- let t = x;
60
- for (let i = 1; i < m; i++)
61
- t = mul(t, t).add(x);
62
- return t.testBit(0);
63
- };
64
- const traceOnb = (x, m) => {
65
- let t = 0;
66
- for (let i = 0; i < m; i++)
67
- t ^= x.value.testn(i) ? 1 : 0;
68
- return t;
69
- };
70
- const invert = (f) => {
71
- let r = mod(f), s = modulo;
72
- let u = Field.get1(), v = Field.get0();
73
- while (r.value.bitLength() > 1) {
74
- let j = s.value.bitLength() - r.value.bitLength();
75
- if (j < 0) {
76
- [r, s] = [s, r];
77
- [u, v] = [v, u];
78
- j = -j;
79
- }
80
- s = s.add(r.shiftLeft(j));
81
- v = v.add(u.shiftLeft(j));
82
- }
83
- return u;
84
- };
85
- const fsquad = (v) => {
86
- const range_to = Math.floor((m - 1) / 2);
87
- const val_a = mod(v);
88
- let val_z = val_a.clone();
89
- for (let idx = 1; idx <= range_to; idx++) {
90
- val_z = mul(val_z, val_z);
91
- val_z = mul(val_z, val_z).add(val_a);
92
- }
93
- const val_w = mul(val_z, val_z).add(val_z);
94
- if (val_w.value.cmp(val_a.value) == 0)
95
- return mod(val_z);
96
- throw new Error("squad eq fail: no square root exists");
97
- };
98
- const x = new Field(new BN(xBytes));
99
- const bit = x.testBit(0);
10
+ const bInt = field.fromHexStringOrBytes(params.b);
11
+ const b = (onb && converter) ? converter.onbToPb(bInt) : bInt;
12
+ const x = field.fromHexStringOrBytes(xBytes);
13
+ const bit = field.testBit(x, 0);
100
14
  let xClean = x.clone();
101
- xClean.setBit(0, 0);
102
- const traceX = params.onb ? traceOnb(xClean, m) : trace(xClean);
15
+ xClean = field.setBit(xClean, 0, 0);
16
+ const traceX = onb ? field.traceOnb(xClean) : field.trace(xClean);
103
17
  if ((traceX === 1 && a === 0) || (traceX === 0 && a === 1))
104
- xClean.setBit(0, 1);
18
+ xClean = field.setBit(xClean, 0, 1);
105
19
  const qxOut = xClean.clone();
106
- if (params.onb)
107
- xClean = new Field(converter.onbToPb(xClean.value));
108
- const x2 = mul(xClean, xClean);
109
- let rhs = mul(x2, xClean);
20
+ if (onb && converter)
21
+ xClean = converter.onbToPb(xClean);
22
+ const x2 = field.sqr(xClean);
23
+ let rhs = field.mul(x2, xClean);
110
24
  if (a === 1)
111
- rhs = rhs.add(x2);
25
+ rhs = rhs.xor(x2);
112
26
  if (b)
113
- rhs = rhs.add(b);
114
- const x2inv = invert(x2);
115
- const c = mul(rhs, x2inv);
116
- const z = fsquad(c);
117
- const traceZ = trace(z);
118
- if ((traceZ === 0 && bit === 1) || (traceZ === 1 && bit === 0)) {
119
- const currentBit = z.testBit(0);
120
- z.setBit(0, 1 ^ currentBit);
121
- }
122
- let y = mul(z, xClean);
123
- if (params.onb)
124
- y = new Field(converter.pbToOnb(y.value));
27
+ rhs = rhs.xor(b);
28
+ let z = field.solve_quad(field.mul(rhs, field.invert(x2)));
29
+ const traceZ = field.trace(z);
30
+ if ((traceZ === 0 && bit === 1) || (traceZ === 1 && bit === 0))
31
+ z = field.setBit(z, 0, 1 ^ field.testBit(z, 0));
32
+ let y = field.mul(z, xClean);
33
+ if (onb && converter)
34
+ y = converter.pbToOnb(y);
125
35
  return {
126
- x: new Uint8Array(qxOut.value.toArray("be", fieldByteLength)),
127
- y: new Uint8Array(y.value.toArray("be", fieldByteLength)),
36
+ x: field.toBytes(qxOut, field.LENGTH),
37
+ y: field.toBytes(y, field.LENGTH)
128
38
  };
129
39
  };