Crypto Module
Table of contents
Classes
Interfaces
Type Aliases
Variables
Functions
- bigInt2Buffer
- calcDepthFromNumLeaves
- deepCopyBigIntArray
- formatPrivKeyForBabyJub
- genEcdhSharedKey
- genKeypair
- genPrivKey
- genPubKey
- genRandomBabyJubValue
- genRandomSalt
- genTreeCommitment
- genTreeProof
- hash13
- hash2
- hash3
- hash4
- hash5
- hashLeftRight
- hashN
- hashOne
- packPubKey
- poseidonDecrypt
- poseidonDecryptWithoutCheck
- poseidonEncrypt
- sha256Hash
- sign
- stringifyBigInts
- unpackPubKey
- unstringifyBigInts
- verifySignature
Type Aliases
Ciphertext
Ƭ Ciphertext<N>: N[]
Type parameters
| Name | Type | 
|---|---|
| N | bigint | 
Defined in
EcdhSharedKey
Ƭ EcdhSharedKey<N>: [N, N]
Type parameters
| Name | Type | 
|---|---|
| N | bigint | 
Defined in
Leaf
Ƭ Leaf: bigint
Defined in
PathElements
Ƭ PathElements: bigint[][]
Defined in
Plaintext
Ƭ Plaintext<N>: N[]
Type parameters
| Name | Type | 
|---|---|
| N | bigint | 
Defined in
Point
Ƭ Point<N>: [N, N]
Type parameters
| Name | Type | 
|---|---|
| N | SnarkBigNumber | 
Defined in
PrivKey
Ƭ PrivKey: SnarkBigNumber
Defined in
PubKey
Ƭ PubKey<N>: [N, N]
Type parameters
| Name | Type | 
|---|---|
| N | bigint | 
Defined in
Variables
NOTHING_UP_MY_SLEEVE
• Const NOTHING_UP_MY_SLEEVE: bigint
Defined in
SNARK_FIELD_SIZE
• Const SNARK_FIELD_SIZE: bigint = r
Defined in
Functions
bigInt2Buffer
▸ bigInt2Buffer(i): Buffer
Convert a BigInt to a Buffer
Parameters
| Name | Type | Description | 
|---|---|---|
| i | bigint | the bigint to convert | 
Returns
Buffer
the buffer
Defined in
calcDepthFromNumLeaves
▸ calcDepthFromNumLeaves(hashLength, numLeaves): number
Calculate the depth of a tree given the number of leaves
Parameters
| Name | Type | Description | 
|---|---|---|
| hashLength | number | the hashing function param length | 
| numLeaves | number | how many leaves | 
Returns
number
the depth
Defined in
deepCopyBigIntArray
▸ deepCopyBigIntArray(arr): bigint[]
Create a copy of a bigint array
Parameters
| Name | Type | Description | 
|---|---|---|
| arr | bigint[] | the array of bigints to copy | 
Returns
bigint[]
a deep copy of the array
Defined in
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
| Name | Type | Description | 
|---|---|---|
| privKey | SnarkBigNumber | A private key generated using genPrivKey() | 
Returns
bigint
A BabyJub-compatible private key.
Defined in
genEcdhSharedKey
▸ genEcdhSharedKey(privKey, pubKey): EcdhSharedKey
Generates an Elliptic-Curve Diffie–Hellman (ECDH) shared key given a private key and a public key.
Parameters
| Name | Type | Description | 
|---|---|---|
| privKey | SnarkBigNumber | A private key generated using genPrivKey() | 
| pubKey | PubKey | A public key generated using genPubKey() | 
Returns
The ECDH shared key.
Defined in
genKeypair
▸ genKeypair(): Keypair
Generates a keypair.
Returns
a keypair
Defined in
genPrivKey
▸ genPrivKey(): bigint
Generate a private key
Returns
bigint
A random seed for a private key.
Defined in
genPubKey
▸ genPubKey(privKey): PubKey
Parameters
| Name | Type | Description | 
|---|---|---|
| privKey | SnarkBigNumber | A private key generated using genPrivKey() | 
Returns
A public key associated with the private key
Defined in
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
genRandomSalt
▸ genRandomSalt(): bigint
Generate a random value
Returns
bigint
A BabyJub-compatible salt.
Defined in
genTreeCommitment
▸ genTreeCommitment(leaves, salt, depth): bigint
A helper function which hashes a list of results with a salt and returns the hash.
Parameters
| Name | Type | Description | 
|---|---|---|
| leaves | bigint[] | A list of values | 
| salt | bigint | A random salt | 
| depth | number | The tree depth | 
Returns
bigint
The hash of the leaves and the salt, with the salt last
Defined in
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
| Name | Type | Description | 
|---|---|---|
| index | number | The index of the value to generate the proof for | 
| leaves | bigint[] | A list of values | 
| depth | number | The tree depth | 
Returns
bigint[][]
The proof
Defined in
hash13
▸ hash13(elements): bigint
A convenience function to use Poseidon to hash a Plaintext with no more than 13 elements
Parameters
| Name | Type | Description | 
|---|---|---|
| elements | Plaintext | The elements to hash | 
Returns
bigint
The hash of the elements
Defined in
hash2
▸ hash2(elements): bigint
Parameters
| Name | Type | 
|---|---|
| elements | Plaintext | 
Returns
bigint
Defined in
hash3
▸ hash3(elements): bigint
Parameters
| Name | Type | 
|---|---|
| elements | Plaintext | 
Returns
bigint
Defined in
hash4
▸ hash4(elements): bigint
Parameters
| Name | Type | 
|---|---|
| elements | Plaintext | 
Returns
bigint
Defined in
hash5
▸ hash5(elements): bigint
Parameters
| Name | Type | 
|---|---|
| elements | Plaintext | 
Returns
bigint
Defined in
hashLeftRight
▸ hashLeftRight(left, right): bigint
Hash two BigInts with the Poseidon hash function
Parameters
| Name | Type | Description | 
|---|---|---|
| left | bigint | The left-hand element to hash | 
| right | bigint | The right-hand element to hash | 
Returns
bigint
The hash of the two elements
Defined in
hashN
▸ hashN(numElements, elements): bigint
Hash up to N elements
Parameters
| Name | Type | Description | 
|---|---|---|
| numElements | number | The number of elements to hash | 
| elements | Plaintext | The elements to hash | 
Returns
bigint
The hash of the elements
Defined in
hashOne
▸ hashOne(preImage): bigint
Hash a single BigInt with the Poseidon hash function
Parameters
| Name | Type | Description | 
|---|---|---|
| preImage | bigint | The element to hash | 
Returns
bigint
The hash of the element
Defined in
packPubKey
▸ packPubKey(pubKey): bigint
Losslessly reduces the size of the representation of a public key
Parameters
| Name | Type | Description | 
|---|---|---|
| pubKey | PubKey | The public key to pack | 
Returns
bigint
A packed public key
Defined in
poseidonDecrypt
▸ poseidonDecrypt(ciphertext, key, nonce, length): PlainText<bigint>
Decrypt some ciphertext using poseidon encryption
Parameters
| Name | Type | Description | 
|---|---|---|
| ciphertext | CipherText<bigint> | the ciphertext to decrypt | 
| key | EncryptionKey<bigint> | the key to decrypt with | 
| nonce | bigint | the nonce used to encrypt | 
| length | number | the length of the plaintext | 
Returns
PlainText<bigint>
the plaintext
Defined in
node_modules/.pnpm/@zk-kit+poseidon-cipher@0.2.1/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
| Name | Type | Description | 
|---|---|---|
| ciphertext | CipherText<bigint> | the ciphertext to decrypt | 
| key | EncryptionKey<bigint> | the key to decrypt with | 
| nonce | bigint | the nonce used to encrypt | 
| length | number | the length of the plaintext | 
Returns
PlainText<bigint>
the plaintext
Dev
Do not throw if the plaintext is invalid
Defined in
node_modules/.pnpm/@zk-kit+poseidon-cipher@0.2.1/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
| Name | Type | Description | 
|---|---|---|
| msg | PlainText<bigint> | the message to encrypt | 
| key | EncryptionKey<bigint> | the key to encrypt with | 
| nonce | bigint | the nonce to avoid replay attacks | 
Returns
CipherText<bigint>
the ciphertext
Defined in
node_modules/.pnpm/@zk-kit+poseidon-cipher@0.2.1/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
| Name | Type | Description | 
|---|---|---|
| input | bigint[] | the array of values to hash | 
Returns
bigint
a EVM compatible sha256 hash
Defined in
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
| Name | Type | Description | 
|---|---|---|
| privateKey | BigNumberish | The private key used to sign the message. | 
| message | BigNumberish | The 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/@zk-kit+eddsa-poseidon@0.5.1/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
| Name | Type | Description | 
|---|---|---|
| input | BigIntVariants | The input to convert | 
Returns
StringifiedBigInts
The input with bigint values converted to string
Defined in
unpackPubKey
▸ unpackPubKey(packed): PubKey
Restores the original PubKey from its packed representation
Parameters
| Name | Type | Description | 
|---|---|---|
| packed | bigint | The value to unpack | 
Returns
The unpacked public key
Defined in
unstringifyBigInts
▸ unstringifyBigInts(input): BigIntVariants
Given an input containing string values, convert them to bigint
Parameters
| Name | Type | Description | 
|---|---|---|
| input | StringifiedBigInts | The input to convert | 
Returns
BigIntVariants
the input with string values converted to bigint
Defined in
verifySignature
▸ verifySignature(message, signature, publicKey): boolean
Verifies an EdDSA signature using the Baby Jubjub elliptic curve and Poseidon hash function.
Parameters
| Name | Type | Description | 
|---|---|---|
| message | BigNumberish | The original message that was be signed. | 
| signature | Signature | The EdDSA signature to be verified. | 
| publicKey | Point | The 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/@zk-kit+eddsa-poseidon@0.5.1/node_modules/@zk-kit/eddsa-poseidon/dist/types/eddsa-poseidon.d.ts:40