# Anonymous Credentials

The anonymous credential scheme is a part of the Ontology Crypto library that provides several cryptography related utilities for the Ontology network. The main features provided by the crypto library basically revolve around digital signature. It provides general APIs for processing digital signatures and keys.&#x20;

## Abstract

There are **three** parties involved in an anonymous credential scheme, namely the **issuer**, the **user** (prover), and the **verifier**.

The issuer provides a certificate to the user. This certificate contains a list of the user's attributes and the issuer's signature (using BBS+signature). This protocol is formally called **credential issuance protocol.** The user who is in possession of the credentials can selectively disclose some parts to a verifier. This protocol is formally called **credential presentation protocol**.

## Background

### BBS + Signature

**Setup:** Groups $$G\_1, G\_2$$ and $$G\_t\ .$$ Pairing function $$e: G\_1\*G\_2 \rightarrow G\_t \ ,$$ where $$G\_1$$ and $$G\_2$$ are both of order $$p$$.\
Common parameters:&#x20;

* $$g\_1$$ is the generator of $$G\_1$$&#x20;
* $$g\_2$$ is the generator of $$G\_2$$&#x20;
* $$H\_{rand} \ , h\_1, ... \ , h\_L$$ are elements from $$G\_1$$&#x20;

**KeyGen:** A sample $$x$$ from uniform distribution on $$Z\_p$$, output $$s\_k = x \ ,\ p\_k = {g\_2}^x$$&#x20;

**Sign**$$(s\_k, m\_1, ... \ , m\_L)$$**:** Two random numbers $$E$$ and $$s$$are selected from $$Z\_p$$.  First$$B = g\_1 \* {H\_{Rand}}^s \* ({h\_1}^{m\_1} \* ... \* {h\_L}^{m\_L})$$ is calculated, and then  $$A = B^{1/(E+x)}$$ is computed. The signature is $$(A, B, E, s)$$&#x20;

**Verify** $$(p\_k, m\_1, ...\ ,m\_L, sig)$$**:** Decode $$sig$$ as $$(A, B, E, s)$$ , and check if  $$e(A, {g\_2}^E \* p\_k) == e(B, g\_2)$$ and whether $$B == g\_1 \* {H\_{Rand}}^s \* ({h\_1}^{m\_1} \* ... \* {h\_L}^{m\_L})$$&#x20;

### &#x20;Non-Interactive Proof of Knowledge (PoK) protocol

In this subsection, we will look at an example of the non-interactive proof of knowledge protocol which proves that the public key is generated as specified in the BBS + signature scheme. That is, $$\pi = PoK { x : w = {g\_2}^x \ && \ \_g\_2 = \_{g\_1}^x}$$. This means the prover proves the knowledge of $$x$$ such that $${g\_2}^x = w$$ and $$\_g\_2 = \_{g\_1}^x$$. It is assumed that $$w, g\_2, \_g\_1, \_g\_2$$ are all public.

