Skip to content

Commit 336b265

Browse files
committed
Move _assert into utils
1 parent d2750a1 commit 336b265

File tree

9 files changed

+40
-50
lines changed

9 files changed

+40
-50
lines changed

src/_arx.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ xchacha [^2] uses the subkey and remaining 8 byte nonce with ChaCha20 as normal
3636
3737
* @module
3838
*/
39-
import { abool, abytes, anumber } from './_assert.ts';
40-
import { type XorStream, checkOpts, clean, copyBytes, u32 } from './utils.ts';
39+
// prettier-ignore
40+
import {
41+
type XorStream, abool, abytes, anumber, checkOpts, clean, copyBytes, u32
42+
} from './utils.ts';
4143

4244
// We can't make top-level var depend on utils.utf8ToBytes
4345
// because it's not present in all envs. Creating a similar fn here

src/_micro.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
/*! noble-ciphers - MIT License (c) 2023 Paul Miller (paulmillr.com) */
88
// prettier-ignore
99
import { createCipher, rotl } from './_arx.ts';
10-
import { abytes } from './_assert.ts';
1110
import {
1211
type Cipher,
1312
type XorStream,
13+
abytes,
1414
bytesToHex,
1515
concatBytes,
1616
equalBytes,

src/_poly1305.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
* Check out [original website](https://cr.yp.to/mac.html).
1717
* @module
1818
*/
19-
import { abytes, aexists, aoutput } from './_assert.ts';
20-
import { Hash, type Input, clean, toBytes } from './utils.ts';
19+
import { Hash, type Input, abytes, aexists, aoutput, clean, toBytes } from './utils.ts';
2120

2221
// Based on Public Domain poly1305-donna https://github.com/floodyberry/poly1305-donna
2322
const u8to16 = (a: Uint8Array, i: number) => (a[i++] & 0xff) | ((a[i++] & 0xff) << 8);

src/_polyval.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
*
1313
* @module
1414
*/
15-
import { abytes, aexists, aoutput } from './_assert.ts';
16-
import { clean, copyBytes, createView, Hash, type Input, toBytes, u32 } from './utils.ts';
15+
// prettier-ignore
16+
import {
17+
abytes, aexists, aoutput,
18+
clean, copyBytes, createView, Hash, type Input, toBytes, u32,
19+
} from './utils.ts';
1720

1821
const BLOCK_SIZE = 16;
1922
// TODO: rewrite

src/aes.ts

+5-17
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,13 @@
1414
* and [original proposal](https://csrc.nist.gov/csrc/media/projects/cryptographic-standards-and-guidelines/documents/aes-development/rijndael-ammended.pdf)
1515
* @module
1616
*/
17-
import { abytes } from './_assert.ts';
1817
import { ghash, polyval } from './_polyval.ts';
18+
// prettier-ignore
1919
import {
20-
type Cipher,
21-
type CipherWithOutput,
22-
clean,
23-
complexOverlapBytes,
24-
concatBytes,
25-
copyBytes,
26-
createView,
27-
equalBytes,
28-
getOutput,
29-
isAligned32,
30-
overlapBytes,
31-
setBigUint64,
32-
u32,
33-
u64Lengths,
34-
u8,
35-
wrapCipher,
20+
abytes, clean, complexOverlapBytes, concatBytes,
21+
copyBytes, createView, equalBytes, getOutput, isAligned32, overlapBytes,
22+
setBigUint64, u32, u64Lengths, u8, wrapCipher,
23+
type Cipher, type CipherWithOutput,
3624
} from './utils.ts';
3725

3826
const BLOCK_SIZE = 16;

src/ff1.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
* [NIST 800-38G](https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38G.pdf).
44
* @module
55
*/
6-
import { abytes, anumber } from './_assert.ts';
76
import { unsafe } from './aes.ts';
8-
import { type Cipher, bytesToNumberBE, clean, numberToBytesBE } from './utils.ts';
7+
import { type Cipher, abytes, anumber, bytesToNumberBE, clean, numberToBytesBE } from './utils.ts';
98

109
// NOTE: no point in inlining encrypt instead of encryptBlock, since BigInt stuff will be slow
1110
const { expandKeyLE, encryptBlock } = unsafe;

src/salsa.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* @module
1515
*/
1616
import { createCipher, rotl } from './_arx.ts';
17-
import { abytes } from './_assert.ts';
1817
import { poly1305 } from './_poly1305.ts';
1918
import {
19+
abytes,
2020
type ARXCipher,
2121
type CipherWithOutput,
2222
clean,

src/webcrypto.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
*/
1111
// Use full path so that Node.js can rewrite it to `cryptoNode.js`.
1212
import { crypto } from '@noble/ciphers/crypto';
13-
import { abytes, anumber } from './_assert.ts';
14-
import { type AsyncCipher, type Cipher, concatBytes } from './utils.ts';
13+
import { abytes, anumber, type AsyncCipher, type Cipher, concatBytes } from './utils.ts';
1514

1615
/**
1716
* Secure PRNG. Uses `crypto.getRandomValues`, which defers to OS.

test/utils.test.js

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fc from 'fast-check';
22
import { describe, should } from 'micro-should';
33
import { deepStrictEqual, throws } from 'node:assert';
4-
import * as assert from '../esm/_assert.js';
4+
import * as u from '../esm/utils.js';
55
import {
66
bytesToHex,
77
bytesToUtf8,
@@ -201,38 +201,38 @@ describe('utils', () => {
201201

202202
describe('assert', () => {
203203
should('anumber', () => {
204-
deepStrictEqual(assert.anumber(10), undefined);
205-
throws(() => assert.anumber(1.2));
206-
throws(() => assert.anumber('1'));
207-
throws(() => assert.anumber(true));
208-
throws(() => assert.anumber(NaN));
204+
deepStrictEqual(u.anumber(10), undefined);
205+
throws(() => u.anumber(1.2));
206+
throws(() => u.anumber('1'));
207+
throws(() => u.anumber(true));
208+
throws(() => u.anumber(NaN));
209209
});
210210
should('abytes', () => {
211-
deepStrictEqual(assert.abytes(new Uint8Array(0)), undefined);
212-
if (typeof Buffer !== 'undefined') deepStrictEqual(assert.abytes(Buffer.alloc(10)), undefined);
213-
deepStrictEqual(assert.abytes(new Uint8Array(10)), undefined);
214-
assert.abytes(new Uint8Array(11), 11, 12);
215-
assert.abytes(new Uint8Array(12), 12, 12);
216-
throws(() => assert.abytes('test'));
217-
throws(() => assert.abytes(new Uint8Array(10), 11, 12));
218-
throws(() => assert.abytes(new Uint8Array(10), 11, 12));
211+
deepStrictEqual(u.abytes(new Uint8Array(0)), undefined);
212+
if (typeof Buffer !== 'undefined') deepStrictEqual(u.abytes(Buffer.alloc(10)), undefined);
213+
deepStrictEqual(u.abytes(new Uint8Array(10)), undefined);
214+
u.abytes(new Uint8Array(11), 11, 12);
215+
u.abytes(new Uint8Array(12), 12, 12);
216+
throws(() => u.abytes('test'));
217+
throws(() => u.abytes(new Uint8Array(10), 11, 12));
218+
throws(() => u.abytes(new Uint8Array(10), 11, 12));
219219
});
220220
should('ahash', () => {
221221
const sha256 = () => {};
222222
sha256.blockLen = 1;
223223
sha256.outputLen = 1;
224224
sha256.create = () => {};
225-
deepStrictEqual(assert.ahash(sha256), undefined);
226-
throws(() => assert.ahash({}));
227-
throws(() => assert.ahash({ blockLen: 1, outputLen: 1, create: () => {} }));
225+
deepStrictEqual(u.ahash(sha256), undefined);
226+
throws(() => u.ahash({}));
227+
throws(() => u.ahash({ blockLen: 1, outputLen: 1, create: () => {} }));
228228
});
229229
should('aexists', () => {
230-
deepStrictEqual(assert.aexists({}), undefined);
231-
throws(() => assert.aexists({ destroyed: true }));
230+
deepStrictEqual(u.aexists({}), undefined);
231+
throws(() => u.aexists({ destroyed: true }));
232232
});
233233
should('aoutput', () => {
234-
deepStrictEqual(assert.aoutput(new Uint8Array(10), { outputLen: 5 }), undefined);
235-
throws(() => assert.aoutput(new Uint8Array(1), { outputLen: 5 }));
234+
deepStrictEqual(u.aoutput(new Uint8Array(10), { outputLen: 5 }), undefined);
235+
throws(() => u.aoutput(new Uint8Array(1), { outputLen: 5 }));
236236
});
237237
});
238238

0 commit comments

Comments
 (0)