Skip to main content
@tevm/voltaire
@tevm/voltaire / crypto/ChaCha20Poly1305

crypto/ChaCha20Poly1305

Classes

ChaCha20Poly1305Error

Defined in: src/crypto/ChaCha20Poly1305/errors.js:21 Base error for ChaCha20-Poly1305 operations

See

https://voltaire.tevm.sh/crypto/chacha20poly1305 for crypto documentation

Since

0.0.0

Throws

Example

import { ChaCha20Poly1305Error } from './crypto/ChaCha20Poly1305/index.js';
throw new ChaCha20Poly1305Error('Operation failed', {
  code: 'CHACHA20_POLY1305_ERROR',
  context: { operation: 'encrypt' },
  docsPath: '/crypto/chacha20poly1305#error-handling',
  cause: originalError
});

Extends

Extended by

Constructors

Constructor
new ChaCha20Poly1305Error(message, options?): ChaCha20Poly1305Error
Defined in: src/crypto/ChaCha20Poly1305/errors.js:26
Parameters
message
string
options?
cause?
Error
code?
string
context?
Record<string, unknown>
docsPath?
string
Returns
ChaCha20Poly1305Error
Overrides
CryptoError.constructor

Properties

cause?
optional cause: Error
Defined in: src/primitives/errors/AbstractError.ts:56 Root cause of this error (for error chaining)
Inherited from
CryptoError.cause
code
code: string
Defined in: src/primitives/errors/AbstractError.ts:39 Machine-readable error code for programmatic handling
Example
'INVALID_FORMAT', 'INVALID_LENGTH'
Inherited from
CryptoError.code
context?
optional context: Record<string, unknown>
Defined in: src/primitives/errors/AbstractError.ts:45 Additional context metadata for debugging
Example
{ value: '0x123', expected: '20 bytes' }
Inherited from
CryptoError.context
docsPath?
optional docsPath: string
Defined in: src/primitives/errors/AbstractError.ts:51 Path to documentation for this error
Example
'/primitives/address/from-hex#error-handling'
Inherited from
CryptoError.docsPath
name
name: string
Defined in: src/crypto/ChaCha20Poly1305/errors.js:33
Inherited from
CryptoError.name

Methods

getErrorChain()
getErrorChain(): string
Defined in: src/primitives/errors/AbstractError.ts:94 Get full error chain as string for logging
Returns
string
Inherited from
CryptoError.getErrorChain
toJSON()
toJSON(): Record<string, unknown>
Defined in: src/primitives/errors/AbstractError.ts:110 Serialize error to JSON for logging/telemetry
Returns
Record<string, unknown>
Inherited from
CryptoError.toJSON

DecryptionError

Defined in: src/crypto/ChaCha20Poly1305/errors.js:118 Decryption failure error

See

https://voltaire.tevm.sh/crypto/chacha20poly1305 for crypto documentation

Since

0.0.0

Throws

Example

import { DecryptionError } from './crypto/ChaCha20Poly1305/index.js';
throw new DecryptionError('Authentication failed', {
  code: 'CHACHA20_POLY1305_DECRYPTION_FAILED',
  context: { operation: 'decrypt' },
  docsPath: '/crypto/chacha20poly1305#error-handling',
  cause: originalError
});

Extends

Constructors

Constructor
new DecryptionError(message, options?): DecryptionError
Defined in: src/crypto/ChaCha20Poly1305/errors.js:123
Parameters
message
string
options?
cause?
Error
code?
string
context?
Record<string, unknown>
docsPath?
string
Returns
DecryptionError
Overrides
ChaCha20Poly1305Error.constructor

Properties

cause?
optional cause: Error
Defined in: src/primitives/errors/AbstractError.ts:56 Root cause of this error (for error chaining)
Inherited from
ChaCha20Poly1305Error.cause
code
code: string
Defined in: src/primitives/errors/AbstractError.ts:39 Machine-readable error code for programmatic handling
Example
'INVALID_FORMAT', 'INVALID_LENGTH'
Inherited from
ChaCha20Poly1305Error.code
context?
optional context: Record<string, unknown>
Defined in: src/primitives/errors/AbstractError.ts:45 Additional context metadata for debugging
Example
{ value: '0x123', expected: '20 bytes' }
Inherited from
ChaCha20Poly1305Error.context
docsPath?
optional docsPath: string
Defined in: src/primitives/errors/AbstractError.ts:51 Path to documentation for this error
Example
'/primitives/address/from-hex#error-handling'
Inherited from
ChaCha20Poly1305Error.docsPath
name
name: string
Defined in: src/crypto/ChaCha20Poly1305/errors.js:130
Inherited from
ChaCha20Poly1305Error.name

