finitefields 0.0.8 → 0.0.9
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 +32 -0
- package/galois.ts +3 -10
- package/main.ts +12 -6
- package/maybe.ts +40 -0
- package/package.json +1 -1
package/ellipticcurves.ts
CHANGED
|
@@ -8,12 +8,15 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import type { GF,PrimeField } from './galois.js';
|
|
11
|
+
import { just, nothing, type Maybe, type Nothing } from './maybe.js';
|
|
11
12
|
import { Util } from './galois.js';
|
|
12
13
|
|
|
14
|
+
let 𝒪:Nothing;
|
|
13
15
|
let log:(...x:any[])=>void;
|
|
14
16
|
let fail:(...x:any[])=>never;
|
|
15
17
|
let p256:()=>ECCurve;
|
|
16
18
|
|
|
19
|
+
𝒪 = nothing();
|
|
17
20
|
log = console.log;
|
|
18
21
|
fail = (...args:any[]): never => {
|
|
19
22
|
console.error(...args);
|
|
@@ -45,6 +48,7 @@ p256 = ():ECCurve => ({
|
|
|
45
48
|
});
|
|
46
49
|
|
|
47
50
|
type Tuple<a> = readonly [a,a];
|
|
51
|
+
type Coordinate = Maybe<Point>;
|
|
48
52
|
type infinity = false | true;
|
|
49
53
|
type ECformula = (a:bigint,b:bigint,f:GF)=>Function;
|
|
50
54
|
|
|
@@ -66,6 +70,8 @@ interface ECCurve extends IECCurve,Tuples,hashE {};
|
|
|
66
70
|
interface Iellipticcurves<a> {
|
|
67
71
|
backend: a;
|
|
68
72
|
constructor:Function;
|
|
73
|
+
add:(a:Coordinate,b:Coordinate)=>Coordinate;
|
|
74
|
+
double:(a:Coordinate)=>Coordinate;
|
|
69
75
|
}
|
|
70
76
|
|
|
71
77
|
interface Icurve extends IECCurve {
|
|
@@ -138,6 +144,32 @@ abstract class EllipticCurves<a> implements Iellipticcurves<a> {
|
|
|
138
144
|
public abstract backend: a;
|
|
139
145
|
private curve: Curve<a>;
|
|
140
146
|
|
|
147
|
+
protected addpoints(a:Coordinate,b:Coordinate): Coordinate {
|
|
148
|
+
return a;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
protected doublepoint(a:Coordinate): Coordinate {
|
|
152
|
+
return a;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
public add(a:Coordinate,b:Coordinate): Coordinate {
|
|
156
|
+
if ((a === 𝒪) && (b === 𝒪))
|
|
157
|
+
return <Maybe<Point>>nothing();
|
|
158
|
+
else if (a === 𝒪)
|
|
159
|
+
return <Maybe<Point>>just(b as unknown as Point);
|
|
160
|
+
else if (b === 𝒪)
|
|
161
|
+
return <Maybe<Point>>just(a as unknown as Point);
|
|
162
|
+
else
|
|
163
|
+
return <Maybe<Point>>this.addpoints(a,b);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
public double(a:Coordinate): Coordinate {
|
|
167
|
+
if (a === 𝒪)
|
|
168
|
+
return nothing();
|
|
169
|
+
else
|
|
170
|
+
return this.doublepoint(a);
|
|
171
|
+
}
|
|
172
|
+
|
|
141
173
|
constructor(curve:ECCurve) {
|
|
142
174
|
this.curve = new Curve<a>(curve);
|
|
143
175
|
|
package/galois.ts
CHANGED
|
@@ -146,8 +146,8 @@ Util = {
|
|
|
146
146
|
let a:bigint;
|
|
147
147
|
let e:bigint;
|
|
148
148
|
|
|
149
|
-
sign = (method==methods.SquareAndMultiply) ? '^' : 'x';
|
|
150
|
-
log(`${a_}${sign}${e_} (mod ${p}) = `);
|
|
149
|
+
// sign = (method==methods.SquareAndMultiply) ? '^' : 'x';
|
|
150
|
+
// log(`${a_}${sign}${e_} (mod ${p}) = `);
|
|
151
151
|
|
|
152
152
|
switch (method) {
|
|
153
153
|
case methods.SquareAndMultiply:
|
|
@@ -161,10 +161,8 @@ Util = {
|
|
|
161
161
|
|
|
162
162
|
case methods.DoubleAndAdd:
|
|
163
163
|
sq = (y:bigint,p_:bigint):bigint =>
|
|
164
|
-
// Util.binmethod(y,2n,p_,DoubleAndAdd)
|
|
165
164
|
((y+y)%p_)
|
|
166
165
|
mul = (y:bigint,z:bigint,p_:bigint):bigint =>
|
|
167
|
-
// Util.binmethod(y,z,p_,DoubleAndAdd)
|
|
168
166
|
((y+z)%p_);
|
|
169
167
|
a=e_;
|
|
170
168
|
e=a_;
|
|
@@ -184,18 +182,13 @@ Util = {
|
|
|
184
182
|
.fill(false,0,Number(bitlen))
|
|
185
183
|
.map((x:boolean,idx:number):boolean =>
|
|
186
184
|
(((e>>n--)&1n)===1n) && (!!idx));
|
|
187
|
-
|
|
188
|
-
bitstring.shift();
|
|
185
|
+
bitstring.shift();
|
|
189
186
|
// log(bitstring.map((x:boolean):number => (x)?1:0));
|
|
190
187
|
bitstring
|
|
191
188
|
.forEach((x:boolean):void => {
|
|
192
|
-
// val = (x) ?
|
|
193
|
-
// mul(a,sq(val,p),p) :
|
|
194
|
-
// sq(a,p);
|
|
195
189
|
val = (x) ?
|
|
196
190
|
mul(a,sq(val,p),p) :
|
|
197
191
|
sq(val,p);
|
|
198
|
-
// log(val);
|
|
199
192
|
|
|
200
193
|
return void 0;
|
|
201
194
|
});
|
package/main.ts
CHANGED
|
@@ -2,16 +2,22 @@
|
|
|
2
2
|
import { Polynomial } from './polynomial.js';
|
|
3
3
|
import { Util,PrimeField } from './galois.js';
|
|
4
4
|
import { ecℤp, p256 } from './ellipticcurves.js';
|
|
5
|
+
import { empty, fold, just, nothing } from './maybe.js';
|
|
6
|
+
import type { Maybe, Just, Nothing } from './maybe.js';
|
|
5
7
|
|
|
6
8
|
let log:(...x:any[])=>void;
|
|
7
9
|
log = console.log;
|
|
8
10
|
|
|
9
|
-
let
|
|
10
|
-
|
|
11
|
+
let foo:Maybe<string>;
|
|
12
|
+
foo = just(1..toString());
|
|
13
|
+
log(empty(foo));
|
|
11
14
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
// let field:PrimeField;
|
|
16
|
+
// let curve:ecℤp;
|
|
17
|
+
|
|
18
|
+
// field = new PrimeField(p256().θ, p256().bitlen);
|
|
19
|
+
// log(field);
|
|
20
|
+
// curve = new ecℤp(field, p256());
|
|
21
|
+
// log(curve);
|
|
16
22
|
|
|
17
23
|
export { Polynomial,Util,PrimeField,ecℤp,p256 };
|
package/maybe.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* maybe datatype
|
|
3
|
+
*
|
|
4
|
+
* finitefields package by @dr-Jonas-Birch
|
|
5
|
+
*
|
|
6
|
+
* 2026
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
let log:(...args:any[])=>void;
|
|
11
|
+
let fail:(...args:any[])=>never;
|
|
12
|
+
|
|
13
|
+
log = console.log;
|
|
14
|
+
fail = (...args:any[]): never => {
|
|
15
|
+
console.error(...args);
|
|
16
|
+
throw new Error(args.toString());
|
|
17
|
+
|
|
18
|
+
return void 0 as never;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type Nothing = readonly [];
|
|
22
|
+
type Just<a> = readonly [a];
|
|
23
|
+
type Maybe<a> =
|
|
24
|
+
| Nothing
|
|
25
|
+
| Just<a>;
|
|
26
|
+
|
|
27
|
+
let empty:<a>(x:Maybe<a>)=>boolean;
|
|
28
|
+
let nothing:()=>Nothing;
|
|
29
|
+
let just:<a>(x:a)=>Just<a>;
|
|
30
|
+
let fold:<a>(x:Maybe<a>)=>a|never;
|
|
31
|
+
let x:Function;
|
|
32
|
+
|
|
33
|
+
nothing = (): Nothing => [] as Nothing;
|
|
34
|
+
just = <a>(x:a): Just<a> => [x] as Just<a>;
|
|
35
|
+
empty = <a>(x:Maybe<a>): x is Nothing => !(x&&x.length);
|
|
36
|
+
fold = <a>(x:Maybe<a>): a|never =>
|
|
37
|
+
(!empty(x))?x[0] as a:fail("folding nothing") as never;
|
|
38
|
+
|
|
39
|
+
export type { Maybe, Just, Nothing };
|
|
40
|
+
export { nothing, just, empty, fold };
|