Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
iterm.test.cpp
Go to the documentation of this file.
1#include <unordered_map>
2
4#include "term.hpp"
5
6#include <gtest/gtest.h>
7
8namespace {
10}
11
12using namespace bb;
13using namespace smt_terms;
14
15TEST(ITerm, addition)
16{
17 uint64_t a = engine.get_random_uint32() % (static_cast<uint32_t>(1) << 31);
18 uint64_t b = engine.get_random_uint32() % (static_cast<uint32_t>(1) << 31);
19 uint64_t c = a + b;
20
21 Solver s("30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001", default_solver_config);
22
23 STerm x = IVar("x", &s);
24 STerm y = IVar("y", &s);
25 STerm z = x + y;
26
27 z == c;
28 x == a;
29 ASSERT_TRUE(s.check());
30
31 bb::fr yvals = string_to_fr(s[y], /*base=*/10);
32 ASSERT_EQ(bb::fr(b), yvals);
33}
34
35TEST(ITerm, subtraction)
36{
37 uint64_t c = engine.get_random_uint32() % (static_cast<uint32_t>(1) << 31);
38 uint64_t b = engine.get_random_uint32() % (static_cast<uint32_t>(1) << 31);
39 uint64_t a = c + b;
40
41 Solver s("30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001", default_solver_config);
42
43 STerm x = IVar("x", &s);
44 STerm y = IVar("y", &s);
45 STerm z = x - y;
46
47 x == a;
48 z == c;
49 ASSERT_TRUE(s.check());
50
51 bb::fr yvals = string_to_fr(s[y], /*base=*/10);
52 ASSERT_EQ(bb::fr(b), yvals);
53}
54
55TEST(ITerm, multiplication)
56{
57 uint64_t a = engine.get_random_uint32() % (static_cast<uint32_t>(1) << 31);
58 uint64_t b = engine.get_random_uint32() % (static_cast<uint32_t>(1) << 31);
59 uint64_t c = a * b;
60
61 Solver s("30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001", default_solver_config);
62
63 STerm x = IVar("x", &s);
64 STerm y = IVar("y", &s);
65 STerm z = x * y;
66
67 x == a;
68 y == b;
69
70 ASSERT_TRUE(s.check());
71
72 bb::fr yvals = string_to_fr(s[z], /*base=*/10);
73 ASSERT_EQ(bb::fr(c), yvals);
74}
75
77{
78 uint64_t a = engine.get_random_uint32() % (static_cast<uint32_t>(1) << 31);
79 uint64_t b = engine.get_random_uint32() % (static_cast<uint32_t>(1) << 31) + 1;
80 uint64_t c = a / b;
81
82 Solver s("30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001", default_solver_config);
83
84 STerm x = IVar("x", &s);
85 STerm y = IVar("y", &s);
86 STerm z = x / y;
87
88 x == a;
89 y == b;
90
91 ASSERT_TRUE(s.check());
92
93 bb::fr yvals = string_to_fr(s[z], /*base=*/10);
94 ASSERT_EQ(bb::fr(c), yvals);
95}
96
97// This test aims to check for the absence of unintended
98// behavior. If an unsupported operator is called, an info message appears in stderr
99// and the value is supposed to remain unchanged.
100TEST(ITerm, unsupported_operations)
101{
102 Solver s("30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001");
103
104 STerm x = IVar("x", &s);
105 STerm y = IVar("y", &s);
106
107 STerm z = x ^ y;
108 ASSERT_EQ(z.term, x.term);
109 z = x & y;
110 ASSERT_EQ(z.term, x.term);
111 z = x | y;
112 ASSERT_EQ(z.term, x.term);
113 z = x >> 10;
114 ASSERT_EQ(z.term, x.term);
115 z = x << 10;
116 ASSERT_EQ(z.term, x.term);
117 z = x.rotr(10);
118 ASSERT_EQ(z.term, x.term);
119 z = x.rotl(10);
120 ASSERT_EQ(z.term, x.term);
121
122 cvc5::Term before_term = x.term;
123 x ^= y;
124 ASSERT_EQ(x.term, before_term);
125 x &= y;
126 ASSERT_EQ(x.term, before_term);
127 x |= y;
128 ASSERT_EQ(x.term, before_term);
129 x >>= 10;
130 ASSERT_EQ(x.term, before_term);
131 x <<= 10;
132 ASSERT_EQ(x.term, before_term);
133}
virtual uint32_t get_random_uint32()=0
Class for the solver.
Definition solver.hpp:80
Symbolic term element class.
Definition term.hpp:114
STerm rotl(const uint32_t &n) const
Definition term.cpp:450
STerm rotr(const uint32_t &n) const
Definition term.cpp:439
cvc5::Term term
Definition term.hpp:121
FF a
FF b
numeric::RNG & engine
RNG & get_randomness()
Definition engine.cpp:203
Entry point for Barretenberg command-line interface.
TEST(MegaCircuitBuilder, CopyConstructor)
const SolverConfiguration default_solver_config
Definition solver.hpp:37
STerm IVar(const std::string &name, Solver *slv)
Definition term.cpp:600
bb::fr string_to_fr(const std::string &number, int base, bool is_signed, size_t step)
Converts a string of an arbitrary base to fr. Note: there should be no prefix.
Definition smt_util.cpp:13