# Erasure recovery in G1

**URL:** <https://forum.autonomys.xyz/t/erasure-recovery-in-g1/1301>\
**Category:** Research\
**Created:** [March 23, 2023, 11:57am UTC](https://forum.autonomys.xyz/t/erasure-recovery-in-g1/1301 "2023-03-23T11:57:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dariolina](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.autonomys.xyz/dariolina/32/743_2.png) [@dariolina](https://forum.autonomys.xyz/u/dariolina)\
**Post date:** [March 23, 2023, 11:57am UTC](https://forum.autonomys.xyz/t/erasure-recovery-in-g1/1301/1 "2023-03-23T11:57:18Z")

</div>

To erasure code `N` source chunks of the records (as scalars in `Fr` of the BLS12-381 curve) and source commitments (as `G1` points on the same curve) to `2N`, we use the following approach:

1. Inverse FFT (in Fr or G1, respectively) to interpolate a degree `N-1` polynomial
2. FFT to evaluate it over domain of size `2N`

Then to recover the source chunks from a random sample of `N` chunks we use a known approach described by Vitalik [here](https://ethresear.ch/t/reed-solomon-erasure-code-recovery-in-n-log-2-n-time-with-ffts/3039). @Chen_Feng_2023 was kind to explain it on a simple example:  
Consider a simple polynomial `f(x) = x + 1` over `Z_5`. Note that 2 is a primitive 4th root of unity. We have the following evaluations:

```auto
f(2^0) = 2,
f(2^1) = 3,
f(2^2) = 0,
f(2^3) = 4

```

Say, we are missing two evaluations, `f(2^1) = 3` and `f(2^3) = 4`. How can we recover `f(x)` using FFT-based algorithms?

Input:

```auto
f(2^0) = 2,
f(2^1) = null,
f(2^2) = 0,
f(2^3) = null

```

By definition, we have `Z(x) = (x - 2^1)(x - 2^3)`, `D(x) = f(x)`, and `E(x)` that evaluates to our input with erasures:

```auto
E(2^0) = 2
E(2^1) = 0
E(2^2) = 0
E(2^3) = 0

```

Clearly, we have `D(x) Z(x) = E(x) Z(x)`. Also,

```auto
E(2^0)*Z(2^0) = 2 * 2 = 4
E(2^1)*Z(2^1) = 0 * 0 = 0
E(2^2)*Z(2^2) = 0 * 2 = 0
E(2^3)*Z(2^3) = 0 * 0 = 0

```

By interpolating the above four points, we have a unique polynomial  
`g(x) = (x - 2^1) (x - 2^2) (x - 2^3)`. If we can prove that `D(x) Z(x) = g(x)`, then we can recover `D(x)`, which is the source `f(x)`.  
Note that, by construction, `g(x)` is of degree at most 3, and `Z(x)` is of degree 2. Also, by definition, `f(x)` is of degree at most 1.

**Proof by contradiction**. Suppose that we have another polynomial `f’(x)` of degree at most 1 such that `f’(x) Z(x) = f(x) Z(x)`. Then, we have `f’(2^0) = f(2^0)` and `f’(2^2) = f(2^2)`. Thus, `f’(x) - f(x)` has at least two zeros. This a contradiction because `f’(x) - f(x)` is of degree at most 1.

How can we compute `g(x)/Z(x)`?

Method 1: Extended Euclidean algorithm.

Method 2: Introducing a random `k` as suggested by Vitalik. **Note that `k` shouldn’t be** in the set `{2^0, 2^1, 2^2, 2^3 }`. Vitalik hasn’t emphasized this.

**Open questions**  
Can the recovery algorithm as above be adapted to work with `G1` points so we can recover erased commitments? I feel like it could since we can do FFT in `G1`, but I’m hesitant about `E(x)*Z(x)` multiplication  
Alternatively, is there another algorithm that works in `G1`?

---

<div class="post-metadata">

**Author:** ![dariolina](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.autonomys.xyz/dariolina/32/743_2.png) [@dariolina](https://forum.autonomys.xyz/u/dariolina)\
**Post date:** [March 23, 2023, 3:19pm UTC](https://forum.autonomys.xyz/t/erasure-recovery-in-g1/1301/2 "2023-03-23T15:19:48Z")

</div>

I have noticed that the zero-polynomial `Z(x)` does not have to be in `G1`. Then we can compute `E(x) Z(x)`easily if only `E(x)` has coefficients in `G1`.

---

<div class="post-metadata">

**Author:** ![Barak](https://avatars.discourse-cdn.com/v4/letter/b/dbc845/32.png) [@Barak](https://forum.autonomys.xyz/u/Barak)\
**Post date:** [March 23, 2023, 5:30pm UTC](https://forum.autonomys.xyz/t/erasure-recovery-in-g1/1301/3 "2023-03-23T17:30:06Z")

</div>

I’m actually not sure what you mean by “FFT in `G1`”, but how do you run the original erasure coding on the commitments? The implementation seems to do (inverse) “FFT in `G1`” – my understanding is that it’s FFT in the base field `F_q`, though I couldn’t find the actual code – so you should be able to use the same approach.

The way I believe that it works is the following:  
Assuming affine representation, you represent the commitments as `(x,y) \in (F_q)^2`. Given erasures, you can naively run 2 FFTs on the existing data, one for `x` and one for `y`, and restore the missing points. Since `y` could be represented by a single bit, there may be a more efficient approach using a single FFT.

Note that you can totally ignore the context in which the data set consists of points in some subgroup of an elliptic curve. You just work on `F_q`, and by construction all of the data set points are also in `G1`.

---

<div class="post-metadata">

**Author:** ![dariolina](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.autonomys.xyz/dariolina/32/743_2.png) [@dariolina](https://forum.autonomys.xyz/u/dariolina)\
**Post date:** [March 23, 2023, 7:37pm UTC](https://forum.autonomys.xyz/t/erasure-recovery-in-g1/1301/4 "2023-03-23T19:37:27Z")

</div>

There is FFT in G1 implemented in multiple libraries: the set of `G1` points is represented as evaluations `{w^0, P_0}....{w^N, P_N}` of some polynomial with coefficients in `G1` where w is a primitive Nth root of unity. I already have a prototype implementation of erasure coding this way [here](https://github.com/sifraitech/rust-kzg/commit/df7bd04a851681196e0b8c55d1b8cf738fecd31a#diff-2a4bddf48f7fc9c9800f19b5e1cd9a725a9b4a56881e5063efde9545afe75c7cR122), but we haven’t added it to Subspace yet.

> [@Barak](#):
>
> Note that you can totally ignore the context in which the data set consists of points

We would like to preserve the homomorphic properties and treat commitments here as points - this will allow use to show that commitments to erasure-coded data are correct - because they are themselves erasure coded from source commitments. We get this idea from [Information Dispersal with Provable Retrievability for Rollups](https://eprint.iacr.org/2021/1544.pdf) p.7 eq.10

---

<div class="post-metadata">

**Author:** ![dariolina](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.autonomys.xyz/dariolina/32/743_2.png) [@dariolina](https://forum.autonomys.xyz/u/dariolina)\
**Post date:** [March 24, 2023, 11:00am UTC](https://forum.autonomys.xyz/t/erasure-recovery-in-g1/1301/5 "2023-03-24T11:00:07Z")

</div>

I have resolved the problem and implemented a prototype [here](https://github.com/dariolina/rust-kzg/commit/f158880d445c8a8d8c34a7b1e4e9fe35c5bb85b8). It’s based on `rust-kzg` and the main thing to look at is the `recover_poly_from_samples_g1` [function](https://github.com/dariolina/rust-kzg/commit/f158880d445c8a8d8c34a7b1e4e9fe35c5bb85b8#diff-4ede6c1ba6742d0054410e1967959e55bc2b33841739279c86f652b9d6312563R17).  
Given a set of length `2N` of points in `G1` (`N` source, `N` erasure-coded) where up to `N` are missing (`None`) I want to recover the missing points:

1. I create `Z(x): Fr->Fr`, which evaluates to 0 on the indices of the missing indices.
2. I compute `g(x) = E * Z(x), g(x): Fr->G1` which evaluates to identity in `G1` on the missing indices and to `P_i*Z(i)` on the present points. I do not explicitly compute `E(x)`, but that’s an efficiency detail. Now as we have proven above, `D(x) Z(x) = g(x)` and `D(x):Fr->G1` would evaluate to our original data.
3. To extract `D(x) = g(x)/Z(x)` and avoid `0/0` division, I use the trick with “random” `k` chosen to be `5`, which is practically guaranteed to not be in our original set of evaluation points - powers of the primitive root of unity. So I compute the two scaled polynomials `Q1(x)=D*Z(k*x)` and `Q2(x)=Z(k*x)`
4. Then I compute the division `Q3(x) = Q1(x)/Q2(x) = Q1(x) * Q2^(-1)(x) = D*Z(k*x) * (Z(k*x))^(-1) = D(k*x)`
5. I unscale `D(k*x)` to `D(x)`
6. Lastly, because `D(x)` is in coefficient form with coefficients in `G1`, I perform an FFT in `G1` and obtain the evaluations which equal our original set of points.

I have [tested](https://github.com/dariolina/rust-kzg/commit/f158880d445c8a8d8c34a7b1e4e9fe35c5bb85b8#diff-5e9913e2491dbf414cc88babdd9d2157728c6123314eb33f88dc3b242ab9a560R180) it by generating some `N` random `G1` points, extending them via FFT to a domain twice as large, replacing some random with `None` and trying to recover with `recover_poly_from_samples_g1`. Seems to work and recover correctly.

---

<div class="post-metadata">

**Author:** ![dariolina](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.autonomys.xyz/dariolina/32/743_2.png) [@dariolina](https://forum.autonomys.xyz/u/dariolina)\
**Post date:** [August 21, 2023, 3:23pm UTC](https://forum.autonomys.xyz/t/erasure-recovery-in-g1/1301/6 "2023-08-21T15:23:34Z")

</div>

Integrated into Subspace reconstructor [here](https://github.com/subspace/subspace/commit/a2d931f291120b793506a1452ce63093257c9e45) gives ~10% speedup (in non-parallel case) over current implementation that recommits recovered source pieces. Since my implementation is not state-of-art, there are probably more significant gains.
