ZK-Bootcamp - Homework 6 - Bilinear Pairings
Implement code that verifies the computation for the EC points.
\[0 = -A_1B_2 +\alpha_1\beta_2 + X_1\gamma_2 + C_1\delta_2\\X_1=x_1G1 + x_2G1 + x_3G1\]Pick any (nontrivial) values to generate the points that results a balanced equation.
Note that x1, x2, x3 are uint256 and the rest are G1 or G2 points.
You will need to take in the following as arguments to a public function:
\[A_1, B_2, C_1, x_1,x_2,x_3\] !python -m pip install py-ecc
from py_ecc.optimized_bn128 import G1, G2, add, multiply, pairing
def get_EC1_point(a: int):
return multiply(G1, a)
def get_EC2_point(a: int):
return multiply(G2, a)
# Constants
ALPHA_G1 = get_EC1_point(5)
BETA_G2 = get_EC2_point(3)
GAMMA_G2 = get_EC2_point(6)
DELTA_G2 = get_EC2_point(8)
def verify(A1, B2, C1, x1, x2, x3):
x1g1 = multiply(G1, x1)
x2g1 = multiply(G1, x2)
x3g1 = multiply(G1, x3)
X1 = add(add(x1g1, x2g1) , x3g1)
return (pairing(B2, A1) == pairing(BETA_G2, ALPHA_G1)*pairing(GAMMA_G2, X1)*pairing(DELTA_G2, C1))
# Valid solution
a1 = 7
b2 = 7
c1 = 2
x1 = 1
x2 = 1
x3 = 1
assert verify(get_EC1_point(a1), get_EC2_point(b2), get_EC1_point(c1), x1, x2, x3) == True
# Valid solution
a1 = 7
b2 = 7
c1 = 2
x1 = 3
x2 = 0
x3 = 0
assert verify(get_EC1_point(a1), get_EC2_point(b2), get_EC1_point(c1), x1, x2, x3) == True
# Invalid solution
a1 = 1
b2 = 2
c1 = 3
x1 = 4
x2 = 5
x3 = 6
assert verify(get_EC1_point(a1), get_EC2_point(b2), get_EC1_point(c1), x1, x2, x3) == False