Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 32 additions & 25 deletions plonk.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -333,18 +333,18 @@
"source": [
"# Lets do the first. write fird permutation yourself \n",
"from plonk.copy_constraint import find_permutation\n",
"from plonk.poly import polynomial_eval\n",
"from plonk.poly import polynomial_eval, gen_poly\n",
"\n",
"witness = a + b + c\n",
"eval_domain = range(0,len(witness))\n",
"witness_x_a = find_permutation(range(0,len(a)), range(0,len(a)))\n",
"witness_x_b = find_permutation(range(len(a),len(b)*2), range(len(a),len(b)*2))\n",
"witness_x_c = find_permutation(range(len(b)*2, len(c)*3), range(len(a)*2,len(a)*3))\n",
"index_a = gen_poly(range(0,len(a)), eval_domain[:len(a)])\n",
"index_b = gen_poly(range(0,len(a)), eval_domain[len(a):2*len(a)])\n",
"index_c = gen_poly(range(0,len(a)), eval_domain[2*len(a):])\n",
"\n",
"witness_y = find_permutation(witness, eval_domain)\n",
"witness_y = gen_poly(eval_domain, witness)\n",
"\n",
"for i, val in enumerate(witness):\n",
" assert(val == polynomial_eval(witness_y, i))\n"
" assert(float(val) == polynomial_eval(witness_y, i))\n"
]
},
{
Expand All @@ -361,22 +361,27 @@
"outputs": [],
"source": [
"# Okay now lets rearrange it so that the valuse get swapped when they match\n",
"from plonk.sample_problem import gen_copy_constraints\n",
"from plonk.copy_constraint import find_permutation\n",
"\n",
"witness_x_a_perm, witness_x_b_perm, witness_x_c_perm, copy_constarints = gen_copy_constraints()\n",
"permuted_indices = find_permutation(witness)\n",
"permuted_witness_y = [witness[i] for i in permuted_indices]\n",
"\n",
"perm_a_poly = gen_poly(range(0,len(a)), permuted_indices[:len(a)])\n",
"perm_b_poly = gen_poly(range(0,len(a)), permuted_indices[len(a):2*len(a)])\n",
"perm_c_poly = gen_poly(range(0,len(a)), permuted_indices[2*len(a):])\n",
"perm_witness = gen_poly(eval_domain, permuted_witness_y)\n",
"\n",
"for i in range(0,len(a)):\n",
" assert(polynomial_eval(witness_y , polynomial_eval(witness_x_a, i)) == \n",
" polynomial_eval (witness_y ,polynomial_eval(witness_x_a_perm, i)))\n",
" assert(polynomial_eval(witness_y , polynomial_eval(index_a, i)) == \n",
" polynomial_eval(perm_witness ,polynomial_eval(perm_a_poly, i)))\n",
"\n",
"for i in range(len(a), len(a)*2):\n",
" assert(polynomial_eval(witness_y , polynomial_eval(witness_x_b, i)) == \n",
" polynomial_eval (witness_y ,polynomial_eval(witness_x_b_perm, i)))\n",
"for i in range(0,len(a)):\n",
" assert(polynomial_eval(witness_y , polynomial_eval(index_b, i)) == \n",
" polynomial_eval(perm_witness ,polynomial_eval(perm_b_poly, i)))\n",
" \n",
"for i in range(len(a)*2, len(a)*3):\n",
" assert(polynomial_eval(witness_y , polynomial_eval(witness_x_c, i)) == \n",
" polynomial_eval (witness_y ,polynomial_eval(witness_x_c_perm, i)))\n",
" "
"for i in range(0,len(a)):\n",
" assert(polynomial_eval(witness_y , polynomial_eval(index_c, i)) == \n",
" polynomial_eval (perm_witness ,polynomial_eval(perm_c_poly, i)))"
]
},
{
Expand All @@ -398,7 +403,6 @@
"metadata": {},
"outputs": [],
"source": [
"\n",
"from plonk.copy_constraint import copy_constraint_simple \n",
"\n",
"# we have to generate v1 and v2 after a, b and c have been fixed.\n",
Expand All @@ -407,15 +411,18 @@
"\n",
"eval_domain = range(0, len(a)*3)\n",
"\n",
"a_poly = gen_poly(range(0,len(a)), a)\n",
"b_poly = gen_poly(range(0,len(a)), b)\n",
"c_poly = gen_poly(range(0,len(a)), c)\n",
"\n",
"x, Y , Px_a, rlc = copy_constraint_simple(range(0,len(a)), witness_x_a, witness_y, v1, v2)\n",
"x, Y , Px_b, rlc = copy_constraint_simple(range(len(a),len(a)*2), witness_x_b, witness_y, v1, v2)\n",
"x, Y , Px_c, rlc = copy_constraint_simple(range(len(a)*2,len(a)*3), witness_x_c, witness_y, v1, v2)\n",
"x, Y , Px_a, rlc = copy_constraint_simple(range(0,len(a)), index_a, a_poly, v1, v2)\n",
"x, Y , Px_b, rlc = copy_constraint_simple(range(0,len(a)), index_b, b_poly, v1, v2)\n",
"x, Y , Px_c, rlc = copy_constraint_simple(range(0,len(a)), index_c, c_poly, v1, v2)\n",
"\n",
"# calcualte permutated polynomial \n",
"x_1, Y_1 , Px_a_prime, rlc_1 = copy_constraint_simple(range(0,len(a)), witness_x_a_perm, witness_y, v1, v2)\n",
"x_1, Y_1 , Px_b_prime, rlc_1 = copy_constraint_simple(range(len(a),len(a)*2), witness_x_b_perm, witness_y, v1, v2)\n",
"x_1, Y_1 , Px_c_prime, rlc_1 = copy_constraint_simple(range(len(a)*2,len(a)*3), witness_x_c_perm, witness_y, v1, v2)\n",
"x_1, Y_1 , Px_a_prime, rlc_1 = copy_constraint_simple(range(0,len(a)), perm_a_poly, a_poly, v1, v2)\n",
"x_1, Y_1 , Px_b_prime, rlc_1 = copy_constraint_simple(range(0,len(a)), perm_b_poly, b_poly, v1, v2)\n",
"x_1, Y_1 , Px_c_prime, rlc_1 = copy_constraint_simple(range(0,len(a)), perm_c_poly, c_poly, v1, v2)\n",
"\n",
"assert(Px_a[-1] * Px_b[-1] * Px_c[-1] == Px_a_prime[-1] * Px_b_prime[-1] * Px_c_prime[-1])\n",
"assert(Px_a[0] == Px_b[0] == Px_c[0] == Px_a_prime[0] == Px_b_prime[0] == Px_c_prime[0] == 1 )"
Expand Down Expand Up @@ -1162,7 +1169,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
"version": "3.7.6"
}
},
"nbformat": 4,
Expand Down
20 changes: 13 additions & 7 deletions plonk/copy_constraint.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .poly import lagrange, polynomial_eval
from .poly import polynomial_eval


