# GHSA-pffg-92cg-xf5c: gnark-crypto's exponentiation in the pairing target group GT using GLV can give incorrect results

> Incorrect results in GLV exponentiation in GT group can lead to cryptographic failures, affecting users of the gnark-crypto library.

- **Severity:** Not scored
- **Exploited in the wild:** Not known
- **Login needed:** Not needed
- **Attackable:** Not stated
- **Victim action needed:** Not needed
- **Fix available:** Yes
- **Published:** 5 Oct 2023
- **Also known as:** GO-2023-2101

## What is GHSA-pffg-92cg-xf5c?

GHSA-pffg-92cg-xf5c is a vulnerability in github.com/consensys/gnark-crypto, published on 5 Oct 2023. 

### Official description

### Impact
When the exponent is bigger than `r`, the group order of the pairing target group `GT`, the exponentiation à la GLV (`ExpGLV`) can *sometimes* give incorrect results compared to normal exponentiation (`Exp`). 

The issue impacts all users using `ExpGLV` for exponentiations in `GT`. This does not impact `Exp` and `ExpCyclotomic` which are sound. Also note that GLV methods in G1 and G2 are sound and _not_ impacted.

### Patches
Fix has been implemented in pull request https://github.com/Consensys/gnark-crypto/pull/451 and merged in commit https://github.com/Consensys/gnark-crypto/commit/ec6be1a037f7c496d595c541a8a8d31c47bcfa3d to master branch.

The fix increased the bounds of the sub-scalars by 1. In fact, since https://github.com/Consensys/gnark-crypto/pull/213, we use a fast scalar decomposition that tradeoffs divisions (needed in the Babai rounding) by right-shifts. We precompute `b=2^m*v/d (m > log2(d))` and then at runtime compute `scalar*b/2^m` (`v` is a lattice vector and `d` the lattice determinant). This increases the bounds on sub-scalars by 1 which we check at runtime before increasing the loop size (we don't target constant-timeness). `m` is chosen to be a machine word twice big than `log2(d)` so that we rarely need to increase the loop size. Hence why the issue happens only *sometimes* if we omit to increase the bounds. This bounds increase was implemented in G1 and G2 but forgot in GT.

### Workarounds
Updating to `v0.12.1+`. Alternatively, use `Exp` or `ExpCyclotomic` instead. We are not aware of any users using `ExpGLV` anyway.

### References
- Fix PR: https://github.com/Consensys/gnark-crypto/pull/451 
- Fast scalar decomposition PR: https://github.com/Consensys/gnark-crypto/pull/213
- https://eprint.iacr.org/2015/565 Sec.4.2

### Acknowledgement
The vulnerability was reported by [Antonio Sanso](https://github.com/asanso) @ [EF](https://crypto.ethereum.org/).

## Which versions are affected by GHSA-pffg-92cg-xf5c?

3 open source packages ship an affected version, including github.com/consensys/gnark-crypto.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `github.com/consensys/gnark-crypto` | Go | All versions | 0.12.1 |
| `github.com/consensys/gnark-crypto` | Go | All versions | 0.12.1 |
| `github.com/consensys/gnark-crypto` | Go | All versions | 0.12.1 |

## How do I fix GHSA-pffg-92cg-xf5c?

Upgrade github.com/consensys/gnark-crypto to 0.12.1 or later.

## Is GHSA-pffg-92cg-xf5c being exploited?

There is no known exploitation of GHSA-pffg-92cg-xf5c in the wild and no public exploit code.

## Fix prompt for AI coding agents

```text
Check this codebase for GHSA-pffg-92cg-xf5c and fix it if it is affected.

What it is: GHSA-pffg-92cg-xf5c is a vulnerability in github.com/consensys/gnark-crypto, published on 5 Oct 2023. 
Exploitation: There is no known exploitation of GHSA-pffg-92cg-xf5c in the wild and no public exploit code.

Affected packages and fixed versions:
- github.com/consensys/gnark-crypto (Go): affected all versions; fixed in 0.12.1
- github.com/consensys/gnark-crypto (Go): affected all versions; fixed in 0.12.1
- github.com/consensys/gnark-crypto (Go): affected all versions; fixed in 0.12.1

Task:
1. Search the dependency manifests and lockfiles (for example package.json, package-lock.json, requirements.txt, poetry.lock, uv.lock, pom.xml, build.gradle, go.mod, Cargo.lock, Gemfile.lock) and any Dockerfiles for the affected packages, including transitive dependencies.
2. If an affected version is in use, upgrade it to the fixed version for its release line with the project's package manager, so the lockfile is regenerated. Prefer the smallest upgrade that fixes it.
3. If there is no fix, or the upgrade would break the build, do not guess: explain the risk and the safest workaround instead.
4. Run the tests and summarise what changed and why.
5. If nothing in the project is affected, say so and change nothing.

Reference: https://insights.sbomify.com/GHSA-pffg-92cg-xf5c/ (Markdown: https://insights.sbomify.com/GHSA-pffg-92cg-xf5c/index.md)
```

## Sources

- OSV: https://osv.dev/vulnerability/GHSA-pffg-92cg-xf5c

---
From sbomify Insights: https://insights.sbomify.com/GHSA-pffg-92cg-xf5c/
Summaries are generated and may contain mistakes; the official description above is the record.
