@noble/post-quantum 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.ts ADDED
@@ -0,0 +1,113 @@
1
+ /*! noble-post-quantum - MIT License (c) 2024 Paul Miller (paulmillr.com) */
2
+ import { bytes as abytes } from '@noble/hashes/_assert';
3
+ import { TypedArray, randomBytes as randb } from '@noble/hashes/utils';
4
+
5
+ export const ensureBytes = abytes;
6
+ export const randomBytes = randb;
7
+
8
+ // Compares 2 u8a-s in kinda constant time
9
+ export function equalBytes(a: Uint8Array, b: Uint8Array) {
10
+ if (a.length !== b.length) return false;
11
+ let diff = 0;
12
+ for (let i = 0; i < a.length; i++) diff |= a[i] ^ b[i];
13
+ return diff === 0;
14
+ }
15
+
16
+ export type Signer = {
17
+ signRandBytes: number;
18
+ keygen: (seed: Uint8Array) => {
19
+ secretKey: Uint8Array;
20
+ publicKey: Uint8Array;
21
+ };
22
+ sign: (secretKey: Uint8Array, msg: Uint8Array, random?: Uint8Array) => Uint8Array;
23
+ verify: (publicKey: Uint8Array, msg: Uint8Array, sig: Uint8Array) => boolean;
24
+ };
25
+
26
+ export interface Coder<F, T> {
27
+ encode(from: F): T;
28
+ decode(to: T): F;
29
+ }
30
+
31
+ export interface BytesCoder<T> extends Coder<T, Uint8Array> {
32
+ encode: (data: T) => Uint8Array;
33
+ decode: (bytes: Uint8Array) => T;
34
+ }
35
+
36
+ export type BytesCoderLen<T> = BytesCoder<T> & { bytesLen: number };
37
+
38
+ // nano-packed, because struct encoding is hard.
39
+ type UnCoder<T> = T extends BytesCoder<infer U> ? U : never;
40
+ type SplitOut<T extends (number | BytesCoderLen<any>)[]> = {
41
+ [K in keyof T]: T[K] extends number ? Uint8Array : UnCoder<T[K]>;
42
+ };
43
+ export function splitCoder<T extends (number | BytesCoderLen<any>)[]>(
44
+ ...lengths: T
45
+ ): BytesCoder<SplitOut<T>> & { bytesLen: number } {
46
+ const getLength = (c: number | BytesCoderLen<any>) => (typeof c === 'number' ? c : c.bytesLen);
47
+ const bytesLen: number = lengths.reduce((sum: number, a) => sum + getLength(a), 0);
48
+ return {
49
+ bytesLen,
50
+ encode: (bufs: T) => {
51
+ const res = new Uint8Array(bytesLen);
52
+ for (let i = 0, pos = 0; i < lengths.length; i++) {
53
+ const c = lengths[i];
54
+ const l = getLength(c);
55
+ const b: Uint8Array = typeof c === 'number' ? (bufs[i] as any) : c.encode(bufs[i]);
56
+ ensureBytes(b, l);
57
+ res.set(b, pos);
58
+ if (typeof c !== 'number') b.fill(0); // clean
59
+ pos += l;
60
+ }
61
+ return res;
62
+ },
63
+ decode: (buf: Uint8Array) => {
64
+ ensureBytes(buf, bytesLen);
65
+ const res = [];
66
+ for (const c of lengths) {
67
+ const l = getLength(c);
68
+ const b = buf.subarray(0, l);
69
+ res.push(typeof c === 'number' ? b : c.decode(b));
70
+ buf = buf.subarray(l);
71
+ }
72
+ return res as SplitOut<T>;
73
+ },
74
+ } as any;
75
+ }
76
+ // nano-packed.array (fixed size)
77
+ export function vecCoder<T>(c: BytesCoderLen<T>, vecLen: number): BytesCoderLen<T[]> {
78
+ const bytesLen = vecLen * c.bytesLen;
79
+ return {
80
+ bytesLen,
81
+ encode: (u: T[]): Uint8Array => {
82
+ if (u.length !== vecLen)
83
+ throw new Error(`vecCoder.encode: wrong length=${u.length}. Expected: ${vecLen}`);
84
+ const res = new Uint8Array(bytesLen);
85
+ for (let i = 0, pos = 0; i < u.length; i++) {
86
+ const b = c.encode(u[i]);
87
+ res.set(b, pos);
88
+ b.fill(0); // clean
89
+ pos += b.length;
90
+ }
91
+ return res;
92
+ },
93
+ decode: (a: Uint8Array): T[] => {
94
+ ensureBytes(a, bytesLen);
95
+ const r: T[] = [];
96
+ for (let i = 0; i < a.length; i += c.bytesLen)
97
+ r.push(c.decode(a.subarray(i, i + c.bytesLen)));
98
+ return r;
99
+ },
100
+ };
101
+ }
102
+
103
+ // cleanBytes(new Uint8Array(), [new Uint16Array(), new Uint32Array()])
104
+ export function cleanBytes(...list: (TypedArray | TypedArray[])[]) {
105
+ for (const t of list) {
106
+ if (Array.isArray(t)) for (const b of t) b.fill(0);
107
+ else t.fill(0);
108
+ }
109
+ }
110
+
111
+ export function getMask(bits: number) {
112
+ return (1 << bits) - 1; // 4 -> 0b1111
113
+ }
package/utils.d.ts ADDED
@@ -0,0 +1,38 @@
1
+ /*! noble-post-quantum - MIT License (c) 2024 Paul Miller (paulmillr.com) */
2
+ import { bytes as abytes } from '@noble/hashes/_assert';
3
+ import { TypedArray, randomBytes as randb } from '@noble/hashes/utils';
4
+ export declare const ensureBytes: typeof abytes;
5
+ export declare const randomBytes: typeof randb;
6
+ export declare function equalBytes(a: Uint8Array, b: Uint8Array): boolean;
7
+ export type Signer = {
8
+ signRandBytes: number;
9
+ keygen: (seed: Uint8Array) => {
10
+ secretKey: Uint8Array;
11
+ publicKey: Uint8Array;
12
+ };
13
+ sign: (secretKey: Uint8Array, msg: Uint8Array, random?: Uint8Array) => Uint8Array;
14
+ verify: (publicKey: Uint8Array, msg: Uint8Array, sig: Uint8Array) => boolean;
15
+ };
16
+ export interface Coder<F, T> {
17
+ encode(from: F): T;
18
+ decode(to: T): F;
19
+ }
20
+ export interface BytesCoder<T> extends Coder<T, Uint8Array> {
21
+ encode: (data: T) => Uint8Array;
22
+ decode: (bytes: Uint8Array) => T;
23
+ }
24
+ export type BytesCoderLen<T> = BytesCoder<T> & {
25
+ bytesLen: number;
26
+ };
27
+ type UnCoder<T> = T extends BytesCoder<infer U> ? U : never;
28
+ type SplitOut<T extends (number | BytesCoderLen<any>)[]> = {
29
+ [K in keyof T]: T[K] extends number ? Uint8Array : UnCoder<T[K]>;
30
+ };
31
+ export declare function splitCoder<T extends (number | BytesCoderLen<any>)[]>(...lengths: T): BytesCoder<SplitOut<T>> & {
32
+ bytesLen: number;
33
+ };
34
+ export declare function vecCoder<T>(c: BytesCoderLen<T>, vecLen: number): BytesCoderLen<T[]>;
35
+ export declare function cleanBytes(...list: (TypedArray | TypedArray[])[]): void;
36
+ export declare function getMask(bits: number): number;
37
+ export {};
38
+ //# sourceMappingURL=utils.d.ts.map
package/utils.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["src/utils.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,OAAO,EAAE,KAAK,IAAI,MAAM,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,WAAW,IAAI,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAEvE,eAAO,MAAM,WAAW,eAAS,CAAC;AAClC,eAAO,MAAM,WAAW,cAAQ,CAAC;AAGjC,wBAAgB,UAAU,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,WAKtD;AAED,MAAM,MAAM,MAAM,GAAG;IACnB,aAAa,EAAE,MAAM,CAAC;IACtB,MAAM,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK;QAC5B,SAAS,EAAE,UAAU,CAAC;QACtB,SAAS,EAAE,UAAU,CAAC;KACvB,CAAC;IACF,IAAI,EAAE,CAAC,SAAS,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,CAAC,EAAE,UAAU,KAAK,UAAU,CAAC;IAClF,MAAM,EAAE,CAAC,SAAS,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,KAAK,OAAO,CAAC;CAC9E,CAAC;AAEF,MAAM,WAAW,KAAK,CAAC,CAAC,EAAE,CAAC;IACzB,MAAM,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC;IACnB,MAAM,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC;CAClB;AAED,MAAM,WAAW,UAAU,CAAC,CAAC,CAAE,SAAQ,KAAK,CAAC,CAAC,EAAE,UAAU,CAAC;IACzD,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,UAAU,CAAC;IAChC,MAAM,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,CAAC,CAAC;CAClC;AAED,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,GAAG;IAAE,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC;AAGpE,KAAK,OAAO,CAAC,CAAC,IAAI,CAAC,SAAS,UAAU,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;AAC5D,KAAK,QAAQ,CAAC,CAAC,SAAS,CAAC,MAAM,GAAG,aAAa,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI;KACxD,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,GAAG,UAAU,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CACjE,CAAC;AACF,wBAAgB,UAAU,CAAC,CAAC,SAAS,CAAC,MAAM,GAAG,aAAa,CAAC,GAAG,CAAC,CAAC,EAAE,EAClE,GAAG,OAAO,EAAE,CAAC,GACZ,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,GAAG;IAAE,QAAQ,EAAE,MAAM,CAAA;CAAE,CA8BhD;AAED,wBAAgB,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,aAAa,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,aAAa,CAAC,CAAC,EAAE,CAAC,CAwBnF;AAGD,wBAAgB,UAAU,CAAC,GAAG,IAAI,EAAE,CAAC,UAAU,GAAG,UAAU,EAAE,CAAC,EAAE,QAKhE;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,UAEnC"}
package/utils.js ADDED
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getMask = exports.cleanBytes = exports.vecCoder = exports.splitCoder = exports.equalBytes = exports.randomBytes = exports.ensureBytes = void 0;
4
+ /*! noble-post-quantum - MIT License (c) 2024 Paul Miller (paulmillr.com) */
5
+ const _assert_1 = require("@noble/hashes/_assert");
6
+ const utils_1 = require("@noble/hashes/utils");
7
+ exports.ensureBytes = _assert_1.bytes;
8
+ exports.randomBytes = utils_1.randomBytes;
9
+ // Compares 2 u8a-s in kinda constant time
10
+ function equalBytes(a, b) {
11
+ if (a.length !== b.length)
12
+ return false;
13
+ let diff = 0;
14
+ for (let i = 0; i < a.length; i++)
15
+ diff |= a[i] ^ b[i];
16
+ return diff === 0;
17
+ }
18
+ exports.equalBytes = equalBytes;
19
+ function splitCoder(...lengths) {
20
+ const getLength = (c) => (typeof c === 'number' ? c : c.bytesLen);
21
+ const bytesLen = lengths.reduce((sum, a) => sum + getLength(a), 0);
22
+ return {
23
+ bytesLen,
24
+ encode: (bufs) => {
25
+ const res = new Uint8Array(bytesLen);
26
+ for (let i = 0, pos = 0; i < lengths.length; i++) {
27
+ const c = lengths[i];
28
+ const l = getLength(c);
29
+ const b = typeof c === 'number' ? bufs[i] : c.encode(bufs[i]);
30
+ (0, exports.ensureBytes)(b, l);
31
+ res.set(b, pos);
32
+ if (typeof c !== 'number')
33
+ b.fill(0); // clean
34
+ pos += l;
35
+ }
36
+ return res;
37
+ },
38
+ decode: (buf) => {
39
+ (0, exports.ensureBytes)(buf, bytesLen);
40
+ const res = [];
41
+ for (const c of lengths) {
42
+ const l = getLength(c);
43
+ const b = buf.subarray(0, l);
44
+ res.push(typeof c === 'number' ? b : c.decode(b));
45
+ buf = buf.subarray(l);
46
+ }
47
+ return res;
48
+ },
49
+ };
50
+ }
51
+ exports.splitCoder = splitCoder;
52
+ // nano-packed.array (fixed size)
53
+ function vecCoder(c, vecLen) {
54
+ const bytesLen = vecLen * c.bytesLen;
55
+ return {
56
+ bytesLen,
57
+ encode: (u) => {
58
+ if (u.length !== vecLen)
59
+ throw new Error(`vecCoder.encode: wrong length=${u.length}. Expected: ${vecLen}`);
60
+ const res = new Uint8Array(bytesLen);
61
+ for (let i = 0, pos = 0; i < u.length; i++) {
62
+ const b = c.encode(u[i]);
63
+ res.set(b, pos);
64
+ b.fill(0); // clean
65
+ pos += b.length;
66
+ }
67
+ return res;
68
+ },
69
+ decode: (a) => {
70
+ (0, exports.ensureBytes)(a, bytesLen);
71
+ const r = [];
72
+ for (let i = 0; i < a.length; i += c.bytesLen)
73
+ r.push(c.decode(a.subarray(i, i + c.bytesLen)));
74
+ return r;
75
+ },
76
+ };
77
+ }
78
+ exports.vecCoder = vecCoder;
79
+ // cleanBytes(new Uint8Array(), [new Uint16Array(), new Uint32Array()])
80
+ function cleanBytes(...list) {
81
+ for (const t of list) {
82
+ if (Array.isArray(t))
83
+ for (const b of t)
84
+ b.fill(0);
85
+ else
86
+ t.fill(0);
87
+ }
88
+ }
89
+ exports.cleanBytes = cleanBytes;
90
+ function getMask(bits) {
91
+ return (1 << bits) - 1; // 4 -> 0b1111
92
+ }
93
+ exports.getMask = getMask;
94
+ //# sourceMappingURL=utils.js.map
package/utils.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["src/utils.ts"],"names":[],"mappings":";;;AAAA,4EAA4E;AAC5E,mDAAwD;AACxD,+CAAuE;AAE1D,QAAA,WAAW,GAAG,eAAM,CAAC;AACrB,QAAA,WAAW,GAAG,mBAAK,CAAC;AAEjC,0CAA0C;AAC1C,SAAgB,UAAU,CAAC,CAAa,EAAE,CAAa;IACrD,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IACxC,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE;QAAE,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IACvD,OAAO,IAAI,KAAK,CAAC,CAAC;AACpB,CAAC;AALD,gCAKC;AA6BD,SAAgB,UAAU,C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