Crypto Module
Table of contents
Classes
Interfaces
Type Aliases
Variables
Functions
- bigInt2Buffer
- calcDepthFromNumLeaves
- deepCopyBigIntArray
- formatPrivKeyForBabyJub
- genEcdhSharedKey
- genKeypair
- genPrivKey
- genPubKey
- genRandomBabyJubValue
- genRandomSalt
- genTreeCommitment
- genTreeProof
- hash12
- hash2
- hash3
- hash4
- hash5
- hashLeftRight
- hashN
- hashOne
- inCurve
- packPubKey
- poseidonDecrypt
- poseidonDecryptWithoutCheck
- poseidonEncrypt
- sha256Hash
- sign
- stringifyBigInts
- unpackPubKey
- unstringifyBigInts
- verifySignature
Type Aliases
Ciphertext
Ƭ Ciphertext<N
>: N
[]
Type parameters
Name | Type |
---|---|
N | bigint |
Defined in
crypto/ts/types.ts:21
EcdhSharedKey
Ƭ EcdhSharedKey<N
>: [N
, N
]
Type parameters
Name | Type |
---|---|
N | bigint |
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
Name | Type |
---|---|
N | bigint |
Defined in
crypto/ts/types.ts:18
Point
Ƭ Point<N
>: [N
, N
]
Type parameters
Name | Type |
---|---|
N | SnarkBigNumber |
Defined in
crypto/ts/types.ts:15
PrivKey
Ƭ PrivKey: SnarkBigNumber
Defined in
crypto/ts/types.ts:6
PubKey
Ƭ PubKey<N
>: [N
, N
]
Type parameters
Name | Type |
---|---|
N | bigint |
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
Name | Type | Description |
---|---|---|
i | bigint | the 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
Name | Type | Description |
---|---|---|
hashLength | number | the hashing function param length |
numLeaves | number | how 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
Name | Type | Description |
---|---|---|
arr | bigint [] | 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
Name | Type | Description |
---|---|---|
privKey | SnarkBigNumber | A 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
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
crypto/ts/keys.ts:76
genKeypair
▸ genKeypair(): Keypair
Generates a keypair.
Returns
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
Name | Type | Description |
---|---|---|
privKey | SnarkBigNumber | A private key generated using genPrivKey() |
Returns
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
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
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
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
crypto/ts/utils.ts:47
hash12
▸ hash12(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
crypto/ts/hashing.ts:130
hash2
▸ hash2(elements
): bigint
Parameters
Name | Type |
---|---|
elements | Plaintext |
Returns
bigint
Defined in
crypto/ts/hashing.ts:119
hash3
▸ hash3(elements
): bigint
Parameters
Name | Type |
---|---|
elements | Plaintext |
Returns
bigint
Defined in
crypto/ts/hashing.ts:120
hash4
▸ hash4(elements
): bigint
Parameters
Name | Type |
---|---|
elements | Plaintext |
Returns
bigint
Defined in
crypto/ts/hashing.ts:121
hash5
▸ hash5(elements
): bigint
Parameters
Name | Type |
---|---|
elements | Plaintext |
Returns
bigint
Defined in
crypto/ts/hashing.ts:122
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
crypto/ts/hashing.ts:85
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
crypto/ts/hashing.ts:101
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
crypto/ts/hashing.ts:159
inCurve
▸ inCurve(p
): boolean
Determines if a given point lies on the Baby Jubjub elliptic curve by verifying the curve equation.
This function checks if the point satisfies the curve equation ax^2 + y^2 = 1 + dx^2y^2
.
Parameters
Name | Type | Description |
---|---|---|
p | Point | The point to check, represented as a pair of bigint values. |
Returns
boolean
True if the point is on the curve, otherwise false.
Defined in
node_modules/.pnpm/@zk-kit+baby-jubjub@1.0.1/node_modules/@zk-kit/baby-jubjub/dist/types/baby-jubjub.d.ts:38
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
crypto/ts/keys.ts:35
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.3.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.3.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.3.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
crypto/ts/hashing.ts:15
sign
▸ sign(privateKey
, message
): Signature
<bigint
>
Signs a message using the provided private key, employing Poseidon hashing and EdDSA with the Baby Jubjub elliptic curve.
The private key must be an instance of Buffer, Uint8Array or a string. The input will be used to generate entropy and there is no limit in size. The string is used as a set of raw bytes (in UTF-8) and is typically used to pass passwords or secret messages. If you want to pass a bigint, a number or a hexadecimal, be sure to convert them to one of the supported types first. The 'conversions' module in @zk-kit/utils provides a set of functions that may be useful in case you need to convert types.
Parameters
Name | Type | Description |
---|---|---|
privateKey | string | Uint8Array | Buffer | The private key used to sign the message. |
message | BigNumberish | The message to be signed. |
Returns
Signature
<bigint
>
The signature object, containing properties relevant to EdDSA signatures, such as 'R8' and 'S' values.
Defined in
node_modules/.pnpm/@zk-kit+eddsa-poseidon@1.0.2/node_modules/@zk-kit/eddsa-poseidon/dist/types/eddsa-poseidon.d.ts:57
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
crypto/ts/bigIntUtils.ts:78
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
crypto/ts/keys.ts:42
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
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
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@1.0.2/node_modules/@zk-kit/eddsa-poseidon/dist/types/eddsa-poseidon.d.ts:65