cloudflare / voprf-ts

A TypeScript library for Oblivious Pseudorandom Functions
BSD 3-Clause "New" or "Revised" License
28 stars 12 forks source link

refactor: non singleton CryptoProvider #48

Closed sublimator closed 1 year ago

sublimator commented 1 year ago

Taking #47 further Facade supporting more than one CryptoProvider per process

( More cheap AI writing ... )

Section: Decoupling CryptoProvider for Greater Flexibility

Introduction to the Change

In a recent update, the internal architecture of the library has seen a significant shift. Previously, a singleton pattern was used for the CryptoProvider via the CryptoImpl class, which was responsible for crypto-related functionalities. Now, the CryptoProvider is passed explicitly to functions and methods that require cryptographic operations, effectively decoupling it from the CryptoImpl class.

Here's a brief overview of what has changed:

  1. The CryptoImpl class has been removed, and its responsibilities are now passed directly to the CryptoProvider.
  2. All key functions and methods that relied on CryptoImpl now require an explicit CryptoProvider argument.
  3. The Oprf class no longer includes a getGroup method tied to CryptoImpl; it's replaced by a function that takes a CryptoProvider.

The Code Changes

The changes in the code reflect this new design philosophy. For example, the getKeySizes function previously had an optional crypto parameter that defaulted to CryptoImpl.provider. Now, it requires an explicit CryptoProvider:

- export function getKeySizes(id: SuiteID, crypto = CryptoImpl.provider): { Nsk: number; Npk: number }
+ export function getKeySizes(id: SuiteID, crypto: CryptoProvider): { Nsk: number; Npk: number }

Similarly, functions like validatePrivateKey, validatePublicKey, randomPrivateKey, and derivePrivateKey now explicitly require a CryptoProvider:

- export async function randomPrivateKey(id: SuiteID, crypto: CryptoProvider = CryptoImpl): Promise<Uint8Array>
+ export async function randomPrivateKey(id: SuiteID, crypto: CryptoProvider): Promise<Uint8Array>

Benefits of the Change

  1. Simplifies Codebase: This approach simplifies the codebase by removing the need for a separate CryptoImpl class to manage the crypto provider.

  2. Enhanced Flexibility: By passing the CryptoProvider explicitly, the internal code now has more flexibility in choosing or even changing the cryptographic backend.

  3. Easier Testing: With the crypto functionalities decoupled from a singleton class, it becomes easier to write unit tests that mock the CryptoProvider.

  4. Forward Compatibility: This change is a step toward supporting multiple crypto providers in a single application without risking inconsistencies.

Considerations

While this approach enhances flexibility, it also makes it the developer's responsibility to manage the CryptoProvider instances correctly. This is not an issue for the facade API, which will serve as the public API and handle these concerns transparently.

Conclusion

By removing the CryptoImpl class and requiring an explicit CryptoProvider, the internal code is now both more flexible and easier to manage. This change is particularly beneficial for future extensions and for developers who wish to use different crypto providers for different tasks.