Methods

getErrorChain()
getErrorChain(): string
Defined in: src/primitives/errors/AbstractError.ts:94 Get full error chain as string for logging
Returns
string
Inherited from
ChaCha20Poly1305Error.getErrorChain
toJSON()
toJSON(): Record<string, unknown>
Defined in: src/primitives/errors/AbstractError.ts:110 Serialize error to JSON for logging/telemetry
Returns
Record<string, unknown>
Inherited from
ChaCha20Poly1305Error.toJSON

InvalidKeyError

Defined in: src/crypto/ChaCha20Poly1305/errors.js:53 Invalid key error

See

https://voltaire.tevm.sh/crypto/chacha20poly1305 for crypto documentation

Since

0.0.0

Throws

Example

import { InvalidKeyError } from './crypto/ChaCha20Poly1305/index.js';
throw new InvalidKeyError('Invalid key size', {
  code: 'CHACHA20_POLY1305_INVALID_KEY_SIZE',
  context: { size: 16, expected: 32 },
  docsPath: '/crypto/chacha20poly1305#error-handling'
});

Extends

Constructors

Constructor
new InvalidKeyError(message, options?): InvalidKeyError
Defined in: src/crypto/ChaCha20Poly1305/errors.js:58
Parameters
message
string
options?
cause?
Error
code?
string
context?
Record<string, unknown>
docsPath?
string
Returns
InvalidKeyError
Overrides
ChaCha20Poly1305Error.constructor

Properties

cause?
optional cause: Error
Defined in: src/primitives/errors/AbstractError.ts:56 Root cause of this error (for error chaining)
Inherited from
ChaCha20Poly1305Error.cause
code
code: string
Defined in: src/primitives/errors/AbstractError.ts:39 Machine-readable error code for programmatic handling
Example
'INVALID_FORMAT', 'INVALID_LENGTH'
Inherited from
ChaCha20Poly1305Error.code
context?
optional context: Record<string, unknown>
Defined in: src/primitives/errors/AbstractError.ts:45 Additional context metadata for debugging
Example
{ value: '0x123', expected: '20 bytes' }
Inherited from
ChaCha20Poly1305Error.context
docsPath?
optional docsPath: string
Defined in: src/primitives/errors/AbstractError.ts:51 Path to documentation for this error
Example
'/primitives/address/from-hex#error-handling'
Inherited from
ChaCha20Poly1305Error.docsPath
name
name: string
Defined in: src/crypto/ChaCha20Poly1305/errors.js:65
Inherited from
ChaCha20Poly1305Error.name

Methods

getErrorChain()
getErrorChain(): string
Defined in: src/primitives/errors/AbstractError.ts:94 Get full error chain as string for logging
Returns
string
Inherited from
ChaCha20Poly1305Error.getErrorChain
toJSON()
toJSON(): Record<string, unknown>
Defined in: src/primitives/errors/AbstractError.ts:110 Serialize error to JSON for logging/telemetry
Returns
Record<string, unknown>
Inherited from
ChaCha20Poly1305Error.toJSON

InvalidNonceError

Defined in: src/crypto/ChaCha20Poly1305/errors.js:85 Invalid nonce error

See

https://voltaire.tevm.sh/crypto/chacha20poly1305 for crypto documentation

Since

0.0.0

Throws

Example

import { InvalidNonceError } from './crypto/ChaCha20Poly1305/index.js';
throw new InvalidNonceError('Nonce must be 12 bytes', {
  code: 'CHACHA20_POLY1305_INVALID_NONCE_LENGTH',
  context: { length: 8, expected: 12 },
  docsPath: '/crypto/chacha20poly1305#error-handling'
});

Extends

Constructors

