-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_parity_demo.js
261 lines (224 loc) · 6.03 KB
/
compute_parity_demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// @flow
/* eslint-env browser */
'use strict';
/* ::
import {
VChildError,
handleVChildError,
strictParseInt,
parseListCapped,
isEdge,
listPattern,
textInput,
spanNoWrap,
matrixStringLengthBound,
} from './demo_common';
import {
parseHex,
byteLaTeX,
byteArrayComponent,
} from './erasure_code_demo_common';
import { Field256Element } from './field_256';
import { Matrix, type LaTeXStringOptions } from './matrix';
import { computeParityMatrix, computeParity } from './cauchy_erasure_code';
import { inlineMath, displayMath } from './math';
*/
/*
global
preact,
BigInteger,
VChildError,
handleVChildError,
strictParseInt,
parseListCapped,
isEdge,
listPattern,
textInput,
spanNoWrap,
matrixStringLengthBound,
parseHex,
byteLaTeX,
byteArrayComponent,
Field256Element,
Matrix,
computeParityMatrix,
computeParity,
inlineMath,
displayMath,
*/
const mnBoundCompute = 50;
// Work around
// https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/12263977/
// .
const mPattern = isEdge ? '0*[0-9]{1,2}' : '(0*[0-9])|(0*[1-4][0-9])|(0*50)';
const parseByte = (
name /* : string */,
s /* : string */
) /* : Field256Element */ => {
const n = parseHex(name, s);
if (n.signum() < 0 || n.compareTo(new BigInteger('ff', 16)) > 0) {
throw new VChildError([
inlineMath(name),
' must be non-negative and fit in a byte.',
]);
}
return new Field256Element(n.intValue());
};
const parseByteList = (
name /* : string */,
s /* : string */
) /* : Field256Element[] */ => {
const strs = parseListCapped(name, s, mnBoundCompute);
return strs.map((t, i) => parseByte(`{${name}}_{${i}}`, t));
};
const bytePattern = '0*[0-9A-Fa-f]{1,2}';
const dataInput = (
dValue /* : string */,
onDChange /* : (string) => void */,
inputClass /* : ?string */
) => {
const inputSize = 30;
return spanNoWrap(
inlineMath('d = ['),
' ',
textInput(
dValue,
s => onDChange(s),
inputSize,
listPattern(bytePattern, mnBoundCompute),
inputClass
),
' ',
inlineMath(']')
);
};
const parsePositiveInt = (
name /* : string */,
s /* : string */
) /* : BigInteger */ => {
const n = strictParseInt(name, s);
if (n.signum() <= 0) {
throw new VChildError([inlineMath(name), ' cannot be negative.']);
}
return n;
};
/* ::
type ComputeParityDemoProps = {
name: string,
detailed: boolean,
header?: HTMLElement,
containerClass?: string,
inputClass?: string,
resultColor: string,
};
type ComputeParityDemoState = {
d: string,
m: string,
};
*/
// eslint-disable-next-line no-unused-vars
class ComputeParityDemo extends preact.Component /* :: <ComputeParityDemoProps, ComputeParityDemoState> */ {
constructor(
// Should be { initialD: d, initialM: m, ...props }, but that is
// unsupported by Safari.
props /* : ComputeParityDemoProps & {
initialD: string,
initialM: string,
} */
) {
super(props);
this.state = { d: props.initialD, m: props.initialM };
}
onDChange(d /* : string */) {
// Should be { ...state, d }, but that is unsupported by Safari.
this.setState(state => ({ d, m: state.m }));
}
onMChange(m /* : string */) {
// Should be { ...state, m }, but that is unsupported by Safari.
this.setState(state => ({ d: state.d, m }));
}
render(
props /* : ComputeParityDemoProps */,
state /* : ComputeParityDemoState */
) {
const children = handleVChildError(() => {
const d = parseByteList('d', state.d);
const n = d.length;
const mBig = parsePositiveInt('m', state.m);
if (mBig.compareTo(new BigInteger(mnBoundCompute.toString(10), 10)) > 0) {
throw new VChildError([
inlineMath('m'),
' must be less than or equal to ',
inlineMath(mnBoundCompute.toString()),
'.',
]);
}
const nBig = new BigInteger(n.toString(10), 10);
if (mBig.add(nBig).compareTo(new BigInteger('256', 10)) > 0) {
throw new VChildError([
'The total byte count must be less than or equal to ',
inlineMath('256'),
'.',
]);
}
const m = mBig.intValue();
const p = computeParity(d, m);
const pComponents = byteArrayComponent(p, 'p', props.resultColor, '.');
if (!props.detailed) {
return [' Then the output parity bytes are ', pComponents];
}
const P = computeParityMatrix(n, m);
const PStr = P.toLaTeXString({ elementToLaTeXString: byteLaTeX });
if (PStr.length > matrixStringLengthBound) {
throw new VChildError([
'The Cauchy matrix parity matrix is too big to display.',
]);
}
const colOptions /* : LaTeXStringOptions<Field256Element> */ = {
environment: 'bmatrix',
elementToLaTeXString: byteLaTeX,
};
const pCol = new Matrix(m, 1, p);
const dCol = new Matrix(n, 1, d);
const dColStr = dCol.toLaTeXString(colOptions);
const pColStr = pCol.toLaTeXString(colOptions);
return [
' Then, with the input byte count ',
inlineMath(`n = ${n}`),
', the ',
inlineMath('m \\times n'),
' Cauchy parity matrix computed using ',
inlineMath('x_i = n + i'),
' and ',
inlineMath('y_i = i'),
' is ',
displayMath(`${PStr}\\text{.}`),
' Therefore, the parity bytes are computed as',
displayMath(`${PStr} \\cdot ${dColStr} = ${pColStr}\\text{,}`),
' and thus the output parity bytes are ',
pComponents,
];
});
return preact.h(
'div',
{ class: props.containerClass },
props.header,
'Let ',
dataInput(state.d, s => this.onDChange(s), props.inputClass),
' be the input data bytes, and let ',
spanNoWrap(
inlineMath('m ='),
' ',
textInput(
state.m,
s => this.onMChange(s),
mnBoundCompute.toString(10).length,
mPattern,
props.inputClass
)
),
' be the desired parity byte count. ',
children
);
}
}