@es3d/es3d 1.0.0 → 1.0.1

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.
Files changed (79) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +86 -86
  3. package/docs/SPEC.md +417 -417
  4. package/package.json +1 -1
  5. package/scripts/es3d-cli.mjs +377 -377
  6. package/src/cache/geometry-store.js +392 -392
  7. package/src/cache/mesh-bundle.js +123 -123
  8. package/src/cache/sha256.js +73 -73
  9. package/src/format/ast.js +238 -238
  10. package/src/format/colors.js +93 -93
  11. package/src/format/compact-parser.js +171 -171
  12. package/src/format/compact-tokenizer.js +165 -165
  13. package/src/format/errors.js +247 -247
  14. package/src/format/json.js +55 -55
  15. package/src/format/limits.js +14 -14
  16. package/src/format/parse.js +92 -92
  17. package/src/format/schema.js +617 -617
  18. package/src/format/serializer.js +333 -333
  19. package/src/format/validator.js +330 -330
  20. package/src/format/value-error.js +12 -12
  21. package/src/format/value-format.js +314 -314
  22. package/src/format/values.js +459 -459
  23. package/src/format/xml-parser.js +136 -136
  24. package/src/format/xml-tokenizer.js +237 -237
  25. package/src/geometry/contour.js +86 -86
  26. package/src/geometry/extrude.js +175 -175
  27. package/src/geometry/gear.js +85 -85
  28. package/src/geometry/geometry-cache.js +159 -159
  29. package/src/geometry/lod-error.js +178 -178
  30. package/src/geometry/mesh.js +458 -458
  31. package/src/geometry/primitives.js +299 -299
  32. package/src/geometry/registry.js +645 -645
  33. package/src/geometry/revolve.js +136 -136
  34. package/src/geometry/sweep.js +441 -441
  35. package/src/index.js +47 -47
  36. package/src/io/compression.js +67 -67
  37. package/src/io/convert.js +236 -236
  38. package/src/io/download.js +13 -13
  39. package/src/io/external-scene.js +127 -127
  40. package/src/io/file-loader.js +28 -28
  41. package/src/io/gltf.js +630 -630
  42. package/src/io/mesh-import.js +104 -104
  43. package/src/io/obj.js +352 -352
  44. package/src/io/step-brep.js +439 -439
  45. package/src/io/step-exact.js +1149 -1149
  46. package/src/io/step.js +1418 -1418
  47. package/src/io/stl.js +193 -193
  48. package/src/io/svg-import.js +787 -787
  49. package/src/material/materials.js +154 -154
  50. package/src/math/mat4.js +475 -475
  51. package/src/math/random.js +30 -30
  52. package/src/math/trig.js +65 -65
  53. package/src/math/vec3.js +81 -81
  54. package/src/optimize/optimizer.js +579 -579
  55. package/src/path/path-flatten.js +322 -322
  56. package/src/path/path-parser.js +332 -332
  57. package/src/path/shapes.js +129 -129
  58. package/src/path/triangulate.js +517 -517
  59. package/src/pipeline.js +74 -74
  60. package/src/render/camera.js +168 -168
  61. package/src/render/frame-builder.js +362 -362
  62. package/src/render/geometry-pool.js +149 -149
  63. package/src/render/lighting.js +149 -149
  64. package/src/render/orbit-controls.js +237 -237
  65. package/src/render/renderer.js +258 -249
  66. package/src/render/scroll-timeline.js +104 -104
  67. package/src/render/shaders/glsl.js +285 -285
  68. package/src/render/shaders/wgsl.js +252 -252
  69. package/src/render/stage.js +364 -352
  70. package/src/render/texture-cache.js +40 -40
  71. package/src/render/textures.js +223 -223
  72. package/src/render/webgl2/webgl2-backend.js +586 -532
  73. package/src/render/webgpu/webgpu-backend.js +482 -480
  74. package/src/scene/flatten.js +754 -754
  75. package/src/scene/picking.js +161 -161
  76. package/src/scene/scene-graph.js +320 -320
  77. package/src/transform/transform.js +211 -211
  78. package/src/wasm/kernels-binary.js +3 -3
  79. package/src/wasm/kernels.js +385 -385