Constructor
new InvalidNonceError(message, options?): InvalidNonceError
Defined in: src/crypto/ChaCha20Poly1305/errors.js:90
Parameters
message
string
options?
cause?
Error
code?
string
context?
Record<string, unknown>
docsPath?
string
Returns
InvalidNonceError
Overrides
ChaCha20Poly1305Error.constructor

Properties

cause?
optional cause: Error
Defined in: src/primitives/errors/AbstractError.ts:56 Root cause of this error (for error chaining)
Inherited from
ChaCha20Poly1305Error.cause
code
code: string
Defined in: src/primitives/errors/AbstractError.ts:39 Machine-readable error code for programmatic handling
Example
'INVALID_FORMAT', 'INVALID_LENGTH'
Inherited from
ChaCha20Poly1305Error.code
context?
optional context: Record<string, unknown>
Defined in: src/primitives/errors/AbstractError.ts:45 Additional context metadata for debugging
Example
{ value: '0x123', expected: '20 bytes' }
Inherited from
ChaCha20Poly1305Error.context
docsPath?
optional docsPath: string
Defined in: src/primitives/errors/AbstractError.ts:51 Path to documentation for this error
Example
'/primitives/address/from-hex#error-handling'
Inherited from
ChaCha20Poly1305Error.docsPath
name
name: string
Defined in: src/crypto/ChaCha20Poly1305/errors.js:97
Inherited from
ChaCha20Poly1305Error.name

Methods

getErrorChain()
getErrorChain(): string
Defined in: src/primitives/errors/AbstractError.ts:94 Get full error chain as string for logging
Returns
string
Inherited from
ChaCha20Poly1305Error.getErrorChain
toJSON()
toJSON(): Record<string, unknown>
Defined in: src/primitives/errors/AbstractError.ts:110 Serialize error to JSON for logging/telemetry
Returns
Record<string, unknown>
Inherited from
ChaCha20Poly1305Error.toJSON

Variables

ChaCha20Poly1305

const ChaCha20Poly1305: object
Defined in: src/crypto/ChaCha20Poly1305/ChaCha20Poly1305.js:39 ChaCha20-Poly1305 Authenticated Encryption (RFC 8439) Provides authenticated encryption using ChaCha20 stream cipher with Poly1305 MAC. Optimized for software implementations. Uses @noble/ciphers for cryptographic operations.

Type Declaration

