@pinkparrot/qsafe-mayo-wasm 0.0.13 → 0.0.14
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/package.json +1 -1
- package/dist/mayo_api.browser.js +0 -135
package/package.json
CHANGED
package/dist/mayo_api.browser.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import Mayo1Module from './mayo1.cjs';
|
|
2
|
-
import Mayo2Module from './mayo2.cjs';
|
|
3
|
-
|
|
4
|
-
/** @typedef {{ secretKey: Uint8Array, publicKey: Uint8Array }} Keypair */
|
|
5
|
-
|
|
6
|
-
const SIZES = {
|
|
7
|
-
mayo1: { secretKeySize: 24, publicKeySize: 1420, signatureSize: 454 },
|
|
8
|
-
mayo2: { secretKeySize: 24, publicKeySize: 4912, signatureSize: 186 },
|
|
9
|
-
}; // Note: if we want to use MAYO5 we needs at least 40 bytes of seed.
|
|
10
|
-
|
|
11
|
-
function alloc(m, size) {
|
|
12
|
-
const ptr = m._malloc(size);
|
|
13
|
-
if (ptr === 0) throw new Error(`malloc failed (${size} bytes)`);
|
|
14
|
-
return ptr;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export class MayoSigner {
|
|
18
|
-
#m = null;
|
|
19
|
-
#secretKey = null;
|
|
20
|
-
#secretKeySize; #publicKeySize; #signatureSize;
|
|
21
|
-
|
|
22
|
-
/** @param {string} [variant] Default: 'mayo1' */
|
|
23
|
-
constructor(variant = 'mayo1') {
|
|
24
|
-
if (variant !== 'mayo1' && variant !== 'mayo2') throw new Error(`Unsupported MAYO variant: ${variant}`);
|
|
25
|
-
this.variant = variant;
|
|
26
|
-
({ secretKeySize: this.#secretKeySize,
|
|
27
|
-
publicKeySize: this.#publicKeySize,
|
|
28
|
-
signatureSize: this.#signatureSize } = SIZES[variant]);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/** Factory — preferred way to instantiate.
|
|
32
|
-
* @param {string} [variant] Default: 'mayo1' */
|
|
33
|
-
static async create(variant = 'mayo1') {
|
|
34
|
-
const instance = new MayoSigner(variant);
|
|
35
|
-
await instance.init();
|
|
36
|
-
return instance;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** Loads the WASM module. Must be called before any crypto operation if not using create(). */
|
|
40
|
-
async init() {
|
|
41
|
-
if (this.#m) return;
|
|
42
|
-
if (this.variant === 'mayo1') this.#m = await Mayo1Module();
|
|
43
|
-
if (this.variant === 'mayo2') this.#m = await Mayo2Module();
|
|
44
|
-
throw new Error(`Unsupported MAYO variant: ${this.variant}`);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** Indicates whether the WASM module is loaded and ready for crypto operations. */
|
|
48
|
-
get ready() { return this.#m !== null; }
|
|
49
|
-
|
|
50
|
-
#assertReady() {
|
|
51
|
-
if (!this.#m) throw new Error('MayoSigner not initialized — call await init() first');
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/** Derives a keypair deterministically from a seed, and loads the secret key.
|
|
55
|
-
* @param {Uint8Array} seed - Must be exactly 24 bytes
|
|
56
|
-
* @param {boolean} storeSecretKey - Whether to store the secret key internally for subsequent sign() calls. Set to false if you only need to generate keypair.
|
|
57
|
-
* @returns {Keypair|null} null if key generation failed */
|
|
58
|
-
keypairFromSeed(seed, storeSecretKey = true) {
|
|
59
|
-
this.#assertReady();
|
|
60
|
-
if (!(seed instanceof Uint8Array) || seed.length !== this.#secretKeySize)
|
|
61
|
-
throw new TypeError(`seed must be a Uint8Array of ${this.#secretKeySize} bytes`);
|
|
62
|
-
|
|
63
|
-
const m = this.#m;
|
|
64
|
-
const seedPtr = alloc(m, seed.length);
|
|
65
|
-
const publicKeyPtr = alloc(m, this.#publicKeySize);
|
|
66
|
-
const secretKeyPtr = alloc(m, this.#secretKeySize);
|
|
67
|
-
|
|
68
|
-
m.HEAPU8.set(seed, seedPtr);
|
|
69
|
-
const ret = m._keypair_from_seed(seedPtr, publicKeyPtr, secretKeyPtr);
|
|
70
|
-
const keypair = ret === 0 ? {
|
|
71
|
-
publicKey: m.HEAPU8.slice(publicKeyPtr, publicKeyPtr + this.#publicKeySize),
|
|
72
|
-
secretKey: m.HEAPU8.slice(secretKeyPtr, secretKeyPtr + this.#secretKeySize),
|
|
73
|
-
} : null;
|
|
74
|
-
|
|
75
|
-
m._free(seedPtr); m._free(publicKeyPtr); m._free(secretKeyPtr);
|
|
76
|
-
|
|
77
|
-
if (keypair && storeSecretKey) this.#secretKey = keypair.secretKey;
|
|
78
|
-
return keypair;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** Loads a secret key for subsequent sign() calls.
|
|
82
|
-
* @param {Uint8Array} secretKey - Must be exactly 24 bytes */
|
|
83
|
-
loadSecretKey(secretKey) {
|
|
84
|
-
if (!(secretKey instanceof Uint8Array) || secretKey.length !== this.#secretKeySize)
|
|
85
|
-
throw new TypeError(`secretKey must be a Uint8Array of ${this.#secretKeySize} bytes`);
|
|
86
|
-
this.#secretKey = secretKey;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/** Signs a message using the loaded secret key.
|
|
90
|
-
* @param {Uint8Array} msg @returns {Uint8Array|null} signature, or null if signing failed */
|
|
91
|
-
sign(msg) {
|
|
92
|
-
this.#assertReady();
|
|
93
|
-
if (!this.#secretKey) throw new Error('No secret key loaded — call keypairFromSeed() or loadSecretKey() first');
|
|
94
|
-
if (!(msg instanceof Uint8Array)) throw new TypeError('msg must be a Uint8Array');
|
|
95
|
-
|
|
96
|
-
const m = this.#m;
|
|
97
|
-
const msgPtr = alloc(m, msg.length);
|
|
98
|
-
const secretKeyPtr = alloc(m, this.#secretKeySize);
|
|
99
|
-
const signaturePtr = alloc(m, this.#signatureSize);
|
|
100
|
-
const signatureLenPtr = alloc(m, 4); // size_t in WASM32
|
|
101
|
-
|
|
102
|
-
m.HEAPU8.set(msg, msgPtr);
|
|
103
|
-
m.HEAPU8.set(this.#secretKey, secretKeyPtr);
|
|
104
|
-
|
|
105
|
-
const ret = m._sign(msgPtr, msg.length, secretKeyPtr, signaturePtr, signatureLenPtr);
|
|
106
|
-
const result = ret === 0 ? m.HEAPU8.slice(signaturePtr, signaturePtr + this.#signatureSize) : null;
|
|
107
|
-
|
|
108
|
-
m._free(msgPtr); m._free(secretKeyPtr); m._free(signaturePtr); m._free(signatureLenPtr);
|
|
109
|
-
return result;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/** Verifies a signature against a message and public key.
|
|
113
|
-
* @param {Uint8Array} msg @param {Uint8Array} signature @param {Uint8Array} publicKey */
|
|
114
|
-
verify(msg, signature, publicKey) {
|
|
115
|
-
this.#assertReady();
|
|
116
|
-
if (!(msg instanceof Uint8Array)) throw new TypeError('msg must be a Uint8Array');
|
|
117
|
-
if (!(signature instanceof Uint8Array) || signature.length !== this.#signatureSize)
|
|
118
|
-
throw new TypeError(`signature must be a Uint8Array of ${this.#signatureSize} bytes`);
|
|
119
|
-
if (!(publicKey instanceof Uint8Array) || publicKey.length !== this.#publicKeySize)
|
|
120
|
-
throw new TypeError(`publicKey must be a Uint8Array of ${this.#publicKeySize} bytes`);
|
|
121
|
-
|
|
122
|
-
const m = this.#m;
|
|
123
|
-
const msgPtr = alloc(m, msg.length);
|
|
124
|
-
const signaturePtr = alloc(m, this.#signatureSize);
|
|
125
|
-
const publicKeyPtr = alloc(m, this.#publicKeySize);
|
|
126
|
-
|
|
127
|
-
m.HEAPU8.set(msg, msgPtr);
|
|
128
|
-
m.HEAPU8.set(signature, signaturePtr);
|
|
129
|
-
m.HEAPU8.set(publicKey, publicKeyPtr);
|
|
130
|
-
|
|
131
|
-
const ret = m._verify(msgPtr, msg.length, signaturePtr, publicKeyPtr);
|
|
132
|
-
m._free(msgPtr); m._free(signaturePtr); m._free(publicKeyPtr);
|
|
133
|
-
return ret === 0;
|
|
134
|
-
}
|
|
135
|
-
}
|