-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathformat.ts
More file actions
159 lines (152 loc) · 5.49 KB
/
format.ts
File metadata and controls
159 lines (152 loc) · 5.49 KB
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
import { flattenBinArray } from '../../../format/format.js';
import type {
AuthenticationProgramStateError,
AuthenticationProgramStateStack,
Operation,
} from '../../../lib.js';
import {
pushToStack,
pushToStackChecked,
useOneStackItem,
useOneVmNumber,
useTwoStackItems,
} from './combinators.js';
import { ConsensusCommon } from './consensus.js';
import { applyError, AuthenticationErrorCommon } from './errors.js';
import { bigIntToVmNumber } from './instruction-sets-utils.js';
export const createOpCat =
({
maximumStackItemLength = ConsensusCommon.maximumStackItemLength as number,
} = {}) =>
<
State extends AuthenticationProgramStateError &
AuthenticationProgramStateStack,
>(
state: State,
) =>
useTwoStackItems(state, (nextState, [a, b]) =>
pushToStackChecked(nextState, flattenBinArray([a, b]), {
maximumStackItemLength,
}),
);
export const opCat = createOpCat();
export const opSplit = <
State extends AuthenticationProgramStateError &
AuthenticationProgramStateStack,
>(
state: State,
) =>
useOneVmNumber(state, (nextState, value) => {
const index = Number(value);
return useOneStackItem(nextState, (finalState, [item]) =>
index < 0 || index > item.length
? applyError(
finalState,
AuthenticationErrorCommon.invalidSplitIndex,
`stack item length: ${item.length}; requested split index: ${index}.`,
)
: pushToStack(finalState, [item.slice(0, index), item.slice(index)]),
);
});
const enum Constants {
positiveSign = 0x00,
negativeSign = 0x80,
}
/**
* Pad a minimally-encoded VM number for `OP_NUM2BIN`.
*/
export const padMinimallyEncodedVmNumber = (
vmNumber: Uint8Array,
length: number,
) => {
// eslint-disable-next-line functional/no-let
let signBit = Constants.positiveSign;
// eslint-disable-next-line functional/no-conditional-statements
if (vmNumber.length > 0) {
// eslint-disable-next-line functional/no-expression-statements, no-bitwise, @typescript-eslint/no-non-null-assertion
signBit = vmNumber[vmNumber.length - 1]! & Constants.negativeSign;
// eslint-disable-next-line functional/no-expression-statements, no-bitwise, @typescript-eslint/no-non-null-assertion
vmNumber[vmNumber.length - 1]! &= Constants.negativeSign - 1;
}
const result = Array.from(vmNumber);
// eslint-disable-next-line functional/no-loop-statements
while (result.length < length - 1) {
// eslint-disable-next-line functional/no-expression-statements, functional/immutable-data
result.push(0);
}
// eslint-disable-next-line functional/no-expression-statements, functional/immutable-data
result.push(signBit);
return Uint8Array.from(result);
};
export const createOpNum2Bin =
<
State extends AuthenticationProgramStateError &
AuthenticationProgramStateStack,
>({
maximumStackItemLength = ConsensusCommon.maximumStackItemLength,
}: { maximumStackItemLength?: number } = {}): Operation<State> =>
(state: State) =>
useOneVmNumber(state, (nextState, value) => {
const targetLength = Number(value);
return targetLength > maximumStackItemLength
? applyError(
nextState,
AuthenticationErrorCommon.exceededMaximumStackItemLength,
`Maximum stack item length: ${maximumStackItemLength} bytes. Item length: ${targetLength} bytes.`,
)
: useOneVmNumber(
nextState,
(finalState, [target]) => {
const minimallyEncoded = bigIntToVmNumber(target);
return minimallyEncoded.length > targetLength
? applyError(
finalState,
AuthenticationErrorCommon.insufficientLength,
`Minimum necessary byte length: ${minimallyEncoded.length}. Requested byte length: ${targetLength}.`,
)
: minimallyEncoded.length === targetLength
? pushToStack(finalState, [minimallyEncoded])
: pushToStack(finalState, [
padMinimallyEncodedVmNumber(
minimallyEncoded,
targetLength,
),
]);
},
{
maximumVmNumberByteLength: maximumStackItemLength,
requireMinimalEncoding: false,
},
);
});
export const opNum2Bin = createOpNum2Bin();
export const createOpBin2Num =
<
State extends AuthenticationProgramStateError &
AuthenticationProgramStateStack,
>({
maximumStackItemLength = ConsensusCommon.maximumStackItemLength,
maximumVmNumberByteLength = ConsensusCommon.maximumVmNumberByteLength,
}: {
maximumStackItemLength?: number;
maximumVmNumberByteLength?: number;
} = {}): Operation<State> =>
(state: State) =>
useOneVmNumber(
state,
(nextState, [target]) => {
const minimallyEncoded = bigIntToVmNumber(target);
return maximumVmNumberByteLength && minimallyEncoded.length > maximumVmNumberByteLength
? applyError(
nextState,
AuthenticationErrorCommon.exceededMaximumVmNumberByteLength,
`Maximum VM number byte length: ${maximumVmNumberByteLength}; required byte length: ${minimallyEncoded.length}.`,
)
: pushToStack(nextState, [minimallyEncoded]);
},
{
maximumVmNumberByteLength: maximumStackItemLength,
requireMinimalEncoding: false,
},
);
export const opBin2Num = createOpBin2Num();