@factoidal/core 0.1.0

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 (43) hide show
  1. package/CHANGELOG.md +121 -0
  2. package/LICENSE +201 -0
  3. package/README.md +514 -0
  4. package/browser-wasm.js +872 -0
  5. package/browser.d.ts +514 -0
  6. package/browser.js +2276 -0
  7. package/factoidal-npm-entry.js +32609 -0
  8. package/factoidal-npm-entry.wasm.assets/code-7ac046580f1bbdda8dc6.wasm +0 -0
  9. package/factoidal-npm-entry.wasm.js +455 -0
  10. package/factoidal.js +27560 -0
  11. package/factoidal.wasm.assets/code-bbe6099bfb5b10c4c3ab.wasm +0 -0
  12. package/factoidal.wasm.js +457 -0
  13. package/fn.d.ts +519 -0
  14. package/fn.js +916 -0
  15. package/hacl-init.js +92 -0
  16. package/hacl-wasm/FStar.wasm +0 -0
  17. package/hacl-wasm/Hacl_Bignum.wasm +0 -0
  18. package/hacl-wasm/Hacl_Bignum25519_51.wasm +0 -0
  19. package/hacl-wasm/Hacl_Bignum_Base.wasm +0 -0
  20. package/hacl-wasm/Hacl_Curve25519_51.wasm +0 -0
  21. package/hacl-wasm/Hacl_Ed25519.wasm +0 -0
  22. package/hacl-wasm/Hacl_Ed25519_PrecompTable.wasm +0 -0
  23. package/hacl-wasm/Hacl_Hash_Base.wasm +0 -0
  24. package/hacl-wasm/Hacl_Hash_SHA2.wasm +0 -0
  25. package/hacl-wasm/Hacl_IntTypes_Intrinsics.wasm +0 -0
  26. package/hacl-wasm/LowStar_Endianness.wasm +0 -0
  27. package/hacl-wasm/WasmSupport.wasm +0 -0
  28. package/hacl-wasm/api.js +775 -0
  29. package/hacl-wasm/api.json +3787 -0
  30. package/hacl-wasm/layouts.json +1 -0
  31. package/hacl-wasm/loader.js +568 -0
  32. package/hacl-wasm/shell.js +12 -0
  33. package/index.d.ts +1068 -0
  34. package/index.js +237 -0
  35. package/index.mjs +85 -0
  36. package/lib/api.js +2140 -0
  37. package/lib/engine-js.js +165 -0
  38. package/lib/engine-wasm.js +300 -0
  39. package/package.json +101 -0
  40. package/rdfjs.js +540 -0
  41. package/version.json +101 -0
  42. package/wasm.d.ts +130 -0
  43. package/wasm.js +158 -0