The protocol that we provide is a standard sigma protocol. It involves three steps, which are commit, challenge, and response. Sigma protocol is an interactive protocol and can be modified to be a non-interactive zero knowledge proof by using the well-known [**Fiat-Shamir heuristic**](https://en.wikipedia.org/wiki/Fiat%E2%80%93Shamir_heuristic). The proof $$\pi = { C, S}.$$&#x20;

#### 1. Commitment (Prover)

```go
r = rand(Zp)

t1 = g2^r

t2 = _g1^r
```

#### 2. Proof (Prover)

```go
P = t1 || t2 || g2 || _g1 || w || _g2    //join them together in binary format

C = hash_to_int(P)                       //C is challenge

S = (r + C * x) mod p                    //response to verifier
```

#### 3. Verify (Verifier)

```go
_t1 = g2^S * w^(-c)

_t2 = _g1^S * _g2^(-c)

_P = _t1 || _t2 || g2 || _g1 || w || _g2

_C = hash_to_int(_P)

// use C to compare with _C, which was calculated just now
if C == _C {
    return true
} else {
    return false
} 
```

## Setup of the Issuer's key pair

Given an array of the attribute names `AttributeNames`, the issuer's key pair is generated in the following manner:

1. Select a random element $$x$$ from $$Z\_p$$, and compute $$w = {g\_2}^x$$
2. Select a random element $$\_g\_1$$from $$G\_1$$, and compute $$\_g\_2 = \_{g\_1}^x$$
3. Generate non-interactive proof of knowledge  $$\pi = PoK{ x: w = {g\_2}^x  \ && \ {\_g\_1}^x  } = (C, S)$$\
   Here,&#x20;

* `r` **:** A random element $$r$$ from $$Z\_p$$&#x20;
* `t1` **:** Computed as $$t\_1 = {g\_2}^r$$&#x20;
* `t2` **:** Computed as $$t\_2 = \_{g\_1}^r$$&#x20;
* `C` **:**  $$C = H(t\_1 || t\_2 || g\_2 || \_g\_1||w||\_g\_2)$$&#x20;
* `s` **:** $$S = (r + C \*x) \bmod p$$&#x20;

4\. Select an array of elements from $$G\_1$$ from `AttributeNames`. Next, calculate `HAttrs[i] = random(G1)` for each attribute in `AttributeNames`

5\. Select two random elements `HRand` And `HSk` from $$G\_1$$ \
6\. The issuer's public key is set to  `ipk = (w, _g1, _g2, π, HAttrs, AttributeNames, HRand, HSk)`, and the private key is set to `isk = x`\
7\. Return `isk` and `ipk`

The following are the reference data structures for the issuer's key pair:

```go
type IssuerSecretKey struct {
    x BigNum
}
```

```go
type IssuerPublicKey struct {
    AttributeNames []string
    HAttrs         []G1Point // one G1-element for one attribute
    HRand          G1Point   // a random G1 point 
    HSk            G1Point   // a random G1 point to encode user's secret key 

    w              G2Point   // element from G2  
    _g1            G1Point   // point of G1
    _g2            G1Point   // point of G1

    //PoK{x: w = g2^x && _g2 = _g1^x}
    C              BigNum    // challenge
    S              BigNum    // response
}
```

## Issuance Protocol

Issuance protocol is an interactive protocol that consists of the following steps:

1. The issuer sends a random [**nonce**](/glossary.md#nonce) to the user
2. The user creates a **credential request** using the public key, the secret, and the nonce. This request consists of a commitment to the user secret (can be seen as a public key), and a zero-knowledge proof of the knowledge of the user secret key. The user **sends** this credential request to the issuer
3. The **issuer verifies** the credential request by verifying the zero-knowledge proof \
   If the request is **valid**, the issuer issues a credential to the user by **signing** the **commitment** to the secret key along with the attribute values and then **sends** the credential back to the user
4. The **user verifies** the issuer's **signature** and **stores** the **credential** that consists of the signature value, a randomness used to create the signature, the user secret, and the attribute values

The following diagram represents the interaction between the user and the issuer:

![](/files/-LwvEMQ37aHmlV1xWV0j)

* The credential request CredRequest contains a commitment $$N\_{ym}$$ to user's secret key which is of the form $${H\_{Sk}}^{sk}$$ and a `zk-PoK` of the $$N\_{ym}$$&#x20;
* Credential contains the `BBS+signature` on the attributes and the `Nym`

### Generating credential request

The user generates the credential request using the attribute values and the nonce as input. The process is as follows:

1. Select a random element **sk** from $$Z\_p$$ as the user's master secret key
2. Calculate $$N\_{ym} = {H\_{Sk}}^{sk}$$ , which represents the commitment to the user's master secret
3. Generate the zero knowledge proof $$\pi = PoK { sk : N\_{ym} = {H\_{Sk}}^{sk} } = (C, S)$$ in the following manner-

* Select a random element $$sk$$ from $$Z\_p$$ which acts as the user's master secret
* Calculate $$t\_1 = {H\_{Sk}}^r$$&#x20;
* Compute the challenge $$C = H(t\_1 || H\_{Sk}||N\_{ym}||nonce)$$&#x20;
* Compute the response $$S = (r+C\*sk) \bmod p$$&#x20;

The data structure of the credential request is of the following manner:

```go
type CredRequest struct {
   Nym             G1Point  //commitment to user's master secret
   IssuerNonce     BigNum   //nonce 
   Attrs           []BigNum //user's attributes

   //PoK that Nym is constructed as in the issuance protocol
   // i.e. PoK{(sk): HSk^sk = Nym }
   C               BigNum   //challenge in Sigma-protocol
   S               BigNum   //response in Sigma-protocol
}
```

### Issuing Credential

After receiving the credential request from the user, the issuer verifies $$\pi = (C, S)$$ and generates credentials for the user. The credential is generated using the issuer's private key $$i\_{sk}$$ as follows:

1. Select two random elements $$e, s$$ from $$Z\_p$$&#x20;
2. Calculate  `B = g1 · HRand^s · Nym · MulAll(HAttrs[i]^(Attrs[i]))`
3. Compute  `A = B^(1/(e+x))`
4. Return the credential $$(A, B, e, s, Attrs)$$&#x20;

The data structure of a credential looks something like:

```go
type Credential struct {
   A               G1Point
   B               G1Point
   e               BigNum
   s               BigNum
   Attrs           []BigNum
}
```

## Presentation Protocol

In the presentation protocol, the prover tries to convince the verifier that they are aware of some secret input, such that some hypothetical predicate is true. A typical example of a predicate is that the prover is in possession of an anonymous credential, and they can selectively disclose certain attributes while hiding the other attributes.

The information that is available to the user is:

* User's secret key $$sk$$ and its commitment $$N\_{ym}$$&#x20;
* Attribute values $$attrs = (a\_1, ...\  , a\_L)$$&#x20;
* BBS +signature `(A, B, e, s)`
* Extra input&#x20;
  * (D, I) **:** Attribute predicate, describes what attributes will be disclosed. If `D[j] == 1`, `I[j] = attrs[j] = aj`, else `I[j] = null`

### Proving Algorithm

The selective disclosure proof can be generated in the following manner:

1. Randomize A **:** Select a random element $$r\_1$$ from $${Z\_p}^\*$$, and compute $$A' = A^{r\_1}$$&#x20;
2. Calculate $$\_A = A'^{(−e)} \ · B^{r\_1},\ r\_3 = 1/r\_1$$&#x20;
3. Select an element $$r\_2$$ from $$Z\_p$$&#x20;
4. Calculate $$B' = B^{r\_1} · {H\_{Rand}}^{-r\_2}$$ , $$s' = s - r\_2 · r\_3$$&#x20;
5. Generate zero knowledge proof $$\pi = PoK{ (s\_k, {a\_i}\_{hidden}, e, r\_2, r\_3, s') }$$ such that-

* &#x20;`_A/B' = A'^(-e) · HRand^r2`
* `g1 · MulAll(hi^ai_reveal) = (B')^r3 · HRand^(-s') · HSk^(-sk) ·MulAll(hi^(-ai_hidden))`, where `hi` stands for `HAttrs[i]`

&#x20;        The proof can be generated as follows:

```yaml
r_ai : for i belongs to _D(attributes not disclosed), means D[i]==0
r_e : random from Zp
r_r2 : random from Zp
r_r3 : random from Zp
r_s' : random from Zp
r_sk : random from Zp
E : E = HSk^r_sk
t1 : t1 = A'^r_e · HRand^r_r2
t2 : t2 = (B')^r_r3 · HRand^r_s' · E^(-1) · MulAll(hi^r_ai)
c' : c' = H(A', _A, B', nym, t1, t2, g1, HRand, h1, ... , hL, w)
nonce : nonce, with τ bit length, randomly generated again
c : c = H(nonce, c', (D, I))
s_sk : s_sk = r_sk + c · sk
s_ai : s_ai = r_ai - c · ai, for i belongs to _D(attributes not disclosed)
s_e : s_e = r_e - c · e
s_r2 : s_r2 = r_r2 + c · r2
s_r3 : s_r3 = r_r3 + c · r3
s_s' : s_s' = r_s' - c · s'
π : {c, s_sk, {s_ai}, s_e, s_r2, s_r3, s_s', nonce}, i belong to _D
```

The output is $$(A', \_A, d, n\_{ym}, \pi)$$ where $$\pi = {c, s\_{sk}, s\_{ai}, s\_e, s\_{r\_2}, s\_{r\_3}, {s\_s}', nonce}$$&#x20;

Here is the reference data structure for the zero knowledge proof:

```go
type Proof struct {
    APrime             G1Point  // randomized credential signature values
    ABar               G1Point  // randomized credential signature values
    BPrime             G1Point  // randomized credential signature values

    /* challenge in sigma-protocol */
    ProofC             BigNum
    /* response in sigma-protocol */
    ProofSSk           BigNum
    ProofSE            BigNum
    ProofSR2           BigNum
    ProofSR3           BigNum
    ProofSSPrime       BigNum
    ProofSAttrs        []BigNum

    Nonce              BigNum   // nonce used to avoid replay attack
    Nym                G1Point  
}
```

### Verification

The verifier has the following input information available:

* &#x20;$$(A', \_A, B', n\_{ym}, \pi)$$ : from the signer
* &#x20;$${c, s\_{sk}, {s\_{a\_i}}, s\_e, s\_{r\_2}, s\_{r\_3}, {s\_s}', nonce}$$ : obtained by parsing $$\pi$$&#x20;

The verification algorithm proceeds as in the following manner:

1. Check if `A' != 1` in G1; if false, return `false`.
2. Check if `e(A', w) == e(_A, g2)`; if false, return `false`. This is $$z\_k-PoK$$ for **A**.
3. Parse $$\pi$$ : `{c, s_sk, {s_ai}, s_e, s_r2, s_r3, s_s', nonce} <- π`; if failed, return `false`.
4. &#x20;\~ $$t\_1$$ : `~t1 = A'^s_e · HRand^s_r2 · (_A/B')^(-c)` . This is $$z\_k-PoK$$ for **e**, **r2***.*
5. &#x20;\~ $$t\_2$$ : `(B')^s_r3 · HRand^s_s' · HSk^(-s_sk) · MulAll(hi^(-s_ai)) · (g1·MulAll(hi^ai))^(-c)`

   * the `i` above, first `MulAll( )` belongs to `_D`, where `D[i]==0(false)`
   * the `i` above, second `MulAll( )` belongs to `D`, where `D[i]==1(true)`

   This is $$Z\_k - PoK$$ for **r3**, **s'**, **gsk**, **ai** of \_D.
6. &#x20;$$c'$$ : `c' = H(nonce, H(A', _A, B', nym, ~t1, ~t2, g1, HRand, h1, ... , hL, w), (D, I))`
7. Check if `c == c'` : if false, return `false`. Otherwise return `true`.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ont.io/decentralized-identity-and-data/ontid/trust-mechanism/anonymous-credentials.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
