Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
helpers.test.cpp
Go to the documentation of this file.
1#include "helpers.hpp"
7
8using namespace bb;
9
10using namespace smt_terms;
11
16TEST(helpers, shl)
17{
18 Solver s("30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001",
20 /*base=*/16,
21 /*bvsize=*/32);
22
23 STerm x = BVVar("x", &s);
24 STerm y = BVVar("y", &s);
25 STerm z = shl(x, y, &s);
26 x == 5;
27 y == 1;
28 // z should be z == 10;
29 s.check();
30 std::unordered_map<std::string, cvc5::Term> terms({ { "x", x }, { "y", y }, { "z", z } });
32 info("x = ", vals["x"]);
33 info("y = ", vals["y"]);
34 info("z = ", vals["z"]);
35 // z == 1010 in binary
36 EXPECT_TRUE(vals["z"] == "00000000000000000000000000001010");
37}
38
43TEST(helpers, shr)
44{
45 Solver s("30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001",
47 /*base=*/16,
48 /*bvsize=*/32);
49
50 STerm x = BVVar("x", &s);
51 STerm y = BVVar("y", &s);
52 STerm z = shr(x, y, &s);
53 x == 5;
54 y == 1;
55 // z should be z == 2;
56 s.check();
57 std::unordered_map<std::string, cvc5::Term> terms({ { "x", x }, { "y", y }, { "z", z } });
59 info("x = ", vals["x"]);
60 info("y = ", vals["y"]);
61 info("z = ", vals["z"]);
62 // z == 10 in binary
63 EXPECT_TRUE(vals["z"] == "00000000000000000000000000000010");
64}
65
70TEST(helpers, buggy_shr)
71{
72 // using smt solver i found that 1879048194 >> 16 == 0
73 // its strange...
74 Solver s("30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001",
76 /*base=*/16,
77 /*bvsize=*/32);
78
79 STerm x = BVVar("x", &s);
80 STerm y = BVVar("y", &s);
81 STerm z = shr(x, y, &s);
82 x == 1879048194;
83 y == 16;
84 // z should be z == 28672;
85 s.check();
86 std::unordered_map<std::string, cvc5::Term> terms({ { "x", x }, { "y", y }, { "z", z } });
88 info("x = ", vals["x"]);
89 info("y = ", vals["y"]);
90 info("z = ", vals["z"]);
91 // z == 28672 in binary
92 EXPECT_TRUE(vals["z"] == "00000000000000000111000000000000");
93}
94
99TEST(helpers, pow2)
100{
101 Solver s("30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001",
103 /*base=*/16,
104 /*bvsize=*/32);
105
106 STerm x = BVVar("x", &s);
107 STerm z = pow2_8(x, &s);
108 x == 11;
109 // z should be z == 2048;
110 s.check();
111 std::unordered_map<std::string, cvc5::Term> terms({ { "x", x }, { "z", z } });
113 info("x = ", vals["x"]);
114 info("z = ", vals["z"]);
115 // z == 2048 in binary
116 EXPECT_TRUE(vals["z"] == "00000000000000000000100000000000");
117}
118
123TEST(helpers, signed_div)
124{
125 Solver s("30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001",
127 /*base=*/16,
128 /*bvsize=*/32);
129
130 STerm x = BVVar("x", &s);
131 STerm y = BVVar("y", &s);
132 STerm z = idiv(x, y, 2, &s);
133 // 00 == 0
134 x == 0;
135 // 11 == -1
136 y == 3;
137 s.check();
138 std::unordered_map<std::string, cvc5::Term> terms({ { "x", x }, { "y", y }, { "z", z } });
140 info("x = ", vals["x"]);
141 info("y = ", vals["y"]);
142 info("z = ", vals["z"]);
143 EXPECT_TRUE(vals["z"] == "00000000000000000000000000000000");
144}
145
150TEST(helpers, signed_div_1)
151{
152 Solver s("30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001",
154 /*base=*/16,
155 /*bvsize=*/32);
156
157 STerm x = BVVar("x", &s);
158 STerm y = BVVar("y", &s);
159 STerm z = idiv(x, y, 2, &s);
160 // 01 == 1
161 x == 1;
162 // 11 == -1
163 y == 3;
164 s.check();
165 std::unordered_map<std::string, cvc5::Term> terms({ { "x", x }, { "y", y }, { "z", z } });
167 info("x = ", vals["x"]);
168 info("y = ", vals["y"]);
169 info("z = ", vals["z"]);
170 EXPECT_TRUE(vals["z"] == "00000000000000000000000000000011");
171}
172
177TEST(helpers, signed_div_2)
178{
179 Solver s("30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001",
181 /*base=*/16,
182 /*bvsize=*/32);
183
184 STerm x = BVVar("x", &s);
185 STerm y = BVVar("y", &s);
186 STerm z = idiv(x, y, 4, &s);
187 // 0111 == 7
188 x == 7;
189 // 0010 == 2
190 y == 2;
191 s.check();
192 std::unordered_map<std::string, cvc5::Term> terms({ { "x", x }, { "y", y }, { "z", z } });
194 info("x = ", vals["x"]);
195 info("y = ", vals["y"]);
196 info("z = ", vals["z"]);
197 EXPECT_TRUE(vals["z"] == "00000000000000000000000000000011");
198}
199
204TEST(helpers, shl_overflow)
205{
206 Solver s("30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001",
208 /*base=*/16,
209 /*bvsize=*/32);
210
211 STerm x = BVVar("x", &s);
212 STerm y = BVVar("y", &s);
213 STerm z = shl32(x, y, &s);
214 x == 1;
215 y == 50;
216 s.check();
217 std::unordered_map<std::string, cvc5::Term> terms({ { "x", x }, { "y", y }, { "z", z } });
219 info("x = ", vals["x"]);
220 info("y = ", vals["y"]);
221 info("z = ", vals["z"]);
222 // z == 1010 in binary
223 EXPECT_TRUE(vals["z"] == "00000000000000000000000000000000");
224}
Class for the solver.
Definition solver.hpp:80
std::unordered_map< std::string, std::string > model(std::unordered_map< std::string, cvc5::Term > &terms) const
Definition solver.cpp:80
Symbolic term element class.
Definition term.hpp:114
void info(Args... args)
Definition log.hpp:70
smt_circuit::STerm shr(smt_circuit::STerm v0, smt_circuit::STerm v1, smt_solver::Solver *solver)
Right shift operation.
Definition helpers.cpp:40
smt_circuit::STerm idiv(smt_circuit::STerm v0, smt_circuit::STerm v1, uint32_t bit_size, smt_solver::Solver *solver)
Signed division in noir-style.
Definition helpers.cpp:61
smt_circuit::STerm shl(smt_circuit::STerm v0, smt_circuit::STerm v1, smt_solver::Solver *solver)
Left shift operation without truncation.
Definition helpers.cpp:34
smt_circuit::STerm shl32(smt_circuit::STerm v0, smt_circuit::STerm v1, smt_solver::Solver *solver)
Left shift operation with 32-bit truncation.
Definition helpers.cpp:54
smt_circuit::STerm pow2_8(smt_circuit::STerm v0, smt_solver::Solver *solver)
Calculates power of 2.
Definition helpers.cpp:16
TEST(helpers, shl)
Test left shift operation Tests that 5 << 1 = 10 using SMT solver.
Entry point for Barretenberg command-line interface.
const SolverConfiguration default_solver_config
Definition solver.hpp:37
STerm BVVar(const std::string &name, Solver *slv)
Definition term.cpp:615
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13