Skip to main content

Nullifier

SnarkyJS / Modules / Nullifier

Class: Nullifier

Nullifiers are used as a public commitment to a specific anonymous account, to forbid actions like double spending, or allow a consistent identity between anonymous actions.

RFC: https://github.com/o1-labs/snarkyjs/issues/756

Paper: https://eprint.iacr.org/2022/1255.pdf

Hierarchy

  • { private: { c: Field; g_r: Group; h_m_pk_r: Group; } ; public: { nullifier: Group; s: Scalar; } ; publicKey: Group = Group }

    Nullifier

Table of contents

Constructors

Properties

Methods

Constructors

constructor

new Nullifier(value)

Parameters

NameTypeDefault value
valueObjectundefined
value.private{ c: Field; g_r: Group; h_m_pk_r: Group; }undefined
value.public{ nullifier: Group; s: Scalar; }undefined
value.publicKeyGroupGroup

Inherited from

Struct({ publicKey: Group, public: { nullifier: Group, s: Scalar, }, private: { c: Field, g_r: Group, h_m_pk_r: Group, }, }).constructor

Defined in

lib/circuit_value.ts:365

Properties

private

private: Object

Type declaration

NameType
cField
g_rGroup
h_m_pk_rGroup

Inherited from

Struct({ publicKey: Group, public: { nullifier: Group, s: Scalar, }, private: { c: Field, g_r: Group, h_m_pk_r: Group, }, }).private

Defined in

lib/nullifier.ts:25


public

public: Object

Type declaration

NameType
nullifierGroup
sScalar

Inherited from

Struct({ publicKey: Group, public: { nullifier: Group, s: Scalar, }, private: { c: Field, g_r: Group, h_m_pk_r: Group, }, }).public

Defined in

lib/nullifier.ts:21


publicKey

publicKey: Group = Group

Inherited from

Struct({ publicKey: Group, public: { nullifier: Group, s: Scalar, }, private: { c: Field, g_r: Group, h_m_pk_r: Group, }, }).publicKey

Defined in

lib/nullifier.ts:20


_isStruct

Static _isStruct: true

Inherited from

Struct({ publicKey: Group, public: { nullifier: Group, s: Scalar, }, private: { c: Field, g_r: Group, h_m_pk_r: Group, }, })._isStruct

Defined in

lib/circuit_value.ts:365


check

Static check: (value: { private: { c: Field; g_r: Group; h_m_pk_r: Group; } ; public: { nullifier: Group; s: Scalar; } ; publicKey: Group = Group }) => void

Type declaration

▸ (value): void

Add assertions to the proof to check if value is a valid member of type T. This function does not return anything, rather creates any number of assertions on the proof to prove value is a valid member of the type T.

For instance, calling check function on the type Bool asserts that the value of the element is either 1 or 0.

Parameters
NameTypeDefault valueDescription
valueObjectundefinedthe element of type T to put assertions on.
value.private{ c: Field; g_r: Group; h_m_pk_r: Group; }undefined-
value.public{ nullifier: Group; s: Scalar; }undefined-
value.publicKeyGroupGroup-
Returns

void

Inherited from

Struct({ publicKey: Group, public: { nullifier: Group, s: Scalar, }, private: { c: Field, g_r: Group, h_m_pk_r: Group, }, }).check

Defined in

snarky.d.ts:134


fromFields

Static fromFields: (fields: Field[]) => { private: { c: Field; g_r: Group; h_m_pk_r: Group; } ; public: { nullifier: Group; s: Scalar; } ; publicKey: Group = Group }

Type declaration

▸ (fields): Object

A function that returns an element of type T from the given provable data.

Important: For any element of type T, this function is the reverse operation of calling toFields method on an element of type T.

Parameters
NameTypeDescription
fieldsField[]an array of Field elements describing the provable data of the new T element.
Returns

Object

An element of type T generated from the given provable data.

NameType
private{ c: Field; g_r: Group; h_m_pk_r: Group; }
public{ nullifier: Group; s: Scalar; }
publicKeyGroup

Inherited from

Struct({ publicKey: Group, public: { nullifier: Group, s: Scalar, }, private: { c: Field, g_r: Group, h_m_pk_r: Group, }, }).fromFields

Defined in

snarky.d.ts:115


toAuxiliary

Static toAuxiliary: (value?: { private: { c: Field; g_r: Group; h_m_pk_r: Group; } ; public: { nullifier: Group; s: Scalar; } ; publicKey: Group = Group }) => any[]

Type declaration

▸ (value?): any[]

A function that takes value (optional), an element of type T, as argument and returns an array of any type that make up the "auxiliary" (non-provable) data of value. As any element of the interface ProvablePure<T> includes no "auxiliary" data by definition, this function always returns a default value.

Parameters
NameTypeDefault valueDescription
value?Objectundefinedthe element of type T to generate the auxiliary data array from, optional. If not provided, a default value for auxiliary data is returned.
value.private{ c: Field; g_r: Group; h_m_pk_r: Group; }undefined-
value.public{ nullifier: Group; s: Scalar; }undefined-
value.publicKeyGroupGroup-
Returns

any[]

An empty array, as any element of the interface ProvablePure<T> includes no "auxiliary" data by definition.

Inherited from

Struct({ publicKey: Group, public: { nullifier: Group, s: Scalar, }, private: { c: Field, g_r: Group, h_m_pk_r: Group, }, }).toAuxiliary

Defined in

snarky.d.ts:104


toFields

Static toFields: (value: { private: { c: Field; g_r: Group; h_m_pk_r: Group; } ; public: { nullifier: Group; s: Scalar; } ; publicKey: Group = Group }) => Field[]

