finitefields 0.0.6 → 0.0.7
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/ellipticcurves.ts +147 -0
- package/main.js +2 -1
- package/main.ts +2 -1
- package/package.json +1 -1
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* elliptic curves
|
|
3
|
+
*
|
|
4
|
+
* finitefields package by @dr-Jonas-Birch
|
|
5
|
+
*
|
|
6
|
+
* 2026
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { PrimeField } from "./galois.js";
|
|
11
|
+
|
|
12
|
+
let log:(...x:any[])=>void;
|
|
13
|
+
let fail:(...x:any[])=>never;
|
|
14
|
+
let p256:ECCurve;
|
|
15
|
+
|
|
16
|
+
log = console.log;
|
|
17
|
+
fail = (...args:any[]): never => {
|
|
18
|
+
console.error(...args);
|
|
19
|
+
throw new Error(args.toString());
|
|
20
|
+
|
|
21
|
+
return void 0 as never;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
p256 = {
|
|
25
|
+
'#E': 0xffffffff00000000_ffffffffffffffff_bce6faada7179e84_f3b9cac2fc632551n,
|
|
26
|
+
'θ': 0xffffffff00000001_0000000000000000_00000000ffffffff_ffffffffffffffffn,
|
|
27
|
+
'b': 0x5ac635d8aa3a93e7_b3ebbd55769886bc_651d06b0cc53b0f6_3bce3c3e27d2604bn,
|
|
28
|
+
'p':
|
|
29
|
+
[
|
|
30
|
+
0x6b17d1f2e12c4247_f8bce6e563a440f2_77037d812deb33a0_f4a13945d898c296n,
|
|
31
|
+
0x4fe342e2fe1a7f9b_8ee7eb4a7c0f9e16_2bce33576b315ece_cbb6406837bf51f5n
|
|
32
|
+
],
|
|
33
|
+
'q':
|
|
34
|
+
[
|
|
35
|
+
0xc97445f45cdef9f0_d3e05e1e585fc297_235b82b5be8ff3ef_ca67c59852018192n,
|
|
36
|
+
0xb28ef557ba31dfcb_dd21ac46e2a91e3c_304f44cb87058ada_2cb815151e610046n
|
|
37
|
+
]
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
type Tuple<a> = readonly [a,a];
|
|
41
|
+
type infinity = false | true;
|
|
42
|
+
|
|
43
|
+
interface Tuples {
|
|
44
|
+
'p': Tuple<bigint>;
|
|
45
|
+
'q': Tuple<bigint>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface IECCurve {
|
|
49
|
+
'θ': bigint;
|
|
50
|
+
'b': bigint;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
type hashE = {'#E':bigint};
|
|
54
|
+
interface ECCurve extends IECCurve,Tuples,hashE {};
|
|
55
|
+
|
|
56
|
+
interface Iellipticcurves<a> {
|
|
57
|
+
backend: a;
|
|
58
|
+
constructor:Function;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
interface Icurve extends IECCurve {
|
|
62
|
+
p: Point;
|
|
63
|
+
q: Point;
|
|
64
|
+
constructor:Function;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
interface Ipoint {
|
|
68
|
+
'x': bigint;
|
|
69
|
+
'y': bigint;
|
|
70
|
+
'∞': infinity;
|
|
71
|
+
constructor: Function;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
class Point implements Ipoint {
|
|
75
|
+
public 'x':bigint;
|
|
76
|
+
public 'y':bigint;
|
|
77
|
+
public '∞':infinity;
|
|
78
|
+
|
|
79
|
+
constructor(point:Tuple<bigint>) {
|
|
80
|
+
[ this['x'], this['y'], this['∞'] ] = [0n,0n,false];
|
|
81
|
+
|
|
82
|
+
switch (point.every((xy:bigint):xy is bigint=>!xy)) {
|
|
83
|
+
case true:
|
|
84
|
+
[ this['x'], this['y'] ] = point;
|
|
85
|
+
break;
|
|
86
|
+
case false:
|
|
87
|
+
(this['∞'] as unknown as number)++;
|
|
88
|
+
break;
|
|
89
|
+
default:
|
|
90
|
+
return fail();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
class Curve<a> implements Icurve {
|
|
98
|
+
#E: bigint;
|
|
99
|
+
public θ: bigint;
|
|
100
|
+
public b: bigint;
|
|
101
|
+
public p: Point;
|
|
102
|
+
public q: Point;
|
|
103
|
+
private object: EllipticCurves<a>;
|
|
104
|
+
|
|
105
|
+
constructor(object:EllipticCurves<a>,curve:ECCurve) {
|
|
106
|
+
let m:string;
|
|
107
|
+
|
|
108
|
+
m = 'argument error (incomplete curve params)' as const;
|
|
109
|
+
this.#E = curve['#E']
|
|
110
|
+
|| fail(m);
|
|
111
|
+
this.θ = curve.θ
|
|
112
|
+
|| fail(m);
|
|
113
|
+
this.b = curve.b
|
|
114
|
+
|| fail(m);
|
|
115
|
+
this.p = new Point(curve.p);
|
|
116
|
+
this.q = new Point(curve.q);
|
|
117
|
+
this.object = object;
|
|
118
|
+
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
abstract class EllipticCurves<a> implements Iellipticcurves<a> {
|
|
124
|
+
public abstract backend: a;
|
|
125
|
+
private curve: Curve<a>;
|
|
126
|
+
|
|
127
|
+
constructor(curve:ECCurve) {
|
|
128
|
+
this.curve = new Curve<a>(this,curve);
|
|
129
|
+
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
class ECoverℤp extends EllipticCurves<PrimeField>
|
|
135
|
+
implements Iellipticcurves<PrimeField>
|
|
136
|
+
{
|
|
137
|
+
public backend:PrimeField;
|
|
138
|
+
|
|
139
|
+
constructor(field:PrimeField, curve:ECCurve) {
|
|
140
|
+
super(curve);
|
|
141
|
+
this.backend = field;
|
|
142
|
+
|
|
143
|
+
return this;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export { ECoverℤp };
|
package/main.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/* main.ts */
|
|
2
2
|
import { Polynomial } from './polynomial.js';
|
|
3
3
|
import { Util, PrimeField } from './galois.js';
|
|
4
|
+
import { ECoverℤp } from './ellipticcurves.js';
|
|
4
5
|
let log;
|
|
5
6
|
log = console.log;
|
|
6
7
|
let p1;
|
|
@@ -21,5 +22,5 @@ p2 = new Polynomial({ tobject: 0n, x3: 1n, x2: 0n, x: 1n, 1: 1n }, 2n);
|
|
|
21
22
|
p3 = new Polynomial(1n << 254n, 2n);
|
|
22
23
|
log(p3, p3.eval());
|
|
23
24
|
// log(p3.eval());
|
|
24
|
-
export { Polynomial, Util, PrimeField };
|
|
25
|
+
export { Polynomial, Util, PrimeField, ECoverℤp };
|
|
25
26
|
//# sourceMappingURL=main.js.map
|
package/main.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/* main.ts */
|
|
2
2
|
import { Polynomial } from './polynomial.js';
|
|
3
3
|
import { Util,PrimeField } from './galois.js';
|
|
4
|
+
import { ECoverℤp } from './ellipticcurves.js';
|
|
4
5
|
|
|
5
6
|
let log:(...x:any[])=>void;
|
|
6
7
|
log = console.log;
|
|
@@ -27,4 +28,4 @@ p3 = new Polynomial(1n<<254n, 2n);
|
|
|
27
28
|
log(p3, p3.eval());
|
|
28
29
|
// log(p3.eval());
|
|
29
30
|
|
|
30
|
-
export { Polynomial,Util,PrimeField };
|
|
31
|
+
export { Polynomial,Util,PrimeField,ECoverℤp };
|