polar_codec_wasm 1.0.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.
- package/LICENSE +7 -0
- package/README.md +150 -0
- package/dist/index.cjs +793 -0
- package/dist/index.d.cts +389 -0
- package/dist/index.d.mts +389 -0
- package/dist/index.iife.js +2 -0
- package/dist/index.mjs +789 -0
- package/package.json +56 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
//#region pkg/polar_codec_wasm.d.ts
|
|
2
|
+
/* tslint:disable */
|
|
3
|
+
/* eslint-disable */
|
|
4
|
+
/**
|
|
5
|
+
* Description of a user-defined CRC algorithm.
|
|
6
|
+
*
|
|
7
|
+
* Fields follow the [Catalogue of parametrised CRC algorithms](https://reveng.sourceforge.io/crc-catalogue/all.htm).
|
|
8
|
+
* Instances are created from JavaScript via the `new Algorithm(...)` constructor.
|
|
9
|
+
*/
|
|
10
|
+
declare class Algorithm {
|
|
11
|
+
free(): void;
|
|
12
|
+
[Symbol.dispose](): void;
|
|
13
|
+
/**
|
|
14
|
+
* Construct a new CRC algorithm descriptor.
|
|
15
|
+
*
|
|
16
|
+
* This is the JavaScript-callable constructor.
|
|
17
|
+
*/
|
|
18
|
+
constructor(width: number, poly: bigint, init: bigint, refin: boolean, refout: boolean, xorout: bigint);
|
|
19
|
+
/**
|
|
20
|
+
* The settings of the bit cells at the start of each calculation,
|
|
21
|
+
* before reading the first message bit.
|
|
22
|
+
*/
|
|
23
|
+
init: bigint;
|
|
24
|
+
/**
|
|
25
|
+
* The generator polynomial that sets the feedback tap positions of the
|
|
26
|
+
* shift register. The least significant bit corresponds to the inward
|
|
27
|
+
* end of the shift register and is always set. The highest-order term
|
|
28
|
+
* is omitted.
|
|
29
|
+
*/
|
|
30
|
+
poly: bigint;
|
|
31
|
+
/**
|
|
32
|
+
* If `true`, the characters of the message are read bit-by-bit,
|
|
33
|
+
* **least** significant bit first. If `false`, most significant bit
|
|
34
|
+
* first.
|
|
35
|
+
*/
|
|
36
|
+
refin: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* If `true`, the register contents are reflected before the final XOR.
|
|
39
|
+
*/
|
|
40
|
+
refout: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* The number of bit cells in the linear feedback shift register; the
|
|
43
|
+
* degree of the generator polynomial, minus one.
|
|
44
|
+
*/
|
|
45
|
+
width: number;
|
|
46
|
+
/**
|
|
47
|
+
* The XOR value applied to the register after the last message bit has
|
|
48
|
+
* been read (and after the optional reflection).
|
|
49
|
+
*/
|
|
50
|
+
xorout: bigint;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* WebAssembly-exported Polar Codec.
|
|
54
|
+
*
|
|
55
|
+
* Wraps the Rust `polar_codec::PolarCodec` and provides encode/decode
|
|
56
|
+
* methods that accept and return flat arrays suitable for JavaScript.
|
|
57
|
+
*/
|
|
58
|
+
declare class Codec {
|
|
59
|
+
free(): void;
|
|
60
|
+
[Symbol.dispose](): void;
|
|
61
|
+
/**
|
|
62
|
+
* Decode LLRs back to a byte array.
|
|
63
|
+
*
|
|
64
|
+
* The inverse of [`encode`](Codec::encode): decodes the LLRs to bits and
|
|
65
|
+
* packs them into bytes. The returned array has length `k / 8` (rounded up).
|
|
66
|
+
*/
|
|
67
|
+
decode(llr: Float32Array): Uint8Array;
|
|
68
|
+
/**
|
|
69
|
+
* Decode a codeword given as **log-likelihood ratios** back to information bits.
|
|
70
|
+
*
|
|
71
|
+
* `llr` must contain `n` floats. A negative value represents bit 1,
|
|
72
|
+
* a positive value represents bit 0 (higher magnitude = higher confidence).
|
|
73
|
+
* Returns a `Uint8Array` of length `k` with the decoded information bits.
|
|
74
|
+
*/
|
|
75
|
+
decode_bit(llr: Float32Array): Uint8Array;
|
|
76
|
+
/**
|
|
77
|
+
* Encode a byte array into a codeword.
|
|
78
|
+
*
|
|
79
|
+
* Internally converts bytes to bits, encodes, and returns the result as
|
|
80
|
+
* packed bytes. `src.len() * 8` must equal `k`.
|
|
81
|
+
*/
|
|
82
|
+
encode(src: Uint8Array): Uint8Array;
|
|
83
|
+
/**
|
|
84
|
+
* Encode a sequence of **information bits** into a codeword of `n` bits.
|
|
85
|
+
*
|
|
86
|
+
* `src` must contain exactly `k` elements, each `0` or `1`.
|
|
87
|
+
* Returns a `Uint8Array` of length `n` with the encoded bits (0/1).
|
|
88
|
+
*/
|
|
89
|
+
encode_bit(src: Uint8Array): Uint8Array;
|
|
90
|
+
/**
|
|
91
|
+
* Create a new Polar Codec instance.
|
|
92
|
+
*
|
|
93
|
+
* # Arguments
|
|
94
|
+
*
|
|
95
|
+
* * `k` - Number of information bits per codeword.
|
|
96
|
+
* * `n` - Total codeword length (power of two, >= k).
|
|
97
|
+
* * `l` - SCL list size (higher = better but slower).
|
|
98
|
+
* * `crc` - CRC algorithm selector (see [`Crc`]).
|
|
99
|
+
* * `fb` - Frozen-bits construction method (see [`FrozenBits`]).
|
|
100
|
+
* * `user_crc`- Optional custom CRC algorithm (used when `crc == Crc::UserDefined`).
|
|
101
|
+
* * `sigma` - Optional sigma for the GA method (defaults to 0.5).
|
|
102
|
+
*/
|
|
103
|
+
constructor(k: number, n: number, l: number, crc: Crc, fb: FrozenBits, user_crc?: Algorithm | null, sigma?: number | null);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Enumeration of all CRC algorithms available in the `crc` crate.
|
|
107
|
+
*
|
|
108
|
+
* Each variant maps 1-to-1 with a constant in the `crc` crate (e.g.
|
|
109
|
+
* `crc::CRC_16_UMTS`). The numeric values are explicit to keep the
|
|
110
|
+
* JavaScript enum stable.
|
|
111
|
+
*/
|
|
112
|
+
declare enum Crc {
|
|
113
|
+
/**
|
|
114
|
+
* No CRC -- pass-through.
|
|
115
|
+
*/
|
|
116
|
+
None = 0,
|
|
117
|
+
/**
|
|
118
|
+
* Marker: use the user-supplied [`Algorithm`] instead of a built-in.
|
|
119
|
+
*/
|
|
120
|
+
UserDefined = 1,
|
|
121
|
+
CRC_3_GSM = 2,
|
|
122
|
+
CRC_3_ROHC = 3,
|
|
123
|
+
CRC_4_G_704 = 4,
|
|
124
|
+
CRC_4_INTERLAKEN = 5,
|
|
125
|
+
CRC_5_EPC_C1G2 = 6,
|
|
126
|
+
CRC_5_G_704 = 7,
|
|
127
|
+
CRC_5_USB = 8,
|
|
128
|
+
CRC_6_CDMA2000_A = 9,
|
|
129
|
+
CRC_6_CDMA2000_B = 10,
|
|
130
|
+
CRC_6_DARC = 11,
|
|
131
|
+
CRC_6_G_704 = 12,
|
|
132
|
+
CRC_6_GSM = 13,
|
|
133
|
+
CRC_7_MMC = 14,
|
|
134
|
+
CRC_7_ROHC = 15,
|
|
135
|
+
CRC_7_UMTS = 16,
|
|
136
|
+
CRC_8_AUTOSAR = 17,
|
|
137
|
+
CRC_8_BLUETOOTH = 18,
|
|
138
|
+
CRC_8_CDMA2000 = 19,
|
|
139
|
+
CRC_8_DARC = 20,
|
|
140
|
+
CRC_8_DVB_S2 = 21,
|
|
141
|
+
CRC_8_GSM_A = 22,
|
|
142
|
+
CRC_8_GSM_B = 23,
|
|
143
|
+
CRC_8_HITAG = 24,
|
|
144
|
+
CRC_8_I_432_1 = 25,
|
|
145
|
+
CRC_8_I_CODE = 26,
|
|
146
|
+
CRC_8_LTE = 27,
|
|
147
|
+
CRC_8_MAXIM_DOW = 28,
|
|
148
|
+
CRC_8_MIFARE_MAD = 29,
|
|
149
|
+
CRC_8_NRSC_5 = 30,
|
|
150
|
+
CRC_8_OPENSAFETY = 31,
|
|
151
|
+
CRC_8_ROHC = 32,
|
|
152
|
+
CRC_8_SAE_J1850 = 33,
|
|
153
|
+
CRC_8_SMBUS = 34,
|
|
154
|
+
CRC_8_TECH_3250 = 35,
|
|
155
|
+
CRC_8_WCDMA = 36,
|
|
156
|
+
CRC_10_ATM = 37,
|
|
157
|
+
CRC_10_CDMA2000 = 38,
|
|
158
|
+
CRC_10_GSM = 39,
|
|
159
|
+
CRC_11_FLEXRAY = 40,
|
|
160
|
+
CRC_11_UMTS = 41,
|
|
161
|
+
CRC_12_CDMA2000 = 42,
|
|
162
|
+
CRC_12_DECT = 43,
|
|
163
|
+
CRC_12_GSM = 44,
|
|
164
|
+
CRC_12_UMTS = 45,
|
|
165
|
+
CRC_13_BBC = 46,
|
|
166
|
+
CRC_14_DARC = 47,
|
|
167
|
+
CRC_14_GSM = 48,
|
|
168
|
+
CRC_15_CAN = 49,
|
|
169
|
+
CRC_15_MPT1327 = 50,
|
|
170
|
+
CRC_16_ARC = 51,
|
|
171
|
+
CRC_16_CDMA2000 = 52,
|
|
172
|
+
CRC_16_CMS = 53,
|
|
173
|
+
CRC_16_DDS_110 = 54,
|
|
174
|
+
CRC_16_DECT_R = 55,
|
|
175
|
+
CRC_16_DECT_X = 56,
|
|
176
|
+
CRC_16_DNP = 57,
|
|
177
|
+
CRC_16_EN_13757 = 58,
|
|
178
|
+
CRC_16_GENIBUS = 59,
|
|
179
|
+
CRC_16_GSM = 60,
|
|
180
|
+
CRC_16_IBM_3740 = 61,
|
|
181
|
+
CRC_16_IBM_SDLC = 62,
|
|
182
|
+
CRC_16_ISO_IEC_14443_3_A = 63,
|
|
183
|
+
CRC_16_KERMIT = 64,
|
|
184
|
+
CRC_16_LJ1200 = 65,
|
|
185
|
+
CRC_16_M17 = 66,
|
|
186
|
+
CRC_16_MAXIM_DOW = 67,
|
|
187
|
+
CRC_16_MCRF4XX = 68,
|
|
188
|
+
CRC_16_MODBUS = 69,
|
|
189
|
+
CRC_16_NRSC_5 = 70,
|
|
190
|
+
CRC_16_OPENSAFETY_A = 71,
|
|
191
|
+
CRC_16_OPENSAFETY_B = 72,
|
|
192
|
+
CRC_16_PROFIBUS = 73,
|
|
193
|
+
CRC_16_RIELLO = 74,
|
|
194
|
+
CRC_16_SPI_FUJITSU = 75,
|
|
195
|
+
CRC_16_T10_DIF = 76,
|
|
196
|
+
CRC_16_TELEDISK = 77,
|
|
197
|
+
CRC_16_TMS37157 = 78,
|
|
198
|
+
CRC_16_UMTS = 79,
|
|
199
|
+
CRC_16_USB = 80,
|
|
200
|
+
CRC_16_XMODEM = 81,
|
|
201
|
+
CRC_17_CAN_FD = 82,
|
|
202
|
+
CRC_21_CAN_FD = 83,
|
|
203
|
+
CRC_24_BLE = 84,
|
|
204
|
+
CRC_24_FLEXRAY_A = 85,
|
|
205
|
+
CRC_24_FLEXRAY_B = 86,
|
|
206
|
+
CRC_24_INTERLAKEN = 87,
|
|
207
|
+
CRC_24_LTE_A = 88,
|
|
208
|
+
CRC_24_LTE_B = 89,
|
|
209
|
+
CRC_24_OPENPGP = 90,
|
|
210
|
+
CRC_24_OS_9 = 91,
|
|
211
|
+
CRC_30_CDMA = 92,
|
|
212
|
+
CRC_31_PHILIPS = 93,
|
|
213
|
+
CRC_32_AIXM = 94,
|
|
214
|
+
CRC_32_AUTOSAR = 95,
|
|
215
|
+
CRC_32_BASE91_D = 96,
|
|
216
|
+
CRC_32_BZIP2 = 97,
|
|
217
|
+
CRC_32_CD_ROM_EDC = 98,
|
|
218
|
+
CRC_32_CKSUM = 99,
|
|
219
|
+
CRC_32_ISCSI = 100,
|
|
220
|
+
CRC_32_ISO_HDLC = 101,
|
|
221
|
+
CRC_32_JAMCRC = 102,
|
|
222
|
+
CRC_32_MEF = 103,
|
|
223
|
+
CRC_32_MPEG_2 = 104,
|
|
224
|
+
CRC_32_XFER = 105,
|
|
225
|
+
CRC_40_GSM = 106,
|
|
226
|
+
CRC_64_ECMA_182 = 107,
|
|
227
|
+
CRC_64_GO_ISO = 108,
|
|
228
|
+
CRC_64_MS = 109,
|
|
229
|
+
CRC_64_NVME = 110,
|
|
230
|
+
CRC_64_REDIS = 111,
|
|
231
|
+
CRC_64_WE = 112,
|
|
232
|
+
CRC_64_XZ = 113,
|
|
233
|
+
CRC_82_DARC = 114
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Frozen-bits construction method exposed to JavaScript.
|
|
237
|
+
*/
|
|
238
|
+
declare enum FrozenBits {
|
|
239
|
+
/**
|
|
240
|
+
* 5G NR frozen-bits pattern (default for N <= 1024).
|
|
241
|
+
*/
|
|
242
|
+
_5G = 0,
|
|
243
|
+
/**
|
|
244
|
+
* Gaussian Approximation -- sigma can be tuned.
|
|
245
|
+
*/
|
|
246
|
+
GA = 1,
|
|
247
|
+
/**
|
|
248
|
+
* Reed-Muller based construction (default for N > 1024).
|
|
249
|
+
*/
|
|
250
|
+
RM = 2
|
|
251
|
+
}
|
|
252
|
+
interface InitOutput {
|
|
253
|
+
readonly memory: WebAssembly.Memory;
|
|
254
|
+
readonly __wbg_algorithm_free: (a: number, b: number) => void;
|
|
255
|
+
readonly __wbg_get_algorithm_init: (a: number) => [bigint, bigint];
|
|
256
|
+
readonly __wbg_get_algorithm_poly: (a: number) => [bigint, bigint];
|
|
257
|
+
readonly __wbg_get_algorithm_refin: (a: number) => number;
|
|
258
|
+
readonly __wbg_get_algorithm_refout: (a: number) => number;
|
|
259
|
+
readonly __wbg_get_algorithm_width: (a: number) => number;
|
|
260
|
+
readonly __wbg_get_algorithm_xorout: (a: number) => [bigint, bigint];
|
|
261
|
+
readonly __wbg_set_algorithm_init: (a: number, b: bigint, c: bigint) => void;
|
|
262
|
+
readonly __wbg_set_algorithm_poly: (a: number, b: bigint, c: bigint) => void;
|
|
263
|
+
readonly __wbg_set_algorithm_refin: (a: number, b: number) => void;
|
|
264
|
+
readonly __wbg_set_algorithm_refout: (a: number, b: number) => void;
|
|
265
|
+
readonly __wbg_set_algorithm_width: (a: number, b: number) => void;
|
|
266
|
+
readonly __wbg_set_algorithm_xorout: (a: number, b: bigint, c: bigint) => void;
|
|
267
|
+
readonly algorithm_new: (a: number, b: bigint, c: bigint, d: bigint, e: bigint, f: number, g: number, h: bigint, i: bigint) => number;
|
|
268
|
+
readonly __wbg_codec_free: (a: number, b: number) => void;
|
|
269
|
+
readonly codec_decode: (a: number, b: number, c: number) => [number, number];
|
|
270
|
+
readonly codec_decode_bit: (a: number, b: number, c: number) => [number, number];
|
|
271
|
+
readonly codec_encode: (a: number, b: number, c: number) => [number, number];
|
|
272
|
+
readonly codec_encode_bit: (a: number, b: number, c: number) => [number, number];
|
|
273
|
+
readonly codec_new: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => number;
|
|
274
|
+
readonly __wbindgen_externrefs: WebAssembly.Table;
|
|
275
|
+
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
276
|
+
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
277
|
+
readonly __wbindgen_start: () => void;
|
|
278
|
+
}
|
|
279
|
+
//#endregion
|
|
280
|
+
//#region lib/index.d.ts
|
|
281
|
+
/**
|
|
282
|
+
* Synchronously initialize the WebAssembly environment.
|
|
283
|
+
*
|
|
284
|
+
* This function is idempotent -- it is safe to call multiple times; subsequent
|
|
285
|
+
* calls will return the cached module immediately.
|
|
286
|
+
*
|
|
287
|
+
* @returns The initialized Wasm module ready for use.
|
|
288
|
+
*
|
|
289
|
+
* @example
|
|
290
|
+
* ```ts
|
|
291
|
+
* import { initWasmSync } from 'polar_codec_wasm';
|
|
292
|
+
* initWasmSync();
|
|
293
|
+
* // Now PolarCodec can be instantiated.
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
296
|
+
declare function initWasmSync(): InitOutput;
|
|
297
|
+
/**
|
|
298
|
+
* Description of a user-defined CRC algorithm.
|
|
299
|
+
*
|
|
300
|
+
* Fields follow the [Catalogue of parametrised CRC algorithms](https://reveng.sourceforge.io/crc-catalogue/all.htm).
|
|
301
|
+
*/
|
|
302
|
+
interface DefinedCrc {
|
|
303
|
+
/** Optional human-readable name (e.g. `"CRC-16/USB"`). */
|
|
304
|
+
name?: string;
|
|
305
|
+
/** Polynomial degree in bits (e.g. 16 for CRC-16). */
|
|
306
|
+
width: number;
|
|
307
|
+
/** Generator polynomial (without the leading x^width term). */
|
|
308
|
+
poly: number | bigint;
|
|
309
|
+
/** Initial value of the CRC register. */
|
|
310
|
+
init: number | bigint;
|
|
311
|
+
/** Reflect input bytes before processing? Defaults to `refout` if omitted. */
|
|
312
|
+
refin?: boolean;
|
|
313
|
+
/** Reflect the register before final XOR? Defaults to `refin` if omitted. */
|
|
314
|
+
refout?: boolean;
|
|
315
|
+
/** XOR mask applied after processing all bytes. */
|
|
316
|
+
xorout?: number | bigint;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Frozen-bits selection method.
|
|
320
|
+
*
|
|
321
|
+
* | Value | Description |
|
|
322
|
+
* |-------|-------------|
|
|
323
|
+
* | `"5G"` | 5G NR (default for N <= 1024) |
|
|
324
|
+
* | `"RM"` | Reed-Muller (default for N > 1024) |
|
|
325
|
+
* | `"GA"` | Gaussian Approximation with configurable sigma |
|
|
326
|
+
*/
|
|
327
|
+
type FrozenBitsMethod = "5G" | "RM" | {
|
|
328
|
+
type: "5G" | "RM";
|
|
329
|
+
} | {
|
|
330
|
+
type: "GA";
|
|
331
|
+
sigma?: number;
|
|
332
|
+
};
|
|
333
|
+
/**
|
|
334
|
+
* Configuration options accepted by {@link PolarCodec}.
|
|
335
|
+
*/
|
|
336
|
+
interface PolarCodecOptions {
|
|
337
|
+
/** Frozen-bits construction method. */
|
|
338
|
+
frozenBits?: FrozenBitsMethod;
|
|
339
|
+
/** CRC scheme -- pass `null` / `undefined` for no CRC, a `Crc` enum value, or a {@link DefinedCrc} object. */
|
|
340
|
+
crc?: Crc | DefinedCrc | null;
|
|
341
|
+
}
|
|
342
|
+
declare function bits_to_llr(bits: Uint8Array): Float32Array;
|
|
343
|
+
/**
|
|
344
|
+
* High-level wrapper around the Rust-backed {@link Codec}.
|
|
345
|
+
*
|
|
346
|
+
* `PolarCodec` manages Wasm initialization and provides a convenient
|
|
347
|
+
* constructor that translates human-readable option names into the
|
|
348
|
+
* low-level enum values expected by the Wasm layer.
|
|
349
|
+
*
|
|
350
|
+
* @typeParam K - Number of information bits (set at construction time).
|
|
351
|
+
* @typeParam N - Codeword length (must be a power of two, >= K).
|
|
352
|
+
*
|
|
353
|
+
* @example
|
|
354
|
+
* ```ts
|
|
355
|
+
* // 5G-style frozen bits, no CRC, list-size L=4 (default)
|
|
356
|
+
* const codec = new PolarCodec(96, 128);
|
|
357
|
+
*
|
|
358
|
+
* // With CRC-16 and GA frozen bits
|
|
359
|
+
* const codec2 = new PolarCodec(96, 128, 4, {
|
|
360
|
+
* frozenBits: { type: "GA", sigma: 0.5 },
|
|
361
|
+
* crc: Crc.CRC_16_UMTS,
|
|
362
|
+
* });
|
|
363
|
+
* ```
|
|
364
|
+
*/
|
|
365
|
+
declare class PolarCodec extends Codec {
|
|
366
|
+
/** Number of information bits per codeword. */
|
|
367
|
+
readonly K: number;
|
|
368
|
+
/** Total codeword length (information + frozen bits). */
|
|
369
|
+
readonly N: number;
|
|
370
|
+
/** SCL list size used during decoding. */
|
|
371
|
+
readonly L: number;
|
|
372
|
+
/** Frozen-bits construction method name. */
|
|
373
|
+
readonly METHOD: "5G" | "RM" | "GA";
|
|
374
|
+
/** Resolved CRC parameters, or `undefined` when no CRC is used. */
|
|
375
|
+
readonly CRC: Required<DefinedCrc> | undefined;
|
|
376
|
+
/**
|
|
377
|
+
* Create a new Polar Codec instance.
|
|
378
|
+
*
|
|
379
|
+
* @param k Number of information bits (must be <= n).
|
|
380
|
+
* @param n Codeword length, must be a power of two.
|
|
381
|
+
* @param l SCL list size (default `4`). Larger values improve error
|
|
382
|
+
* correction at the cost of higher latency and memory.
|
|
383
|
+
* @param options Optional CRC and frozen-bits configuration.
|
|
384
|
+
*/
|
|
385
|
+
constructor(k: number, n?: number, l?: number, options?: Partial<PolarCodecOptions>);
|
|
386
|
+
static bits_to_llr(bits: Uint8Array): Float32Array;
|
|
387
|
+
}
|
|
388
|
+
//#endregion
|
|
389
|
+
export { Crc, DefinedCrc, FrozenBitsMethod, PolarCodec, PolarCodecOptions, bits_to_llr, initWasmSync };
|