Skip to main content
Version: 2.0.0

@unirep/utils

Classes

Interfaces

Type Aliases

Node

Ƭ Node: any

Defined in

node_modules/@zk-kit/incremental-merkle-tree/dist/types/types/index.d.ts:1


SnarkPublicSignals

Ƭ SnarkPublicSignals: bigint[]

Type of snark public signals.

Defined in

packages/utils/src/snark.ts:4

Variables

ATTESTER_ID_BITS

Const ATTESTER_ID_BITS: bigint

The number of bits in an attester ID variable. It is defined as BigInt(160).

Defined in

packages/utils/src/crypto.ts:21


CHAIN_ID_BITS

Const CHAIN_ID_BITS: bigint

The number of bits in a chain id variable. It is defined as BigInt(36).

Defined in

packages/utils/src/crypto.ts:29


EPOCH_BITS

Const EPOCH_BITS: bigint

The number of bits in an epoch variable. It is defined as BigInt(48).

Defined in

packages/utils/src/crypto.ts:25


F

Const F: bigint

A bigint representation of the field prime.

Defined in

packages/utils/src/crypto.ts:12


MAX_EPOCH

Const MAX_EPOCH: number

A number representation of the maximum epoch value. Equivalent to 2**48-1.

Defined in

packages/utils/src/crypto.ts:45


NONCE_BITS

Const NONCE_BITS: bigint

The number of bits in an epoch key nonce variable. It is defined as BigInt(8).

Defined in

packages/utils/src/crypto.ts:17


ONE_BIT

Const ONE_BIT: bigint

It indicates a one bit variable. It is defined as BigInt(1).

Defined in

packages/utils/src/crypto.ts:41


REP_BITS

Const REP_BITS: bigint

The number of bits in a Rep variable. It is defined as BigInt(64).

Defined in

packages/utils/src/crypto.ts:37


REVEAL_NONCE_BITS

Const REVEAL_NONCE_BITS: bigint

The number of bits in a reveal nonce variable. It is defined as BigInt(1).

Defined in

packages/utils/src/crypto.ts:33


SNARK_SCALAR_FIELD

Const SNARK_SCALAR_FIELD: "21888242871839275222246405745257275088548364400416034343698204186575808495617"

A decimal string representing the field prime.

Defined in

packages/utils/src/crypto.ts:7

Functions

genEpochKey

genEpochKey(identitySecret, attesterId, epoch, nonce, chainId): bigint

Calculate an epoch key.

Parameters

NameTypeDescription
identitySecretbigintThe secret of a user's Semaphore identity.
attesterIdstring | bigintEither an EOA or a smart contract address of an attester.
epochnumber | bigintThe epoch information.
noncenumber | bigintA epoch key nonce chosed by user.
chainIdnumber | bigintThe current chain id.

Returns

bigint

The epoch key result.

Example

import { Identity } from '@semaphore-protocol/identity'
import { genEpochKey } from '@unirep/utils'

const id = new Identity()
const attesterId = '0x1234'
const epoch = 0
const nonce = 0
const chainId = 1
const epochKey = genEpochKey(
id.secret,
attesterId,
epoch,
nonce,
chainId
)

Defined in

packages/utils/src/crypto.ts:86


genEpochTreeLeaf

genEpochTreeLeaf(epochKey, data): bigint

Calculate an epoch tree leaf in an epoch tree

Parameters

NameTypeDescription
epochKeystring | bigintThe epoch key information.
data(string | number | bigint)[]The array of data of the epoch key in the epoch tree.

Returns

bigint

The epoch tree leaf.

Example

import { genEpochTreeLeaf } from '@unirep/utils'

const epochKey = '0x3456'
const FIELD_COUNT = 6
const data = Array(FIELD_COUNT).fill(0)
const leaf = genEpochTreeLeaf(
epochKey,
data
)

Defined in

packages/utils/src/crypto.ts:218


genIdentityHash

genIdentityHash(identitySecret, attesterId, epoch, chainId): bigint

Calculate an identity hash for a user. It is used for user signup. The state tree leaf should follow the format: stateTreeLeaf = H(identityHash, H(data)) where identityHash = H(identitySecret, attesterId + (epoch << 160) + (chainId << 208)).

Parameters

NameTypeDescription
identitySecretbigintThe secret of a user's Semaphore identity.
attesterIdstring | bigintEither an EOA or a smart contract address of an attester.
epochnumber | bigintThe epoch information.
chainIdnumber | bigintThe current chain id.

Returns

bigint

The identity hash.

Example

import { Identity } from '@semaphore-protocol/identity'
import { genIdentityHash } from '@unirep/utils'

const id = new Identity()
const attesterId = '0x1234'
const epoch = 0
const chainId = 1
const idHash = genIdentityHash(
id.secret,
attesterId,
epoch,
chainId
)

Defined in

packages/utils/src/crypto.ts:134


genRandomSalt

genRandomSalt(): bigint

Generate a random bigint in the snark finite field.

Returns

bigint

Example

import { genRandomSalt } from '@unirep/utils'

// generate random bigint
const salt = genRandomSalt()

Defined in

packages/utils/src/crypto.ts:57


genStateTreeLeaf

genStateTreeLeaf(identitySecret, attesterId, epoch, data, chainId): bigint

Calculate a state tree leaf for a user.

Parameters

NameTypeDescription
identitySecretbigintThe secret of a user's Semaphore identity.
attesterIdstring | bigintEither an EOA or a smart contract address of an attester.
epochnumber | bigintThe epoch information.
data(string | number | bigint)[]The array of user data in the current epoch.
chainIdnumber | bigintThe current chain id.

Returns

bigint

The state tree leaf.

Example

import { Identity } from '@semaphore-protocol/identity'
import { genStateTreeLeaf } from '@unirep/utils'

const id = new Identity()
const attesterId = '0x1234'
const epoch = 0
const FIELD_COUNT = 6
const data = Array(FIELD_COUNT).fill(0)
const chainId = 1
const leaf = genStateTreeLeaf(
id.secret,
attesterId,
epoch,
data,
chainId
)

Defined in

packages/utils/src/crypto.ts:179


stringifyBigInts

stringifyBigInts(o): any

Stringify all bigints in an object, a string, or an array.

Parameters

NameTypeDescription
oanyAn object with bigint, an array of bigints, or a bigint.

Returns

any

Stringified object, an array of string, or a string.

Example

import { stringifyBigInts } from '@unirep/utils'

stringifyBigInts(BigInt(3))
// '3'

stringifyBigInts([BigInt(3)])
// ['3']

stringifyBigInts({
item: BigInt(3)
})
// { item: '3' }

Defined in

packages/utils/src/stringify.ts:32


unstringifyBigInts

unstringifyBigInts(o): any

Unstringify all strings in an object, a string, or an array to bigints

Parameters

NameTypeDescription
oanyStringified object, an array of string, or a string.

Returns

any

An object with bigint, an array of bigints, or a bigint.

Example

import { unstringifyBigInts } from '@unirep/utils'

const values = {
input1: '1',
input2: '2',
input3: '3',
}

unstringifyBigInts(values)
// { input1: 1n, input2: 2n, input3: 3n }

Defined in

packages/utils/src/stringify.ts:69