> For the complete documentation index, see [llms.txt](https://unirep.gitbook.io/unirep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://unirep.gitbook.io/unirep/protocol/glossary/user-state-transition.md).

# User State Transition

Definition of user state transition in UniRep protocol.

### Why users have to perform user state transition?

User state transition is used to&#x20;

* Make sure users process their attestations correctly including the bad reputation.
* Generate a new user state in a new epoch to prove the latest reputation.

After user performs user state transition, he can&#x20;

1. Prove the latest reputation status.
2. Generate new epoch key proofs to receive attestations in the latest epoch.

### Workflow of a user state transition

#### 1. User computes epoch key of the latest transition (or sign up) epoch

![Epoch keys are iterated computed in the circuits.](https://1939836936-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mb9Vp6gNlb57m_A4qKo%2Fuploads%2FbWYavtQguW9aUKIxnxWD%2F%E6%88%AA%E5%9C%96%202022-07-21%20%E4%B8%8B%E5%8D%883.40.29.png?alt=media\&token=20357452-b71b-4312-bc53-de597849ed99)

#### 2. Update user state tree

![Step 1: update leaf index 3](https://1939836936-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mb9Vp6gNlb57m_A4qKo%2Fuploads%2Fvte3BqiMDDhZctJwlrux%2F8.png?alt=media\&token=afb3dad1-238e-49dc-ad76-f57ac35f6775)

![Step 2: update leaf index 1](https://1939836936-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mb9Vp6gNlb57m_A4qKo%2Fuploads%2FAjgrZ1UVeB46ZOxLnzx6%2F9.png?alt=media\&token=f062bdba-752d-4e82-891e-0ea21d407df5)

#### 3. Check if epoch tree root matches computed hashchains and epoch keys

![](https://1939836936-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mb9Vp6gNlb57m_A4qKo%2Fuploads%2FaLGqVJPZXkZlnmemwGsU%2Fepoch%20tree.png?alt=media\&token=76cad797-a24c-4c08-be83-3790133f1cb1)

#### 4. Compute a new global state tree leaf

```typescript
const newLeaf = hash(idCommitment, userStateTreeRoot)
```

![How a new global state tree is computed.](https://1939836936-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mb9Vp6gNlb57m_A4qKo%2Fuploads%2FmRgGqMoOm3dmvvfEhALW%2F%E6%88%AA%E5%9C%96%202022-07-22%20%E4%B8%8B%E5%8D%8812.15.52.png?alt=media\&token=5eb2ae00-e903-424b-90d9-68672498f40f)

#### 5. Call unirep smart contract to insert a new global state tree leaf

User performs user state transition by calling [`updateUserStateRoot()`](https://github.com/Unirep/Unirep/blob/f3502e1a551f63ab44b73444b60ead8731d45167/packages/contracts/contracts/Unirep.sol#L559)

* User will attach a [User State Transition Proof](/unirep/protocol/circuits/user-state-transition-proof.md) when calling `updateUserStateRoot`. Others can make sure if the user state transition is correct by verifying the User State Transition Proof.
* Once the user performed user state transition, his user state will be inserted into the [global state tree](/unirep/protocol/glossary/trees.md#global-state-tree) of the latest epoch.
* So if a user does not perform user state transition during an epoch, his user state will not be in the global state tree of that epoch.

{% hint style="info" %}
See also

* [Trees](/unirep/protocol/glossary/trees.md)
* [Epoch Transition](/unirep/protocol/glossary/epoch.md)
* [User State Transition Proof](/unirep/protocol/circuits/user-state-transition-proof.md)
  {% endhint %}
