Skip to main content
Version: v1.2

Crypto Module

Table of contents

Classes

Interfaces

Type Aliases

Variables

Functions

Type Aliases

Ciphertext

Ƭ Ciphertext<N>: N[]

Type parameters

NameType
Nbigint

Defined in

crypto/ts/types.ts:21


EcdhSharedKey

Ƭ EcdhSharedKey<N>: [N, N]

Type parameters

NameType
Nbigint

Defined in

crypto/ts/types.ts:12


Leaf

Ƭ Leaf: bigint

Defined in

crypto/ts/types.ts:64


PathElements

Ƭ PathElements: bigint[][]

Defined in

crypto/ts/types.ts:24


Plaintext

Ƭ Plaintext<N>: N[]

Type parameters

NameType
Nbigint

Defined in

crypto/ts/types.ts:18


Point

Ƭ Point<N>: [N, N]

Type parameters

NameType
NSnarkBigNumber

Defined in

crypto/ts/types.ts:15


PrivKey

Ƭ PrivKey: SnarkBigNumber

Defined in

crypto/ts/types.ts:6


PubKey

Ƭ PubKey<N>: [N, N]

Type parameters

NameType
Nbigint

Defined in

crypto/ts/types.ts:9

Variables

NOTHING_UP_MY_SLEEVE

Const NOTHING_UP_MY_SLEEVE: bigint

Defined in

crypto/ts/constants.ts:10


SNARK_FIELD_SIZE

Const SNARK_FIELD_SIZE: bigint = r

Defined in

crypto/ts/constants.ts:6

Functions

bigInt2Buffer

bigInt2Buffer(i): Buffer

Convert a BigInt to a Buffer

Parameters

NameTypeDescription
ibigintthe bigint to convert

Returns

Buffer

the buffer

Defined in

crypto/ts/bigIntUtils.ts:127


calcDepthFromNumLeaves

calcDepthFromNumLeaves(hashLength, numLeaves): number

Calculate the depth of a tree given the number of leaves

Parameters

NameTypeDescription
hashLengthnumberthe hashing function param length
numLeavesnumberhow many leaves

Returns

number

the depth

Defined in

crypto/ts/utils.ts:10


deepCopyBigIntArray

deepCopyBigIntArray(arr): bigint[]

Create a copy of a bigint array

Parameters

NameTypeDescription
arrbigint[]the array of bigints to copy

Returns

bigint[]

a deep copy of the array

Defined in

crypto/ts/bigIntUtils.ts:110


formatPrivKeyForBabyJub

formatPrivKeyForBabyJub(privKey): bigint

An internal function which formats a random private key to be compatible with the BabyJub curve. This is the format which should be passed into the PubKey and other circuits.

Parameters

NameTypeDescription
privKeySnarkBigNumberA private key generated using genPrivKey()

Returns

bigint

A BabyJub-compatible private key.

Defined in

crypto/ts/keys.ts:28


genEcdhSharedKey

genEcdhSharedKey(privKey, pubKey): EcdhSharedKey

Generates an Elliptic-Curve Diffie–Hellman (ECDH) shared key given a private key and a public key.

Parameters

NameTypeDescription
privKeySnarkBigNumberA private key generated using genPrivKey()
pubKeyPubKeyA public key generated using genPubKey()

Returns

EcdhSharedKey

The ECDH shared key.

Defined in

crypto/ts/keys.ts:76


genKeypair

genKeypair(): Keypair

Generates a keypair.

Returns

Keypair

a keypair

Defined in

crypto/ts/keys.ts:60


genPrivKey

genPrivKey(): bigint

Generate a private key

Returns

bigint

A random seed for a private key.

Defined in

crypto/ts/keys.ts:13


genPubKey

genPubKey(privKey): PubKey

Parameters

NameTypeDescription
privKeySnarkBigNumberA private key generated using genPrivKey()

Returns

PubKey

A public key associated with the private key

