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
8 changes: 4 additions & 4 deletions circkit/const_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def output(self, value):


def const_2_int(value):
if hasattr(value, "integer_representation"):
if hasattr(value, "to_integer"):
# sage's GF(p**k)
return value.integer_representation()
return value.to_integer()
# try whatever
return int(value)

Expand All @@ -53,15 +53,15 @@ def __init__(self, circuit):
super().__init__(circuit)

self.base_ring = circuit.base_ring
self.has_int_repr = hasattr(self.base_ring(0), "integer_representation")
self.has_int_repr = hasattr(self.base_ring(0), "to_integer")

def create(self, value):
if isinstance(value, self.circuit.Node):
assert value.operation.opname == "CONST"
return self.create(value.value)
if isinstance(value, int) or type(value).__name__ == "Integer":
if self.has_int_repr:
return self.base_ring.fetch_int(value)
return self.base_ring.from_integer(value)
return self.base_ring(value)
elif hasattr(value, "parent") and value.parent().order() == self.base_ring.order():
return value
Expand Down
20 changes: 13 additions & 7 deletions tests/test_arithmetic_field.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from circkit.arithmetic import ArithmeticCircuit
from sage.all import GF
import random
import pytest

sage_all = pytest.importorskip("sage.all")
if sage_all:
GF = sage_all.GF

from circkit.arithmetic import ArithmeticCircuit

K = GF(2**8)


Expand All @@ -23,17 +29,17 @@ def test_toycircuit():
out = C.evaluate(inp)

def stdresults(a, b):
a = K.fetch_int(a)
b = K.fetch_int(b)
a = K.from_integer(a)
b = K.from_integer(b)

c5 = K.fetch_int(5)
c3 = K.fetch_int(3)
c5 = K.from_integer(5)
c3 = K.from_integer(3)

x0 = a + b
x1 = x0 - c5
x2 = x1 * x0
x3 = x2 / c3
x4 = x3 ** 4
x5 = ~x4
return [x4.integer_representation(), x5.integer_representation()]
return [x4.to_integer(), x5.to_integer()]
assert out == stdresults(*inp)
20 changes: 13 additions & 7 deletions tests/test_builtin_circuit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from circkit.arithmetic import ArithmeticCircuit
from sage.all import GF
import random
import pytest

sage_all = pytest.importorskip("sage.all")
if sage_all:
GF = sage_all.GF

from circkit.arithmetic import ArithmeticCircuit

K = GF(2**8)


Expand All @@ -23,17 +29,17 @@ def test_toycircuit():
out = C.evaluate(inp)

def stdresults(a, b):
a = K.fetch_int(a)
b = K.fetch_int(b)
a = K.from_integer(a)
b = K.from_integer(b)

c5 = K.fetch_int(5)
c3 = K.fetch_int(3)
c5 = K.from_integer(5)
c3 = K.from_integer(3)

x0 = a + b
x1 = x0 - c5
x2 = x1 * x0
x3 = x2 / c3
x4 = x3 ** 4
x5 = ~x4
return [x4.integer_representation(), x5.integer_representation()]
return [x4.to_integer(), x5.to_integer()]
assert out == stdresults(*inp)