Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Operators #27

Merged
merged 29 commits into from
Aug 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
91a8560
Add .zkif files to gitignore
gxavier38 Jun 17, 2021
38454e1
Move fixedpoint back to its own file
gxavier38 Jun 10, 2021
e9d5620
Add LinComb operators
gxavier38 Jun 21, 2021
d0d85d2
Add fixedpoint operators
gxavier38 Jun 21, 2021
cb8c489
Add LinCombBool and add bitwise ops to LinComb
gxavier38 Jun 21, 2021
73212f4
Add tests
gxavier38 Jun 21, 2021
6d2fb8a
Add CircleCI
gxavier38 Jun 21, 2021
c75f476
Rename ggh_hash
gxavier38 Jun 16, 2021
9b7e12f
Add Poseidon hash
gxavier38 Jun 17, 2021
8d7e4bb
Fix benchmark comment
gxavier38 Jun 21, 2021
2cb743e
Add scaling back to constructor in LinCombFxp
gxavier38 Jun 22, 2021
47edd87
Fix Poseidon dropping LinCombFxps and LinCombBools
gxavier38 Jun 22, 2021
a5d7534
Make fixedpoint truediv lose resolution rather than throw an error
gxavier38 Jun 22, 2021
00a4456
Fix truediv constraint with ignore_errors
gxavier38 Jun 29, 2021
87a9030
Make divmod return PrivVal instead of ConstVal
gxavier38 Jun 29, 2021
7c93f8c
Add missing division constraint and update constraint docs
gxavier38 Jun 29, 2021
ea49bc2
Remove unneeded scaling in fixedpoint multiplication
gxavier38 Jul 6, 2021
ebe33c0
Make LinComb rshift use to_bits
gxavier38 Jul 6, 2021
ab6b8c3
Add more bench tests for LinCombFxp operations
gxavier38 Jul 6, 2021
277e8ca
Improve LinCombBool __pow__
gxavier38 Jul 12, 2021
a63f950
Remove unneeded constraint from assert_range
gxavier38 Jul 12, 2021
63b1d37
Simplify check_nonzero
gxavier38 Jul 12, 2021
b0bbeb0
Improve check_zero performance
gxavier38 Jul 12, 2021
2dfc9a1
Allow unlimited exponents in pow
gxavier38 Jul 12, 2021
13d2bd8
Allow boolean operations with non-bool/non-int types
gxavier38 Jul 12, 2021
2b4ce09
Test LinCombBool operators with non-LinComb types
gxavier38 Jul 12, 2021
347c89b
Fix scaling removal in LinCombFxp.__mul__
gxavier38 Jul 12, 2021
9063e2c
Remove unneeded comment
gxavier38 Jul 12, 2021
b3c8971
Increase nobackend modulus to pass tests
gxavier38 Jul 15, 2021
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
Empty file added test/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest
from pysnark import runtime

@pytest.fixture(scope="session")
def disable_output():
"""
Disables all circuit outputs when running tests
"""
yield
runtime.autoprove = False

def load_backend(backend):
import sys
import importlib
from pysnark import runtime

backends = [
["libsnark", "pysnark.libsnark.backend"],
["libsnarkgg", "pysnark.libsnark.backendgg"],
["qaptools", "pysnark.qaptools.backend"],
["snarkjs", "pysnark.snarkjsbackend"],
["zkinterface", "pysnark.zkinterface.backend"],
["zkifbellman", "pysnark.zkinterface.backendbellman"],
["zkifbulletproofs", "pysnark.zkinterface.backendbulletproofs"],
["nobackend", "pysnark.nobackend"]
]

for mod in backends:
if backend == mod[0]:
runtime.backend = importlib.import_module(mod[1])
return

raise RuntimeError("Invalid backend for test")
31 changes: 31 additions & 0 deletions test/test_branching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest
from pysnark.runtime import PrivVal
from pysnark.boolean import PrivValBool
from pysnark.fixedpoint import PrivValFxp
from pysnark.branching import if_then_else

class TestLinCombFxp():
def test_lincomb_branch(self):
assert if_then_else(PrivValBool(0), PrivVal(1), PrivVal(2)).val() == 2
assert if_then_else(PrivValBool(1), PrivVal(1), PrivVal(2)).val() == 1

