From: Chris Duncan Date: Tue, 26 Nov 2024 22:31:05 +0000 (-0800) Subject: Formatting. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=e1b4d4a13ef0614c4fe6506a6106655e3d65ba6b;p=libnemo.git Formatting. --- diff --git a/src/lib/blake2b.ts b/src/lib/blake2b.ts index ce9689e..81fe42a 100644 --- a/src/lib/blake2b.ts +++ b/src/lib/blake2b.ts @@ -3,15 +3,16 @@ 'use strict' -// Implementation derived from blake2b@2.1.4 -// Copyright 2017 Emil Bay -// Used with permission. See LICENSES/ISC.txt -// See for details: https://github.com/emilbayes/blake2b -// -// Modified in 2024 by Chris Duncan to eliminate dependencies, port to -// TypeScript, and bundle into web workers. -// Original source commit: https://github.com/emilbayes/blake2b/blob/1f63e02e3f226642959506cdaa67c8819ff145cd/index.js - +/** +* Implementation derived from blake2b@2.1.4 +* Copyright 2017 Emil Bay +* Used with permission. See LICENSES/ISC.txt +* See for details: https://github.com/emilbayes/blake2b +* +* Modified in 2024 by Chris Duncan to eliminate dependencies, port to +* TypeScript, and bundle into web workers. +* Original source commit: https://github.com/emilbayes/blake2b/blob/1f63e02e3f226642959506cdaa67c8819ff145cd/index.js +*/ const BYTES_MIN = 16 const BYTES_MAX = 64 const KEYBYTES_MIN = 16 @@ -19,9 +20,11 @@ const KEYBYTES_MAX = 64 const SALTBYTES = 16 const PERSONALBYTES = 16 -// 64-bit unsigned addition -// Sets v[a,a+1] += v[b,b+1] -// v should be a Uint32Array +/** +* 64-bit unsigned addition +* Sets v[a,a+1] += v[b,b+1] +* v should be a Uint32Array +*/ function ADD64AA (v, a, b) { var o0 = v[a] + v[b] var o1 = v[a + 1] + v[b + 1] @@ -32,9 +35,11 @@ function ADD64AA (v, a, b) { v[a + 1] = o1 } -// 64-bit unsigned addition -// Sets v[a,a+1] += b -// b0 is the low 32 bits of b, b1 represents the high 32 bits +/** +* 64-bit unsigned addition +* Sets v[a,a+1] += b +* b0 is the low 32 bits of b, b1 represents the high 32 bits +*/ function ADD64AC (v, a, b0, b1) { var o0 = v[a] + b0 if (b0 < 0) { @@ -56,8 +61,10 @@ function B2B_GET32 (arr, i) { (arr[i + 3] << 24)) } -// G Mixing function -// The ROTRs are inlined for speed +/** +* G Mixing function +* The ROTRs are inlined for speed +*/ function B2B_G (a, b, c, d, ix, iy) { var x0 = m[ix] var x1 = m[ix + 1] @@ -122,13 +129,17 @@ var SIGMA8 = [ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 ] -// These are offsets into a uint64 buffer. -// Multiply them all by 2 to make them offsets into a uint32 buffer, -// because this is Javascript and we don't have uint64s +/** +* These are offsets into a uint64 buffer. +* Multiply them all by 2 to make them offsets into a uint32 buffer, +* because this is Javascript and we don't have uint64s +*/ var SIGMA82 = new Uint8Array(SIGMA8.map(function (x) { return x * 2 })) -// Compression function. 'last' flag indicates last block. -// Note we're representing 16 uint64s as 32 uint32s +/** +* Compression function. 'last' flag indicates last block. +* Note we're representing 16 uint64s as 32 uint32s +*/ var v = new Uint32Array(32) var m = new Uint32Array(32) function blake2bCompress (ctx, last) { @@ -193,9 +204,11 @@ var parameter_block = new Uint8Array([ 0, 0, 0, 0 // 60: personal ]) -// Creates a BLAKE2b hashing context -// Requires an output length between 1 and 64 bytes -// Takes an optional Uint8Array key +/** +* Creates a BLAKE2b hashing context +* Requires an output length between 1 and 64 bytes +* Takes an optional Uint8Array key +*/ function Blake2b (outlen: number, key?: Uint8Array, salt?: Uint8Array, personal?: Uint8Array) { // zero out parameter_block before usage parameter_block.fill(0) @@ -248,8 +261,10 @@ Blake2b.prototype.digest = function (out?: 'binary' | 'hex' | Uint8Array) { Blake2b.prototype.final = Blake2b.prototype.digest -// Updates a BLAKE2b streaming hash -// Requires hash context and Uint8Array (byte array) +/** +* Updates a BLAKE2b streaming hash +* Requires hash context and Uint8Array (byte array) +*/ function blake2bUpdate (ctx, input: Uint8Array) { for (var i = 0; i < input.length; i++) { if (ctx.c === 128) { // buffer full ? @@ -261,8 +276,10 @@ function blake2bUpdate (ctx, input: Uint8Array) { } } -// Completes a BLAKE2b streaming hash -// Returns a Uint8Array containing the message digest +/** +* Completes a BLAKE2b streaming hash +* Returns a Uint8Array containing the message digest +*/ function blake2bFinal (ctx, out: Uint8Array) { ctx.t += ctx.c // mark last block offset @@ -285,7 +302,7 @@ function hexSlice (buf: Uint8Array) { function toHex (n: number) { if (typeof n !== 'number') - throw new TypeError(`expected number to convert to hex, received convert ${typeof n}`) + throw new TypeError(`expected number to convert to hex, received convert ${typeof n}`) if (n < 0 || n > 255) throw new RangeError(`expected byte value 0-255, received ${n}`) return n.toString(16).padStart(2, '0')