]> git.codecow.com Git - nano25519.git/commitdiff
Move mutex release into Mutex object for clarity of intent.
authorChris Duncan <chris@codecow.com>
Thu, 1 Oct 2026 00:23:01 +0000 (17:23 -0700)
committerChris Duncan <chris@codecow.com>
Thu, 1 Oct 2026 00:23:01 +0000 (17:23 -0700)
src/lib/derive.ts
src/lib/sign.ts
src/lib/verify.ts
src/lib/wasm.ts

index 160fa9b986a5aa9654caf96b95f36d29ab04ebaf..3a40eae2bdfb0fa808ce46afa387af593e24e2c0 100644 (file)
@@ -2,7 +2,7 @@
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
 import { allocate, byteLength, bytesToHex, copy, fill, isBytes, normalize } from './primordials'
-import { clearMemory, constants, getOutput, Mutex, setInput, derive as wasm_derive, } from './wasm'
+import { Mutex, constants, getOutput, setInput, derive as wasm_derive, } from './wasm'
 
 const { KEY_BYTELENGTH, } = constants
 
@@ -28,6 +28,6 @@ export function derive (prv: unknown, out?: unknown): string | void {
        } finally {
                fill(privateKey, 0)
                fill(publicKey, 0)
-               clearMemory()
+               Mutex.unlock()
        }
 }
index 3f103080f0173beec8c825d9c26493d053914c11..ce11676e1b307535b3b00970a060cf003304e560 100644 (file)
@@ -2,7 +2,7 @@
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
 import { allocate, byteLength, bytesToHex, copy, fill, isBytes, normalize } from './primordials'
-import { Mutex, clearMemory, constants, getOutput, setInput, sign as wasm_sign } from './wasm'
+import { Mutex, constants, getOutput, setInput, sign as wasm_sign, } from './wasm'
 
 const { KEY_BYTELENGTH, MAX_MESSAGE_BYTELENGTH, SIGNATURE_BYTELENGTH, } = constants
 
@@ -33,6 +33,6 @@ export function sign (msg: unknown, prv: unknown, pub: unknown, out?: unknown):
                fill(privateKey, 0)
                fill(publicKey, 0)
                fill(signature, 0)
-               clearMemory()
+               Mutex.unlock()
        }
 }
index a0b9aed9509eb62b043d395be9b7f5739a32fa90..20eccd45ac755d040b245b00527e787a8df85c70 100644 (file)
@@ -2,7 +2,7 @@
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
 import { allocate, byteLength, copy, isArray, normalize } from './primordials'
-import { Mutex, clearMemory, constants, getOutput, setInput, verify as wasm_verify, verify_blocks as wasm_verify_blocks, } from './wasm'
+import { Mutex, constants, getOutput, setInput, verify as wasm_verify, verify_blocks as wasm_verify_blocks, } from './wasm'
 
 const { BLOCKHASH_BYTELENGTH, KEY_BYTELENGTH, MAX_MESSAGE_BYTELENGTH, MAX_VERIFY_BLOCKS, SIGNATURE_BYTELENGTH, } = constants
 
@@ -19,7 +19,7 @@ export function verify (sig: unknown, msg: unknown, pub: unknown): boolean {
                const verified = getOutput('verify', 0)
                return verified === 1
        } finally {
-               clearMemory()
+               Mutex.unlock()
        }
 }
 
@@ -62,6 +62,6 @@ export function verify_blocks (pub: unknown, data: unknown): boolean[] {
                }
                return verified
        } finally {
-               clearMemory()
+               Mutex.unlock()
        }
 }
index d986d309e0b8eef8a7b93f1fb9dfbfc37f9b0d02..9d5578eb851bd0503c5e43c90902c2f9cca634e5 100644 (file)
@@ -45,9 +45,21 @@ type Exports = WebAssembly.Instance['exports'] & {
 
 let locked = false
 export const Mutex = {
+       /**
+        * Sets a module-scoped variable to prevent reentry into the WASM module. This
+        * method *must* be called at the start of every `nano25519` API function.
+        */
        lock (): void {
                if (locked) throw new Nano25519TypeError('Failed to acquire mutex')
                locked = true
+       },
+       /**
+        * Tells the WASM module to clear its own I/O buffers internally, and then
+        * releases the mutex lock.
+        */
+       unlock (): void {
+               exports.clearMemory()
+               locked = false
        }
 }
 
@@ -286,15 +298,6 @@ function setInputSig (sig: Bytes): void {
        setInputSigEndian(s)
 }
 
-/**
- * Tells the WASM module to clear its own I/O buffers internally, and then
- * releases the mutex lock.
- */
-export function clearMemory (): void {
-       exports.clearMemory()
-       locked = false
-}
-
 export const constants = {
        BLOCKHASH_BYTELENGTH,
        KEY_BYTELENGTH,