def test_lincombbool_branch(self):
assert if_then_else(PrivValBool(0), PrivValBool(1), PrivValBool(0)).val() == 0
assert if_then_else(PrivValBool(1), PrivValBool(1), PrivValBool(0)).val() == 1

def test_lincombfxp_branch(self):
assert if_then_else(PrivValBool(0), PrivValFxp(1.5), PrivValFxp(2.5)).val() == 2.5
assert if_then_else(PrivValBool(1), PrivValFxp(1.5), PrivValFxp(2.5)).val() == 1.5

def test_mixed_branch(self):
assert if_then_else(PrivValBool(0), PrivVal(1), PrivValFxp(2.5)).val() == 2.5
assert if_then_else(PrivValBool(1), PrivValFxp(1.5), PrivValBool(1)).val() == 1.5
assert if_then_else(PrivValBool(1), PrivValBool(1), PrivVal(2)).val() == 1

def test_branch_condition(self):
with pytest.raises(RuntimeError):
if_then_else(PrivVal(True), PrivVal(1), PrivVal(2))
with pytest.raises(RuntimeError):
if_then_else(PrivVal(1), PrivVal(1), PrivVal(2))
with pytest.raises(RuntimeError):
if_then_else(PrivValFxp(1), PrivVal(1), PrivVal(2))
148 changes: 148 additions & 0 deletions test/test_lincomb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
from pysnark import runtime
from pysnark.runtime import PrivVal, PubVal

class TestLinComb():
def test_priv_val(self):
assert PrivVal(3).val() == 3

def test_pub_val(self):
assert PubVal(3).val() == 3

def test_add(self):
assert (PubVal(1) + 2).val() == 3
assert (2 + PubVal(1)).val() == 3
assert (PrivVal(1) + PrivVal(2)).val() == 3

def test_mul(self):
assert (PrivVal(2) * 2).val() == 4
assert (2 * PrivVal(2)).val() == 4
assert (PrivVal(2) * PrivVal(2)).val() == 4

def test_sub(self):
assert (PrivVal(2) - 1).val() == 1
assert (2 - PrivVal(1)).val() == 1
assert (PrivVal(2) - PrivVal(1)).val() == 1

def test_div_integer(self):
assert (PrivVal(4) / 2).val() == 2
assert (4 / PrivVal(2)).val() == 2
assert (PrivVal(4) / PrivVal(2)).val() == 2