@@ -0,0 +1 @@
1
+ {"Spec_Hash_Definitions_hash_alg":["LEnum"],"Prims_string":["LBuiltin",["I32"],["A32"]],"Prims_int":["LBuiltin",["I32"],["A32"]],"K___uint32_t_uint32_t":["LFlat",{"size":8,"fields":[["fst",[0,["Int",["A32"]]]],["snd",[4,["Int",["A32"]]]]]}],"__bool_bool_bool_bool":["LFlat",{"size":4,"fields":[["fst",[0,["Int",["A8"]]]],["snd",[1,["Int",["A8"]]]],["thd",[2,["Int",["A8"]]]],["f3",[3,["Int",["A8"]]]]]}],"__bool_bool":["LFlat",{"size":2,"fields":[["fst",[0,["Int",["A8"]]]],["snd",[1,["Int",["A8"]]]]]}],"Hacl_Streaming_Types_error_code":["LEnum"],"Hacl_Streaming_Poly1305_32_poly1305_32_state":["LFlat",{"size":20,"fields":[["block_state",[0,["Pointer",["Int",["A64"]]]]],["buf",[4,["Pointer",["Int",["A8"]]]]],["total_len",[8,["Int",["A64"]]]],["p_key",[16,["Pointer",["Int",["A8"]]]]]]}],"Hacl_Streaming_MD_state_64":["LFlat",{"size":16,"fields":[["block_state",[0,["Pointer",["Int",["A64"]]]]],["buf",[4,["Pointer",["Int",["A8"]]]]],["total_len",[8,["Int",["A64"]]]]]}],"Hacl_Streaming_MD_state_32":["LFlat",{"size":16,"fields":[["block_state",[0,["Pointer",["Int",["A32"]]]]],["buf",[4,["Pointer",["Int",["A8"]]]]],["total_len",[8,["Int",["A64"]]]]]}],"Hacl_Streaming_Keccak_state":["LFlat",{"size":24,"fields":[["block_state",[0,["Layout","Hacl_Streaming_Keccak_hash_buf"]]],["buf",[8,["Pointer",["Int",["A8"]]]]],["total_len",[16,["Int",["A64"]]]]]}],"hash_buf2":["LFlat",{"size":16,"fields":[["fst",[0,["Layout","Hacl_Streaming_Keccak_hash_buf"]]],["snd",[8,["Layout","Hacl_Streaming_Keccak_hash_buf"]]]]}],"Hacl_Streaming_Keccak_hash_buf":["LFlat",{"size":8,"fields":[["fst",[0,["Int",["A32"]]]],["snd",[4,["Pointer",["Int",["A64"]]]]]]}],"Hacl_Streaming_Blake2s_128_blake2s_128_state":["LFlat",{"size":24,"fields":[["block_state",[0,["Layout","Hacl_Streaming_Blake2s_128_blake2s_128_block_state"]]],["buf",[8,["Pointer",["Int",["A8"]]]]],["total_len",[16,["Int",["A64"]]]]]}],"Hacl_Streaming_Blake2s_128_blake2s_128_block_state":["LFlat",{"size":8,"fields":[["fst",[0,["Pointer",["Unknown"]]]],["snd",[4,["Pointer",["Unknown"]]]]]}],"Hacl_Streaming_Blake2b_256_blake2b_256_state":["LFlat",{"size":24,"fields":[["block_state",[0,["Layout","Hacl_Streaming_Blake2b_256_blake2b_256_block_state"]]],["buf",[8,["Pointer",["Int",["A8"]]]]],["total_len",[16,["Int",["A64"]]]]]}],"Hacl_Streaming_Blake2b_256_blake2b_256_block_state":["LFlat",{"size":8,"fields":[["fst",[0,["Pointer",["Unknown"]]]],["snd",[4,["Pointer",["Unknown"]]]]]}],"Hacl_Streaming_Blake2_blake2s_32_state":["LFlat",{"size":24,"fields":[["block_state",[0,["Layout","Hacl_Streaming_Blake2_blake2s_32_block_state"]]],["buf",[8,["Pointer",["Int",["A8"]]]]],["total_len",[16,["Int",["A64"]]]]]}],"Hacl_Streaming_Blake2_blake2s_32_block_state":["LFlat",{"size":8,"fields":[["fst",[0,["Pointer",["Int",["A32"]]]]],["snd",[4,["Pointer",["Int",["A32"]]]]]]}],"Hacl_Streaming_Blake2_blake2b_32_state":["LFlat",{"size":24,"fields":[["block_state",[0,["Layout","Hacl_Streaming_Blake2_blake2b_32_block_state"]]],["buf",[8,["Pointer",["Int",["A8"]]]]],["total_len",[16,["Int",["A64"]]]]]}],"Hacl_Streaming_Blake2_blake2b_32_block_state":["LFlat",{"size":8,"fields":[["fst",[0,["Pointer",["Int",["A64"]]]]],["snd",[4,["Pointer",["Int",["A64"]]]]]]}],"Hacl_Impl_SHA2_Types_uint8_8p":["LFlat",{"size":56,"fields":[["fst",[0,["Pointer",["Int",["A8"]]]]],["snd",[8,["Layout","Hacl_Impl_SHA2_Types_uint8_7p"]]]]}],"Hacl_Impl_SHA2_Types_uint8_7p":["LFlat",{"size":48,"fields":[["fst",[0,["Pointer",["Int",["A8"]]]]],["snd",[8,["Layout","Hacl_Impl_SHA2_Types_uint8_6p"]]]]}],"Hacl_Impl_SHA2_Types_uint8_6p":["LFlat",{"size":40,"fields":[["fst",[0,["Pointer",["Int",["A8"]]]]],["snd",[8,["Layout","Hacl_Impl_SHA2_Types_uint8_5p"]]]]}],"Hacl_Impl_SHA2_Types_uint8_5p":["LFlat",{"size":32,"fields":[["fst",[0,["Pointer",["Int",["A8"]]]]],["snd",[8,["Layout","Hacl_Impl_SHA2_Types_uint8_4p"]]]]}],"Hacl_Impl_SHA2_Types_uint8_4p":["LFlat",{"size":24,"fields":[["fst",[0,["Pointer",["Int",["A8"]]]]],["snd",[8,["Layout","Hacl_Impl_SHA2_Types_uint8_3p"]]]]}],"Hacl_Impl_SHA2_Types_uint8_3p":["LFlat",{"size":16,"fields":[["fst",[0,["Pointer",["Int",["A8"]]]]],["snd",[8,["Layout","Hacl_Impl_SHA2_Types_uint8_2p"]]]]}],"Hacl_Impl_SHA2_Types_uint8_2x8p":["LFlat",{"size":112,"fields":[["fst",[0,["Layout","Hacl_Impl_SHA2_Types_uint8_8p"]]],["snd",[56,["Layout","Hacl_Impl_SHA2_Types_uint8_8p"]]]]}],"Hacl_Impl_SHA2_Types_uint8_2x4p":["LFlat",{"size":48,"fields":[["fst",[0,["Layout","Hacl_Impl_SHA2_Types_uint8_4p"]]],["snd",[24,["Layout","Hacl_Impl_SHA2_Types_uint8_4p"]]]]}],"Hacl_Impl_SHA2_Types_uint8_2p":["LFlat",{"size":8,"fields":[["fst",[0,["Pointer",["Int",["A8"]]]]],["snd",[4,["Pointer",["Int",["A8"]]]]]]}],"Hacl_Impl_HPKE_context_s":["LFlat",{"size":16,"fields":[["ctx_key",[0,["Pointer",["Int",["A8"]]]]],["ctx_nonce",[4,["Pointer",["Int",["A8"]]]]],["ctx_seq",[8,["Pointer",["Int",["A64"]]]]],["ctx_exporter",[12,["Pointer",["Int",["A8"]]]]]]}],"Hacl_HMAC_DRBG_state":["LFlat",{"size":12,"fields":[["k",[0,["Pointer",["Int",["A8"]]]]],["v",[4,["Pointer",["Int",["A8"]]]]],["reseed_counter",[8,["Pointer",["Int",["A32"]]]]]]}],"Hacl_Bignum_MontArithmetic_bn_mont_ctx_u64":["LFlat",{"size":20,"fields":[["len",[0,["Int",["A32"]]]],["n",[4,["Pointer",["Int",["A64"]]]]],["mu",[8,["Int",["A64"]]]],["r2",[16,["Pointer",["Int",["A64"]]]]]]}],"Hacl_Bignum_MontArithmetic_bn_mont_ctx_u32":["LFlat",{"size":16,"fields":[["len",[0,["Int",["A32"]]]],["n",[4,["Pointer",["Int",["A32"]]]]],["mu",[8,["Int",["A32"]]]],["r2",[12,["Pointer",["Int",["A32"]]]]]]}],"FStar_UInt128_uint128":["LFlat",{"size":16,"fields":[["low",[0,["Int",["A64"]]]],["high",[8,["Int",["A64"]]]]]}],"EverCrypt_Hash_Incremental_hash_state":["LFlat",{"size":16,"fields":[["block_state",[0,["Pointer",["Layout","EverCrypt_Hash_state_s"]]]],["buf",[4,["Pointer",["Int",["A8"]]]]],["total_len",[8,["Int",["A64"]]]]]}],"state_s_tags":["LEnum"],"EverCrypt_Hash_state_s":["LFlat",{"size":12,"fields":[["tag",[0,["Int",["A32"]]]],["val",[8,["Union",[["Pointer",["Int",["A32"]]],["Pointer",["Int",["A32"]]],["Pointer",["Int",["A32"]]],["Pointer",["Int",["A32"]]],["Pointer",["Int",["A64"]]],["Pointer",["Int",["A64"]]],["Pointer",["Int",["A64"]]],["Pointer",["Int",["A64"]]],["Pointer",["Int",["A64"]]],["Pointer",["Int",["A64"]]],["Pointer",["Int",["A32"]]],["Pointer",["Unknown"]],["Pointer",["Int",["A64"]]],["Pointer",["Unknown"]]]]]]]}],"EverCrypt_Error_error_code":["LEnum"],"C_String_t_":["LBuiltin",["I32"],["A32"]],"C_String_t":["LBuiltin",["I32"],["A32"]],"C_Compat_String_t_":["LBuiltin",["I32"],["A32"]],"C_Compat_String_t":["LBuiltin",["I32"],["A32"]],"exit_code":["LBuiltin",["I32"],["A32"]],"clock_t":["LBuiltin",["I32"],["A32"]]}
@@ -0,0 +1,568 @@
1
+ // This module performs low-level WASM manipulations, such as linking several
2
+ // modules together and inspecting the debug zone at the bottom of the memory.
3
+ // This module is aware of the compilation strategy of KaRaMeL. It can be
4
+ // included from either a browser or a shell context, as long as my_print is
5
+ // defined in scope.
6
+
7
+ "use strict";
8
+
9
+ var my_print, my_random;
10
+
11
+ // When loaded "as is" via d8 or ch, we inherit the caller's scope. When loaded
12
+ // via node, we don't, so we redefine my_print.
13
+ if (typeof process !== "undefined") {
14
+ my_print = console.log;
15
+ let crypto = require("crypto");
16
+ my_random = (mem, addr, len) => {
17
+ var m8 = new Uint8Array(mem.buffer);
18
+ crypto.randomFillSync(m8, addr, len);
19
+ return true;
20
+ };
21
+ } else {
22
+ my_random = (mem, addr, len) => {
23
+ throw new Error("TODO: random bytes for non-Node platforms");
24
+ };
25
+ }
26
+
27
+ /******************************************************************************/
28
+ /* Some printing helpers */
29
+ /******************************************************************************/
30
+
31
+ function p8(n) {
32
+ return ("0"+Number(n).toString(16)).slice(-2);
33
+ }
34
+
35
+ function hex(m8, start, len) {
36
+ let s = "";
37
+ for (let i = 0; i < len; ++i)
38
+ s += p8(m8[start + i]);
39
+ return s;
40
+ }
41
+
42
+ function hexle(m8, i, m) {
43
+ let buf = "";
44
+ let n = m - 1;
45
+ while (n >= 0) {
46
+ buf += p8(m8[i+n]);
47
+ n--;
48
+ }
49
+ return buf;
50
+ }
51
+
52
+ function hex32(m8, start) {
53
+ return hexle(m8, start, 4);
54
+ }
55
+
56
+ function hex64(m8, start) {
57
+ return hexle(m8, start, 8);
58
+ }
59
+
60
+ function p32(n) {
61
+ return p8((n >>> 24) & 255) + p8((n >>> 16) & 255) + p8((n >>> 8) & 255) + p8(n & 255);
62
+ }
63
+
64
+ // Print the contents of the memory in a format similar to xxd or hexdump.
65
+ // @param {WebAssembly.Memory} mem The memory to print
66
+ // @param {Number} size The amount of memory to print out, in bytes
67
+ // @param {Number} ofs Optional. The offset at which printing
68
+ // starts.
69
+ function dump(mem, size, ofs) {
70
+ let m8 = new Uint8Array(mem.buffer);
71
+ if (!ofs)
72
+ ofs = 0;
73
+ let i = 0;
74
+ let buf = "";
75
+ while (i < size) {
76
+ buf += p32(ofs + i) + " ";
77
+ for (let j = 0; j < 32; ++j) {
78
+ buf += p8(m8[ofs+i+j]);
79
+ if (j % 4 == 3)
80
+ buf += " ";
81
+ }
82
+ buf += " ";
83
+ for (let j = 0; j < 32; ++j) {
84
+ let c = m8[ofs+i+j];
85
+ if (32 <= c && c < 128)
86
+ buf += String.fromCharCode(c);
87
+ else
88
+ buf += ".";
89
+ }
90
+ buf += "\n";
91
+ i += 32;
92
+ }
93
+ my_print(buf);
94
+ }
95
+
96
+ /******************************************************************************/
97
+ /* Implementations of the karamel runtime support library */
98
+ /******************************************************************************/
99
+
100
+ function stringAtAddr(mem, addr) {
101
+ let m8 = new Uint8Array(mem.buffer);
102
+ let buf = "";
103
+ while (m8[addr] != 0) {
104
+ buf += String.fromCharCode(m8[addr++]);
105
+ }
106
+ return buf;
107
+ }
108
+
109
+ function readLeAtAddr(mem, addr, bytes) {
110
+ let m8 = new Uint8Array(mem.buffer);
111
+ let i = 0;
112
+ while (bytes-- > 0)
113
+ i = (i << 8) + m8[addr + bytes];
114
+ return i;
115
+ }
116
+
117
+ function writeLeAtAddr(mem, addr, n, bytes) {
118
+ let m8 = new Uint8Array(mem.buffer);
119
+ for (let i = 0; i < bytes; ++i) {
120
+ m8[addr + i] = n & 255;
121
+ n >>>= 8;
122
+ }
123
+ }
124
+
125
+ function writeBeAtAddr(mem, addr, n, bytes) {
126
+ let m8 = new Uint8Array(mem.buffer);
127
+ while (bytes > 0) {
128
+ m8[addr + bytes - 1] = n & 255;
129
+ bytes--;
130
+ n >>>= 8;
131
+ }
132
+ }
133
+
134
+ function dummyModule(funcs, globals) {
135
+ let module = {};
136
+ for (let f of funcs)
137
+ module[f] = () => {
138
+ my_print("Not implemented: " + s);
139
+ throw new Error();
140
+ };
141
+ for (let g of globals)
142
+ module[g] = 0;
143
+ return module;
144
+ }
145
+
146
+ let mkWasmSupport = (mem) => ({
147
+ WasmSupport_trap: (s) => {
148
+ dump(mem, 2*1024);
149
+ my_print(stringAtAddr(mem, s));
150
+ my_print("Run-time trap, e.g. zero-sized array or abort. See error message above.");
151
+ throw new Error();
152
+ },
153
+ WasmSupport_malloc: (sz) => {
154
+ // We now provide experimental support for heap allocations. This only
155
+ // caters to a very specific scenario, wherein the entirety of heap
156
+ // allocations performed within a function call are reachable via this
157
+ // function's return value. (In effect, this captures all of the
158
+ // `create_in` functions from HACL.). The caller, upon dealing with such a
159
+ // return value, copies the entire object graph using a combination of:
160
+ // - a JSON description of the layout generated by krml, and
161
+ // - the header tag of heap allocations, which contains their length.
162
+ //
163
+ // The allocator is bare-bones: the final word in memory contains the amount
164
+ // of heap data allocated (default: zero). Allocations are aligned on a
165
+ // 64-bit boundary.
166
+ let m32 = new Uint32Array(mem.buffer);
167
+ let memSize = m32.byteLength;
168
+ let heap_size_addr = m32.byteLength/4 - 1;
169
+ let heap_size = m32[heap_size_addr];
170
+ // Size of stack + size of heap + desired allocation + 4 for allocator bump
171
+ // pointer + 8 for rounding on a 64-bit boundary + 8 for header.
172
+ if (m32[0] + heap_size + sz + 20 > memSize)
173
+ throw new Error("malloc: out of memory (TODO: resize & move)");
174
+
175
+ // Header.
176
+ // First four bytes are allocation size, next four bytes are
177
+ // reserved. Data follows.
178
+ my_print("malloc: requested " + p32(sz) + " bytes");
179
+ sz += 8;
180
+
181
+ // Tentative pointer.
182
+ let p = (memSize - 4) - heap_size - sz;
183
+ // Adjusted to fall on a 64-bit boundary; header is 64 bits so data is
184
+ // aligned, too.
185
+ p -= (p % 8);
186
+ // The heap size doesn't take into account the heap size counter itself.
187
+ let new_heap_size = (memSize - 4) - p;
188
+ m32[heap_size_addr] = new_heap_size;
189
+ // Header contains size of allocation block.
190
+ m32[p/4] = sz;
191
+ my_print("total mem_size: " + p32(memSize) + "; new allocation: " + p32(sz) + " bytes, header: 0x" + p32(p));
192
+ my_print("new heap size: "+p32(new_heap_size));
193
+ return p+8;
194
+ }
195
+ });
196
+
197
+ let mkFStar = () => dummyModule(
198
+ [ "FStar_UInt128_constant_time_carry_ok", "FStar_PropositionalExtensionality_axiom" ],
199
+ [ "FStar_Monotonic_Heap_lemma_mref_injectivity" ]);
200
+
201
+ let mkLowStar = (mem) => ({
202
+ LowStar_Monotonic_Buffer_is_null: (addr) => (addr == 0)
203
+ });
204
+
205
+ let mkSteelReference = (mem) => ({
206
+ Steel_Reference_is_null: (addr) => (addr == 0)
207
+ });
208
+
209
+ let mkPrims = (mem) => ({
210
+ Prims_op_Addition: (x, y) => { return x + y; }
211
+ });
212
+
213
+ let mkC = (mem) => ({
214
+ srand: () => { throw new Error("todo: srand") },
215
+ rand: () => { throw new Error("todo: rand") },
216
+ exit: (errno) => { throw new Error("Program exited via C.exit [errno="+errno+"]") },
217
+ portable_exit: (errno) => { throw new Error("Program exited via C.portable_exit [errno="+errno+"]") },
218
+ print_bytes: () => { throw new Error("todo: print_bytes") },
219
+
220
+ char_uint8: () => { throw new Error("todo: char_uint8") },
221
+ uint8_char: () => { throw new Error("todo: uint8_char") },
222
+ char_of_uint8: (x) => { throw new Error("todo: char_of_uint8") },
223
+ uint8_of_char: (x) => { throw new Error("todo: uint8_of_char") },
224
+
225
+ // A Prims_string generates a literal allocated in the data segment;
226
+ // string_of_literal is just a typing trick.
227
+ string_of_literal: (x) => x,
228
+ print_string: (addr) => my_print(stringAtAddr(mem, addr)),
229
+
230
+ // Truncated
231
+ clock: () => Date.now(),
232
+ });
233
+
234
+ let mkLibRandomBufferSystem = (mem) => ({
235
+ Lib_RandomBuffer_System_randombytes: (addr, len) => { return my_random(mem, addr, len); }
236
+ });
237
+
238
+ let mkLibMemzero = (mem) => ({
239
+ Lib_Memzero_clear_words_u8: (nwords, addr) => {
240
+ let m8 = new Uint8Array(mem.buffer);
241
+ for (let i = 0; i < nwords; ++i) {
242
+ m8[addr+i] = 0;
243
+ }
244
+ },
245
+ Lib_Memzero_clear_words_u16: (nwords, addr) => {
246
+ let m8 = new Uint8Array(mem.buffer);
247
+ for (let i = 0; i < nwords; ++i) {
248
+ m8[addr+2*i] = 0;
249
+ m8[addr+2*i+1] = 0;
250
+ }
251
+ }
252
+ });
253
+
254
+ let mkLowStarEndianness = (mem) => ({
255
+ htole16: (x) => { throw new Error("todo: htole16") },
256
+ le16toh: (x) => { throw new Error("todo: le16toh") },
257
+ htole32: (x) => { throw new Error("todo: htole32") },
258
+ le32toh: (x) => { throw new Error("todo: le32toh") },
259
+ htole64: (x) => { throw new Error("todo: htole64") },
260
+ le64toh: (x) => { throw new Error("todo: le64toh") },
261
+ htobe16: (x) => { throw new Error("todo: htobe16") },
262
+ be16toh: (x) => { throw new Error("todo: be16toh") },
263
+ htobe32: (x) => { throw new Error("todo: htobe32") },
264
+ be32toh: (x) => { throw new Error("todo: be32toh") },
265
+ htobe64: (x) => { throw new Error("todo: htobe64") },
266
+ be64toh: (x) => { throw new Error("todo: be64toh") },
267
+ index_32_be: (x) => { throw new Error("todo: index_32_be") },
268
+ index_32_le: (x) => { throw new Error("todo: index_32_le") },
269
+ index_64_be: (x) => { throw new Error("todo: index_64_be") },
270
+ index_64_le: (x) => { throw new Error("todo: index_64_le") },
271
+ // all of these: implemented natively in CFlatToWasm
272
+ store16_le: (addr, n) => { throw new Error("todo: store16_le") },
273
+ load16_le: (addr) => { throw new Error("todo: load16_le") },
274
+ store16_be: (addr, n) => { throw new Error("todo: store16_be") },
275
+ load16_be: (addr) => { throw new Error("todo: load16_be") },
276
+ store32_le: (addr, n) => { throw new error("todo: store32_le") },
277
+ store32_be: (addr, n) => { throw new error("todo: store32_be") },
278
+ load32_be: (addr) => { throw new Error("todo: load32_be") },
279
+ store64_le: (addr, n) => { throw new Error("todo: store64_le") },
280
+ load64_le: (addr) => { throw new Error("todo: load64_le") },
281
+ store64_be: (addr, n) => { throw new Error("todo: store64_be") },
282
+ load64_be: (addr) => { throw new Error("todo: load64_be") },
283
+ store128_le: (addr, n) => { throw new Error("todo: store128_le") },
284
+ load128_le: (addr) => { throw new Error("todo: load128_le") },
285
+ store128_be: (addr, n) => { throw new Error("todo: store128_be") },
286
+ load128_be: (addr) => { throw new Error("todo: load128_be") },
287
+ // this definition works for both C.Endianness and LowStar.Endianness
288
+ store16_le_i: (addr, n) => { throw new Error("todo_i: store16_le") },
289
+ load16_le_i: (addr) => { throw new Error("todo_i: load16_le") },
290
+ store16_be_i: (addr, n) => { throw new Error("todo_i: store16_be") },
291
+ load16_be_i: (addr) => { throw new Error("todo_i: load16_be") },
292
+ store32_le_i: (addr, n) => { throw new error("todo_i: store32_le") },
293
+ store32_be_i: (addr, n) => { throw new error("todo_i: store32_be") },
294
+ load32_be_i: (addr) => { throw new Error("todo_i: load32_be") },
295
+ store64_le_i: (addr, n) => { throw new Error("todo_i: store64_le") },
296
+ load64_le_i: (addr) => { throw new Error("todo_i: load64_le") },
297
+ store64_be_i: (addr, n) => { throw new Error("todo_i: store64_be") },
298
+ load64_be_i: (addr) => { throw new Error("todo_i: load64_be") },
299
+ store128_le_i: (addr, n) => { throw new Error("todo_i: store128_le") },
300
+ load128_le_i: (addr) => { throw new Error("todo_i: load128_le") },
301
+ store128_be_i: (addr, n) => { throw new Error("todo_i: store128_be") },
302
+ load128_be_i: (addr) => { throw new Error("todo_i: load128_be") }
303
+ });
304
+
305
+ let mkCNullity = (mem) => ({
306
+ C_Nullity_is_null: () => { throw new Error("todo: is_null") }
307
+ });
308
+
309
+ let mkCString = (mem, prefix) => ({
310
+ [prefix + '_print']: (ptr) => my_print(stringAtAddr(mem, ptr)),
311
+ [prefix + '_of_literal']: (ptr) => { throw new Error("primitive: "+prefix+"_of_literal") },
312
+ [prefix + '_strlen']: (ptr) => {
313
+ let m8 = new Uint8Array(mem.buffer);
314
+ let ofs = 0;
315
+ while (m8[ptr+ofs] != 0)
316
+ ofs++;
317
+ return ofs;
318
+ },
319
+ [prefix + '_memcpy']: (dst, src, n) => {
320
+ let m8 = new Uint8Array(mem.buffer);
321
+ let i = 0;
322
+ while (i < n) {
323
+ m8[dst+i] = m8[src+i];
324
+ i++;
325
+ }
326
+ },
327
+ });
328
+
329
+ function checkEq(mem, name) {
330
+ return (x1, x2) => {
331
+ if (x1 != x2) {
332
+ dump(mem, 2*1024);
333
+ my_print(name + ": equality failure, "+x1+" != "+x2);
334
+ throw new Error();
335
+ }
336
+ return 0;
337
+ };
338
+ }
339
+
340
+ let mkTestLib = (mem, prefix) => ({
341
+ [prefix + '_touch']: () => 0,
342
+ [prefix + '_check8']: checkEq(mem, prefix+"_check8"),
343
+ [prefix + '_check16']: checkEq(mem, prefix+"_check16"),
344
+ [prefix + '_check32']: checkEq(mem, prefix+"_check32"),
345
+ [prefix + '_check64']: checkEq(mem, prefix+"_check64"),
346
+ [prefix + '_checku8']: checkEq(mem, prefix+"_checku8"),
347
+ [prefix + '_checku16']: checkEq(mem, prefix+"_checku16"),
348
+ [prefix + '_checku32']: checkEq(mem, prefix+"_checku32"),
349
+ [prefix + '_checku64']: checkEq(mem, prefix+"_checku64"),
350
+ [prefix + '_check']: (b) => {
351
+ if (!b) {
352
+ dump(mem, 2*1024);
353
+ my_print(prefix+"_check: assertion failure");
354
+ throw new Error();
355
+ }
356
+ return 0;
357
+ },
358
+ [prefix + '_unsafe_malloc']: () => { throw new Error("todo: unsafe_malloc") },
359
+ [prefix + '_perr']: (err) => {
360
+ my_print("Got error code "+err);
361
+ return 0;
362
+ },
363
+ [prefix + '_uint8_p_null']: 0,
364
+ [prefix + '_uint32_p_null']: 0,
365
+ [prefix + '_uint64_p_null']: 0,
366
+ [prefix + '_compare_and_print']: (addr, b1, b2, len) => {
367
+ let m8 = new Uint8Array(mem.buffer);
368
+ let str = stringAtAddr(m8, addr);
369
+ let hex1 = hex(m8, b1, len);
370
+ let hex2 = hex(m8, b2, len);
371
+ my_print("[test] expected output "+str+" is "+hex1);
372
+ my_print("[test] computed output "+str+" is "+hex2);
373
+ for (let i = 0; i < len; ++i) {
374
+ if (m8[b1+i] != m8[b2+i]) {
375
+ my_print("[test] reference "+str+" and expected "+str+" differ at byte "+i);
376
+ my_print("b1="+p32(b1)+",b2="+p32(b2));
377
+ dump(mem, 4*1024);
378
+ throw new Error("test failure");
379
+ }
380
+ }
381
+ my_print("[test] "+str+" is a success\n");
382
+ },
383
+ [prefix + '_print_clock_diff']: (t1, t2) => {
384
+ my_print("[benchmark] took: " + (t2 - t1) + "ms\n");
385
+ },
386
+ });
387
+
388
+ /******************************************************************************/
389
+ /* Memory layout */
390
+ /******************************************************************************/
391
+
392
+ // mem[0..3] = top-of-the-stack pointer (I32);
393
+ // mem[4..127] = scratch space for debugging.
394
+ // Conventions for debugging and "debugging format" (lolz) are in WasmDebug.ml
395
+ const header_size = 128;
396
+
397
+ // this sets up the base data pointer and the debug entry point for the WASM
398
+ // context; WASM-generated code expects to have these defined.
399
+ function init() {
400
+ let mem = new WebAssembly.Memory({ initial: 16 });
401
+ let m8 = new Uint8Array(mem.buffer);
402
+ let nest = 0;
403
+
404
+ let debug = () => {
405
+ let i = 0;
406
+ let buf = "";
407
+ let string = () => {
408
+ let ptr = m8[i] + (m8[i+1] << 8) + (m8[i+2] << 16) + (m8[i+3] << 24);
409
+ i += 4;
410
+ while (m8[ptr] != 0) {
411
+ buf += String.fromCharCode(m8[ptr]);
412
+ ptr++;
413
+ }
414
+ };
415
+ let int32 = () => {
416
+ buf += "0x";
417
+ buf += hex32(m8, i);
418
+ i += 4;
419
+ };
420
+ let int64 = () => {
421
+ buf += "0x";
422
+ buf += hex64(m8, i);
423
+ i += 8;
424
+ };
425
+ // Dump the stack pointer
426
+ int32();
427
+ buf += " | ";
428
+ buf += " ".repeat(nest);
429
+ while (m8[i] != 0 && i < 128) {
430
+ let c = m8[i];
431
+ i++;
432
+ switch (c) {
433
+ case 1:
434
+ string();
435
+ break;
436
+ case 2:
437
+ int32();
438
+ break;
439
+ case 3:
440
+ int64();
441
+ break;
442
+ case 4:
443
+ nest++;
444
+ break;
445
+ case 5:
446
+ nest--;
447
+ break;
448
+ default:
449
+ my_print(hex(m8, 0, 128));
450
+ throw "unrecognized debug format:\n buf="+buf+"\n c=0x"+p8(c)+"\n i="+i;
451
+ }
452
+ buf += " ";
453
+ }
454
+ my_print(buf);
455
+ };
456
+
457
+ // Initialize the highwater mark.
458
+ new Uint32Array(mem.buffer)[0] = header_size;
459
+
460
+ // Base stuff that appears as requirement, because they're exposed in TestLib
461
+ // and/or C.
462
+ let imports = {
463
+ Karamel: {
464
+ mem: mem,
465
+ debug: debug,
466
+ data_start: header_size
467
+ },
468
+ WasmSupport: mkWasmSupport(mem),
469
+ FStar: mkFStar(mem),
470
+ LowStar: mkLowStar(mem),
471
+ Steel_Reference: mkSteelReference(mem),
472
+ Prims: mkPrims(mem),
473
+ LowStar_Endianness: mkLowStarEndianness(mem),
474
+ C: mkC(mem),
475
+ C_Compat: mkC(mem),
476
+ C_Endianness: mkLowStarEndianness(mem),
477
+ C_Compat_Endianness: mkLowStarEndianness(mem),
478
+ C_Nullity: mkCNullity(mem),
479
+ C_String: mkCString(mem, 'C_String'),
480
+ C_Compat_String: mkCString(mem, 'C_Compat_String'),
481
+ TestLib: mkTestLib(mem, 'TestLib'),
482
+ TestLib_Compat: mkTestLib(mem, 'TestLib_Compat'),
483
+ Lib_RandomBuffer_System: mkLibRandomBufferSystem(mem),
484
+ Lib_Memzero: mkLibMemzero(mem)
485
+ };
486
+ return imports;
487
+ }
488
+
489
+ // One MUST call this function after loading a module.
490
+ function propagate(module_name, imports, instance) {
491
+ my_print("Module", module_name, "successfully loaded");
492
+ my_print("Adding exports into global import table:\n ", Object.keys(instance.exports));
493
+ if (!(module_name in imports))
494
+ imports[module_name] = {};
495
+ for (let o of Object.keys(instance.exports)) {
496
+ imports[module_name][o] = instance.exports[o];
497
+ }
498
+ my_print("This module has a data segment of size: ", instance.exports.data_size);
499
+ imports.Karamel.data_start += instance.exports.data_size;
500
+ my_print("Next data segment will start at: ", imports.Karamel.data_start);
501
+ // Set the highwater mark right after the data segment
502
+ new Uint32Array(imports.Karamel.mem.buffer)[0] = imports.Karamel.data_start;
503
+ my_print();
504
+ }
505
+
506
+ // One MAY only call this function after all modules have been loaded and
507
+ // suitable calls to propagate have been performed.
508
+ function reserve(mem, size, alignment) {
509
+ if (!alignment)
510
+ alignment = 1;
511
+
512
+ let m32 = new Uint32Array(mem.buffer);
513
+ let p = m32[0];
514
+ if ((p % alignment) != 0)
515
+ p += alignment - (p % alignment);
516
+ let curr = m32.byteLength;
517
+ let want = p + size;
518
+ if (want > curr) {
519
+ // The number of WebAssembly pages you want to grow the memory by (each one is 64KiB in size).
520
+ let need = Math.ceil((want - curr) / (64 * 1024)) + 1;
521
+ // One extra page for suitable stack space.
522
+ mem.grow(need);
523
+ }
524
+ m32[0] = want;
525
+ // my_print("Reserved memory area 0x"+p32(p)+"..0x"+p32(m32[0]));
526
+ // NOTE: we don't care for malloc'd data since we expect the caller to copy
527
+ // everything out right after a WASM call and prior to any calls to `reserve`.
528
+ return p;
529
+ }
530
+
531
+ function link(imports, modules) {
532
+ let fold = async (imports, modules) => {
533
+ if (!modules.length)
534
+ return imports;
535
+
536
+ let [{ name, buf }, ...ms] = modules;
537
+ // Note: this may throw with:
538
+ // error: module is not an object or function
539
+ // This merely indicates a missing import. Look at the .wast to figure it
540
+ // out.
541
+ my_print("Attempting to instantiate", name);
542
+ let { module, instance } = await WebAssembly.instantiate(buf, imports);
543
+ propagate(name, imports, instance);
544
+ return fold(imports, ms);
545
+ };
546
+
547
+ let i = init ();
548
+ my_print("Custom imports:", imports);
549
+ for (let k in imports)
550
+ i[k] = imports[k](i.Karamel.mem);
551
+
552
+ return fold(i, modules);
553
+ }
554
+
555
+ function setMyPrint(f) {
556
+ my_print = f;
557
+ return;
558
+ }
559
+
560
+ if (typeof module !== "undefined")
561
+ module.exports = {
562
+ setMyPrint: setMyPrint,
563
+ link: link,
564
+ reserve: reserve,
565
+ dump: dump,
566
+ hex: hex,
567
+ p32: p32
568
+ };
@@ -0,0 +1,12 @@
1
+
2
+ // To be loaded by main.js
3
+ var my_js_files = ["./test.js"];
4
+ var my_modules = ["WasmSupport", "FStar", "LowStar_Endianness", "Hacl_Impl_Blake2_Constants", "Hacl_Hash_Blake2", "Hacl_Hash_Blake2b_256", "Hacl_Hash_Blake2s_128", "Hacl_Hash_SHA3", "Hacl_Hash_Base", "Hacl_Hash_MD5", "Hacl_Hash_SHA1", "Hacl_Hash_SHA2", "EverCrypt_TargetConfig", "EverCrypt", "Vale", "EverCrypt_Hash", "Hacl_Chacha20", "Hacl_Salsa20", "Hacl_IntTypes_Intrinsics", "Hacl_Bignum_Base", "Hacl_Bignum", "Hacl_Bignum25519_51", "Hacl_Curve25519_51", "Hacl_Ed25519_PrecompTable", "Hacl_Ed25519", "Hacl_Poly1305_32", "Hacl_NaCl", "Hacl_P256_PrecompTable", "Hacl_P256", "Hacl_Bignum_K256", "Hacl_K256_PrecompTable", "Hacl_K256_ECDSA", "Hacl_HMAC", "Hacl_HKDF", "Hacl_Chacha20Poly1305_32", "Hacl_HPKE_Curve51_CP32_SHA256", "Hacl_HPKE_Curve51_CP32_SHA512", "Hacl_Streaming_Blake2b_256", "Hacl_Streaming_Blake2s_128", "Hacl_GenericField32", "Hacl_SHA2_Vec256", "Hacl_EC_K256", "Hacl_Bignum4096", "Hacl_Chacha20_Vec32", "Hacl_Bignum4096_32", "Hacl_HMAC_Blake2s_128", "Hacl_HKDF_Blake2s_128", "Hacl_GenericField64", "Hacl_Bignum32", "Hacl_Bignum256_32", "Hacl_SHA2_Vec128", "Hacl_Streaming_Poly1305_32", "Hacl_HMAC_DRBG", "Hacl_Streaming_Blake2", "Hacl_Bignum64", "Hacl_HMAC_Blake2b_256", "Hacl_HKDF_Blake2b_256", "Hacl_EC_Ed25519", "Hacl_Bignum256"];
5
+ var my_debug = false;
6
+
7
+ if (typeof module !== "undefined")
8
+ module.exports = {
9
+ my_js_files: my_js_files,
10
+ my_modules: my_modules,
11
+ my_debug: my_debug
12
+ }