The Semaphore identity is essentially an EdDSA public/private key pair. The EdDSA implementation in this library uses Baby Jubjub for public key generation and Poseidon for signatures. In addition, the commitment, i.e. the hash of the public key, is used to represent Semaphore identities in groups, adding an additional layer of privacy and security. The private key of the identity is stored as a hexadecimal string or text. The other attributes are stored as stringified bigint.

Constructors

  • Initializes the class attributes based on a given private key, which must be a hexadecimal string or a text. Hexadecimal strings must not start with '0x' or '0X'. If the private key is not passed as a parameter, a random hexadecimal key will be generated. The EdDSAPoseidon class is used to generate the secret scalar and the public key. Additionally, the constructor computes a commitment of the public key using a hash function (Poseidon).

    Parameters

    • Optional privateKey: string

      The private key used to derive the public key (hexadecimal or string).

    Returns Identity

    Example

    // Generates an identity.
    const { privateKey, publicKey, commitment } = new Identity("private-key")

    Example

    // Generates an identity with a random private key.
    const { privateKey, publicKey, commitment } = new Identity()

Properties

_commitment: bigint
_privateKey: string
_publicKey: Point<bigint>
_secretScalar: bigint

Accessors

  • get commitment(): bigint
  • Returns the commitment hash of the public key.

    Returns bigint

    The commitment as a string.

  • get privateKey(): string
  • Returns the private key.

    Returns string

    The private key as a string (hexadecimal or text).

  • get publicKey(): Point<bigint>
  • Returns the public key as a Baby Jubjub Point.

    Returns Point<bigint>

    The public key as a point.

  • get secretScalar(): bigint
  • Returns the secret scalar.

    Returns bigint

    The secret scalar as a string.

Methods

  • Generates a signature for a given message using the private key. This method demonstrates how to sign a message and could be used for authentication or data integrity.

    Parameters

    • message: BigNumberish

      The message to be signed.

    Returns Signature<bigint>

    A Signature object containing the signature components.

    Example

    const identity = new Identity()
    const signature = identity.signMessage("message")
  • Verifies a signature against a given message and public key. This static method allows for the verification of signatures without needing an instance of the Identity class. It's useful for cases where you only have the public key, the message and a signature, and need to verify if they match.

    Parameters

    • message: BigNumberish

      The message that was signed.

    • signature: Signature

      The signature to verify.

    • publicKey: Point

      The public key to use for verification.

    Returns boolean

    A boolean indicating whether the signature is valid.

    Example

    const identity = new Identity()
    const signature = identity.signMessage("message")
    Identity.verifySignature("message", signature, identity.publicKey)