def test_floor_div(self):
assert (PrivVal(4) // 2).val() == 2
assert (4 // PrivVal(2)).val() == 2
assert (PrivVal(4) // PrivVal(2)).val() == 2

assert (PrivVal(5) // 2).val() == 2
assert (5 // PrivVal(2)).val() == 2
assert (PrivVal(5) // PrivVal(2)).val() == 2

def test_mod(self):
assert (PrivVal(5) % 2).val() == 1
assert (5 % PrivVal(2)).val() == 1
assert (PrivVal(5) % PrivVal(2)).val() == 1

assert (PrivVal(5) % 1).val() == 0
assert (5 % PrivVal(1)).val() == 0
assert (PrivVal(5) % PrivVal(1)).val() == 0

def test_pow(self):
runtime.bitlength = 5

assert (PrivVal(2) ** 4).val() == 16
assert (2 ** PrivVal(4)).val() == 16
assert (PrivVal(2) ** PrivVal(4)).val() == 16

assert (PrivVal(2) ** 0).val() == 1
assert (2 ** PrivVal(0)).val() == 1
assert (PrivVal(2) ** PrivVal(0)).val() == 1

runtime.bitlength = 16

def test_rshift(self):
runtime.bitlength = 5

assert (PrivVal(2) << 2).val() == 8
assert (2 << PrivVal(2)).val() == 8
assert (PrivVal(2) << PrivVal(2)).val() == 8

runtime.bitlength = 16

def test_lshift(self):
runtime.bitlength = 5

assert (PrivVal(8) >> 2).val() == 2
assert (8 >> PrivVal(2)).val() == 2
assert (PrivVal(8) >> PrivVal(2)).val() == 2

runtime.bitlength = 16

def test_lt(self):
assert((PrivVal(1) < 2).val() == 1)
assert((1 < PrivVal(2)).val() == 1)
assert((PrivVal(1) < PrivVal(2)).val() == 1)

assert((PrivVal(2) < 1).val() == 0)
assert((2 < PrivVal(1)).val() == 0)
assert((PrivVal(2) < PrivVal(1)).val() == 0)

assert((PrivVal(1) < 1).val() == 0)
assert((1 < PrivVal(1)).val() == 0)
assert((PrivVal(1) < PrivVal(1)).val() == 0)

def test_gt(self):
assert((PrivVal(2) > 1).val() == 1)
assert((2 > PrivVal(1)).val() == 1)
assert((PrivVal(2) > PrivVal(1)).val() == 1)

assert((PrivVal(1) > 2).val() == 0)
assert((1 > PrivVal(2)).val() == 0)
assert((PrivVal(1) > PrivVal(2)).val() == 0)

assert((PrivVal(1) > 1).val() == 0)
assert((1 > PrivVal(1)).val() == 0)
assert((PrivVal(1) > PrivVal(1)).val() == 0)

def test_lte(self):
assert((PrivVal(1) <= 2).val() == 1)
assert((1 <= PrivVal(2)).val() == 1)
assert((PrivVal(1) <= PrivVal(2)).val() == 1)

assert((PrivVal(2) <= 1).val() == 0)
assert((2 <= PrivVal(1)).val() == 0)
assert((PrivVal(2) <= PrivVal(1)).val() == 0)

assert((PrivVal(1) <= 1).val() == 1)
assert((1 <= PrivVal(1)).val() == 1)
assert((PrivVal(1) <= PrivVal(1)).val() == 1)

def test_gte(self):
assert((PrivVal(2) >= 1).val() == 1)
assert((2 >= PrivVal(1)).val() == 1)
assert((PrivVal(2) >= PrivVal(1)).val() == 1)

assert((PrivVal(1) >= 2).val() == 0)
assert((1 >= PrivVal(2)).val() == 0)
assert((PrivVal(1) >= PrivVal(2)).val() == 0)

assert((PrivVal(1) >= 1).val() == 1)
assert((1 >= PrivVal(1)).val() == 1)
assert((PrivVal(1) >= PrivVal(1)).val() == 1)

def test_eq(self):
assert((PrivVal(2) == 2).val() == 1)
assert((2 == PrivVal(2)).val() == 1)
assert((PrivVal(2) == PrivVal(2)).val() == 1)

assert((PrivVal(2) == 1).val() == 0)
assert((2 == PrivVal(1)).val() == 0)
assert((PrivVal(2) == PrivVal(1)).val() == 0)

def test_neq(self):
assert((PrivVal(2) != 1).val() == 1)
assert((2 != PrivVal(1)).val() == 1)
assert((PrivVal(2) != PrivVal(1)).val() == 1)

assert((PrivVal(2) != 2).val() == 0)
assert((2 != PrivVal(2)).val() == 0)
assert((PrivVal(2) != PrivVal(2)).val() == 0)
33 changes: 33 additions & 0 deletions test/test_lincomb_bitwise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest
from pysnark.runtime import PrivVal
from pysnark.boolean import LinCombBool

class TestLinCombBitwise():
def test_to_bits(self):
bits = PrivVal(5).to_bits()
assert bits[3].val() == 0
assert bits[2].val() == 1
assert bits[1].val() == 0
assert bits[0].val() == 1

def test_and(self):
assert (3 & PrivVal(7)).val() == 3
assert (PrivVal(7) & 3).val() == 3
assert (PrivVal(7) & PrivVal(3)).val() == 3
assert (PrivVal(7) & PrivVal(0)).val() == 0
assert (PrivVal(0) & PrivVal(7)).val() == 0

def test_or(self):
assert (3 | PrivVal(7)).val() == 7
assert (PrivVal(7) | 3).val() == 7
assert (PrivVal(7) | PrivVal(3)).val() == 7
assert (PrivVal(7) | PrivVal(0)).val() == 7
assert (PrivVal(0) | PrivVal(7)).val() == 7

def test_xor(self):
assert (3 ^ PrivVal(7)).val() == 4
assert (PrivVal(7) ^ 3).val() == 4
assert (PrivVal(7) ^ PrivVal(3)).val() == 4
assert (PrivVal(7) ^ PrivVal(0)).val() == 7
assert (PrivVal(0) ^ PrivVal(7)).val() == 7

Loading