import { BUFFER_INPUT_MSG, BUFFER_INPUT_PRV, BUFFER_INPUT_PUB, BUFFER_INPUT_SIG, BUFFER_OUTPUT_DERIVE, BUFFER_OUTPUT_SIGN, BUFFER_OUTPUT_VERIFY, ERROR_EQUALBYTES_SELFTEST_XFAIL_ONE, ERROR_EQUALBYTES_SELFTEST_XFAIL_PAIR, ERROR_EQUALBYTES_SELFTEST_XPASS, ERROR_FE_ISZERO_SELFTEST_XFAIL, ERROR_FE_ISZERO_SELFTEST_XPASS, ERROR_GE_IS_CANONICAL_SELFTEST_XFAIL, ERROR_GE_IS_CANONICAL_SELFTEST_XPASS, ERROR_INDEX_OUT_OF_RANGE, ERROR_INVALID_BLOCK_COUNT, ERROR_INVALID_MESSAGE_LENGTH, ERROR_INVALID_PUBLIC_KEY, ERROR_UNKNOWN, } from './errors'
import './tests'
-export { BLOCKHASH_BYTELENGTH, BUFFER_INPUT_MSG, BUFFER_INPUT_PRV, BUFFER_INPUT_PUB, BUFFER_INPUT_SIG, BUFFER_OUTPUT_DERIVE, BUFFER_OUTPUT_SIGN, BUFFER_OUTPUT_VERIFY, ERROR_INDEX_OUT_OF_RANGE, ERROR_INVALID_BLOCK_COUNT, ERROR_INVALID_MESSAGE_LENGTH, ERROR_INVALID_PUBLIC_KEY, ERROR_EQUALBYTES_SELFTEST_XFAIL_ONE, ERROR_EQUALBYTES_SELFTEST_XFAIL_PAIR, ERROR_EQUALBYTES_SELFTEST_XPASS, ERROR_FE_ISZERO_SELFTEST_XFAIL, ERROR_FE_ISZERO_SELFTEST_XPASS, ERROR_GE_IS_CANONICAL_SELFTEST_XFAIL, ERROR_GE_IS_CANONICAL_SELFTEST_XPASS, ERROR_UNKNOWN, KEY_BYTELENGTH, MAX_MESSAGE_BYTELENGTH, MAX_VERIFY_BLOCKS, MAX_VERIFY_BLOCKS_BYTELENGTH, SIGNATURE_BYTELENGTH }
+export { BLOCKHASH_BYTELENGTH, BUFFER_INPUT_MSG, BUFFER_INPUT_PRV, BUFFER_INPUT_PUB, BUFFER_INPUT_SIG, BUFFER_OUTPUT_DERIVE, BUFFER_OUTPUT_SIGN, BUFFER_OUTPUT_VERIFY, ERROR_EQUALBYTES_SELFTEST_XFAIL_ONE, ERROR_EQUALBYTES_SELFTEST_XFAIL_PAIR, ERROR_EQUALBYTES_SELFTEST_XPASS, ERROR_FE_ISZERO_SELFTEST_XFAIL, ERROR_FE_ISZERO_SELFTEST_XPASS, ERROR_GE_IS_CANONICAL_SELFTEST_XFAIL, ERROR_GE_IS_CANONICAL_SELFTEST_XPASS, ERROR_INDEX_OUT_OF_RANGE, ERROR_INVALID_BLOCK_COUNT, ERROR_INVALID_MESSAGE_LENGTH, ERROR_INVALID_PUBLIC_KEY, ERROR_UNKNOWN, KEY_BYTELENGTH, MAX_MESSAGE_BYTELENGTH, MAX_VERIFY_BLOCKS, MAX_VERIFY_BLOCKS_BYTELENGTH, SIGNATURE_BYTELENGTH }
// Static I/O buffers
const INPUT_MSG = new StaticArray<u8>(MAX_MESSAGE_BYTELENGTH)
}
/**
- * Sign up to 64 KiB of data using a 64-byte secret key. Parameters are read as
- * bytes from the input buffer in the following order:
- *
- * - `[0,31]: private key`
- * - `[32,63]: public key`
- *
- * The message itself is read from a separate buffer. The signature is detached
- * from the message and written to the output buffer.
+ * Sign up to 64 KiB of data using a 32-byte private key. The 32-byte public key
+ * is also required for data integrity. Parameters are read as bytes from
+ * specific input buffers, and the message itself is read from a separate
+ * buffer. The signature is detached from the message and written to the output
+ * buffer.
* @param {i32} mlen Byte length of message to be signed, up to 65536
*/
export function sign (mlen: i32): void {
* concatenated value. The performance gain of verifying in bulk scales as
* `(1-(1/n)) x (k/(k+r))` where `n` is block count, `k` is the time required to
* verify the public key, and `r` is the time required to compute all the
- * remaining verification steps. Testing shows `k` to be ~12% the total
- * verification time, so batching up to 64 block signature/hash pairs reaches
- * ~96% of that gain; any more blocks would simply add latency returning the
- * final results.
+ * remaining verification steps. Testing shows public key derivation to be ~12%
+ * the total verification time, so batching up to 64 block signature/hash pairs
+ * reaches ~96% of that gain; any more blocks would simply add latency returning
+ * the final results.
*
* All pairs are verified against a single public key. The results are written
* to the output buffer in the same order they were received.
* written in one pass rather than looped over an intermediate array because
* allocating costs more than the copy itself.
*
- * Whole 32-byte chunks are read a word at a time through a `Uint32Array` view.
- * A `Uint32Array` is laid out little-endian on every platform that can run
- * WASM, so a word read is already in the order the module's `store<u32>` wants
- * and needs no shifting — measured over 64 KiB, that takes the loop from ~45 µs
- * to ~13 µs, which is the cost of the 2048 boundary crossings alone. The
- * remaining bytes, fewer than 32, go through `setInputMsgBytes`, which handles
- * the zero padding.
+ * On little-endian systems, the message input is first read in 32-byte chunks
+ * as 4-byte words through a `Uint32Array` view, and any remaining bytes are
+ * read individually through a `Uint8Array`. Big-endian systems read the input
+ * one byte at a time for the entire message since byte order must be reversed
+ * to meet WASM spec which is little-endian by definition.
+ *
+ * The vast majority of hardware is little-endian and will benefit from the
+ * word-based capture: measured over 64 KiB, it reduces copy time by ~66% which
+ * is the cost of the 2048 boundary crossings alone.
*/
function setInputMsg (message: Bytes): void {
const mlen = byteLength(message)