Skip to content

Commit

Permalink
Update arithmetic_circuits.py
Browse files Browse the repository at this point in the history
  • Loading branch information
dple committed Mar 21, 2024
1 parent 97405b8 commit f13accc
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions Python/arithmetic_circuits.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
"""
Demonstrate some ZKP examples in Python
This Python code demonstrate the examples discussed in the following article by Rareskills:
How arithmetic circuits are used to verify zero knowledge proofs
https://www.rareskills.io/post/zk-circuits
"""

from py_ecc.bn128 import curve_order

# 1. Prove b is the inverse of a
# The order of the base field of the BN254 curve
base_field = 21888242871839275222246405745257275088548364400416034343698204186575808495617
p = curve_order
def compute_inverse(a):
return pow(a, -1, base_field)
return pow(a, -1, p)

# Prover --> compute the inverse of a, then give it to the verifier
a = 15
b = compute_inverse(a)

# Verifier --> just verify, won't carry out computation
assert (a*b) % base_field == 1, "Given number is not an inverse of a"
# Verifier --> just verify, won't carry out any inversion
assert (a*b) % p == 1, "Given number b is not an inverse of a"

# 2. Prove x is the solution of a polynomial
def polyeval(x, a, b, c):
Expand All @@ -24,10 +29,9 @@ def polyeval(x, a, b, c):
a = 2
b = -23
c = 11
s = polyeval(x, a, b, c)

# Verifier --> just verify, won't carry out computation
assert s == 0, "Given x is not a solution of the polynomial"
# Verifier --> just verify if x is the solution, don't carry out any computations how to find x
assert 0 == polyeval(x, a, b, c), "Given x is not a solution of the polynomial"

# 3. Proving x is the maximum element in a list of elements
def maxList(lst):
Expand All @@ -38,11 +42,10 @@ def maxList(lst):
m = maxList(lst)
x = 11

# Verifier --> just verify, won't carry out computation
# Verifier --> just verify, don't execute any computations how to find the max value
assert x == m, "Given input is not the maximum of the list"

# 4. Prove if the given list was sorted

# Prover --> sort the list then give it to the verifier
lst = [1, 2, 3, 4, 5, 6]
sortedList = sorted(lst)
Expand All @@ -51,7 +54,6 @@ def maxList(lst):
assert lst == sortedList, "List is not sorted"

# 5. Prove if the list has no duplicates

# Prover --> convert the list to a set, then compare their size
s = set(lst)

Expand Down

0 comments on commit f13accc

Please sign in to comment.