decrypt()
decrypt: (ciphertext, key, nonce, additionalData?) => Uint8Array<ArrayBufferLike>
Decrypt data with ChaCha20-Poly1305
Parameters
ciphertext
Uint8Array<ArrayBufferLike> Encrypted data with authentication tag
key
Uint8Array<ArrayBufferLike> 32-byte key (256 bits)
nonce
Uint8Array<ArrayBufferLike> 12-byte nonce (96 bits) used during encryption
additionalData?
Uint8Array<ArrayBufferLike> Optional additional authenticated data
Returns
Uint8Array<ArrayBufferLike> Decrypted plaintext
See
https://voltaire.tevm.sh/crypto/chacha20poly1305 for crypto documentation
Since
0.0.0
Throws
If key is not 32 bytes
Throws
If nonce is not 12 bytes
Throws
If authentication fails or decryption error
Example
import * as ChaCha20Poly1305 from './crypto/ChaCha20Poly1305/index.js';
const key = ChaCha20Poly1305.generateKey();
const nonce = ChaCha20Poly1305.generateNonce();
const decrypted = ChaCha20Poly1305.decrypt(ciphertext, key, nonce);
const message = new TextDecoder().decode(decrypted);
encrypt()
encrypt: (plaintext, key, nonce, additionalData?) => Uint8Array<ArrayBufferLike>
Encrypt data with ChaCha20-Poly1305
Parameters
plaintext
Uint8Array<ArrayBufferLike> Data to encrypt
key
Uint8Array<ArrayBufferLike> 32-byte key (256 bits)
nonce
Uint8Array<ArrayBufferLike> 12-byte nonce (96 bits)
additionalData?
Uint8Array<ArrayBufferLike> Optional additional authenticated data
Returns
Uint8Array<ArrayBufferLike> Ciphertext with authentication tag appended (16 bytes)
See
https://voltaire.tevm.sh/crypto/chacha20poly1305 for crypto documentation
Since
0.0.0
Throws
If key is not 32 bytes
Throws
If nonce is not 12 bytes
Throws
If encryption fails
Example
import * as ChaCha20Poly1305 from './crypto/ChaCha20Poly1305/index.js';
const plaintext = new TextEncoder().encode('Secret message');
const key = ChaCha20Poly1305.generateKey();
const nonce = ChaCha20Poly1305.generateNonce();
const ciphertext = ChaCha20Poly1305.encrypt(plaintext, key, nonce);
generateKey()
generateKey: () => Uint8Array<ArrayBufferLike>
Generate random ChaCha20-Poly1305 key
Returns
Uint8Array<ArrayBufferLike> 32-byte random key (256 bits)
See
https://voltaire.tevm.sh/crypto/chacha20poly1305 for crypto documentation
Since
0.0.0
Throws
Example
import * as ChaCha20Poly1305 from './crypto/ChaCha20Poly1305/index.js';
const key = ChaCha20Poly1305.generateKey();
console.log(key.length); // 32
generateNonce()
generateNonce: () => Uint8Array<ArrayBufferLike>
Generate random nonce
Returns
Uint8Array<ArrayBufferLike> 12-byte random nonce (96 bits)
See
https://voltaire.tevm.sh/crypto/chacha20poly1305 for crypto documentation
Since
0.0.0
Throws
Example
import * as ChaCha20Poly1305 from './crypto/ChaCha20Poly1305/index.js';
const nonce = ChaCha20Poly1305.generateNonce();
console.log(nonce.length); // 12
KEY_SIZE
KEY_SIZE: number
ChaCha20-Poly1305 key size in bytes (256 bits)
See
https://voltaire.tevm.sh/crypto/chacha20poly1305 for crypto documentation
Since
0.0.0
Throws
Example
import * as ChaCha20Poly1305 from './crypto/ChaCha20Poly1305/index.js';
const keyBytes = new Uint8Array(ChaCha20Poly1305.KEY_SIZE);
NONCE_SIZE
NONCE_SIZE: number
Nonce/IV size in bytes (96 bits)
See
https://voltaire.tevm.sh/crypto/chacha20poly1305 for crypto documentation
Since
0.0.0
Throws
Example
import * as ChaCha20Poly1305 from './crypto/ChaCha20Poly1305/index.js';
const nonce = new Uint8Array(ChaCha20Poly1305.NONCE_SIZE);
TAG_SIZE
TAG_SIZE: number
Authentication tag size in bytes (128 bits)
See
https://voltaire.tevm.sh/crypto/chacha20poly1305 for crypto documentation
Since
0.0.0
Throws
Example
import * as ChaCha20Poly1305 from './crypto/ChaCha20Poly1305/index.js';
console.log('Tag size:', ChaCha20Poly1305.TAG_SIZE);

Example

import { ChaCha20Poly1305 } from './ChaCha20Poly1305.js';

// Generate key
const key = ChaCha20Poly1305.generateKey();

// Encrypt data
const plaintext = new TextEncoder().encode('Hello, world!');
const nonce = ChaCha20Poly1305.generateNonce();
const ciphertext = ChaCha20Poly1305.encrypt(plaintext, key, nonce);

// Decrypt data
const decrypted = ChaCha20Poly1305.decrypt(ciphertext, key, nonce);

KEY_SIZE

const KEY_SIZE: 32 = 32
Defined in: src/crypto/ChaCha20Poly1305/constants.js:13 ChaCha20-Poly1305 key size in bytes (256 bits)

See

https://voltaire.tevm.sh/crypto/chacha20poly1305 for crypto documentation

Since

0.0.0

Throws

Example

import * as ChaCha20Poly1305 from './crypto/ChaCha20Poly1305/index.js';
const keyBytes = new Uint8Array(ChaCha20Poly1305.KEY_SIZE);

NONCE_SIZE

const NONCE_SIZE: 12 = 12
Defined in: src/crypto/ChaCha20Poly1305/constants.js:27 Nonce/IV size in bytes (96 bits)

See

https://voltaire.tevm.sh/crypto/chacha20poly1305 for crypto documentation

Since

0.0.0

Throws

Example

import * as ChaCha20Poly1305 from './crypto/ChaCha20Poly1305/index.js';
const nonce = new Uint8Array(ChaCha20Poly1305.NONCE_SIZE);

TAG_SIZE