Type declaration

▸ (value): Field[]

A function that takes value, an element of type T, as argument and returns an array of Field elements that make up the provable data of value.

Parameters
NameTypeDefault valueDescription
valueObjectundefinedthe element of type T to generate the Field array from.
value.private{ c: Field; g_r: Group; h_m_pk_r: Group; }undefined-
value.public{ nullifier: Group; s: Scalar; }undefined-
value.publicKeyGroupGroup-
Returns

Field[]

A Field array describing how this T element is made up of Field elements.

Inherited from

Struct({ publicKey: Group, public: { nullifier: Group, s: Scalar, }, private: { c: Field, g_r: Group, h_m_pk_r: Group, }, }).toFields

Defined in

snarky.d.ts:94


toInput

Static toInput: (x: { private: { c: Field; g_r: Group; h_m_pk_r: Group; } ; public: { nullifier: Group; s: Scalar; } ; publicKey: Group = Group }) => { fields?: Field[] ; packed?: [Field, number][] }

Type declaration

▸ (x): Object

Parameters
NameTypeDefault value
xObjectundefined
x.private{ c: Field; g_r: Group; h_m_pk_r: Group; }undefined
x.public{ nullifier: Group; s: Scalar; }undefined
x.publicKeyGroupGroup
Returns

Object

NameType
fields?Field[]
packed?[Field, number][]

Inherited from

Struct({ publicKey: Group, public: { nullifier: Group, s: Scalar, }, private: { c: Field, g_r: Group, h_m_pk_r: Group, }, }).toInput

Defined in

lib/circuit_value.ts:368


toJSON

Static toJSON: (x: { private: { c: Field; g_r: Group; h_m_pk_r: Group; } ; public: { nullifier: Group; s: Scalar; } ; publicKey: Group = Group }) => { private: { c: string; g_r: { x: string; y: string; }; h_m_pk_r: { x: string; y: string; }; } ; public: { nullifier: { x: string; y: string; }; s: string; } ; publicKey: { x: string ; y: string } = Group }

Type declaration

▸ (x): Object

Parameters
NameTypeDefault value
xObjectundefined
x.private{ c: Field; g_r: Group; h_m_pk_r: Group; }undefined
x.public{ nullifier: Group; s: Scalar; }undefined
x.publicKeyGroupGroup
Returns

Object

NameType
private{ c: string; g_r: { x: string; y: string; }; h_m_pk_r: { x: string; y: string; }; }
public{ nullifier: { x: string; y: string; }; s: string; }
publicKey{ x: string ; y: string }
publicKey.xstring
publicKey.ystring

Inherited from

Struct({ publicKey: Group, public: { nullifier: Group, s: Scalar, }, private: { c: Field, g_r: Group, h_m_pk_r: Group, }, }).toJSON

Defined in

lib/circuit_value.ts:372

Methods

assertUnused

assertUnused(witness, root): void

Checks if the Nullifier has been used before.

Example

// asserts that the nullifier has not been used before, throws an error otherwise
nullifier.assertUnused();

Parameters

NameType
witnessMerkleMapWitness
rootField

Returns

void

Defined in

lib/nullifier.ts:136


getPublicKey

getPublicKey(): PublicKey

Returns the PublicKey that is associated with this Nullifier.

Example

let pk = nullifier.getPublicKey();

Returns

PublicKey

Defined in

lib/nullifier.ts:165


isUnused

isUnused(witness, root): Bool

Returns the state of the Nullifier.

Example

// returns a Bool based on whether or not the nullifier has been used before
let isUnused = nullifier.isUnused();

Parameters

NameType
witnessMerkleMapWitness
rootField

Returns

Bool

Defined in

lib/nullifier.ts:116


key

key(): Field

The key of the nullifier, which belongs to a unique message and a public key. Used as an index in Merkle trees.

Example

// returns the key of the nullifier which can be used as index in a Merkle tree/map
let key = nullifier.key();

Returns

Field

Defined in

lib/nullifier.ts:103


setUsed

setUsed(witness): Field

Sets the Nullifier, returns the new Merkle root.

Example

// calculates the new root of the Merkle tree in which the nullifier is set to used
let newRoot = nullifier.setUsed(witness);

Parameters

NameType
witnessMerkleMapWitness

Returns

Field

Defined in

lib/nullifier.ts:151


verify

verify(message): void

Verifies that the Nullifier belongs to a specific message. Throws an error if the Nullifier is incorrect.

Example

let nullifierMessage = [voteId, ...otherData];
// throws an error if the nullifier is invalid or doesn't belong to this specific message
nullifier.verify(nullifierMessage);

Parameters

NameType
messageField[]

Returns

void

Defined in

lib/nullifier.ts:46


fromJSON

Static fromJSON(json): Nullifier

Parameters

NameType
jsonNullifier

Returns

Nullifier

Overrides

Struct({ publicKey: Group, public: { nullifier: Group, s: Scalar, }, private: { c: Field, g_r: Group, h_m_pk_r: Group, }, }).fromJSON

Defined in

lib/nullifier.ts:31


sizeInFields

Static sizeInFields(): number

Return the size of the T type in terms of Field type, as Field is the primitive type.

Warning: This function returns a number, so you cannot use it to prove something on chain. You can use it during debugging or to understand the memory complexity of some type.

Returns

number

A number representing the size of the T type in terms of Field type.

Inherited from

Struct({ publicKey: Group, public: { nullifier: Group, s: Scalar, }, private: { c: Field, g_r: Group, h_m_pk_r: Group, }, }).sizeInFields

Defined in

snarky.d.ts:124