polar_codec_wasm 1.0.0 → 1.0.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/LICENSE +6 -6
- package/README.md +117 -78
- package/dist/index.cjs +1 -1
- package/dist/index.iife.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +17 -8
package/LICENSE
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
Copyright 2026 Aluria
|
|
2
|
-
|
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
-
|
|
5
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
-
|
|
1
|
+
Copyright 2026 Aluria
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
7
|
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,84 +1,139 @@
|
|
|
1
1
|
# polar_codec_wasm
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/polar_codec_wasm)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
[](https://www.typescriptlang.org/)
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
A high-performance, **zero-dependency** WebAssembly implementation of **Polar Codes** — a class of Forward Error Correction (FEC) codes that achieve the capacity of symmetric binary-input discrete memoryless channels.
|
|
8
|
+
|
|
9
|
+
The heavy-lifting core is written in Rust ([`polar_codec`](https://crates.io/crates/polar_codec)), compiled to WebAssembly, and embedded directly via Base64. This architectural design completely eliminates runtime garbage collection (GC) overhead during intensive decoding arrays, while bypassing the headache of asynchronous `.wasm` file fetching.
|
|
10
|
+
|
|
11
|
+
It works entirely synchronously out-of-the-box in **Node.js**, **Browsers**, and **Deno**.
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- **Zero Dependencies:** The Wasm binary is base64-inlined. Import it and run it synchronously anywhere.
|
|
16
|
+
- **High Performance:** Core SCL decoding operations are executed in Rust, circumventing JS engine limitations and GC pauses.
|
|
17
|
+
- **SCL Decoding:** Successive Cancellation List decoding with a configurable list size `L`.
|
|
18
|
+
- **CRC-Aided:** 80+ built-in CRC algorithms (CRC-8 through CRC-82) + full support for user-defined polynomials.
|
|
19
|
+
- **Frozen Bits Generation:** Out-of-the-box support for 5G NR, Reed-Muller, and Gaussian Approximation (GA) construction methods.
|
|
20
|
+
- **Bit & Byte APIs:** Effortlessly encode/decode raw byte arrays (`Uint8Array`) or specific 0/1 bit arrays.
|
|
6
21
|
|
|
7
22
|
## Install
|
|
8
23
|
|
|
24
|
+
### Package Managers
|
|
25
|
+
|
|
9
26
|
```bash
|
|
10
27
|
npm install polar_codec_wasm
|
|
11
28
|
# or
|
|
12
29
|
pnpm add polar_codec_wasm
|
|
13
|
-
|
|
14
|
-
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Via CDN (Direct Browser Usage)
|
|
34
|
+
|
|
35
|
+
For native browser environments without bundlers, you can include the IIFE bundle directly via **jsDelivr** or **unpkg**. The package will automatically expose a global `PolarCodec` constructor on the `window` object.
|
|
36
|
+
|
|
37
|
+
```html
|
|
38
|
+
<script src="https://cdn.jsdelivr.net/npm/polar_codec_wasm@1.0.0/dist/index.iife.min.js"></script>
|
|
39
|
+
|
|
40
|
+
<script src="https://unpkg.com/polar_codec_wasm@1.0.0/dist/index.iife.js"></script>
|
|
41
|
+
|
|
15
42
|
```
|
|
16
43
|
|
|
17
44
|
## Quick Start
|
|
18
45
|
|
|
46
|
+
### 1. Browser Environment (via CDN)
|
|
47
|
+
|
|
48
|
+
```html
|
|
49
|
+
<script src="https://cdn.jsdelivr.net/npm/polar_codec_wasm/dist/index.iife.js"></script>
|
|
50
|
+
<script>
|
|
51
|
+
// Encode string data at byte-level
|
|
52
|
+
const inputString = "Hello, Polar Codec!";
|
|
53
|
+
const data = new TextEncoder().encode(inputString);
|
|
54
|
+
|
|
55
|
+
// Initialize Codec (K = data.length * 8 bits)
|
|
56
|
+
const codec = new PolarCodec(data.length * 8);
|
|
57
|
+
const encoded = codec.encode(data);
|
|
58
|
+
|
|
59
|
+
// Simulate BPSK channel (bits to Log-Likelihood Ratios)
|
|
60
|
+
const llr = PolarCodec.bits_to_llr(encoded);
|
|
61
|
+
|
|
62
|
+
// Decode back to bytes
|
|
63
|
+
const decoded = codec.decode(llr);
|
|
64
|
+
console.log("Decoded:", new TextDecoder().decode(decoded)); // "Hello, Polar Codec!"
|
|
65
|
+
</script>
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### 2. Bundler / Node.js Environment (Byte-Level)
|
|
70
|
+
|
|
19
71
|
```ts
|
|
20
|
-
import { PolarCodec
|
|
72
|
+
import { PolarCodec } from "polar_codec_wasm";
|
|
21
73
|
|
|
22
|
-
|
|
23
|
-
const codec = new PolarCodec(
|
|
74
|
+
const data = new Uint8Array([72, 101, 108, 108, 111]); // "Hello"
|
|
75
|
+
const codec = new PolarCodec(data.length * 8, 64);
|
|
24
76
|
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
const
|
|
77
|
+
const encoded = codec.encode(data);
|
|
78
|
+
const llr = PolarCodec.bits_to_llr(encoded);
|
|
79
|
+
const decoded = codec.decode(llr);
|
|
28
80
|
|
|
29
|
-
// Decode (LLR: negative = bit 1, positive = bit 0)
|
|
30
|
-
const llr = to_llr(encoded);
|
|
31
|
-
const decoded = codec.decode_bit(llr);
|
|
32
81
|
```
|
|
33
82
|
|
|
34
|
-
|
|
83
|
+
### 3. Bit-Level Communication Simulation
|
|
35
84
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
85
|
+
```ts
|
|
86
|
+
import { PolarCodec, Crc } from "polar_codec_wasm";
|
|
87
|
+
|
|
88
|
+
// K=96 info bits, N=128 codeword length, List Size=4, 5G Frozen bits + CRC-16
|
|
89
|
+
const codec = new PolarCodec(96, 128, 4, {
|
|
90
|
+
frozenBits: "5G",
|
|
91
|
+
crc: Crc.CRC_16_UMTS
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const dataBits = new Uint8Array([1, 0, 1, 1, 0, 0, 1, 0 /* ... up to 96 bits */]);
|
|
95
|
+
const encodedBits = codec.encode_bit(dataBits);
|
|
96
|
+
|
|
97
|
+
// Receiver side LLR mapping: negative = bit 1, positive = bit 0
|
|
98
|
+
const llr = PolarCodec.bits_to_llr(encodedBits);
|
|
99
|
+
const decodedBits = codec.decode_bit(llr);
|
|
44
100
|
|
|
45
|
-
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## API Reference
|
|
46
104
|
|
|
47
|
-
### `new PolarCodec(k, n
|
|
105
|
+
### `new PolarCodec(k, n?, l?, options?)`
|
|
48
106
|
|
|
49
107
|
| Param | Type | Default | Description |
|
|
50
|
-
|
|
51
|
-
| `k` | `number` |
|
|
52
|
-
| `n` | `number` | | Codeword length (power of two,
|
|
53
|
-
| `l` | `number` | `4` | SCL list size (higher = better correction, slower) |
|
|
54
|
-
| `options.frozenBits` | `FrozenBitsMethod` | auto | `"5G"`, `"RM"`, or `{ type: "GA", sigma?: number }` |
|
|
55
|
-
| `options.crc` | `Crc
|
|
108
|
+
| --- | --- | --- | --- |
|
|
109
|
+
| `k` | `number` | | Information bits per codeword |
|
|
110
|
+
| `n` | `number` | `2^ceil(log2(k))` | Codeword length (must be power of two, $\ge k$) |
|
|
111
|
+
| `l` | `number` | `4` | SCL list size (higher = better correction, slower execution) |
|
|
112
|
+
| `options.frozenBits` | `FrozenBitsMethod` | `auto` | `"5G"`, `"RM"`, or `{ type: "GA", sigma?: number }` |
|
|
113
|
+
| `options.crc` | `Crc | DefinedCrc | null` | `null` | CRC algorithm or `null` for none |
|
|
114
|
+
|
|
115
|
+
> **Note on Browser (CDN) Built-in CRCs:** When using the global CDN script, built-in CRC enums can be accessed via `polar_codec.Crc` (e.g., `polar_codec.Crc.CRC_16_UMTS`).
|
|
56
116
|
|
|
57
|
-
### Methods
|
|
117
|
+
### Core Methods
|
|
58
118
|
|
|
59
119
|
| Method | Input | Output | Description |
|
|
60
|
-
|
|
61
|
-
| `encode_bit(src)` | `Uint8Array` of 0/1 | `Uint8Array` of 0/1 (length n) | Encode information bits |
|
|
62
|
-
| `decode_bit(llr)` | `Float32Array` (length n) | `Uint8Array` of 0/1 (length k) | Decode LLRs to information bits |
|
|
120
|
+
| --- | --- | --- | --- |
|
|
63
121
|
| `encode(src)` | `Uint8Array` (bytes) | `Uint8Array` (packed bytes) | Byte-level encode |
|
|
64
|
-
| `decode(llr)` | `Float32Array` (length n) | `Uint8Array` (packed bytes) | Byte-level decode |
|
|
122
|
+
| `decode(llr)` | `Float32Array` (length n) | `Uint8Array` (packed bytes) | Byte-level SCL decode |
|
|
123
|
+
| `encode_bit(src)` | `Uint8Array` of 0/1 | `Uint8Array` of 0/1 | Bit-level encode |
|
|
124
|
+
| `decode_bit(llr)` | `Float32Array` (length n) | `Uint8Array` of 0/1 | Bit-level SCL decode |
|
|
125
|
+
| `PolarCodec.bits_to_llr(bits)` | `Uint8Array` | `Float32Array` | Utility: Maps `0 -> +10.0`, `1 -> -10.0` |
|
|
65
126
|
|
|
66
|
-
###
|
|
127
|
+
### User-Defined CRC
|
|
67
128
|
|
|
68
|
-
|
|
69
|
-
|--------|-------------|
|
|
70
|
-
| `"5G"` | 5G NR standard frozen-bits pattern (default for N <= 1024) |
|
|
71
|
-
| `"RM"` | Reed-Muller based construction (default for N > 1024) |
|
|
72
|
-
| `"GA"` | Gaussian Approximation with configurable `sigma` parameter |
|
|
73
|
-
|
|
74
|
-
### User-defined CRC
|
|
129
|
+
You can strictly define custom polynomials if the built-in standards don't match your protocol:
|
|
75
130
|
|
|
76
131
|
```ts
|
|
77
|
-
import { PolarCodec
|
|
132
|
+
import { PolarCodec } from "polar_codec_wasm";
|
|
78
133
|
|
|
79
134
|
const codec = new PolarCodec(96, 128, 4, {
|
|
80
135
|
crc: {
|
|
81
|
-
name: "My CRC",
|
|
136
|
+
name: "My Custom CRC",
|
|
82
137
|
width: 16,
|
|
83
138
|
poly: 0x8005,
|
|
84
139
|
init: 0xFFFF,
|
|
@@ -87,64 +142,48 @@ const codec = new PolarCodec(96, 128, 4, {
|
|
|
87
142
|
xorout: 0xFFFF,
|
|
88
143
|
},
|
|
89
144
|
});
|
|
90
|
-
```
|
|
91
145
|
|
|
92
|
-
### Built-in CRC Algorithms
|
|
93
|
-
|
|
94
|
-
All variants from the `crc` crate are available via the `Crc` enum:
|
|
95
|
-
|
|
96
|
-
```ts
|
|
97
|
-
import { Crc } from "polar_codec_wasm";
|
|
98
|
-
|
|
99
|
-
new PolarCodec(96, 128, 4, { crc: Crc.CRC_16_UMTS });
|
|
100
|
-
new PolarCodec(96, 128, 4, { crc: Crc.CRC_24_LTE_A });
|
|
101
|
-
new PolarCodec(96, 128, 4, { crc: Crc.CRC_32_ISCSI });
|
|
102
146
|
```
|
|
103
147
|
|
|
104
148
|
## Examples
|
|
105
149
|
|
|
106
|
-
|
|
150
|
+
Runnable demos are located in the `examples/` directory. Clone the repo and run:
|
|
107
151
|
|
|
108
152
|
```bash
|
|
109
153
|
# Basic round-trip (bit-level and byte-level)
|
|
110
154
|
npx tsx examples/basic.ts
|
|
111
155
|
|
|
112
156
|
# Frozen-bits and CRC parameter combinations
|
|
113
|
-
npx tsx examples/
|
|
157
|
+
npx tsx examples/parameters.ts
|
|
114
158
|
|
|
115
|
-
# BER simulation under Gaussian noise
|
|
159
|
+
# BER (Bit Error Rate) simulation under Gaussian noise
|
|
116
160
|
npx tsx examples/noise.ts
|
|
117
161
|
|
|
118
|
-
# Encode/decode latency benchmark
|
|
119
|
-
npx tsx examples/
|
|
162
|
+
# Encode/decode latency and throughput benchmark
|
|
163
|
+
npx tsx examples/performance.ts
|
|
164
|
+
|
|
120
165
|
```
|
|
121
166
|
|
|
122
|
-
##
|
|
167
|
+
## Architecture & Build
|
|
123
168
|
|
|
124
|
-
```
|
|
169
|
+
```text
|
|
125
170
|
TypeScript API ──► wasm-bindgen ──► Rust Codec ──► polar_codec crate
|
|
126
171
|
│
|
|
127
|
-
└── Wasm binary embedded as Base64 (
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
The build pipeline (`pnpm run build`):
|
|
131
|
-
|
|
132
|
-
1. Compiles Rust to `wasm32-unknown-unknown` via `wasm-pack build --target web`.
|
|
133
|
-
2. Converts the `.wasm` file to a Base64 string embedded in `lib/wasm-b64.ts`.
|
|
134
|
-
3. Bundles three output formats via esbuild: **ESM**, **CJS**, and **IIFE** (for `<script>` tags).
|
|
135
|
-
4. Generates `.d.ts` declaration files with `tsc`.
|
|
172
|
+
└── Wasm binary embedded as Base64 (No asynchronous file I/O)
|
|
136
173
|
|
|
137
|
-
|
|
174
|
+
```
|
|
138
175
|
|
|
139
|
-
|
|
176
|
+
Building from source:
|
|
140
177
|
|
|
141
178
|
```bash
|
|
142
|
-
git clone https://github.com/Wu-Yijun/
|
|
143
|
-
cd
|
|
179
|
+
git clone https://github.com/Wu-Yijun/polar_codec_wasm.git
|
|
180
|
+
cd polar_codec_wasm
|
|
144
181
|
pnpm install
|
|
145
|
-
pnpm run build
|
|
182
|
+
pnpm run build
|
|
183
|
+
|
|
146
184
|
```
|
|
147
185
|
|
|
148
186
|
## License
|
|
149
187
|
|
|
150
|
-
MIT
|
|
188
|
+
[MIT](LICENSE) © Aluria
|
|
189
|
+
|