From fb055bcb40f2c7116e700ca598079274d8f9afaa Mon Sep 17 00:00:00 2001 From: Aleksei Udovenko Date: Fri, 14 Jun 2024 09:25:07 +0200 Subject: [PATCH 1/2] skip sage tests if running without sage (requires pytest) --- tests/test_arithmetic_field.py | 10 ++++++++-- tests/test_builtin_circuit.py | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/test_arithmetic_field.py b/tests/test_arithmetic_field.py index 1d76a73..e05990b 100644 --- a/tests/test_arithmetic_field.py +++ b/tests/test_arithmetic_field.py @@ -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) diff --git a/tests/test_builtin_circuit.py b/tests/test_builtin_circuit.py index 1d76a73..e05990b 100644 --- a/tests/test_builtin_circuit.py +++ b/tests/test_builtin_circuit.py @@ -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) From 401db4525c880255a75d78dda0b856829efba7df Mon Sep 17 00:00:00 2001 From: Aleksei Udovenko Date: Fri, 14 Jun 2024 09:28:45 +0200 Subject: [PATCH 2/2] fix SageMath's GF API (fetch_int / integer_representatino deprecated, replace with to_integer/from_integer) --- circkit/const_manager.py | 8 ++++---- tests/test_arithmetic_field.py | 10 +++++----- tests/test_builtin_circuit.py | 10 +++++----- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/circkit/const_manager.py b/circkit/const_manager.py index 697ed0c..36fb89b 100644 --- a/circkit/const_manager.py +++ b/circkit/const_manager.py @@ -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) @@ -53,7 +53,7 @@ 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): @@ -61,7 +61,7 @@ def create(self, value): 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 diff --git a/tests/test_arithmetic_field.py b/tests/test_arithmetic_field.py index e05990b..c321da7 100644 --- a/tests/test_arithmetic_field.py +++ b/tests/test_arithmetic_field.py @@ -29,11 +29,11 @@ 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 @@ -41,5 +41,5 @@ def stdresults(a, b): 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) diff --git a/tests/test_builtin_circuit.py b/tests/test_builtin_circuit.py index e05990b..c321da7 100644 --- a/tests/test_builtin_circuit.py +++ b/tests/test_builtin_circuit.py @@ -29,11 +29,11 @@ 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 @@ -41,5 +41,5 @@ def stdresults(a, b): 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)