functionalscript 0.2.1 → 0.2.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/CHANGELOG.md +4 -0
- package/com/cpp/module.f.mjs +10 -1
- package/com/cs/module.f.mjs +11 -1
- package/com/rust/module.f.mjs +13 -1
- package/dev/module.mjs +1 -1
- package/issues/lang/3370-type-inference.md +69 -0
- package/issues/lang/3380-promise.md +22 -0
- package/issues/lang/3390-class.md +3 -0
- package/issues/lang/README.md +79 -8
- package/jsr.json +60 -59
- package/out/com/cpp/module.f.d.mts +4 -1
- package/out/com/cpp/module.f.mjs +8 -1
- package/out/com/cs/module.f.d.mts +4 -1
- package/out/com/cs/module.f.mjs +10 -1
- package/out/com/rust/module.f.d.mts +1 -2
- package/out/com/rust/module.f.mjs +12 -1
- package/out/dev/module.mjs +1 -1
- package/out/prime_field/module.f.d.mts +28 -2
- package/out/prime_field/module.f.mjs +25 -2
- package/out/prime_field/test.f.d.mts +1 -0
- package/out/prime_field/test.f.mjs +7 -0
- package/out/secp/module.f.d.mts +68 -6
- package/out/secp/module.f.mjs +72 -12
- package/out/secp/test.f.d.mts +1 -0
- package/out/secp/test.f.mjs +18 -3
- package/out/types/bigint/test.f.mjs +7 -0
- package/out/types/bit_vec/module.f.d.mts +72 -0
- package/out/types/bit_vec/module.f.mjs +83 -0
- package/out/types/bit_vec/test.f.d.mts +9 -0
- package/out/types/bit_vec/test.f.mjs +60 -0
- package/package.json +1 -1
- package/prime_field/module.f.mjs +25 -2
- package/prime_field/test.f.mjs +5 -0
- package/secp/module.f.mjs +73 -12
- package/secp/test.f.mjs +18 -3
- package/types/bigint/test.f.mjs +5 -0
- package/types/bit_vec/module.f.mjs +90 -0
- package/types/bit_vec/test.f.mjs +41 -0
- /package/issues/lang/{3310-expression.md → 3410-expression.md} +0 -0
- /package/issues/lang/{3320-one-parameter.md → 3420-one-parameter.md} +0 -0
- /package/issues/lang/{3330-assignments.md → 3430-assignments.md} +0 -0
package/secp/module.f.mjs
CHANGED
|
@@ -5,11 +5,22 @@ import * as bi from '../types/bigint/module.f.mjs'
|
|
|
5
5
|
const { scalar_mul } = bi
|
|
6
6
|
const { prime_field, sqrt } = pf
|
|
7
7
|
|
|
8
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* A 2D point represented as a pair of `bigint` values `[x, y]`.
|
|
10
|
+
*
|
|
11
|
+
* @typedef {readonly[bigint, bigint]} Point2D
|
|
12
|
+
*/
|
|
9
13
|
|
|
10
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* A 2D point on an elliptic curve, represented as a pair of `bigint` values.
|
|
16
|
+
* `null` represents the point at infinity (`O`).
|
|
17
|
+
*
|
|
18
|
+
* @typedef {Point2D|null} Point
|
|
19
|
+
*/
|
|
11
20
|
|
|
12
21
|
/**
|
|
22
|
+
* Initialization parameters for an elliptic curve.
|
|
23
|
+
*
|
|
13
24
|
* @typedef {{
|
|
14
25
|
* readonly p: bigint
|
|
15
26
|
* readonly a: readonly[bigint, bigint]
|
|
@@ -19,6 +30,8 @@ const { prime_field, sqrt } = pf
|
|
|
19
30
|
*/
|
|
20
31
|
|
|
21
32
|
/**
|
|
33
|
+
* Represents an elliptic curve and its associated operations.
|
|
34
|
+
*
|
|
22
35
|
* @typedef {{
|
|
23
36
|
* readonly pf: pf.PrimeField
|
|
24
37
|
* readonly nf: pf.PrimeField
|
|
@@ -30,7 +43,28 @@ const { prime_field, sqrt } = pf
|
|
|
30
43
|
* }} Curve
|
|
31
44
|
*/
|
|
32
45
|
|
|
33
|
-
/**
|
|
46
|
+
/**
|
|
47
|
+
* Constructs an elliptic curve with the given initialization parameters.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
*
|
|
51
|
+
* ```js
|
|
52
|
+
* const curveParams = {
|
|
53
|
+
* p: 23n,
|
|
54
|
+
* a: [0n, 1n],
|
|
55
|
+
* g: [1n, 1n],
|
|
56
|
+
* n: 19n
|
|
57
|
+
* };
|
|
58
|
+
* const curveInstance = curve(curveParams);
|
|
59
|
+
*
|
|
60
|
+
* // Access curve operations
|
|
61
|
+
* const point = curveInstance.add([1n, 1n])([2n, 5n]); // Add two points
|
|
62
|
+
* const negPoint = curveInstance.neg([1n, 1n]); // Negate a point
|
|
63
|
+
* const mulPoint = curveInstance.mul([1n, 1n])(3n); // Multiply a point by 3
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @type {(i: Init) => Curve}
|
|
67
|
+
*/
|
|
34
68
|
export const curve = ({ p, a: [a0, a1], n }) => {
|
|
35
69
|
const pf = prime_field(p)
|
|
36
70
|
const { pow2, pow3, sub, add, mul, neg, div } = pf
|
|
@@ -95,7 +129,30 @@ export const eq = a => b => {
|
|
|
95
129
|
return ax === bx && ay === by
|
|
96
130
|
}
|
|
97
131
|
|
|
98
|
-
/**
|
|
132
|
+
/**
|
|
133
|
+
* https://neuromancer.sk/std/secg/secp192r1
|
|
134
|
+
*
|
|
135
|
+
* @type {Init}
|
|
136
|
+
*/
|
|
137
|
+
export const secp192r1 = {
|
|
138
|
+
p: 0xffffffff_ffffffff_ffffffff_fffffffe_ffffffff_ffffffffn,
|
|
139
|
+
a: [
|
|
140
|
+
0x64210519_e59c80e7_0fa7e9ab_72243049_feb8deec_c146b9b1n,
|
|
141
|
+
0xffffffff_ffffffff_ffffffff_fffffffe_ffffffff_fffffffcn
|
|
142
|
+
],
|
|
143
|
+
g: [
|
|
144
|
+
0x188da80e_b03090f6_7cbf20eb_43a18800_f4ff0afd_82ff1012n,
|
|
145
|
+
0x07192b95_ffc8da78_631011ed_6b24cdd5_73f977a1_1e794811n
|
|
146
|
+
],
|
|
147
|
+
n: 0xffffffff_ffffffff_ffffffff_99def836_146bc9b1_b4d22831n,
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* https://en.bitcoin.it/wiki/Secp256k1
|
|
152
|
+
* https://neuromancer.sk/std/secg/secp256k1
|
|
153
|
+
*
|
|
154
|
+
* @type {Init}
|
|
155
|
+
*/
|
|
99
156
|
export const secp256k1 = {
|
|
100
157
|
p: 0xffffffff_ffffffff_ffffffff_ffffffff_ffffffff_ffffffff_fffffffe_fffffc2fn,
|
|
101
158
|
a: [7n, 0n],
|
|
@@ -106,16 +163,20 @@ export const secp256k1 = {
|
|
|
106
163
|
n: 0xffffffff_ffffffff_ffffffff_fffffffe_baaedce6_af48a03b_bfd25e8c_d0364141n,
|
|
107
164
|
}
|
|
108
165
|
|
|
109
|
-
/**
|
|
110
|
-
|
|
111
|
-
|
|
166
|
+
/**
|
|
167
|
+
* https://neuromancer.sk/std/secg/secp256r1
|
|
168
|
+
*
|
|
169
|
+
* @type { Init }
|
|
170
|
+
*/
|
|
171
|
+
export const secp256r1 ={
|
|
172
|
+
p: 0xffffffff_00000001_00000000_00000000_00000000_ffffffff_ffffffff_ffffffffn,
|
|
112
173
|
a: [
|
|
113
|
-
|
|
114
|
-
|
|
174
|
+
0x5ac635d8_aa3a93e7_b3ebbd55_769886bc_651d06b0_cc53b0f6_3bce3c3e_27d2604bn, //< b
|
|
175
|
+
0xffffffff_00000001_00000000_00000000_00000000_ffffffff_ffffffff_fffffffcn, //< a
|
|
115
176
|
],
|
|
116
177
|
g: [
|
|
117
|
-
|
|
118
|
-
|
|
178
|
+
0x6b17d1f2_e12c4247_f8bce6e5_63a440f2_77037d81_2deb33a0_f4a13945_d898c296n, //< x
|
|
179
|
+
0x4fe342e2_fe1a7f9b_8ee7eb4a_7c0f9e16_2bce3357_6b315ece_cbb64068_37bf51f5n, //< y
|
|
119
180
|
],
|
|
120
|
-
n:
|
|
181
|
+
n: 0xffffffff_00000000_ffffffff_ffffffff_bce6faad_a7179e84_f3b9cac2_fc632551n,
|
|
121
182
|
}
|
package/secp/test.f.mjs
CHANGED
|
@@ -1,7 +1,21 @@
|
|
|
1
1
|
import * as _ from './module.f.mjs'
|
|
2
|
-
const { curve, secp256k1, secp192r1, eq } = _
|
|
2
|
+
const { curve, secp256k1, secp192r1, secp256r1, eq } = _
|
|
3
3
|
|
|
4
4
|
export default {
|
|
5
|
+
example: () => {
|
|
6
|
+
/** @type {_.Init} */
|
|
7
|
+
const curveParams = {
|
|
8
|
+
p: 23n,
|
|
9
|
+
a: [0n, 1n],
|
|
10
|
+
g: [1n, 1n],
|
|
11
|
+
n: 19n
|
|
12
|
+
}
|
|
13
|
+
const c = curve(curveParams)
|
|
14
|
+
// Access curve operations
|
|
15
|
+
const point = c.add([1n, 1n])([2n, 5n]); // Add two points
|
|
16
|
+
const negPoint = c.neg([1n, 1n]); // Negate a point
|
|
17
|
+
const mulPoint = c.mul([1n, 1n])(3n); // Multiply a point by 3
|
|
18
|
+
},
|
|
5
19
|
test: () => {
|
|
6
20
|
/** @type {(c: _.Init) => void} */
|
|
7
21
|
const test_curve = c => {
|
|
@@ -9,10 +23,10 @@ export default {
|
|
|
9
23
|
const { mul, neg, pf: { abs }, y: yf, nf: { p: n } } = curve(c)
|
|
10
24
|
/** @type {(p: _.Point) => void} */
|
|
11
25
|
const point_check = p => {
|
|
12
|
-
if (p === null) { throw 'null' }
|
|
26
|
+
if (p === null) { throw 'p === null' }
|
|
13
27
|
const [x, y] = p
|
|
14
28
|
const ye = yf(x)
|
|
15
|
-
if (ye === null) { throw 'null' }
|
|
29
|
+
if (ye === null) { throw 'ye === null' }
|
|
16
30
|
if (abs(ye) !== abs(y)) { throw 'ye' }
|
|
17
31
|
}
|
|
18
32
|
point_check(g)
|
|
@@ -44,5 +58,6 @@ export default {
|
|
|
44
58
|
}
|
|
45
59
|
test_curve(secp256k1)
|
|
46
60
|
test_curve(secp192r1)
|
|
61
|
+
test_curve(secp256r1)
|
|
47
62
|
}
|
|
48
63
|
}
|
package/types/bigint/test.f.mjs
CHANGED
|
@@ -77,6 +77,11 @@ export default {
|
|
|
77
77
|
const result = log2(1n << v)
|
|
78
78
|
if (result !== v) { throw result }
|
|
79
79
|
},
|
|
80
|
+
() => {
|
|
81
|
+
const v = 0x18945n
|
|
82
|
+
const result = log2(v)
|
|
83
|
+
if (result !== 16n) { throw result }
|
|
84
|
+
}
|
|
80
85
|
],
|
|
81
86
|
toString2: () => {
|
|
82
87
|
// max for Bun (131_072 Bytes)
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// @ts-self-types="./module.f.d.mts"
|
|
2
|
+
import { log2 } from '../bigint/module.f.mjs'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A vector of bits represented as a `bigint`.
|
|
6
|
+
*
|
|
7
|
+
* @typedef {bigint} Vec
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* An empty vector of bits.
|
|
12
|
+
*/
|
|
13
|
+
export const empty = 1n
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Calculates the length of the given vector of bits.
|
|
17
|
+
*/
|
|
18
|
+
export const len = log2
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Extract an unsigned integer of the given `uintLen` from the given vector.
|
|
22
|
+
*
|
|
23
|
+
* @type {(uintLen: bigint) => (v: Vec) => bigint}
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
*
|
|
27
|
+
* ```js
|
|
28
|
+
* const vector = 0b110101n;
|
|
29
|
+
* const extract3Bits = uint(3n);
|
|
30
|
+
* const result = extract3Bits(vector); // result is 0b101n (5n)
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export const uint = uintLen => {
|
|
34
|
+
const mask = (1n << uintLen) - 1n
|
|
35
|
+
return data => data & mask
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Creates a vector of bits of the given `vecLen` from the given unsigned integer.
|
|
40
|
+
*
|
|
41
|
+
* @type {(vecLen: bigint) => (ui: bigint) => Vec}
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
*
|
|
45
|
+
* ```js
|
|
46
|
+
* const createVector = vec(4n);
|
|
47
|
+
* const vector = createVector(5n); // vector is 0b10101n
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export const vec = vecLen => {
|
|
51
|
+
const stop = 1n << vecLen
|
|
52
|
+
const mask = stop - 1n
|
|
53
|
+
return data => (data & mask) | stop
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Appends the vector `b` to the back of the vector `a`.
|
|
58
|
+
*
|
|
59
|
+
* @type {(a: Vec) => (b: Vec) => Vec}
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
*
|
|
63
|
+
* ```js
|
|
64
|
+
* const vec8 = vec(8n)
|
|
65
|
+
* const a = vec8(0x345n)
|
|
66
|
+
* const b = vec8(0x789n)
|
|
67
|
+
* const ab = appendBack(a)(b) // 0x18945n
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export const appendBack = a => {
|
|
71
|
+
const aLen = len(a)
|
|
72
|
+
return b => a | (b << aLen)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Removes the first `len` bits from the given vector.
|
|
77
|
+
*
|
|
78
|
+
* @type {(len: bigint) => (v: Vec) => Vec}
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
*
|
|
82
|
+
* ```js
|
|
83
|
+
* const v = vec(17n)(0x12345n) // v = 0x32345n
|
|
84
|
+
* const r = removeFront(9n)(v) // r = 0x191n
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export const removeFront = len => v => {
|
|
88
|
+
const r = v >> len
|
|
89
|
+
return r === 0n ? empty : r
|
|
90
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { empty, len, appendBack, vec, uint, removeFront } from './module.f.mjs'
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
uintExample: () => {
|
|
5
|
+
const vector = 0b110101n;
|
|
6
|
+
const extract3Bits = uint(3n);
|
|
7
|
+
const result = extract3Bits(vector); // result is 0b101n (5n)
|
|
8
|
+
if (result !== 0b101n) { throw result }
|
|
9
|
+
},
|
|
10
|
+
vecExample: () => {
|
|
11
|
+
const createVector = vec(4n);
|
|
12
|
+
const vector = createVector(5n); // vector is 0b10101n
|
|
13
|
+
if (vector !== 0b10101n) { throw vector }
|
|
14
|
+
},
|
|
15
|
+
length: () => {
|
|
16
|
+
const i = len(empty)
|
|
17
|
+
if (i !== 0n) { throw i}
|
|
18
|
+
},
|
|
19
|
+
bitset: () => {
|
|
20
|
+
const v = vec(8n)(0x5FEn)
|
|
21
|
+
if (v !== 0x1FEn) { throw v }
|
|
22
|
+
if (len(v) !== 8n) { throw 'len' }
|
|
23
|
+
const u = uint(8n)(v)
|
|
24
|
+
if (u !== 0xFEn) { throw v }
|
|
25
|
+
},
|
|
26
|
+
appendBack: () => {
|
|
27
|
+
const vec8 = vec(8n)
|
|
28
|
+
const a = vec8(0x345n)
|
|
29
|
+
const b = vec8(0x789n)
|
|
30
|
+
const ab = appendBack(a)(b)
|
|
31
|
+
if (ab !== 0x18945n) { throw ab }
|
|
32
|
+
const s = len(ab)
|
|
33
|
+
if (s !== 16n) { throw `appendBack: ${s}` }
|
|
34
|
+
},
|
|
35
|
+
removeBack: () => {
|
|
36
|
+
const v = vec(17n)(0x12345n)
|
|
37
|
+
if (v !== 0x32345n) { throw v.toString(16) }
|
|
38
|
+
const r = removeFront(9n)(v)
|
|
39
|
+
if (r !== 0x191n) { throw r.toString(16) }
|
|
40
|
+
}
|
|
41
|
+
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|