functionalscript 0.2.0 → 0.2.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.
- package/dev/module.mjs +1 -1
- package/jsr.json +59 -59
- package/out/com/cpp/module.f.mjs +123 -0
- package/out/com/cpp/test.f.mjs +40 -0
- package/out/com/cpp/testlib.f.mjs +7 -0
- package/out/com/cs/module.f.mjs +95 -0
- package/out/com/cs/test.f.mjs +43 -0
- package/out/com/cs/testlib.f.mjs +7 -0
- package/out/com/rust/module.f.mjs +213 -0
- package/out/com/rust/test.f.mjs +123 -0
- package/out/com/rust/testlib.f.mjs +7 -0
- package/out/com/test/build.f.mjs +98 -0
- package/out/com/test/build.mjs +40 -0
- package/out/com/types/module.f.mjs +51 -0
- package/out/com/types/testlib.f.mjs +30 -0
- package/out/commonjs/build/module.f.mjs +107 -0
- package/out/commonjs/build/test.f.mjs +102 -0
- package/out/commonjs/module/function/module.f.mjs +15 -0
- package/out/commonjs/module/module.f.mjs +48 -0
- package/out/commonjs/module.f.mjs +10 -0
- package/out/commonjs/module.mjs +26 -0
- package/out/commonjs/package/dependencies/module.f.mjs +21 -0
- package/out/commonjs/package/dependencies/test.f.mjs +15 -0
- package/out/commonjs/package/module.f.mjs +40 -0
- package/out/commonjs/package/test.f.mjs +27 -0
- package/out/commonjs/path/module.f.mjs +171 -0
- package/out/commonjs/path/test.f.mjs +231 -0
- package/out/commonjs/test.mjs +87 -0
- package/out/dev/index.mjs +2 -0
- package/out/dev/module.f.mjs +2 -0
- package/out/dev/module.mjs +167 -0
- package/out/dev/test/module.f.mjs +134 -0
- package/out/dev/test.f.mjs +58 -0
- package/out/dev/test.mjs +52 -0
- package/out/djs/module.f.mjs +75 -0
- package/out/djs/parser/module.f.mjs +432 -0
- package/out/djs/parser/test.f.mjs +535 -0
- package/out/djs/test.f.mjs +84 -0
- package/out/djs/tokenizer/module.f.mjs +87 -0
- package/out/djs/tokenizer/test.f.mjs +480 -0
- package/out/fsc/module.f.mjs +105 -0
- package/out/fsc/test.f.mjs +19 -0
- package/out/fsm/module.f.mjs +80 -0
- package/out/fsm/test.f.mjs +138 -0
- package/out/html/module.f.mjs +94 -0
- package/out/html/test.f.mjs +45 -0
- package/out/issues/test.f.mjs +66 -0
- package/out/js/tokenizer/module.f.mjs +686 -0
- package/out/js/tokenizer/test.f.mjs +844 -0
- package/out/json/module.f.mjs +89 -0
- package/out/json/parser/module.f.mjs +224 -0
- package/out/json/parser/test.f.mjs +321 -0
- package/out/json/serializer/module.f.mjs +67 -0
- package/out/json/serializer/test.f.mjs +87 -0
- package/out/json/test.f.mjs +61 -0
- package/out/json/tokenizer/module.f.mjs +78 -0
- package/out/json/tokenizer/test.f.mjs +420 -0
- package/out/nanvm-lib/tests/test.f.mjs +87 -0
- package/out/nodejs/version/main.mjs +3 -0
- package/out/nodejs/version/module.f.mjs +34 -0
- package/out/nodejs/version/test.f.mjs +97 -0
- package/out/prime_field/module.f.mjs +87 -0
- package/out/prime_field/test.f.mjs +145 -0
- package/out/secp/module.f.mjs +113 -0
- package/out/secp/test.f.mjs +63 -0
- package/out/sha2/module.f.mjs +172 -0
- package/out/sha2/test.f.mjs +86 -0
- package/out/text/ascii/module.f.mjs +154 -0
- package/out/text/ascii/test.f.mjs +14 -0
- package/out/text/module.f.mjs +19 -0
- package/out/text/sgr/module.f.mjs +17 -0
- package/out/text/test.f.mjs +19 -0
- package/out/text/utf16/module.f.mjs +86 -0
- package/out/text/utf16/test.f.mjs +145 -0
- package/out/text/utf8/module.f.mjs +126 -0
- package/out/text/utf8/test.f.mjs +175 -0
- package/out/types/array/module.f.mjs +95 -0
- package/out/types/array/test.f.mjs +116 -0
- package/out/types/bigfloat/module.f.mjs +77 -0
- package/out/types/bigfloat/test.f.mjs +349 -0
- package/out/types/bigint/module.f.mjs +114 -0
- package/out/types/bigint/test.f.mjs +192 -0
- package/out/types/btree/find/module.f.mjs +137 -0
- package/out/types/btree/find/test.f.mjs +156 -0
- package/out/types/btree/module.f.mjs +34 -0
- package/out/types/btree/remove/module.f.mjs +209 -0
- package/out/types/btree/remove/test.f.mjs +638 -0
- package/out/types/btree/set/module.f.mjs +114 -0
- package/out/types/btree/set/test.f.mjs +390 -0
- package/out/types/btree/test.f.mjs +83 -0
- package/out/types/btree/types/module.f.mjs +50 -0
- package/out/types/byte_set/module.f.mjs +42 -0
- package/out/types/byte_set/test.f.mjs +123 -0
- package/out/types/function/compare/module.f.mjs +22 -0
- package/out/types/function/compare/test.f.mjs +8 -0
- package/out/types/function/module.f.mjs +44 -0
- package/out/types/function/operator/module.f.mjs +60 -0
- package/out/types/function/test.f.mjs +15 -0
- package/out/types/list/module.f.mjs +269 -0
- package/out/types/list/test.f.mjs +401 -0
- package/out/types/map/module.f.mjs +54 -0
- package/out/types/map/test.f.mjs +115 -0
- package/out/types/nibble_set/module.f.mjs +19 -0
- package/out/types/nibble_set/test.f.mjs +90 -0
- package/out/types/nullable/module.f.mjs +9 -0
- package/out/types/nullable/test.f.mjs +12 -0
- package/out/types/number/module.f.mjs +12 -0
- package/out/types/number/test.f.mjs +126 -0
- package/out/types/object/module.f.mjs +27 -0
- package/out/types/object/test.f.mjs +17 -0
- package/out/types/range/module.f.mjs +6 -0
- package/out/types/range/test.f.mjs +18 -0
- package/out/types/range_map/module.f.mjs +84 -0
- package/out/types/range_map/test.f.mjs +201 -0
- package/out/types/result/module.f.mjs +25 -0
- package/out/types/result/module.mjs +16 -0
- package/out/types/sorted_list/module.f.mjs +102 -0
- package/out/types/sorted_list/test.f.mjs +66 -0
- package/out/types/sorted_set/module.f.mjs +29 -0
- package/out/types/sorted_set/test.f.mjs +80 -0
- package/out/types/string/module.f.mjs +17 -0
- package/out/types/string/test.f.mjs +58 -0
- package/out/types/string_set/module.f.mjs +29 -0
- package/out/types/string_set/test.f.mjs +65 -0
- package/package.json +5 -4
- package/tsconfig.json +2 -2
- /package/{com → out/com}/cpp/module.f.d.mts +0 -0
- /package/{com → out/com}/cpp/test.f.d.mts +0 -0
- /package/{com → out/com}/cpp/testlib.f.d.mts +0 -0
- /package/{com → out/com}/cs/module.f.d.mts +0 -0
- /package/{com → out/com}/cs/test.f.d.mts +0 -0
- /package/{com → out/com}/cs/testlib.f.d.mts +0 -0
- /package/{com → out/com}/rust/module.f.d.mts +0 -0
- /package/{com → out/com}/rust/test.f.d.mts +0 -0
- /package/{com → out/com}/rust/testlib.f.d.mts +0 -0
- /package/{com → out/com}/test/build.d.mts +0 -0
- /package/{com → out/com}/test/build.f.d.mts +0 -0
- /package/{com → out/com}/types/module.f.d.mts +0 -0
- /package/{com → out/com}/types/testlib.f.d.mts +0 -0
- /package/{commonjs → out/commonjs}/build/module.f.d.mts +0 -0
- /package/{commonjs → out/commonjs}/build/test.f.d.mts +0 -0
- /package/{commonjs → out/commonjs}/module/function/module.f.d.mts +0 -0
- /package/{commonjs → out/commonjs}/module/module.f.d.mts +0 -0
- /package/{commonjs → out/commonjs}/module.d.mts +0 -0
- /package/{commonjs → out/commonjs}/module.f.d.mts +0 -0
- /package/{commonjs → out/commonjs}/package/dependencies/module.f.d.mts +0 -0
- /package/{commonjs → out/commonjs}/package/dependencies/test.f.d.mts +0 -0
- /package/{commonjs → out/commonjs}/package/module.f.d.mts +0 -0
- /package/{commonjs → out/commonjs}/package/test.f.d.mts +0 -0
- /package/{commonjs → out/commonjs}/path/module.f.d.mts +0 -0
- /package/{commonjs → out/commonjs}/path/test.f.d.mts +0 -0
- /package/{commonjs → out/commonjs}/test.d.mts +0 -0
- /package/{dev → out/dev}/index.d.mts +0 -0
- /package/{dev → out/dev}/module.d.mts +0 -0
- /package/{dev → out/dev}/module.f.d.mts +0 -0
- /package/{dev → out/dev}/test/module.f.d.mts +0 -0
- /package/{dev → out/dev}/test.d.mts +0 -0
- /package/{dev → out/dev}/test.f.d.mts +0 -0
- /package/{djs → out/djs}/module.f.d.mts +0 -0
- /package/{djs → out/djs}/parser/module.f.d.mts +0 -0
- /package/{djs → out/djs}/parser/test.f.d.mts +0 -0
- /package/{djs → out/djs}/test.f.d.mts +0 -0
- /package/{djs → out/djs}/tokenizer/module.f.d.mts +0 -0
- /package/{djs → out/djs}/tokenizer/test.f.d.mts +0 -0
- /package/{fsc → out/fsc}/module.f.d.mts +0 -0
- /package/{fsc → out/fsc}/test.f.d.mts +0 -0
- /package/{fsm → out/fsm}/module.f.d.mts +0 -0
- /package/{fsm → out/fsm}/test.f.d.mts +0 -0
- /package/{html → out/html}/module.f.d.mts +0 -0
- /package/{html → out/html}/test.f.d.mts +0 -0
- /package/{issues → out/issues}/test.f.d.mts +0 -0
- /package/{js → out/js}/tokenizer/module.f.d.mts +0 -0
- /package/{js → out/js}/tokenizer/test.f.d.mts +0 -0
- /package/{json → out/json}/module.f.d.mts +0 -0
- /package/{json → out/json}/parser/module.f.d.mts +0 -0
- /package/{json → out/json}/parser/test.f.d.mts +0 -0
- /package/{json → out/json}/serializer/module.f.d.mts +0 -0
- /package/{json → out/json}/serializer/test.f.d.mts +0 -0
- /package/{json → out/json}/test.f.d.mts +0 -0
- /package/{json → out/json}/tokenizer/module.f.d.mts +0 -0
- /package/{json → out/json}/tokenizer/test.f.d.mts +0 -0
- /package/{nanvm-lib → out/nanvm-lib}/tests/test.f.d.mts +0 -0
- /package/{nodejs → out/nodejs}/version/main.d.mts +0 -0
- /package/{nodejs → out/nodejs}/version/module.f.d.mts +0 -0
- /package/{nodejs → out/nodejs}/version/test.f.d.mts +0 -0
- /package/{prime_field → out/prime_field}/module.f.d.mts +0 -0
- /package/{prime_field → out/prime_field}/test.f.d.mts +0 -0
- /package/{secp → out/secp}/module.f.d.mts +0 -0
- /package/{secp → out/secp}/test.f.d.mts +0 -0
- /package/{sha2 → out/sha2}/module.f.d.mts +0 -0
- /package/{sha2 → out/sha2}/test.f.d.mts +0 -0
- /package/{text → out/text}/ascii/module.f.d.mts +0 -0
- /package/{text → out/text}/ascii/test.f.d.mts +0 -0
- /package/{text → out/text}/module.f.d.mts +0 -0
- /package/{text → out/text}/sgr/module.f.d.mts +0 -0
- /package/{text → out/text}/test.f.d.mts +0 -0
- /package/{text → out/text}/utf16/module.f.d.mts +0 -0
- /package/{text → out/text}/utf16/test.f.d.mts +0 -0
- /package/{text → out/text}/utf8/module.f.d.mts +0 -0
- /package/{text → out/text}/utf8/test.f.d.mts +0 -0
- /package/{types → out/types}/array/module.f.d.mts +0 -0
- /package/{types → out/types}/array/test.f.d.mts +0 -0
- /package/{types → out/types}/bigfloat/module.f.d.mts +0 -0
- /package/{types → out/types}/bigfloat/test.f.d.mts +0 -0
- /package/{types → out/types}/bigint/module.f.d.mts +0 -0
- /package/{types → out/types}/bigint/test.f.d.mts +0 -0
- /package/{types → out/types}/btree/find/module.f.d.mts +0 -0
- /package/{types → out/types}/btree/find/test.f.d.mts +0 -0
- /package/{types → out/types}/btree/module.f.d.mts +0 -0
- /package/{types → out/types}/btree/remove/module.f.d.mts +0 -0
- /package/{types → out/types}/btree/remove/test.f.d.mts +0 -0
- /package/{types → out/types}/btree/set/module.f.d.mts +0 -0
- /package/{types → out/types}/btree/set/test.f.d.mts +0 -0
- /package/{types → out/types}/btree/test.f.d.mts +0 -0
- /package/{types → out/types}/btree/types/module.f.d.mts +0 -0
- /package/{types → out/types}/byte_set/module.f.d.mts +0 -0
- /package/{types → out/types}/byte_set/test.f.d.mts +0 -0
- /package/{types → out/types}/function/compare/module.f.d.mts +0 -0
- /package/{types → out/types}/function/compare/test.f.d.mts +0 -0
- /package/{types → out/types}/function/module.f.d.mts +0 -0
- /package/{types → out/types}/function/operator/module.f.d.mts +0 -0
- /package/{types → out/types}/function/test.f.d.mts +0 -0
- /package/{types → out/types}/list/module.f.d.mts +0 -0
- /package/{types → out/types}/list/test.f.d.mts +0 -0
- /package/{types → out/types}/map/module.f.d.mts +0 -0
- /package/{types → out/types}/map/test.f.d.mts +0 -0
- /package/{types → out/types}/nibble_set/module.f.d.mts +0 -0
- /package/{types → out/types}/nibble_set/test.f.d.mts +0 -0
- /package/{types → out/types}/nullable/module.f.d.mts +0 -0
- /package/{types → out/types}/nullable/test.f.d.mts +0 -0
- /package/{types → out/types}/number/module.f.d.mts +0 -0
- /package/{types → out/types}/number/test.f.d.mts +0 -0
- /package/{types → out/types}/object/module.f.d.mts +0 -0
- /package/{types → out/types}/object/test.f.d.mts +0 -0
- /package/{types → out/types}/range/module.f.d.mts +0 -0
- /package/{types → out/types}/range/test.f.d.mts +0 -0
- /package/{types → out/types}/range_map/module.f.d.mts +0 -0
- /package/{types → out/types}/range_map/test.f.d.mts +0 -0
- /package/{types → out/types}/result/module.d.mts +0 -0
- /package/{types → out/types}/result/module.f.d.mts +0 -0
- /package/{types → out/types}/sorted_list/module.f.d.mts +0 -0
- /package/{types → out/types}/sorted_list/test.f.d.mts +0 -0
- /package/{types → out/types}/sorted_set/module.f.d.mts +0 -0
- /package/{types → out/types}/sorted_set/test.f.d.mts +0 -0
- /package/{types → out/types}/string/module.f.d.mts +0 -0
- /package/{types → out/types}/string/test.f.d.mts +0 -0
- /package/{types → out/types}/string_set/module.f.d.mts +0 -0
- /package/{types → out/types}/string_set/test.f.d.mts +0 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// @ts-self-types="./module.f.d.mts"
|
|
2
|
+
import * as Operator from '../types/function/operator/module.f.mjs';
|
|
3
|
+
import * as bi from '../types/bigint/module.f.mjs';
|
|
4
|
+
const { scalar_mul } = bi;
|
|
5
|
+
/** @typedef {Operator.Reduce<bigint>} Reduce */
|
|
6
|
+
/** @typedef {Operator.Unary<bigint, bigint>} Unary*/
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {{
|
|
9
|
+
* readonly p: bigint
|
|
10
|
+
* readonly middle: bigint
|
|
11
|
+
* readonly max: bigint
|
|
12
|
+
* readonly neg: Unary
|
|
13
|
+
* readonly sub: Reduce
|
|
14
|
+
* readonly add: Reduce
|
|
15
|
+
* readonly abs: Unary
|
|
16
|
+
* readonly mul: Reduce
|
|
17
|
+
* readonly reciprocal: Unary
|
|
18
|
+
* readonly div: Reduce
|
|
19
|
+
* readonly pow: Reduce
|
|
20
|
+
* readonly pow2: Unary
|
|
21
|
+
* readonly pow3: Unary
|
|
22
|
+
* }} PrimeField
|
|
23
|
+
*/
|
|
24
|
+
/** @type {(p: bigint) => PrimeField} */
|
|
25
|
+
export const prime_field = p => {
|
|
26
|
+
/** @type {Reduce} */
|
|
27
|
+
const sub = a => b => {
|
|
28
|
+
const r = a - b;
|
|
29
|
+
return r < 0 ? r + p : r;
|
|
30
|
+
};
|
|
31
|
+
/** @type {Reduce} */
|
|
32
|
+
const mul = a => b => a * b % p;
|
|
33
|
+
/** @type {Unary} */
|
|
34
|
+
const reciprocal = a => {
|
|
35
|
+
if (a === 0n) {
|
|
36
|
+
throw '1/0';
|
|
37
|
+
}
|
|
38
|
+
let a1 = a;
|
|
39
|
+
let a0 = p;
|
|
40
|
+
let f0 = 0n;
|
|
41
|
+
let f1 = 1n;
|
|
42
|
+
while (a1 !== 1n) {
|
|
43
|
+
const q = a0 / a1;
|
|
44
|
+
const a2 = a0 % a1;
|
|
45
|
+
a0 = a1;
|
|
46
|
+
a1 = a2;
|
|
47
|
+
const f2 = sub(f0)(mul(f1)(q));
|
|
48
|
+
f0 = f1;
|
|
49
|
+
f1 = f2;
|
|
50
|
+
}
|
|
51
|
+
return f1;
|
|
52
|
+
};
|
|
53
|
+
const middle = p >> 1n;
|
|
54
|
+
/** @type {Unary} */
|
|
55
|
+
const pow2 = a => mul(a)(a);
|
|
56
|
+
/** @type {Reduce} */
|
|
57
|
+
const pow = scalar_mul({ 0: 1n, add: mul });
|
|
58
|
+
return {
|
|
59
|
+
p,
|
|
60
|
+
middle,
|
|
61
|
+
max: p - 1n,
|
|
62
|
+
neg: a => a === 0n ? 0n : p - a,
|
|
63
|
+
sub,
|
|
64
|
+
add: a => b => {
|
|
65
|
+
const r = a + b;
|
|
66
|
+
return r < p ? r : r - p;
|
|
67
|
+
},
|
|
68
|
+
abs: a => middle < a ? p - a : a,
|
|
69
|
+
mul,
|
|
70
|
+
reciprocal,
|
|
71
|
+
div: a => b => mul(a)(reciprocal(b)),
|
|
72
|
+
pow,
|
|
73
|
+
pow2,
|
|
74
|
+
pow3: a => mul(a)(pow2(a))
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
/** @type {(f: PrimeField) => (a: bigint) => bigint|null} */
|
|
78
|
+
export const sqrt = ({ p, mul, pow }) => {
|
|
79
|
+
if ((p & 3n) !== 3n) {
|
|
80
|
+
throw 'sqrt';
|
|
81
|
+
}
|
|
82
|
+
const sqrt_k = (p + 1n) >> 2n;
|
|
83
|
+
return a => {
|
|
84
|
+
const result = pow(a)(sqrt_k);
|
|
85
|
+
return mul(result)(result) === a ? result : null;
|
|
86
|
+
};
|
|
87
|
+
};
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import * as _ from './module.f.mjs';
|
|
2
|
+
const { prime_field, sqrt } = _;
|
|
3
|
+
export default {
|
|
4
|
+
prime_field_test: () => {
|
|
5
|
+
const p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2fn;
|
|
6
|
+
const f = prime_field(p);
|
|
7
|
+
const sqrt_f = sqrt(f);
|
|
8
|
+
return {
|
|
9
|
+
neg: () => {
|
|
10
|
+
if (f.neg(0n) !== 0n) {
|
|
11
|
+
throw '-0';
|
|
12
|
+
}
|
|
13
|
+
if (f.neg(1n) !== p - 1n) {
|
|
14
|
+
throw '-1';
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
sub: () => {
|
|
18
|
+
if (f.sub(10n)(4n) !== 6n) {
|
|
19
|
+
throw '10 - 4';
|
|
20
|
+
}
|
|
21
|
+
if (f.sub(11n)(14n) !== p - 3n) {
|
|
22
|
+
throw '11 - 14';
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
add: () => {
|
|
26
|
+
if (f.add(13n)(24n) !== 37n) {
|
|
27
|
+
throw '13 + 24';
|
|
28
|
+
}
|
|
29
|
+
if (f.add(77n)(f.neg(12n)) !== 65n) {
|
|
30
|
+
throw '77 + (-12)';
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
mul: () => {
|
|
34
|
+
if (f.mul(100n)(0n) !== 0n) {
|
|
35
|
+
throw '100 * 0';
|
|
36
|
+
}
|
|
37
|
+
if (f.mul(101n)(205n) !== 20705n) {
|
|
38
|
+
throw '101 * 205';
|
|
39
|
+
}
|
|
40
|
+
if (f.mul(304n)(f.neg(1n)) !== f.neg(304n)) {
|
|
41
|
+
throw '304 * -1';
|
|
42
|
+
}
|
|
43
|
+
if (f.mul(f.neg(507n))(609n) !== f.neg(308763n)) {
|
|
44
|
+
throw '-507 * 609';
|
|
45
|
+
}
|
|
46
|
+
if (f.mul(f.neg(713n))(f.neg(825n)) !== 588225n) {
|
|
47
|
+
throw '-713 * -825';
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
reciprocal: () => {
|
|
51
|
+
let i = 1n;
|
|
52
|
+
while (i < 10000n) {
|
|
53
|
+
const x = f.reciprocal(i);
|
|
54
|
+
if (f.mul(x)(i) !== 1n) {
|
|
55
|
+
throw i;
|
|
56
|
+
}
|
|
57
|
+
++i;
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
pow: () => {
|
|
61
|
+
/** @type {(a: bigint) => void} */
|
|
62
|
+
const test = a => {
|
|
63
|
+
if (f.pow(a)(0n) !== 1n) {
|
|
64
|
+
throw '**0';
|
|
65
|
+
}
|
|
66
|
+
if (f.pow(a)(1n) !== a) {
|
|
67
|
+
throw '**1';
|
|
68
|
+
}
|
|
69
|
+
// https://en.wikipedia.org/wiki/Fermat%27s_little_theorem
|
|
70
|
+
// a^(p-1) % p = 1
|
|
71
|
+
if (f.abs(f.pow(a)(f.middle)) !== 1n) {
|
|
72
|
+
throw '**middle';
|
|
73
|
+
}
|
|
74
|
+
if (f.pow(a)(f.sub(f.max)(1n)) !== f.reciprocal(a)) {
|
|
75
|
+
throw '**(max-1)';
|
|
76
|
+
}
|
|
77
|
+
if (f.pow(a)(f.max) !== 1n) {
|
|
78
|
+
throw '**max';
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
// 0
|
|
82
|
+
if (f.pow(0n)(0n) !== 1n) {
|
|
83
|
+
throw '0**0';
|
|
84
|
+
}
|
|
85
|
+
if (f.pow(0n)(f.max) !== 0n) {
|
|
86
|
+
throw '0**max';
|
|
87
|
+
}
|
|
88
|
+
// 1
|
|
89
|
+
test(1n);
|
|
90
|
+
// 2
|
|
91
|
+
test(2n);
|
|
92
|
+
if (f.pow(2n)(2n) !== 4n) {
|
|
93
|
+
throw '2**2';
|
|
94
|
+
}
|
|
95
|
+
if (f.pow(2n)(3n) !== 8n) {
|
|
96
|
+
throw '2**3';
|
|
97
|
+
}
|
|
98
|
+
if (f.pow(2n)(128n) !== 1n << 128n) {
|
|
99
|
+
throw '2**128';
|
|
100
|
+
}
|
|
101
|
+
// 3
|
|
102
|
+
test(3n);
|
|
103
|
+
if (f.pow(3n)(2n) !== 9n) {
|
|
104
|
+
throw '3**2';
|
|
105
|
+
}
|
|
106
|
+
if (f.pow(3n)(3n) !== 27n) {
|
|
107
|
+
throw '3**3';
|
|
108
|
+
}
|
|
109
|
+
if (f.pow(3n)(100n) !== 3n ** 100n) {
|
|
110
|
+
throw '3**100';
|
|
111
|
+
}
|
|
112
|
+
if (f.pow(3n)(110n) !== 3n ** 110n) {
|
|
113
|
+
throw '3**110';
|
|
114
|
+
}
|
|
115
|
+
if (f.pow(3n)(120n) !== 3n ** 120n) {
|
|
116
|
+
throw '3**120';
|
|
117
|
+
}
|
|
118
|
+
if (f.pow(3n)(121n) !== 3n ** 121n) {
|
|
119
|
+
throw '3**121';
|
|
120
|
+
}
|
|
121
|
+
//
|
|
122
|
+
test(f.middle);
|
|
123
|
+
test(f.max - 1n);
|
|
124
|
+
test(f.max);
|
|
125
|
+
},
|
|
126
|
+
sqrt: () => {
|
|
127
|
+
/** @type {(a: bigint) => void} */
|
|
128
|
+
const test = a => {
|
|
129
|
+
const a2 = f.mul(a)(a);
|
|
130
|
+
const s = sqrt_f(a2);
|
|
131
|
+
if (s !== null && f.abs(s) !== f.abs(a)) {
|
|
132
|
+
throw 'sqrt';
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
let i = 1n;
|
|
136
|
+
while (i < 1000n) {
|
|
137
|
+
test(i);
|
|
138
|
+
++i;
|
|
139
|
+
}
|
|
140
|
+
test(f.middle);
|
|
141
|
+
test(f.max);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// @ts-self-types="./module.f.d.mts"
|
|
2
|
+
import * as Operator from '../types/function/operator/module.f.mjs';
|
|
3
|
+
import * as pf from '../prime_field/module.f.mjs';
|
|
4
|
+
import * as bi from '../types/bigint/module.f.mjs';
|
|
5
|
+
const { scalar_mul } = bi;
|
|
6
|
+
const { prime_field, sqrt } = pf;
|
|
7
|
+
/** @typedef {readonly[bigint, bigint]} Point2D */
|
|
8
|
+
/** @typedef {Point2D|null} Point */
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {{
|
|
11
|
+
* readonly p: bigint
|
|
12
|
+
* readonly a: readonly[bigint, bigint]
|
|
13
|
+
* readonly g: readonly[bigint, bigint]
|
|
14
|
+
* readonly n: bigint
|
|
15
|
+
* }} Init
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* @typedef {{
|
|
19
|
+
* readonly pf: pf.PrimeField
|
|
20
|
+
* readonly nf: pf.PrimeField
|
|
21
|
+
* readonly y2: (x: bigint) => bigint
|
|
22
|
+
* readonly y: (x: bigint) => bigint|null
|
|
23
|
+
* readonly neg: (a: Point) => Point
|
|
24
|
+
* readonly add: Operator.Reduce<Point>
|
|
25
|
+
* readonly mul: (a: Point) => (n: bigint) => Point
|
|
26
|
+
* }} Curve
|
|
27
|
+
*/
|
|
28
|
+
/** @type {(i: Init) => Curve} */
|
|
29
|
+
export const curve = ({ p, a: [a0, a1], n }) => {
|
|
30
|
+
const pf = prime_field(p);
|
|
31
|
+
const { pow2, pow3, sub, add, mul, neg, div } = pf;
|
|
32
|
+
const mul3 = mul(3n);
|
|
33
|
+
const mul2 = mul(2n);
|
|
34
|
+
const addA1 = add(a1);
|
|
35
|
+
const mulA1 = mul(a1);
|
|
36
|
+
const addA0 = add(a0);
|
|
37
|
+
// y**2 = a1*x**3 + a0
|
|
38
|
+
/** @type {(x: bigint) => bigint} */
|
|
39
|
+
const y2 = x => addA0(add(pow3(x))(mulA1(x)));
|
|
40
|
+
/** @type {Operator.Reduce<Point>} */
|
|
41
|
+
const addPoint = p => q => {
|
|
42
|
+
if (p === null) {
|
|
43
|
+
return q;
|
|
44
|
+
}
|
|
45
|
+
if (q === null) {
|
|
46
|
+
return p;
|
|
47
|
+
}
|
|
48
|
+
const [px, py] = p;
|
|
49
|
+
const [qx, qy] = q;
|
|
50
|
+
const md = px === qx
|
|
51
|
+
// (3 * px ** 2 + a1) / (2 * py)
|
|
52
|
+
? py !== qy || py === 0n ? null : [addA1(mul3(pow2(px))), mul2(py)]
|
|
53
|
+
// (py - qy) / (px - qx)
|
|
54
|
+
: [sub(py)(qy), sub(px)(qx)];
|
|
55
|
+
if (md === null) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
const [ma, mb] = md;
|
|
59
|
+
const m = div(ma)(mb);
|
|
60
|
+
// m ** 2 - px - qx
|
|
61
|
+
const rx = sub(pow2(m))(add(px)(qx));
|
|
62
|
+
// [rx, m * (px - rx) - py]
|
|
63
|
+
return [rx, sub(mul(m)(sub(px)(rx)))(py)];
|
|
64
|
+
};
|
|
65
|
+
const sqrt_p = sqrt(pf);
|
|
66
|
+
return {
|
|
67
|
+
pf,
|
|
68
|
+
nf: prime_field(n),
|
|
69
|
+
y2,
|
|
70
|
+
y: x => sqrt_p(y2(x)),
|
|
71
|
+
neg: p => {
|
|
72
|
+
if (p === null) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
const [x, y] = p;
|
|
76
|
+
return [x, neg(y)];
|
|
77
|
+
},
|
|
78
|
+
add: addPoint,
|
|
79
|
+
mul: scalar_mul({ 0: null, add: addPoint })
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
/** @type {(a: Point) => (b: Point) => boolean} */
|
|
83
|
+
export const eq = a => b => {
|
|
84
|
+
if (a === null || b === null) {
|
|
85
|
+
return a === b;
|
|
86
|
+
}
|
|
87
|
+
const [ax, ay] = a;
|
|
88
|
+
const [bx, by] = b;
|
|
89
|
+
return ax === bx && ay === by;
|
|
90
|
+
};
|
|
91
|
+
/** @type {Init} */
|
|
92
|
+
export const secp256k1 = {
|
|
93
|
+
p: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2fn,
|
|
94
|
+
a: [7n, 0n],
|
|
95
|
+
g: [
|
|
96
|
+
0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798n,
|
|
97
|
+
0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n
|
|
98
|
+
],
|
|
99
|
+
n: 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n,
|
|
100
|
+
};
|
|
101
|
+
/** @type {Init} */
|
|
102
|
+
export const secp192r1 = {
|
|
103
|
+
p: 0xfffffffffffffffffffffffffffffffeffffffffffffffffn,
|
|
104
|
+
a: [
|
|
105
|
+
0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1n,
|
|
106
|
+
0xfffffffffffffffffffffffffffffffefffffffffffffffcn
|
|
107
|
+
],
|
|
108
|
+
g: [
|
|
109
|
+
0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012n,
|
|
110
|
+
0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811n
|
|
111
|
+
],
|
|
112
|
+
n: 0xffffffffffffffffffffffff99def836146bc9b1b4d22831n,
|
|
113
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import * as _ from './module.f.mjs';
|
|
2
|
+
const { curve, secp256k1, secp192r1, eq } = _;
|
|
3
|
+
export default {
|
|
4
|
+
test: () => {
|
|
5
|
+
/** @type {(c: _.Init) => void} */
|
|
6
|
+
const test_curve = c => {
|
|
7
|
+
const { g } = c;
|
|
8
|
+
const { mul, neg, pf: { abs }, y: yf, nf: { p: n } } = curve(c);
|
|
9
|
+
/** @type {(p: _.Point) => void} */
|
|
10
|
+
const point_check = p => {
|
|
11
|
+
if (p === null) {
|
|
12
|
+
throw 'null';
|
|
13
|
+
}
|
|
14
|
+
const [x, y] = p;
|
|
15
|
+
const ye = yf(x);
|
|
16
|
+
if (ye === null) {
|
|
17
|
+
throw 'null';
|
|
18
|
+
}
|
|
19
|
+
if (abs(ye) !== abs(y)) {
|
|
20
|
+
throw 'ye';
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
point_check(g);
|
|
24
|
+
point_check(neg(g));
|
|
25
|
+
/** @type {(p: _.Point) => void} */
|
|
26
|
+
const test_mul = p => {
|
|
27
|
+
if (mul(p)(0n) !== null) {
|
|
28
|
+
throw 'O';
|
|
29
|
+
}
|
|
30
|
+
if (mul(p)(1n) !== p) {
|
|
31
|
+
throw 'p';
|
|
32
|
+
}
|
|
33
|
+
if (mul(p)(n) !== null) {
|
|
34
|
+
throw 'n';
|
|
35
|
+
}
|
|
36
|
+
const pn = neg(p);
|
|
37
|
+
if (!eq(mul(p)(n - 1n))(pn)) {
|
|
38
|
+
throw 'n - 1';
|
|
39
|
+
}
|
|
40
|
+
/** @type {(s: bigint) => void} */
|
|
41
|
+
const f = s => {
|
|
42
|
+
const r = mul(p)(s);
|
|
43
|
+
point_check(r);
|
|
44
|
+
const rn = mul(pn)(s);
|
|
45
|
+
point_check(rn);
|
|
46
|
+
if (!eq(r)(neg(rn))) {
|
|
47
|
+
throw 'r != -rn';
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
f(2n);
|
|
51
|
+
f(3n);
|
|
52
|
+
f(4n << 128n);
|
|
53
|
+
f((5n << 128n) + 6n);
|
|
54
|
+
f(7n << 128n);
|
|
55
|
+
f((8n << 128n) + 9n);
|
|
56
|
+
};
|
|
57
|
+
test_mul(g);
|
|
58
|
+
test_mul(neg(g));
|
|
59
|
+
};
|
|
60
|
+
test_curve(secp256k1);
|
|
61
|
+
test_curve(secp192r1);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// @ts-self-types="./module.f.d.mts"
|
|
2
|
+
import * as arrayT from '../types/array/module.f.mjs';
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {{
|
|
5
|
+
* readonly f: (i: number) => number
|
|
6
|
+
* readonly length: number
|
|
7
|
+
* }} HashInput
|
|
8
|
+
*/
|
|
9
|
+
/** @typedef {arrayT.Array8<number>} Hash8 */
|
|
10
|
+
/** @typedef {arrayT.Array16<number>} Array16 */
|
|
11
|
+
/** @type {(input: number) => (pos: number) => number} */
|
|
12
|
+
const appendOneWithZeros = input => pos => (input >> pos << pos) | (1 << pos);
|
|
13
|
+
/** @type {(input: number) => (pos: number) => number} */
|
|
14
|
+
const mod = a => b => (a % b + b) % b;
|
|
15
|
+
/** @type {(input: readonly number[]) => (bits: number) => HashInput} */
|
|
16
|
+
export const padding = input => bitsCount => {
|
|
17
|
+
const appendBlockIndex = (bitsCount / 32) | 0;
|
|
18
|
+
const length = (bitsCount + mod(447 - bitsCount)(512) + 65) / 32;
|
|
19
|
+
/** @type {(i: number) => number} */
|
|
20
|
+
const f = i => {
|
|
21
|
+
if (i < appendBlockIndex) {
|
|
22
|
+
return input[i];
|
|
23
|
+
}
|
|
24
|
+
if (i === appendBlockIndex) {
|
|
25
|
+
return appendBlockIndex >= input.length ? 0x8000_0000 : appendOneWithZeros(input[appendBlockIndex])(31 - bitsCount % 32);
|
|
26
|
+
}
|
|
27
|
+
if (i === length - 2) {
|
|
28
|
+
return (bitsCount / 0x1_0000_0000) | 0;
|
|
29
|
+
}
|
|
30
|
+
if (i === length - 1) {
|
|
31
|
+
return bitsCount % 0x1_0000_0000;
|
|
32
|
+
}
|
|
33
|
+
return 0;
|
|
34
|
+
};
|
|
35
|
+
return ({ f, length });
|
|
36
|
+
};
|
|
37
|
+
/** @type {(d: number) => (n: number) => number} */
|
|
38
|
+
const rotr = d => {
|
|
39
|
+
const r = 32 - d;
|
|
40
|
+
return n => n >>> d | n << r;
|
|
41
|
+
};
|
|
42
|
+
/** @type {(x: number) => (y: number) => (z: number) => number} */
|
|
43
|
+
const ch = x => y => z => x & y ^ ~x & z;
|
|
44
|
+
/** @type {(x: number) => (y: number) => (z: number) => number} */
|
|
45
|
+
const maj = x => y => z => x & y ^ x & z ^ y & z;
|
|
46
|
+
/** @type {(d: number) => (n: number) => number} */
|
|
47
|
+
const shr = d => n => n >>> d;
|
|
48
|
+
/** @type {(a: number) => (b: number) => (c: number) => (x: number) => number} */
|
|
49
|
+
const bigSigma = a => b => c => {
|
|
50
|
+
const ra = rotr(a);
|
|
51
|
+
const rb = rotr(b);
|
|
52
|
+
const rc = rotr(c);
|
|
53
|
+
return x => ra(x) ^ rb(x) ^ rc(x);
|
|
54
|
+
};
|
|
55
|
+
const bigSigma0 = bigSigma(2)(13)(22);
|
|
56
|
+
const bigSigma1 = bigSigma(6)(11)(25);
|
|
57
|
+
/** @type {(a: number) => (b: number) => (c: number) => (x: number) => number} */
|
|
58
|
+
const smallSigma = a => b => c => {
|
|
59
|
+
const ra = rotr(a);
|
|
60
|
+
const rb = rotr(b);
|
|
61
|
+
const sc = shr(c);
|
|
62
|
+
return x => ra(x) ^ rb(x) ^ sc(x);
|
|
63
|
+
};
|
|
64
|
+
const smallSigma0 = smallSigma(7)(18)(3);
|
|
65
|
+
const smallSigma1 = smallSigma(17)(19)(10);
|
|
66
|
+
/** @type {(a: arrayT.Array4<number>) => number} */
|
|
67
|
+
const wi = ([a0, a1, a2, a3]) => (smallSigma1(a0) + a1 + smallSigma0(a2) + a3) | 0;
|
|
68
|
+
/** @type {(w: Array16) => Array16} */
|
|
69
|
+
const nextW = ([w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, wA, wB, wC, wD, wE, wF]) => {
|
|
70
|
+
w0 = wi([wE, w9, w1, w0]);
|
|
71
|
+
w1 = wi([wF, wA, w2, w1]);
|
|
72
|
+
w2 = wi([w0, wB, w3, w2]);
|
|
73
|
+
w3 = wi([w1, wC, w4, w3]);
|
|
74
|
+
w4 = wi([w2, wD, w5, w4]);
|
|
75
|
+
w5 = wi([w3, wE, w6, w5]);
|
|
76
|
+
w6 = wi([w4, wF, w7, w6]);
|
|
77
|
+
w7 = wi([w5, w0, w8, w7]);
|
|
78
|
+
w8 = wi([w6, w1, w9, w8]);
|
|
79
|
+
w9 = wi([w7, w2, wA, w9]);
|
|
80
|
+
wA = wi([w8, w3, wB, wA]);
|
|
81
|
+
wB = wi([w9, w4, wC, wB]);
|
|
82
|
+
wC = wi([wA, w5, wD, wC]);
|
|
83
|
+
wD = wi([wB, w6, wE, wD]);
|
|
84
|
+
wE = wi([wC, w7, wF, wE]);
|
|
85
|
+
wF = wi([wD, w8, w0, wF]);
|
|
86
|
+
return [w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, wA, wB, wC, wD, wE, wF];
|
|
87
|
+
};
|
|
88
|
+
const k = [
|
|
89
|
+
[
|
|
90
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
91
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
92
|
+
],
|
|
93
|
+
[
|
|
94
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
95
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
96
|
+
],
|
|
97
|
+
[
|
|
98
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
99
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
100
|
+
],
|
|
101
|
+
[
|
|
102
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
103
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
104
|
+
],
|
|
105
|
+
];
|
|
106
|
+
/** @type {(init: Hash8) => (data: Array16) => Hash8} */
|
|
107
|
+
const compress = ([a0, b0, c0, d0, e0, f0, g0, h0]) => data => {
|
|
108
|
+
let w = data;
|
|
109
|
+
let a = a0;
|
|
110
|
+
let b = b0;
|
|
111
|
+
let c = c0;
|
|
112
|
+
let d = d0;
|
|
113
|
+
let e = e0;
|
|
114
|
+
let f = f0;
|
|
115
|
+
let g = g0;
|
|
116
|
+
let h = h0;
|
|
117
|
+
let i = 0;
|
|
118
|
+
while (true) {
|
|
119
|
+
const ki = k[i];
|
|
120
|
+
for (let j = 0; j < 16; ++j) {
|
|
121
|
+
const t1 = h + bigSigma1(e) + ch(e)(f)(g) + ki[j] + w[j];
|
|
122
|
+
const t2 = bigSigma0(a) + maj(a)(b)(c);
|
|
123
|
+
h = g;
|
|
124
|
+
g = f;
|
|
125
|
+
f = e;
|
|
126
|
+
e = (d + t1) | 0;
|
|
127
|
+
d = c;
|
|
128
|
+
c = b;
|
|
129
|
+
b = a;
|
|
130
|
+
a = (t1 + t2) | 0;
|
|
131
|
+
}
|
|
132
|
+
if (i === 3) {
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
++i;
|
|
136
|
+
w = nextW(w);
|
|
137
|
+
}
|
|
138
|
+
return [
|
|
139
|
+
(a0 + a) | 0,
|
|
140
|
+
(b0 + b) | 0,
|
|
141
|
+
(c0 + c) | 0,
|
|
142
|
+
(d0 + d) | 0,
|
|
143
|
+
(e0 + e) | 0,
|
|
144
|
+
(f0 + f) | 0,
|
|
145
|
+
(g0 + g) | 0,
|
|
146
|
+
(h0 + h) | 0,
|
|
147
|
+
];
|
|
148
|
+
};
|
|
149
|
+
/** @type {(init: Hash8) => (input: readonly number[]) => (bitsCount: number) => Hash8} */
|
|
150
|
+
const compute = init => input => bitsCount => {
|
|
151
|
+
const { f, length } = padding(input)(bitsCount);
|
|
152
|
+
let result = init;
|
|
153
|
+
const chunkCount = length / 16;
|
|
154
|
+
for (let i = 0; i < chunkCount; i++) {
|
|
155
|
+
const s = i * 16;
|
|
156
|
+
result = compress(result)([
|
|
157
|
+
f(s + 0), f(s + 1), f(s + 2), f(s + 3), f(s + 4), f(s + 5), f(s + 6), f(s + 7),
|
|
158
|
+
f(s + 8), f(s + 9), f(s + 10), f(s + 11), f(s + 12), f(s + 13), f(s + 14), f(s + 15)
|
|
159
|
+
]);
|
|
160
|
+
}
|
|
161
|
+
return result;
|
|
162
|
+
};
|
|
163
|
+
/** @type {Hash8} */
|
|
164
|
+
const init256 = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];
|
|
165
|
+
/** @type {(input: readonly number[]) => (bitsCount: number) => Hash8} */
|
|
166
|
+
export const computeSha256 = compute(init256);
|
|
167
|
+
/** @type {Hash8} */
|
|
168
|
+
const init224 = [0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4];
|
|
169
|
+
/** @type {(input: readonly number[]) => (bitsCount: number) => Hash8} */
|
|
170
|
+
export const computeSha224 = compute(init224);
|
|
171
|
+
export const compress256 = compress(init256);
|
|
172
|
+
export const compress224 = compress(init224);
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import * as _ from './module.f.mjs';
|
|
2
|
+
import * as json from '../json/module.f.mjs';
|
|
3
|
+
import * as o from '../types/object/module.f.mjs';
|
|
4
|
+
const { sort } = o;
|
|
5
|
+
/** @type {(a: number) => number} */
|
|
6
|
+
const toU32 = x => (x + 0x1_0000_0000) % 0x1_0000_0000;
|
|
7
|
+
/** @type {(a: number) => string} */
|
|
8
|
+
const toHexString = x => toU32(x).toString(16).padStart(8, '0');
|
|
9
|
+
/** @type {(a: readonly json.Unknown[]) => string} */
|
|
10
|
+
const stringify = a => json.stringify(sort)(a);
|
|
11
|
+
// {
|
|
12
|
+
// const result = _.padding([])(0)
|
|
13
|
+
// console.log(result.map(toHexString))
|
|
14
|
+
// }
|
|
15
|
+
// {
|
|
16
|
+
// const result = _.padding([0x61626364, 0x65000000])(40)
|
|
17
|
+
// console.log(result.map(toHexString))
|
|
18
|
+
// }
|
|
19
|
+
// {
|
|
20
|
+
// const result = _.padding([0x11111110])(31)
|
|
21
|
+
// console.log(result.map(toHexString))
|
|
22
|
+
// }
|
|
23
|
+
// {
|
|
24
|
+
// const result = _.padding([0x11111110])(32)
|
|
25
|
+
// console.log(result.map(toHexString))
|
|
26
|
+
// }
|
|
27
|
+
export default {
|
|
28
|
+
empty: {
|
|
29
|
+
sha256: () => {
|
|
30
|
+
const hash = _.computeSha256([])(0);
|
|
31
|
+
const result = stringify(hash.map(toHexString));
|
|
32
|
+
if (result !== '["e3b0c442","98fc1c14","9afbf4c8","996fb924","27ae41e4","649b934c","a495991b","7852b855"]') {
|
|
33
|
+
throw result;
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
sha224: () => {
|
|
37
|
+
const hash = _.computeSha224([])(0);
|
|
38
|
+
const result = stringify(hash.map(toHexString));
|
|
39
|
+
if (result !== '["d14a028c","2a3a2bc9","476102bb","288234c4","15a2b01f","828ea62a","c5b3e42f","bdd387cb"]') {
|
|
40
|
+
throw result;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
sha224compress: [
|
|
45
|
+
() => {
|
|
46
|
+
const hash = _.compress224([0x8000_0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
|
|
47
|
+
const result = stringify(hash.map(toHexString));
|
|
48
|
+
if (result !== '["d14a028c","2a3a2bc9","476102bb","288234c4","15a2b01f","828ea62a","c5b3e42f","bdd387cb"]') {
|
|
49
|
+
throw result;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
],
|
|
53
|
+
sha256: [
|
|
54
|
+
() => {
|
|
55
|
+
//[0x68656C6C, 0x6F20776F, 0x726C6400] represents phrase 'hello world'
|
|
56
|
+
const hash = _.computeSha256([0x68656C6C, 0x6F20776F, 0x726C6400])(88);
|
|
57
|
+
const result = stringify(hash.map(toHexString));
|
|
58
|
+
if (result !== '["b94d27b9","934d3e08","a52e52d7","da7dabfa","c484efe3","7a5380ee","9088f7ac","e2efcde9"]') {
|
|
59
|
+
throw result;
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
() => {
|
|
63
|
+
//[0x68656C6C, 0x6F20776F, 0x726C6488] represents phrase 'hello world' with 1's at the end
|
|
64
|
+
const hash = _.computeSha256([0x68656C6C, 0x6F20776F, 0x726C64FF])(88);
|
|
65
|
+
const result = stringify(hash.map(toHexString));
|
|
66
|
+
if (result !== '["b94d27b9","934d3e08","a52e52d7","da7dabfa","c484efe3","7a5380ee","9088f7ac","e2efcde9"]') {
|
|
67
|
+
throw result;
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
() => {
|
|
71
|
+
const input = Array(8).fill(0x31313131);
|
|
72
|
+
const result = _.computeSha256(input)(256);
|
|
73
|
+
if (toU32(result[0]) !== 0x8a83665f) {
|
|
74
|
+
throw result[0];
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
() => {
|
|
78
|
+
const input = Array(16).fill(0x31313131);
|
|
79
|
+
const hash = _.computeSha256(input)(512);
|
|
80
|
+
const result = stringify(hash.map(toHexString));
|
|
81
|
+
if (result !== '["3138bb9b","c78df27c","473ecfd1","410f7bd4","5ebac1f5","9cf3ff9c","fe4db77a","ab7aedd3"]') {
|
|
82
|
+
throw result;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
]
|
|
86
|
+
};
|