|
| 1 | +import type { |
| 2 | + AuthenticationProgramStateError, |
| 3 | + AuthenticationProgramStateResourceLimits, |
| 4 | + AuthenticationProgramStateStack, |
| 5 | +} from '../../../../lib.js'; |
| 6 | +import { |
| 7 | + applyError, |
| 8 | + pushToStack, |
| 9 | + pushToStackVmNumberChecked, |
| 10 | + useOneStackItem, |
| 11 | + useOneVmNumber, |
| 12 | +} from '../../common/common.js'; |
| 13 | + |
| 14 | +import { ConsensusBch2026 } from './bch-2026-consensus.js'; |
| 15 | +import { AuthenticationErrorBch2026 } from './bch-2026-errors.js'; |
| 16 | + |
| 17 | +const enum Constants { |
| 18 | + allBits = 0xff, |
| 19 | + bitsPerByte = 8, |
| 20 | +} |
| 21 | + |
| 22 | +export const opInvert = < |
| 23 | + State extends AuthenticationProgramStateError & |
| 24 | + AuthenticationProgramStateStack, |
| 25 | +>( |
| 26 | + state: State, |
| 27 | +): State => |
| 28 | + useOneStackItem(state, (nextState, [a]) => |
| 29 | + // eslint-disable-next-line no-bitwise |
| 30 | + pushToStack(nextState, [a.map((v) => v ^ Constants.allBits)]), |
| 31 | + ); |
| 32 | + |
| 33 | +const useOneShiftBitCount = < |
| 34 | + State extends AuthenticationProgramStateError & |
| 35 | + AuthenticationProgramStateResourceLimits & |
| 36 | + AuthenticationProgramStateStack, |
| 37 | +>( |
| 38 | + state: State, |
| 39 | + shiftOperation: (nextState: State, [bitCount]: [bigint]) => State, |
| 40 | + { maximumStackItemLength = ConsensusBch2026.maximumStackItemLength } = {}, |
| 41 | +) => { |
| 42 | + const maximumBitCount = maximumStackItemLength * Constants.bitsPerByte; |
| 43 | + |
| 44 | + return useOneVmNumber(state, (nextState, [bitCount]) => { |
| 45 | + if (bitCount < 0n || bitCount > maximumBitCount) { |
| 46 | + return applyError( |
| 47 | + nextState, |
| 48 | + AuthenticationErrorBch2026.invalidShiftBitCount, |
| 49 | + `Bit count (${bitCount}) is outside of the valid range: 0 to ${maximumBitCount} (inclusive).`, |
| 50 | + ); |
| 51 | + } |
| 52 | + return shiftOperation(nextState, [bitCount]); |
| 53 | + }); |
| 54 | +}; |
| 55 | + |
| 56 | +const createOpShiftNum = |
| 57 | + ( |
| 58 | + maximumStackItemLength: number, |
| 59 | + shiftOperation: (numericValue: bigint, bitCount: bigint) => bigint, |
| 60 | + ) => |
| 61 | + < |
| 62 | + State extends AuthenticationProgramStateError & |
| 63 | + AuthenticationProgramStateResourceLimits & |
| 64 | + AuthenticationProgramStateStack, |
| 65 | + >( |
| 66 | + state: State, |
| 67 | + ): State => |
| 68 | + useOneShiftBitCount( |
| 69 | + state, |
| 70 | + (nextState, [bitCount]) => |
| 71 | + useOneVmNumber(nextState, (finalState, [numericValue]) => { |
| 72 | + const result = shiftOperation(numericValue, bitCount); |
| 73 | + return pushToStackVmNumberChecked(finalState, result, { |
| 74 | + hasEncodingCost: true, |
| 75 | + maximumVmNumberByteLength: maximumStackItemLength, |
| 76 | + }); |
| 77 | + }), |
| 78 | + { maximumStackItemLength }, |
| 79 | + ); |
| 80 | + |
| 81 | +export const createOpLShiftNum = ({ |
| 82 | + maximumStackItemLength = ConsensusBch2026.maximumStackItemLength, |
| 83 | +} = {}) => |
| 84 | + createOpShiftNum( |
| 85 | + maximumStackItemLength, |
| 86 | + // eslint-disable-next-line no-bitwise |
| 87 | + (numericValue, bitCount) => numericValue << bitCount, |
| 88 | + ); |
| 89 | + |
| 90 | +export const createOpRShiftNum = ({ |
| 91 | + maximumStackItemLength = ConsensusBch2026.maximumStackItemLength, |
| 92 | +} = {}) => |
| 93 | + createOpShiftNum( |
| 94 | + maximumStackItemLength, |
| 95 | + // eslint-disable-next-line no-bitwise |
| 96 | + (numericValue, bitCount) => numericValue >> bitCount, |
| 97 | + ); |
| 98 | + |
| 99 | +// eslint-disable-next-line functional/no-return-void |
| 100 | +const copyWholeBytes = ( |
| 101 | + src: Uint8Array, |
| 102 | + dst: Uint8Array, |
| 103 | + byteShift: number, |
| 104 | + left: boolean, |
| 105 | + // eslint-disable-next-line @typescript-eslint/max-params |
| 106 | +) => { |
| 107 | + if (byteShift >= src.length) return; |
| 108 | + if (left) { |
| 109 | + // eslint-disable-next-line functional/no-expression-statements |
| 110 | + dst.set(src.subarray(byteShift), 0); |
| 111 | + return; |
| 112 | + } |
| 113 | + // eslint-disable-next-line functional/no-expression-statements |
| 114 | + dst.set(src.subarray(0, src.length - byteShift), byteShift); |
| 115 | +}; |
| 116 | + |
| 117 | +const residualLeftShift = (buf: Uint8Array, bitShift: number): Uint8Array => { |
| 118 | + // eslint-disable-next-line functional/no-let |
| 119 | + let carry = 0; |
| 120 | + // eslint-disable-next-line functional/no-loop-statements, functional/no-let, no-plusplus |
| 121 | + for (let i = buf.length - 1; i >= 0; i--) { |
| 122 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 123 | + const v = buf[i]!; |
| 124 | + // eslint-disable-next-line functional/no-expression-statements, functional/immutable-data, no-bitwise |
| 125 | + buf[i] = ((v << bitShift) | carry) & Constants.allBits; |
| 126 | + // eslint-disable-next-line functional/no-expression-statements, no-bitwise |
| 127 | + carry = v >> (Constants.bitsPerByte - bitShift); |
| 128 | + } |
| 129 | + return buf; |
| 130 | +}; |
| 131 | + |
| 132 | +const residualRightShift = (buf: Uint8Array, bitShift: number): Uint8Array => { |
| 133 | + // eslint-disable-next-line functional/no-let |
| 134 | + let carry = 0; |
| 135 | + // eslint-disable-next-line functional/no-loop-statements, functional/no-let, no-plusplus |
| 136 | + for (let i = 0; i < buf.length; i++) { |
| 137 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 138 | + const v = buf[i]!; |
| 139 | + // eslint-disable-next-line functional/no-expression-statements, functional/immutable-data, no-bitwise |
| 140 | + buf[i] = ((v >> bitShift) | carry) & Constants.allBits; |
| 141 | + // eslint-disable-next-line functional/no-expression-statements, no-bitwise |
| 142 | + carry = (v << (Constants.bitsPerByte - bitShift)) & Constants.allBits; |
| 143 | + } |
| 144 | + return buf; |
| 145 | +}; |
| 146 | + |
| 147 | +const shiftFixed = ( |
| 148 | + src: Uint8Array, |
| 149 | + bitCount: bigint, |
| 150 | + isLeftShift: boolean, |
| 151 | +): Uint8Array => { |
| 152 | + const s = Number(bitCount); |
| 153 | + if (!s || !src.length) return src.slice(); |
| 154 | + |
| 155 | + const len = src.length; |
| 156 | + const dst = new Uint8Array(len); |
| 157 | + const byteShift = Math.floor(s / Constants.bitsPerByte); |
| 158 | + const bitShift = s % Constants.bitsPerByte; |
| 159 | + |
| 160 | + // eslint-disable-next-line functional/no-expression-statements |
| 161 | + copyWholeBytes(src, dst, byteShift, isLeftShift); |
| 162 | + if (!bitShift) return dst; |
| 163 | + |
| 164 | + return isLeftShift |
| 165 | + ? residualLeftShift(dst, bitShift) |
| 166 | + : residualRightShift(dst, bitShift); |
| 167 | +}; |
| 168 | + |
| 169 | +/** |
| 170 | + * Perform a fixed-length, logical left shift of `bin` by the `bitCount`, |
| 171 | + * equivalent to `OP_LSHIFTBIN` in the Bitcoin Cash VM. |
| 172 | + * @param bin - the Uint8Array to shift. |
| 173 | + * @param bitCount - the count of bits by which to shift `bin`. |
| 174 | + */ |
| 175 | +export const binaryShiftLeft = (bin: Uint8Array, bitCount: bigint) => |
| 176 | + shiftFixed(bin, bitCount, true); |
| 177 | + |
| 178 | +/** |
| 179 | + * Perform a fixed-length, logical right shift of `bin` by the `bitCount`, |
| 180 | + * equivalent to `OP_RSHIFTBIN` in the Bitcoin Cash VM. |
| 181 | + * @param bin - the Uint8Array to shift. |
| 182 | + * @param bitCount - the count of bits by which to shift `bin`. |
| 183 | + */ |
| 184 | +export const binaryShiftRight = (bin: Uint8Array, bitCount: bigint) => |
| 185 | + shiftFixed(bin, bitCount, false); |
| 186 | + |
| 187 | +const createOpShiftBin = |
| 188 | + ( |
| 189 | + maximumStackItemLength: number, |
| 190 | + shiftOperation: (bin: Uint8Array, bitCount: bigint) => Uint8Array, |
| 191 | + ) => |
| 192 | + < |
| 193 | + State extends AuthenticationProgramStateError & |
| 194 | + AuthenticationProgramStateResourceLimits & |
| 195 | + AuthenticationProgramStateStack, |
| 196 | + >( |
| 197 | + state: State, |
| 198 | + ): State => |
| 199 | + useOneShiftBitCount( |
| 200 | + state, |
| 201 | + (nextState, [bitCount]) => |
| 202 | + useOneStackItem(nextState, (finalState, [bin]) => |
| 203 | + pushToStack(finalState, [shiftOperation(bin, bitCount)]), |
| 204 | + ), |
| 205 | + { maximumStackItemLength }, |
| 206 | + ); |
| 207 | + |
| 208 | +export const createOpLShiftBin = ({ |
| 209 | + maximumStackItemLength = ConsensusBch2026.maximumStackItemLength, |
| 210 | +} = {}) => createOpShiftBin(maximumStackItemLength, binaryShiftLeft); |
| 211 | + |
| 212 | +export const createOpRShiftBin = ({ |
| 213 | + maximumStackItemLength = ConsensusBch2026.maximumStackItemLength, |
| 214 | +} = {}) => createOpShiftBin(maximumStackItemLength, binaryShiftRight); |
0 commit comments