Ethereum: `pairing` function in `py_ecc.bn128` results in `RecursionError`

Ethereum pairing function in py_ecc_bn128 leads to recursionerror

Ethereum’s mating function is often used for safe key exchange and digital signature check. When implementing the mating function with py_ecc_bn128, users often encounter a common problem: arecursionerror.

The problem

Ethereum: `pairing` function in `py_ecc.bn128` results in `RecursionError`

The problem arises from the way py_ecc_bn128 processes the exponiation in its mating function. In particular, it uses a technique called “Exponation by squares” to efficiently calculate the pairing result.

If we try to multiply two points with G1 and G2 with G1 * G2, the calls up ‘py_ecc_bn128to calculate the intermediate results of exponentiation by squeaking. In some cases, however, this recursive call can lead to a stack overflow error or another unexpected behavior.

The solution

To fix this problem, we have to change the mating function to avoid calling up recursively when the interim result exceeds a certain threshold. Here is the updated code:

Python

By py_ecc.bn128 Import G1, G2, Pairing, Multiply Multiply

DEF PAIRING_G1G2 (G1, G2):

"" ""

Calculate the mating of two points with exponentation by squares.

Args:

G1 (G1): The first point.

G2 (G2): The second point.

Returns:

Result: the computer pairing result.

"" ""

Calculate the pairings

P1 = pairing (G1, G2)

Q1 = G2.P

Multiply the pairings to get the end result

A = multiply (P1, Q1)

Return A

In this updated implementation, we define a new function pairing_g1g2, which requires two points as input and returns your pairing result. We calculate the pairings with exponiation by squaring them and then multiplying in order to obtain the end result.

Example use

To demonstrate how the modified mating function is used, we create two instances of the objects “G1” and “G2” and carry out a mating operation:

`Python

Create instances of G1 and G2

G1 = g1.from_pem ("your private key")

G2 = g2.from_pem ("your public key")

Calculate the pairings

P1 = pairing (G1, G2)

Q1 = G2.P

Multiply the pairings to get the end result

A = multiply (P1, Q1)

Print (a)

edition: The calculated pairing result

By changing the mating function in this way, you should no longer come across a “recursionerror” for a safe key exchange and digital signature check when using pa_ecc_bn128 for a safe key exchange.

Ethereum Antminers There

Leave a Comment

Your email address will not be published. Required fields are marked *