From 35b2bb2d1333c454e041833873e93045d2458b91 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 27 Oct 2020 09:04:37 +0100 Subject: [PATCH] Use base122 encoding for WASM buffer (#127) --- packages/wasm-crypto/src/js/base122.js | 147 +++++++++++++++++++++++++ scripts/build-package.sh | 4 +- scripts/pack-wasm-base.js | 22 ++++ scripts/pack-wasm-base64.js | 10 -- 4 files changed, 171 insertions(+), 12 deletions(-) create mode 100644 packages/wasm-crypto/src/js/base122.js create mode 100755 scripts/pack-wasm-base.js delete mode 100755 scripts/pack-wasm-base64.js diff --git a/packages/wasm-crypto/src/js/base122.js b/packages/wasm-crypto/src/js/base122.js new file mode 100644 index 00000000..b5051a3b --- /dev/null +++ b/packages/wasm-crypto/src/js/base122.js @@ -0,0 +1,147 @@ +// Copyright 2019-2020 @polkadot/wasm-crypto authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +// Adapted from https://github.com/kevinAlbs/Base122 +// MIT License, Copyright (c) 2016 Kevin Albertson + +const kString = 0; +const kUint8Array = 1; +const kIllegals = [0, 10, 13, 34, 38, 92]; +const kShortened = 0b111; + +/** + * Encodes raw data into base-122. + * @param {Uint8Array|Buffer|Array|String} rawData - The data to be encoded. This can be an array + * or Buffer with raw data bytes or a string of bytes (i.e. the type of argument to btoa()) + * @returns {Array} The base-122 encoded data as a regular array of UTF-8 character byte values. + */ +function encode (rawData) { + const dataType = typeof rawData === 'string' ? kString : kUint8Array; + let curIndex = 0; + let curBit = 0; // Points to current bit needed + const outData = []; + const getByte = dataType === kString ? (i) => rawData.codePointAt(i) : (i) => rawData[i]; + + // Get seven bits of input data. Returns false if there is no input left. + function get7 () { + if (curIndex >= rawData.length) return false; + // Shift, mask, unshift to get first part. + const firstByte = getByte(curIndex); + let firstPart = ((0b11111110 >>> curBit) & firstByte) << curBit; + + // Align it to a seven bit chunk. + firstPart >>= 1; + // Check if we need to go to the next byte for more bits. + curBit += 7; + if (curBit < 8) return firstPart; // Do not need next byte. + curBit -= 8; + curIndex++; + // Now we want bits [0..curBit] of the next byte if it exists. + if (curIndex >= rawData.length) return firstPart; + + const secondByte = getByte(curIndex); + let secondPart = ((0xFF00 >>> curBit) & secondByte) & 0xFF; + + // Align it. + secondPart >>= 8 - curBit; + + return firstPart | secondPart; + } + + while (true) { + // Grab 7 bits. + const bits = get7(); + + if (bits === false) break; + + const illegalIndex = kIllegals.indexOf(bits); + + if (illegalIndex !== -1) { + // Since this will be a two-byte character, get the next chunk of seven bits. + let nextBits = get7(); + + let b1 = 0b11000010; + let b2 = 0b10000000; + + if (nextBits === false) { + b1 |= (0b111 & kShortened) << 2; + nextBits = bits; // Encode these bits after the shortened signifier. + } else { + b1 |= (0b111 & illegalIndex) << 2; + } + + // Push first bit onto first byte, remaining 6 onto second. + const firstBit = (nextBits & 0b01000000) > 0 ? 1 : 0; + + b1 |= firstBit; + b2 |= nextBits & 0b00111111; + outData.push(b1); + outData.push(b2); + } else { + outData.push(bits); + } + } + + return outData; +} + +/** + * Decodes base-122 encoded data back to the original data. + * @param {Uint8Array|Buffer|String} rawData - The data to be decoded. This can be a Uint8Array + * or Buffer with raw data bytes or a string of bytes (i.e. the type of argument to btoa()) + * @returns {Array} The data in a regular array representing byte values. + */ +function decode (base122Data) { + const strData = typeof base122Data === 'string' ? base122Data : utf8DataToString(base122Data); + const decoded = []; + let curByte = 0; + let bitOfByte = 0; + + function push7 (byte) { + byte <<= 1; + // Align this byte to offset for current byte. + curByte |= (byte >>> bitOfByte); + bitOfByte += 7; + + if (bitOfByte >= 8) { + decoded.push(curByte); + bitOfByte -= 8; + // Now, take the remainder, left shift by what has been taken. + curByte = (byte << (7 - bitOfByte)) & 255; + } + } + + for (let i = 0; i < strData.length; i++) { + const c = strData.charCodeAt(i); + + // Check if this is a two-byte character. + if (c > 127) { + // Note, the charCodeAt will give the codePoint, thus + // 0b110xxxxx 0b10yyyyyy will give => xxxxxyyyyyy + const illegalIndex = (c >>> 8) & 7; // 7 = 0b111. + + // We have to first check if this is a shortened two-byte character, i.e. if it only + // encodes <= 7 bits. + if (illegalIndex !== kShortened) push7(kIllegals[illegalIndex]); + + // Always push the rest. + push7(c & 127); + } else { + // One byte characters can be pushed directly. + push7(c); + } + } + + return decoded; +} + +/** + * Converts a sequence of UTF-8 bytes to a string. + * @param {Uint8Array|Buffer} data - The UTF-8 data. + * @returns {String} A string with each character representing a code point. + */ +function utf8DataToString (data) { + return Buffer.from(data).toString('utf-8'); +} + +module.exports = { decode, encode }; diff --git a/scripts/build-package.sh b/scripts/build-package.sh index 52b2543c..a8122414 100755 --- a/scripts/build-package.sh +++ b/scripts/build-package.sh @@ -28,8 +28,8 @@ echo "*** Optimising WASM output" ../../binaryen/bin/wasm-opt $WSM -Os -o $OPT # convert wasm to base64 structure -echo "*** Packing WASM into base64" -node ../../scripts/pack-wasm-base64.js +echo "*** Packing WASM into baseX" +node ../../scripts/pack-wasm-base.js # build asmjs version from the input (optimised) WASM echo "*** Building asm.js version" diff --git a/scripts/pack-wasm-base.js b/scripts/pack-wasm-base.js new file mode 100755 index 00000000..5a85f6c3 --- /dev/null +++ b/scripts/pack-wasm-base.js @@ -0,0 +1,22 @@ +// Copyright 2019-2020 @polkadot/wasm authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +/* eslint-disable @typescript-eslint/no-var-requires */ +const fs = require('fs'); + +const buffer = fs.readFileSync('./build/wasm_opt.wasm'); + +// base 64 +// fs.writeFileSync('./build/wasm_wasm.js', ` +// module.exports = Buffer.from('${buffer.toString('base64')}', 'base64'); +// `); + +// base 122 +const { encode } = require('../packages/wasm-crypto/src/js/base122'); + +fs.writeFileSync('./build/wasm_wasm.js', `// Generated as part of the build, do not edit + +const { decode } = require('./base122'); + +module.exports = Buffer.from(decode("${Buffer.from(encode(buffer)).toString('utf-8')}")); +`, { encoding: 'utf-8' }); diff --git a/scripts/pack-wasm-base64.js b/scripts/pack-wasm-base64.js deleted file mode 100755 index 8ce5f4d7..00000000 --- a/scripts/pack-wasm-base64.js +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright 2019-2020 @polkadot/wasm authors & contributors -// SPDX-License-Identifier: Apache-2.0 - -/* eslint-disable @typescript-eslint/no-var-requires */ -const fs = require('fs'); -const buffer = fs.readFileSync('./build/wasm_opt.wasm'); - -fs.writeFileSync('./build/wasm_wasm.js', ` -module.exports = Buffer.from('${buffer.toString('base64')}', 'base64'); -`);