Defined in

crypto/ts/keys.ts:51


genRandomBabyJubValue

genRandomBabyJubValue(): bigint

Returns a BabyJub-compatible random value. We create it by first generating a random value (initially 256 bits large) modulo the snark field size as described in EIP197. This results in a key size of roughly 253 bits and no more than 254 bits. To prevent modulo bias, we then use this efficient algorithm: http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/lib/libc/crypt/arc4random_uniform.c

Returns

bigint

A BabyJub-compatible random value.

Defined in

crypto/ts/babyjub.ts:115


genRandomSalt

genRandomSalt(): bigint

Generate a random value

Returns

bigint

A BabyJub-compatible salt.

Defined in

crypto/ts/keys.ts:19


genTreeCommitment

genTreeCommitment(leaves, salt, depth): bigint

A helper function which hashes a list of results with a salt and returns the hash.

Parameters

NameTypeDescription
leavesbigint[]A list of values
saltbigintA random salt
depthnumberThe tree depth

Returns

bigint

The hash of the leaves and the salt, with the salt last

Defined in

crypto/ts/utils.ts:30


genTreeProof

genTreeProof(index, leaves, depth): bigint[][]

A helper function to generate the tree proof for the value at the given index in the leaves

Parameters

NameTypeDescription
indexnumberThe index of the value to generate the proof for
leavesbigint[]A list of values
depthnumberThe tree depth

Returns

bigint[][]

The proof

Defined in

crypto/ts/utils.ts:47


hash13

hash13(elements): bigint

A convenience function to use Poseidon to hash a Plaintext with no more than 13 elements

Parameters

NameTypeDescription
elementsPlaintextThe elements to hash

Returns

bigint

The hash of the elements

Defined in

crypto/ts/hashing.ts:130


hash2

hash2(elements): bigint

Parameters

NameType
elementsPlaintext

Returns

bigint

Defined in

crypto/ts/hashing.ts:119


hash3

hash3(elements): bigint

Parameters

NameType
elementsPlaintext

Returns

bigint

Defined in

crypto/ts/hashing.ts:120


hash4

hash4(elements): bigint

Parameters

NameType
elementsPlaintext

Returns

bigint

Defined in

crypto/ts/hashing.ts:121


hash5

hash5(elements): bigint

Parameters

NameType
elementsPlaintext

Returns

bigint

Defined in

crypto/ts/hashing.ts:122


hashLeftRight

hashLeftRight(left, right): bigint

Hash two BigInts with the Poseidon hash function

Parameters

NameTypeDescription
leftbigintThe left-hand element to hash
rightbigintThe right-hand element to hash

Returns

bigint

The hash of the two elements

Defined in

crypto/ts/hashing.ts:85


hashN

hashN(numElements, elements): bigint

Hash up to N elements

Parameters

NameTypeDescription
numElementsnumberThe number of elements to hash
elementsPlaintextThe elements to hash

Returns

bigint

The hash of the elements

Defined in

crypto/ts/hashing.ts:101


hashOne

hashOne(preImage): bigint

Hash a single BigInt with the Poseidon hash function

Parameters

NameTypeDescription
preImagebigintThe element to hash

Returns

bigint

The hash of the element

Defined in

crypto/ts/hashing.ts:160


packPubKey

packPubKey(pubKey): bigint

Losslessly reduces the size of the representation of a public key

Parameters

NameTypeDescription
pubKeyPubKeyThe public key to pack

Returns

bigint

A packed public key

Defined in

crypto/ts/keys.ts:35


poseidonDecrypt

poseidonDecrypt(ciphertext, key, nonce, length): PlainText<bigint>

Decrypt some ciphertext using poseidon encryption

Parameters

NameTypeDescription
ciphertextCipherText<bigint>the ciphertext to decrypt
keyEncryptionKey<bigint>the key to decrypt with
noncebigintthe nonce used to encrypt
lengthnumberthe length of the plaintext

