File: test_finite_field.py

package info (click to toggle)
cvc5 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 87,260 kB
  • sloc: cpp: 383,850; java: 12,207; python: 12,090; sh: 5,679; ansic: 4,729; lisp: 763; perl: 208; makefile: 38
file content (98 lines) | stat: -rw-r--r-- 2,599 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
###############################################################################
# Top contributors (to current version):
#   Alex Ozdemir, Aina Niemetz, Alex Sokolov
#
# This file is part of the cvc5 project.
#
# Copyright (c) 2009-2025 by the authors listed in the file AUTHORS
# in the top-level source directory and their institutional affiliations.
# All rights reserved.  See the file COPYING in the top-level source
# directory for licensing information.
# #############################################################################
##

import pytest
import cvc5
from cvc5 import Sort, Term, Kind


@pytest.fixture
def tm():
    return cvc5.TermManager()
@pytest.fixture
def solver(tm):
    return cvc5.Solver(tm)


def test_basic_finite_field(tm, solver):
    solver.setOption("produce-models", "true")
    F = tm.mkFiniteFieldSort(5)
    a = tm.mkConst(F, "a")
    b = tm.mkConst(F, "b")
    assert 5 == F.getFiniteFieldSize()

    inv = tm.mkTerm(
        Kind.EQUAL,
        tm.mkTerm(Kind.FINITE_FIELD_MULT, a, b),
        tm.mkFiniteFieldElem(1, F),
    )
    aIsTwo = tm.mkTerm(
        Kind.EQUAL,
        a,
        tm.mkFiniteFieldElem(2, F),
    )
    solver.assertFormula(inv)
    solver.assertFormula(aIsTwo)
    r = solver.checkSat()
    assert r.isSat()
    assert solver.getValue(a).toPythonObj() == 2
    assert solver.getValue(b).toPythonObj() == -2

    bIsTwo = tm.mkTerm(
        Kind.EQUAL,
        b,
        tm.mkFiniteFieldElem(2, F),
    )
    solver.assertFormula(bIsTwo)
    r = solver.checkSat()
    assert not r.isSat()

def test_basic_finite_field_base(tm, solver):
    solver.setOption("produce-models", "true")
    F = tm.mkFiniteFieldSort("101", 2)
    a = tm.mkConst(F, "a")
    b = tm.mkConst(F, "b")
    assert 5 == F.getFiniteFieldSize()

    inv = tm.mkTerm(
        Kind.EQUAL,
        tm.mkTerm(Kind.FINITE_FIELD_MULT, a, b),
        tm.mkFiniteFieldElem("1", F, 3),
    )
    aIsTwo = tm.mkTerm(
        Kind.EQUAL,
        a,
        tm.mkFiniteFieldElem("10", F, 2),
    )
    solver.assertFormula(inv)
    solver.assertFormula(aIsTwo)
    r = solver.checkSat()
    assert r.isSat()
    assert solver.getValue(a).toPythonObj() == 2
    assert solver.getValue(b).toPythonObj() == -2

    bIsTwo = tm.mkTerm(
        Kind.EQUAL,
        b,
        tm.mkFiniteFieldElem(2, F),
    )
    solver.assertFormula(bIsTwo)
    r = solver.checkSat()
    assert not r.isSat()

def test_finite_field_base_equality(tm):
    F = tm.mkFiniteFieldSort("11", 4)
    c = tm.mkFiniteFieldElem("-6d", F, 16)
    d = tm.mkFiniteFieldElem("1", F, 16)
    assert c == d