@li0ard/dstu 0.1.0 → 0.1.2

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.
@@ -1,135 +0,0 @@
1
- import BN from "bn.js";
2
- export class Field {
3
- curve = null;
4
- value;
5
- constructor(value = new BN(0), curve = null) {
6
- this.value = value;
7
- this.curve = curve;
8
- }
9
- static fromString(str, base, curve = null) {
10
- return new Field(new BN(str, base), curve);
11
- }
12
- static fromInt(v, curve = null) {
13
- return new Field(new BN(v), curve);
14
- }
15
- static fromU8(v, curve = null) {
16
- return new Field(new BN(v, 16), curve);
17
- }
18
- toString(base = 10) {
19
- return this.value.toString(base);
20
- }
21
- compare(v) {
22
- return this.value.cmp(v.value);
23
- }
24
- clone() {
25
- return Field.fromString(this.toString(16), 16, this.curve);
26
- }
27
- getLength() {
28
- return this.value.bitLength();
29
- }
30
- testBit(i) {
31
- return this.value.testn(i) ? 1 : 0;
32
- }
33
- setBit(i, v) {
34
- this.value = this.value.setn(i, v == 1);
35
- }
36
- shiftLeft(n) {
37
- if (n < 0)
38
- throw new Error("Shift amount cannot be negative.");
39
- return new Field(this.value.ushln(n), this.curve);
40
- }
41
- add(v) {
42
- return new Field(this.value.xor(v.value), this.curve || v.curve);
43
- }
44
- static hashToField(curve, hash) {
45
- return new Field(new BN(hash).maskn(curve.m), curve);
46
- }
47
- static get0(curve = null) {
48
- return new Field(new BN(0), curve);
49
- }
50
- static get1(curve = null) {
51
- return new Field(new BN(1), curve);
52
- }
53
- is0() {
54
- return this.value.isZero();
55
- }
56
- mod() {
57
- if (!this.curve)
58
- throw new Error("Curve not specified");
59
- const m = this.curve.modulo;
60
- const cmp = this.compare(m);
61
- if (cmp === 0)
62
- return Field.get0();
63
- if (cmp < 0)
64
- return this.clone();
65
- return this.div(m)[1];
66
- }
67
- mulmod(v) {
68
- return this.mul(v).mod();
69
- }
70
- mul(v) {
71
- let bag = Field.get0();
72
- let shift = this.clone();
73
- const vLen = v.getLength();
74
- for (let i = 0; i < vLen; i++) {
75
- if (v.testBit(i) == 1)
76
- bag = bag.add(shift);
77
- shift = shift.shiftLeft(1);
78
- }
79
- bag.curve = this.curve || v.curve;
80
- return bag;
81
- }
82
- trace() {
83
- if (!this.curve)
84
- throw new Error("Curve not specified");
85
- const m = this.curve.m;
86
- let t = this.clone();
87
- for (let i = 1; i < m; i++)
88
- t = t.mulmod(t).add(this);
89
- return t.testBit(0);
90
- }
91
- div(v) {
92
- let res = '';
93
- const c = this.compare(v);
94
- if (c === 0)
95
- return [Field.get1(), Field.get0()];
96
- if (c < 0)
97
- return [Field.get0(), this.clone()];
98
- let bag = this.clone();
99
- const vl = v.getLength();
100
- while (true) {
101
- const bl = bag.getLength();
102
- const shift = v.clone().shiftLeft(bl - vl);
103
- bag = bag.add(shift);
104
- res += "1";
105
- const blnew = bag.getLength();
106
- const bdiff = bl - blnew;
107
- if (blnew < vl) {
108
- res += '0'.repeat(bl - vl);
109
- return [Field.fromString(res, 2), bag];
110
- }
111
- if (bdiff > 1)
112
- res += '0'.repeat(bdiff - 1);
113
- }
114
- }
115
- invert() {
116
- let r = this.mod();
117
- let s = this.curve.modulo;
118
- let u = Field.get1(this.curve);
119
- let v = Field.get0(this.curve);
120
- while (r.getLength() > 1) {
121
- let j = s.getLength() - r.getLength();
122
- if (j < 0) {
123
- [r, s] = [s, r];
124
- [u, v] = [v, u];
125
- j = -j;
126
- }
127
- s = s.add(r.shiftLeft(j));
128
- v = v.add(u.shiftLeft(j));
129
- }
130
- return u;
131
- }
132
- buf8() {
133
- return new Uint8Array(this.value.toArray());
134
- }
135
- }
@@ -1,12 +0,0 @@
1
- import { Field } from "./field.js";
2
- export declare class Point {
3
- x: Field;
4
- y: Field;
5
- constructor(x: Field, y: Field);
6
- add(p: Point): Point;
7
- mul(f: Field): Point;
8
- negate(): Point;
9
- clone(): Point;
10
- iszero(): boolean;
11
- compress(): Field;
12
- }
@@ -1,79 +0,0 @@
1
- import { Field } from "./field.js";
2
- export class Point {
3
- x;
4
- y;
5
- constructor(x, y) {
6
- this.x = x;
7
- this.y = y;
8
- }
9
- add(p) {
10
- const curve = this.x.curve;
11
- const a = Field.fromInt(curve?.a ?? 0, curve);
12
- const pz = new Point(Field.get0(curve), Field.get0(curve));
13
- const x0 = this.x.clone();
14
- const y0 = this.y.clone();
15
- const x1 = p.x.clone();
16
- const y1 = p.y.clone();
17
- if (this.iszero())
18
- return p;
19
- if (p.iszero())
20
- return this;
21
- let lbd;
22
- let x2;
23
- if (x0.compare(x1) !== 0) {
24
- const tmp = y0.add(y1);
25
- const tmp2 = x0.add(x1);
26
- lbd = tmp.mulmod(tmp2.invert());
27
- x2 = a.add(lbd.mulmod(lbd));
28
- x2 = x2.add(lbd);
29
- x2 = x2.add(x0);
30
- x2 = x2.add(x1);
31
- }
32
- else {
33
- if (y1.compare(y0) !== 0)
34
- return pz;
35
- if (x1.compare(Field.get0()) === 0)
36
- return pz;
37
- lbd = x1.add(p.y.mulmod(p.x.invert()));
38
- x2 = lbd.mulmod(lbd).add(a);
39
- x2 = x2.add(lbd);
40
- }
41
- let y2 = lbd.mulmod(x1.add(x2));
42
- y2 = y2.add(x2);
43
- y2 = y2.add(y1);
44
- pz.x = x2.clone();
45
- pz.y = y2.clone();
46
- return pz;
47
- }
48
- mul(f) {
49
- let pz = new Point(Field.get0(f.curve), Field.get0(f.curve));
50
- let p = this.clone();
51
- for (let j = f.getLength() - 1; j >= 0; j--) {
52
- if (f.testBit(j) === 1) {
53
- pz = pz.add(p);
54
- p = p.add(p);
55
- }
56
- else {
57
- p = pz.add(p);
58
- pz = pz.add(pz);
59
- }
60
- }
61
- return pz;
62
- }
63
- negate() {
64
- return new Point(this.x, this.x.add(this.y));
65
- }
66
- clone() {
67
- return new Point(this.x, this.y);
68
- }
69
- iszero() {
70
- return this.x.is0() && this.y.is0();
71
- }
72
- compress() {
73
- const x_inv = this.x.invert();
74
- const tmp = x_inv.mulmod(this.y);
75
- const trace = tmp.trace();
76
- this.x.setBit(0, trace == 1 ? 1 : 0);
77
- return this.x;
78
- }
79
- }