Returns

PlainText<bigint>

the plaintext

Defined in

node_modules/.pnpm/@[email protected]/node_modules/@zk-kit/poseidon-cipher/dist/types/poseidonCipher.d.ts:18


poseidonDecryptWithoutCheck

poseidonDecryptWithoutCheck(ciphertext, key, nonce, length): PlainText<bigint>

Decrypt some ciphertext using poseidon encryption

Parameters

NameTypeDescription
ciphertextCipherText<bigint>the ciphertext to decrypt
keyEncryptionKey<bigint>the key to decrypt with
noncebigintthe nonce used to encrypt
lengthnumberthe length of the plaintext

Returns

PlainText<bigint>

the plaintext

Dev

Do not throw if the plaintext is invalid

Defined in

node_modules/.pnpm/@[email protected]/node_modules/@zk-kit/poseidon-cipher/dist/types/poseidonCipher.d.ts:28


poseidonEncrypt

poseidonEncrypt(msg, key, nonce): CipherText<bigint>

Encrypt some plaintext using poseidon encryption

Parameters

NameTypeDescription
msgPlainText<bigint>the message to encrypt
keyEncryptionKey<bigint>the key to encrypt with
noncebigintthe nonce to avoid replay attacks

Returns

CipherText<bigint>

the ciphertext

Defined in

node_modules/.pnpm/@[email protected]/node_modules/@zk-kit/poseidon-cipher/dist/types/poseidonCipher.d.ts:9


sha256Hash

sha256Hash(input): bigint

Hash an array of uint256 values the same way that the EVM does.

Parameters

NameTypeDescription
inputbigint[]the array of values to hash

Returns

bigint

a EVM compatible sha256 hash

Defined in

crypto/ts/hashing.ts:15


sign

sign(privateKey, message): Signature<string>

Signs a message using the provided private key, employing Poseidon hashing and EdDSA with the Baby Jubjub elliptic curve.

Parameters

NameTypeDescription
privateKeyBigNumberishThe private key used to sign the message.
messageBigNumberishThe message to be signed.

Returns

Signature<string>

The signature object, containing properties relevant to EdDSA signatures, such as 'R8' and 'S' values.

Defined in

node_modules/.pnpm/@[email protected]/node_modules/@zk-kit/eddsa-poseidon/dist/types/eddsa-poseidon.d.ts:32


stringifyBigInts

stringifyBigInts(input): StringifiedBigInts

Given an input of bigint values, convert them to their string representations

Parameters

NameTypeDescription
inputBigIntVariantsThe input to convert

Returns

StringifiedBigInts

The input with bigint values converted to string

Defined in

crypto/ts/bigIntUtils.ts:78


unpackPubKey

unpackPubKey(packed): PubKey

Restores the original PubKey from its packed representation

Parameters

NameTypeDescription
packedbigintThe value to unpack

Returns

PubKey

The unpacked public key

Defined in

crypto/ts/keys.ts:42


unstringifyBigInts

unstringifyBigInts(input): BigIntVariants

Given an input containing string values, convert them to bigint

Parameters

NameTypeDescription
inputStringifiedBigIntsThe input to convert

Returns

BigIntVariants

the input with string values converted to bigint

Defined in

crypto/ts/bigIntUtils.ts:9


verifySignature

verifySignature(message, signature, publicKey): boolean

Verifies an EdDSA signature using the Baby Jubjub elliptic curve and Poseidon hash function.

Parameters

NameTypeDescription
messageBigNumberishThe original message that was be signed.
signatureSignatureThe EdDSA signature to be verified.
publicKeyPointThe public key associated with the private key used to sign the message.

Returns

boolean

Returns true if the signature is valid and corresponds to the message and public key, false otherwise.

Defined in

node_modules/.pnpm/@[email protected]/node_modules/@zk-kit/eddsa-poseidon/dist/types/eddsa-poseidon.d.ts:40