@@ -1,123 +1,123 @@
1
- // Mesh bundle: the binary record the persistent geometry cache stores, one
2
- // per document. ES3D's own layout, little-endian:
3
- //
4
- // 0 "ES3G" magic
5
- // 4 u32 layout version (1)
6
- // 8 u32 byte length of the description
7
- // 12 description, UTF-8 JSON, padded with spaces to a multiple of 4 bytes
8
- // .. data: per stored level, at the offset its description gives from the
9
- // start of the data (a multiple of 4): positions f32[3v], normals
10
- // f32[3v], uvs f32[2v], then indices, u16[i] when every index fits in
11
- // 16 bits (`w` 2) else u32[i] (`w` 4)
12
- //
13
- // The description:
14
- // { key, entries: [[{ lod, kind, v, i, w, at, reports }, ...], ...] }
15
- // with one list of levels per cached shape, in the order of the shapes'
16
- // identities (the record's key covers that order). `reports` lists the
17
- // diagnostics generation reported, as [severity, code, message].
18
- //
19
- // Decoding returns views into the stored buffer (only 16-bit indices are
20
- // widened, by a native copy) and checks the structure it relies on: magic,
21
- // layout, lengths, offsets and primitive counts. It does not check that every
22
- // index names a vertex: only code of the same origin can write the store, and
23
- // an index out of range cannot reach memory outside the arrays, in
24
- // JavaScript or on the GPU.
25
-
26
- import { Mesh } from '../geometry/mesh.js';
27
-
28
- const MAGIC = 0x47335345; // "ES3G" read as a little-endian u32
29
- export const BUNDLE_VERSION = 1;
30
- const littleEndian = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
31
- const encoder = new TextEncoder();
32
- const decoder = new TextDecoder();
33
-
34
- const align4 = (n) => (n + 3) & ~3;
35
-
36
- /**
37
- * @param {string} key
38
- * @param {{lod: number, mesh: Mesh, reports?: object[]|null}[][]} entries levels per shape
39
- * @returns {ArrayBuffer}
40
- */
41
- export function encodeBundle(key, entries) {
42
- // lay the arrays out first, then the description that points at them
43
- const plan = [];
44
- let dataBytes = 0;
45
- const description = { key, entries: [] };
46
- for (const list of entries) {
47
- const levels = [];
48
- for (const { lod, mesh, reports } of list) {
49
- const v = mesh.positions.length / 3, i = mesh.indices.length;
50
- const w = v <= 0x10000 ? 2 : 4;
51
- levels.push({ lod, kind: mesh.kind, v, i, w, at: dataBytes, reports: reports ? reports.map((r) => [r.severity, r.code, r.message]) : [] });
52
- plan.push({ mesh, at: dataBytes, w });
53
- dataBytes += v * 32 + align4(i * w);
54
- }
55
- description.entries.push(levels);
56
- }
57
- const meta = encoder.encode(JSON.stringify(description));
58
- const head = 12 + align4(meta.length);
59
- const buffer = new ArrayBuffer(head + dataBytes);
60
- const u8 = new Uint8Array(buffer);
61
- const view = new DataView(buffer);
62
- view.setUint32(0, MAGIC, true);
63
- view.setUint32(4, BUNDLE_VERSION, true);
64
- view.setUint32(8, head - 12, true);
65
- u8.set(meta, 12);
66
- u8.fill(0x20, 12 + meta.length, head);
67
- for (const { mesh, at, w } of plan) {
68
- const v = mesh.positions.length / 3;
69
- const base = head + at;
70
- new Float32Array(buffer, base, v * 3).set(mesh.positions);
71
- new Float32Array(buffer, base + v * 12, v * 3).set(mesh.normals);
72
- new Float32Array(buffer, base + v * 24, v * 2).set(mesh.uvs);
73
- new (w === 2 ? Uint16Array : Uint32Array)(buffer, base + v * 32, mesh.indices.length).set(mesh.indices);
74
- }
75
- return buffer;
76
- }
77
-
78
- const isCount = (n) => Number.isInteger(n) && n >= 0;
79
-
80
- /**
81
- * @param {ArrayBuffer} buffer
82
- * @returns {{key: string, entries: {lod: number, mesh: Mesh, reports: object[]}[][]}|null}
83
- * null when the buffer is not a valid bundle of this layout
84
- */
85
- export function decodeBundle(buffer) {
86
- if (!littleEndian || !(buffer instanceof ArrayBuffer) || buffer.byteLength < 12) return null;
87
- const view = new DataView(buffer);
88
- if (view.getUint32(0, true) !== MAGIC || view.getUint32(4, true) !== BUNDLE_VERSION) return null;
89
- const metaBytes = view.getUint32(8, true);
90
- if (metaBytes % 4 || 12 + metaBytes > buffer.byteLength) return null;
91
- let description;
92
- try {
93
- description = JSON.parse(decoder.decode(new Uint8Array(buffer, 12, metaBytes)));
94
- } catch {
95
- return null;
96
- }
97
- if (!description || typeof description.key !== 'string' || !Array.isArray(description.entries)) return null;
98
- const data = 12 + metaBytes;
99
- const entries = [];
100
- for (const list of description.entries) {
101
- if (!Array.isArray(list)) return null;
102
- const levels = [];
103
- for (const l of list) {
104
- if (!l || !isCount(l.lod) || !isCount(l.v) || !isCount(l.i) || !isCount(l.at) || l.at % 4) return null;
105
- if ((l.kind !== 'triangles' && l.kind !== 'lines') || (l.w !== 2 && l.w !== 4) || (l.w === 2 && l.v > 0x10000)) return null;
106
- if (l.i % (l.kind === 'lines' ? 2 : 3)) return null;
107
- const at = data + l.at;
108
- if (at + l.v * 32 + l.i * l.w > buffer.byteLength) return null;
109
- const indices = l.w === 2 ? new Uint32Array(new Uint16Array(buffer, at + l.v * 32, l.i)) : new Uint32Array(buffer, at + l.v * 32, l.i);
110
- const mesh = new Mesh({
111
- positions: new Float32Array(buffer, at, l.v * 3),
112
- normals: new Float32Array(buffer, at + l.v * 12, l.v * 3),
113
- uvs: new Float32Array(buffer, at + l.v * 24, l.v * 2),
114
- indices,
115
- kind: l.kind,
116
- });
117
- const reports = Array.isArray(l.reports) ? l.reports.filter((r) => Array.isArray(r) && r.length === 3).map(([severity, code, message]) => ({ severity: String(severity), code: String(code), message: String(message) })) : [];
118
- levels.push({ lod: l.lod, mesh, reports });
119
- }
120
- entries.push(levels);
121
- }
122
- return { key: description.key, entries };
123
- }
1
+ // Mesh bundle: the binary record the persistent geometry cache stores, one
2
+ // per document. ES3D's own layout, little-endian:
3
+ //
4
+ // 0 "ES3G" magic
5
+ // 4 u32 layout version (1)
6
+ // 8 u32 byte length of the description
7
+ // 12 description, UTF-8 JSON, padded with spaces to a multiple of 4 bytes
8
+ // .. data: per stored level, at the offset its description gives from the
9
+ // start of the data (a multiple of 4): positions f32[3v], normals
10
+ // f32[3v], uvs f32[2v], then indices, u16[i] when every index fits in
11
+ // 16 bits (`w` 2) else u32[i] (`w` 4)
12
+ //
13
+ // The description:
14
+ // { key, entries: [[{ lod, kind, v, i, w, at, reports }, ...], ...] }
15
+ // with one list of levels per cached shape, in the order of the shapes'
16
+ // identities (the record's key covers that order). `reports` lists the
17
+ // diagnostics generation reported, as [severity, code, message].
18
+ //
19
+ // Decoding returns views into the stored buffer (only 16-bit indices are
20
+ // widened, by a native copy) and checks the structure it relies on: magic,
21
+ // layout, lengths, offsets and primitive counts. It does not check that every
22
+ // index names a vertex: only code of the same origin can write the store, and
23
+ // an index out of range cannot reach memory outside the arrays, in
24
+ // JavaScript or on the GPU.
25
+
26
+ import { Mesh } from '../geometry/mesh.js';
27
+
28
+ const MAGIC = 0x47335345; // "ES3G" read as a little-endian u32
29
+ export const BUNDLE_VERSION = 1;
30
+ const littleEndian = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
31
+ const encoder = new TextEncoder();
32
+ const decoder = new TextDecoder();
33
+
34
+ const align4 = (n) => (n + 3) & ~3;
35
+
36
+ /**
37
+ * @param {string} key
38
+ * @param {{lod: number, mesh: Mesh, reports?: object[]|null}[][]} entries levels per shape
39
+ * @returns {ArrayBuffer}
40
+ */
41
+ export function encodeBundle(key, entries) {
42
+ // lay the arrays out first, then the description that points at them
43
+ const plan = [];
44
+ let dataBytes = 0;
45
+ const description = { key, entries: [] };
46
+ for (const list of entries) {
47
+ const levels = [];
48
+ for (const { lod, mesh, reports } of list) {
49
+ const v = mesh.positions.length / 3, i = mesh.indices.length;
50
+ const w = v <= 0x10000 ? 2 : 4;
51
+ levels.push({ lod, kind: mesh.kind, v, i, w, at: dataBytes, reports: reports ? reports.map((r) => [r.severity, r.code, r.message]) : [] });
52
+ plan.push({ mesh, at: dataBytes, w });
53
+ dataBytes += v * 32 + align4(i * w);
54
+ }
55
+ description.entries.push(levels);
56
+ }
57
+ const meta = encoder.encode(JSON.stringify(description));
58
+ const head = 12 + align4(meta.length);
59
+ const buffer = new ArrayBuffer(head + dataBytes);
60
+ const u8 = new Uint8Array(buffer);
61
+ const view = new DataView(buffer);
62
+ view.setUint32(0, MAGIC, true);
63
+ view.setUint32(4, BUNDLE_VERSION, true);
64
+ view.setUint32(8, head - 12, true);
65
+ u8.set(meta, 12);
66
+ u8.fill(0x20, 12 + meta.length, head);
67
+ for (const { mesh, at, w } of plan) {
68
+ const v = mesh.positions.length / 3;
69
+ const base = head + at;
70
+ new Float32Array(buffer, base, v * 3).set(mesh.positions);
71
+ new Float32Array(buffer, base + v * 12, v * 3).set(mesh.normals);
72
+ new Float32Array(buffer, base + v * 24, v * 2).set(mesh.uvs);
73
+ new (w === 2 ? Uint16Array : Uint32Array)(buffer, base + v * 32, mesh.indices.length).set(mesh.indices);
74
+ }
75
+ return buffer;
76
+ }
77
+
78
+ const isCount = (n) => Number.isInteger(n) && n >= 0;
79
+
80
+ /**
81
+ * @param {ArrayBuffer} buffer
82
+ * @returns {{key: string, entries: {lod: number, mesh: Mesh, reports: object[]}[][]}|null}
83
+ * null when the buffer is not a valid bundle of this layout
84
+ */
85
+ export function decodeBundle(buffer) {
86
+ if (!littleEndian || !(buffer instanceof ArrayBuffer) || buffer.byteLength < 12) return null;
87
+ const view = new DataView(buffer);
88
+ if (view.getUint32(0, true) !== MAGIC || view.getUint32(4, true) !== BUNDLE_VERSION) return null;
89
+ const metaBytes = view.getUint32(8, true);
90
+ if (metaBytes % 4 || 12 + metaBytes > buffer.byteLength) return null;
91
+ let description;
92
+ try {
93
+ description = JSON.parse(decoder.decode(new Uint8Array(buffer, 12, metaBytes)));
94
+ } catch {
95
+ return null;
96
+ }
97
+ if (!description || typeof description.key !== 'string' || !Array.isArray(description.entries)) return null;
98
+ const data = 12 + metaBytes;
99
+ const entries = [];
100
+ for (const list of description.entries) {
101
+ if (!Array.isArray(list)) return null;
102
+ const levels = [];
103
+ for (const l of list) {
104
+ if (!l || !isCount(l.lod) || !isCount(l.v) || !isCount(l.i) || !isCount(l.at) || l.at % 4) return null;
105
+ if ((l.kind !== 'triangles' && l.kind !== 'lines') || (l.w !== 2 && l.w !== 4) || (l.w === 2 && l.v > 0x10000)) return null;
106
+ if (l.i % (l.kind === 'lines' ? 2 : 3)) return null;
107
+ const at = data + l.at;
108
+ if (at + l.v * 32 + l.i * l.w > buffer.byteLength) return null;
109
+ const indices = l.w === 2 ? new Uint32Array(new Uint16Array(buffer, at + l.v * 32, l.i)) : new Uint32Array(buffer, at + l.v * 32, l.i);
110
+ const mesh = new Mesh({
111
+ positions: new Float32Array(buffer, at, l.v * 3),
112
+ normals: new Float32Array(buffer, at + l.v * 12, l.v * 3),
113
+ uvs: new Float32Array(buffer, at + l.v * 24, l.v * 2),
114
+ indices,
115
+ kind: l.kind,
116
+ });
117
+ const reports = Array.isArray(l.reports) ? l.reports.filter((r) => Array.isArray(r) && r.length === 3).map(([severity, code, message]) => ({ severity: String(severity), code: String(code), message: String(message) })) : [];
118
+ levels.push({ lod: l.lod, mesh, reports });
119
+ }
120
+ entries.push(levels);
121
+ }
122
+ return { key: description.key, entries };
123
+ }
@@ -1,73 +1,73 @@
1
- // SHA-256 (FIPS 180-4), synchronous.
2
- //
3
- // The persistent geometry cache names its records by content. Web Crypto's
4
- // digest() would do, but it is asynchronous and only exists in secure
5
- // contexts (https, localhost): a page served over plain http on a local
6
- // network, as the LAN benchmark is, has no crypto.subtle. The inputs hashed
7
- // here are short (shape parameters and path data), so a plain
8
- // implementation is fast enough and gives the same digest everywhere.
9
-
10
- const K = new Uint32Array([
11
- 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
12
- 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
13
- 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
14
- 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
15
- 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
16
- 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
17
- 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
18
- 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
19
- ]);
20
-
21
- const W = new Uint32Array(64);
22
- const encoder = new TextEncoder();
23
-
24
- /** SHA-256 of a string (UTF-8) or of bytes, as 64 lowercase hex digits. */
25
- export function sha256(input) {
26
- const data = typeof input === 'string' ? encoder.encode(input) : input instanceof Uint8Array ? input : new Uint8Array(input.buffer || input, input.byteOffset || 0, input.byteLength);
27
- // padding: 0x80, zeros, then the bit length as a 64-bit big-endian number
28
- const n = data.length;
29
- const total = (n + 9 + 63) & ~63;
30
- const m = new Uint8Array(total);
31
- m.set(data);
32
- m[n] = 0x80;
33
- const view = new DataView(m.buffer);
34
- view.setUint32(total - 8, Math.floor(n / 0x20000000), false);
35
- view.setUint32(total - 4, (n << 3) >>> 0, false);
36
- let h0 = 0x6a09e667, h1 = 0xbb67ae85, h2 = 0x3c6ef372, h3 = 0xa54ff53a;
37
- let h4 = 0x510e527f, h5 = 0x9b05688c, h6 = 0x1f83d9ab, h7 = 0x5be0cd19;
38
- for (let off = 0; off < total; off += 64) {
39
- for (let i = 0; i < 16; i++) W[i] = view.getUint32(off + i * 4, false);
40
- for (let i = 16; i < 64; i++) {
41
- const a = W[i - 15], b = W[i - 2];
42
- const s0 = ((a >>> 7) | (a << 25)) ^ ((a >>> 18) | (a << 14)) ^ (a >>> 3);
43
- const s1 = ((b >>> 17) | (b << 15)) ^ ((b >>> 19) | (b << 13)) ^ (b >>> 10);
44
- W[i] = (W[i - 16] + s0 + W[i - 7] + s1) | 0;
45
- }
46
- let a = h0, b = h1, c = h2, d = h3, e = h4, f = h5, g = h6, h = h7;
47
- for (let i = 0; i < 64; i++) {
48
- const S1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
49
- const t1 = (h + S1 + ((e & f) ^ (~e & g)) + K[i] + W[i]) | 0;
50
- const S0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
51
- const t2 = (S0 + ((a & b) ^ (a & c) ^ (b & c))) | 0;
52
- h = g;
53
- g = f;
54
- f = e;
55
- e = (d + t1) | 0;
56
- d = c;
57
- c = b;
58
- b = a;
59
- a = (t1 + t2) | 0;
60
- }
61
- h0 = (h0 + a) | 0;
62
- h1 = (h1 + b) | 0;
63
- h2 = (h2 + c) | 0;
64
- h3 = (h3 + d) | 0;
65
- h4 = (h4 + e) | 0;
66
- h5 = (h5 + f) | 0;
67
- h6 = (h6 + g) | 0;
68
- h7 = (h7 + h) | 0;
69
- }
70
- let hex = '';
71
- for (const v of [h0, h1, h2, h3, h4, h5, h6, h7]) hex += (v >>> 0).toString(16).padStart(8, '0');
72
- return hex;
73
- }
1
+ // SHA-256 (FIPS 180-4), synchronous.
2
+ //
3
+ // The persistent geometry cache names its records by content. Web Crypto's
4
+ // digest() would do, but it is asynchronous and only exists in secure
5
+ // contexts (https, localhost): a page served over plain http on a local
6
+ // network, as the LAN benchmark is, has no crypto.subtle. The inputs hashed
7
+ // here are short (shape parameters and path data), so a plain
8
+ // implementation is fast enough and gives the same digest everywhere.
9
+
10
+ const K = new Uint32Array([
11
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
12
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
13
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
14
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
15
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
16
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
17
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
18
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
19
+ ]);
20
+
21
+ const W = new Uint32Array(64);
22
+ const encoder = new TextEncoder();
23
+
24
+ /** SHA-256 of a string (UTF-8) or of bytes, as 64 lowercase hex digits. */
25
+ export function sha256(input) {
26
+ const data = typeof input === 'string' ? encoder.encode(input) : input instanceof Uint8Array ? input : new Uint8Array(input.buffer || input, input.byteOffset || 0, input.byteLength);
27
+ // padding: 0x80, zeros, then the bit length as a 64-bit big-endian number
28
+ const n = data.length;
29
+ const total = (n + 9 + 63) & ~63;
30
+ const m = new Uint8Array(total);
31
+ m.set(data);
32
+ m[n] = 0x80;
33
+ const view = new DataView(m.buffer);
34
+ view.setUint32(total - 8, Math.floor(n / 0x20000000), false);
35
+ view.setUint32(total - 4, (n << 3) >>> 0, false);
36
+ let h0 = 0x6a09e667, h1 = 0xbb67ae85, h2 = 0x3c6ef372, h3 = 0xa54ff53a;
37
+ let h4 = 0x510e527f, h5 = 0x9b05688c, h6 = 0x1f83d9ab, h7 = 0x5be0cd19;
38
+ for (let off = 0; off < total; off += 64) {
39
+ for (let i = 0; i < 16; i++) W[i] = view.getUint32(off + i * 4, false);
40
+ for (let i = 16; i < 64; i++) {
41
+ const a = W[i - 15], b = W[i - 2];
42
+ const s0 = ((a >>> 7) | (a << 25)) ^ ((a >>> 18) | (a << 14)) ^ (a >>> 3);
43
+ const s1 = ((b >>> 17) | (b << 15)) ^ ((b >>> 19) | (b << 13)) ^ (b >>> 10);
44
+ W[i] = (W[i - 16] + s0 + W[i - 7] + s1) | 0;
45
+ }
46
+ let a = h0, b = h1, c = h2, d = h3, e = h4, f = h5, g = h6, h = h7;
47
+ for (let i = 0; i < 64; i++) {
48
+ const S1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
49
+ const t1 = (h + S1 + ((e & f) ^ (~e & g)) + K[i] + W[i]) | 0;
50
+ const S0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
51
+ const t2 = (S0 + ((a & b) ^ (a & c) ^ (b & c))) | 0;
52
+ h = g;
53
+ g = f;
54
+ f = e;
55
+ e = (d + t1) | 0;
56
+ d = c;
57
+ c = b;
58
+ b = a;
59
+ a = (t1 + t2) | 0;
60
+ }
61
+ h0 = (h0 + a) | 0;
62
+ h1 = (h1 + b) | 0;
63
+ h2 = (h2 + c) | 0;
64
+ h3 = (h3 + d) | 0;
65
+ h4 = (h4 + e) | 0;
66
+ h5 = (h5 + f) | 0;
67
+ h6 = (h6 + g) | 0;
68
+ h7 = (h7 + h) | 0;
69
+ }
70
+ let hex = '';
71
+ for (const v of [h0, h1, h2, h3, h4, h5, h6, h7]) hex += (v >>> 0).toString(16).padStart(8, '0');
72
+ return hex;
73
+ }