def copy_constraint_simple(eval_domain, Xcoef, Ycoef, v1, v2):
Expand All @@ -9,16 +9,22 @@ def copy_constraint_simple(eval_domain, Xcoef, Ycoef, v1, v2):

for i in range(0, len(eval_domain)):
x.append(polynomial_eval(Xcoef, eval_domain[i]))
Y.append(polynomial_eval(Ycoef, x[i]))
Y.append(polynomial_eval(Ycoef, eval_domain[i]))

rlc.append(v1 + x[i] + v2 * Y[i])
Px.append(Px[i] * (v1 + x[i] + v2 * Y[i]))

return (x, Y, Px, rlc)


def find_permutation(copies, eval_domain):

perm = lagrange(eval_domain, copies)
perm = [float(x) for x in reversed(perm.coefficients)]
return perm
def find_permutation(wires):
# This function takes an array "wires" of arbitrary values and returns an
# array with shuffles the indices of "wires" for repeating values
size = len(wires)
permutation = [i for i in range(size)]
for i in range(size):
for j in range(i + 1, size):
if wires[i] == wires[j]:
permutation[i], permutation[j] = permutation[j], permutation[i]
break
return permutation
28 changes: 14 additions & 14 deletions plonk/plonk.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,38 @@


def verify_naieve(Ql, Qr, Qm, Qo, Qc, a, b, c, witness_y, perm_a, perm_b, perm_c):

# make sure constarints + witness is statisfied
assert is_satisfied(Ql, Qr, Qm, Qo, Qc, a, b, c) == True

# original polynomial

Xcoef = [0, 1]
Xcoef_a = [0, 1]
Xcoef_b = [len(a), 1]
Xcoef_c = [2 * len(a), 1]

# todo: replace with random

v1 = hash(str(a + b + c))
v2 = hash(str(c + a + b))

a_poly = gen_poly(range(0, len(a)), a)
b_poly = gen_poly(range(0, len(a)), b)
c_poly = gen_poly(range(0, len(a)), c)
eval_domain = range(0, len(a) * 3)