const TAG_SIZE: 16 = 16
Defined in: src/crypto/ChaCha20Poly1305/constants.js:41 Authentication tag size in bytes (128 bits)

See

https://voltaire.tevm.sh/crypto/chacha20poly1305 for crypto documentation

Since

0.0.0

Throws

Example

import * as ChaCha20Poly1305 from './crypto/ChaCha20Poly1305/index.js';
console.log('Tag size:', ChaCha20Poly1305.TAG_SIZE);

Functions

decrypt()

decrypt(ciphertext, key, nonce, additionalData?): Uint8Array<ArrayBufferLike>
Defined in: src/crypto/ChaCha20Poly1305/decrypt.js:31 Decrypt data with ChaCha20-Poly1305

Parameters

ciphertext
Uint8Array<ArrayBufferLike> Encrypted data with authentication tag
key
Uint8Array<ArrayBufferLike> 32-byte key (256 bits)
nonce
Uint8Array<ArrayBufferLike> 12-byte nonce (96 bits) used during encryption
additionalData?
Uint8Array<ArrayBufferLike> Optional additional authenticated data

Returns

Uint8Array<ArrayBufferLike> Decrypted plaintext

See

https://voltaire.tevm.sh/crypto/chacha20poly1305 for crypto documentation

Since

0.0.0

Throws

If key is not 32 bytes

Throws

If nonce is not 12 bytes

Throws

If authentication fails or decryption error

Example

import * as ChaCha20Poly1305 from './crypto/ChaCha20Poly1305/index.js';
const key = ChaCha20Poly1305.generateKey();
const nonce = ChaCha20Poly1305.generateNonce();
const decrypted = ChaCha20Poly1305.decrypt(ciphertext, key, nonce);
const message = new TextDecoder().decode(decrypted);

encrypt()

encrypt(plaintext, key, nonce, additionalData?): Uint8Array<ArrayBufferLike>
Defined in: src/crypto/ChaCha20Poly1305/encrypt.js:31 Encrypt data with ChaCha20-Poly1305

Parameters

plaintext
Uint8Array<ArrayBufferLike> Data to encrypt
key
Uint8Array<ArrayBufferLike> 32-byte key (256 bits)
nonce
Uint8Array<ArrayBufferLike> 12-byte nonce (96 bits)
additionalData?
Uint8Array<ArrayBufferLike> Optional additional authenticated data

Returns

Uint8Array<ArrayBufferLike> Ciphertext with authentication tag appended (16 bytes)

See

https://voltaire.tevm.sh/crypto/chacha20poly1305 for crypto documentation

Since

0.0.0

Throws

If key is not 32 bytes

Throws

If nonce is not 12 bytes

Throws

If encryption fails

Example

import * as ChaCha20Poly1305 from './crypto/ChaCha20Poly1305/index.js';
const plaintext = new TextEncoder().encode('Secret message');
const key = ChaCha20Poly1305.generateKey();
const nonce = ChaCha20Poly1305.generateNonce();
const ciphertext = ChaCha20Poly1305.encrypt(plaintext, key, nonce);

generateKey()

generateKey(): Uint8Array<ArrayBufferLike>
Defined in: src/crypto/ChaCha20Poly1305/generateKey.js:17 Generate random ChaCha20-Poly1305 key

Returns

Uint8Array<ArrayBufferLike> 32-byte random key (256 bits)

See

https://voltaire.tevm.sh/crypto/chacha20poly1305 for crypto documentation

Since

0.0.0

Throws

Example

import * as ChaCha20Poly1305 from './crypto/ChaCha20Poly1305/index.js';
const key = ChaCha20Poly1305.generateKey();
console.log(key.length); // 32

generateNonce()

generateNonce(): Uint8Array<ArrayBufferLike>
Defined in: src/crypto/ChaCha20Poly1305/generateNonce.js:17 Generate random nonce

Returns

Uint8Array<ArrayBufferLike> 12-byte random nonce (96 bits)

See

https://voltaire.tevm.sh/crypto/chacha20poly1305 for crypto documentation

Since

0.0.0

Throws

Example

import * as ChaCha20Poly1305 from './crypto/ChaCha20Poly1305/index.js';
const nonce = ChaCha20Poly1305.generateNonce();
console.log(nonce.length); // 12