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

Ethereum pairing function in Py_ecc_bn128 results in recursionror

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

Ethereum’s pairing function is widely used for safe key exchange and a digital signature check. However, when implementing the pairing function using Py_ecc_bn128, users of meeting with the usual problem: ARecursiorror.

Problem

The problem stems from the way Py_Ecc_bn128 manages exposure to its pairing function. In particular, it uses a technique called “separation exposure” to effectively calculate the pairing results.

When we try to multiply two points G1 and G2 using G1 * G2,Py_Ecc_bn128 calls herself recursive to calculate the intermediate results of exponent of squares. However, in some cases, this recursive call can lead to a bundle of beam overflow or other unexpected behavior.

Solution

In order to solve this problem, we need to change the pairing function to avoid recursive calling when the intermediate result exceeds a particular threshold. Here’s an updated code:

`Python

of Py_ecc.bn128 Import G1, G2, Paper, multiply

Def Pairing_g1g2 (G1, G2):

"" "" "

Calculates the pairing of two points using exponent.

ARGS:

G1 (G1): The first point.

G2 (G2): Second point.

Returns:

The result: the result of the calculated pairing.

"" "" "

Calculate couples

P1 = pairing (G1, G2)

Q1 = G2.p

Multiply pairs to achieve the final result

A = Multiply (P1, Q1)

Return a

In this updated implementation, we define a new function pairing_g1g2 that takes two points as an entrance and restores their pairing result. We calculate the lines using exponent by multiplying them and then multiplying them to achieve the final result.

Example of use

To show how to use a modified pairing function, we create two cases of G1 IG2 and performance of pairing surgery:

`Python

Create cases G1 and G2

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

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

Calculate couples

P1 = pairing (G1, G2)

Q1 = G2.p

Multiply pairs to achieve the final result

A = Multiply (P1, Q1)

Print (s)

out: Calculated pairing result

By changing the pairing function in this way, you should no longer come across the Recursorrrror when you use ‘Py_ecc_bn128` for a safe key exchange and a digital signature check.

Ethereum Mistype Address When Payment

Share this post
Facebook
Twitter
LinkedIn
WhatsApp