x, Y, Px_a, rlc = copy_constraint_simple(range(0, len(a)), Xcoef, witness_y, v1, v2)
x, Y, Px_b, rlc = copy_constraint_simple(
range(len(a), len(a) * 2), Xcoef, witness_y, v1, v2
)
x, Y, Px_c, rlc = copy_constraint_simple(
range(len(a) * 2, len(a) * 3), Xcoef, witness_y, v1, v2
)
x, Y, Px_a, rlc = copy_constraint_simple(range(0, len(a)), Xcoef_a, a_poly, v1, v2)
x, Y, Px_b, rlc = copy_constraint_simple(range(0, len(a)), Xcoef_b, b_poly, v1, v2)
x, Y, Px_c, rlc = copy_constraint_simple(range(0, len(a)), Xcoef_c, c_poly, v1, v2)

# calcualte permutated polynomial
x_1, Y_1, Px_a_prime, rlc_1 = copy_constraint_simple(
range(0, len(a)), perm_a, witness_y, v1, v2
range(0, len(a)), perm_a, a_poly, v1, v2
)
x_1, Y_1, Px_b_prime, rlc_1 = copy_constraint_simple(
range(len(a), len(a) * 2), perm_b, witness_y, v1, v2
range(0, len(a)), perm_b, b_poly, v1, v2
)
x_1, Y_1, Px_c_prime, rlc_1 = copy_constraint_simple(
range(len(a) * 2, len(a) * 3), perm_c, witness_y, v1, v2
range(0, len(a)), perm_c, c_poly, v1, v2
)

assert (
Expand All @@ -56,7 +56,7 @@ def verify_naieve(Ql, Qr, Qm, Qo, Qc, a, b, c, witness_y, perm_a, perm_b, perm_c


def create_proof(a, b, c):

poly = gen_poly(a + b + c)
domain = range(0, len(a) * 3)
poly = gen_poly(domain, a + b + c)

return (a, b, c, poly)
3 changes: 1 addition & 2 deletions plonk/poly.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def polynomial_division(poly, q):
return (False, result)


def gen_poly(y):
x = range(0, len(y))
def gen_poly(x, y):
poly = lagrange(x, y)
poly = [float(x) for x in reversed(poly.coefficients)]
return poly
8 changes: 4 additions & 4 deletions plonk/sample_problem.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .copy_constraint import find_permutation
from .constraint import add_mul_constarint, add_add_constarint, add_constant_constraint
from .poly import gen_poly


def gen_witness(x):
Expand Down Expand Up @@ -53,9 +53,9 @@ def gen_copy_constraints():

eval_domain = range(0, len(copy_constraints))

x_a_prime = find_permutation(copy_constraints[0:6], eval_domain[0:6])
x_b_prime = find_permutation(copy_constraints[6:12], eval_domain[6:12])
x_c_prime = find_permutation(copy_constraints[12:18], eval_domain[12:18])
x_a_prime = gen_poly(eval_domain[0:6], copy_constraints[0:6])
x_b_prime = gen_poly(eval_domain[0:6], copy_constraints[6:12])
x_c_prime = gen_poly(eval_domain[0:6], copy_constraints[12:18])

return (x_a_prime, x_b_prime, x_c_prime, copy_constraints)

Expand Down
9 changes: 4 additions & 5 deletions tests/test_constraint.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from plonk.constraint import add_add_constarint, is_satisfied, add_mul_constarint
from plonk.copy_constraint import copy_constraint_simple, find_permutation
from plonk.copy_constraint import copy_constraint_simple
from plonk.sample_problem import gen_witness, setup
from plonk.plonk import create_proof, verify_naieve
from plonk.poly import polynomial_division, polynomial_eval
from plonk.poly import polynomial_division, polynomial_eval, gen_poly


def test_addition():
Expand Down Expand Up @@ -97,7 +97,7 @@ def test_copy_constraint_simple():

copies = [0, 3, 2, 1]

Xcoef = find_permutation(copies, eval_domain)
Xcoef = gen_poly(copies, eval_domain)

eval_domain = [0, 1, 2, 3]

Expand Down Expand Up @@ -134,9 +134,8 @@ def test_permutation():

witness = [3, 9, 27, 1, 1, 30, 3, 3, 3, 5, 35, 5, 9, 27, 30, 5, 35, 35]
eval_domain = [i for i in range(0, len(witness))]
witness_x_1 = find_permutation(eval_domain, eval_domain)

witness_y = find_permutation(witness, eval_domain)
witness_y = gen_poly(eval_domain, witness)

for i, val in enumerate(witness):
assert val == polynomial_eval(witness_y, i)