Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
infrastructure.hpp
Go to the documentation of this file.
1 
10 #include <cmath>
11 #include <cstdarg>
12 #include <cstdint>
13 #include <libff/common/utils.hpp>
14 #include <sstream>
15 #include <string>
16 
17 #ifndef __infrastructure_HPP
18 #define __infrastructure_HPP
19 
20 #ifndef _MSC_VER // emulate the MSVC-specific sprintf_s using the standard
21  // snprintf
22 #define sprintf_s \
23  snprintf // TODO: sprintf_s!=snprintf
24  // (http://blog.verg.es/2008/09/sprintfs-is-not-snprintf.html)
25 #endif
26 
27 #ifdef _DEBUG // MSVC Debug build
28 #define DEBUG // gcc Debug flag
29 #endif
30 
31 /********************************************************/
32 /**************** Class Writing Helpers *****************/
33 /********************************************************/
34 // A macro to disallow any non-defined constructors
35 // This should be used in the private: declarations for a class
36 #define DISALLOW_CONSTRUCTION(TypeName) TypeName();
37 
38 // A macro to disallow the copy constructor and operator= functions
39 // This should be used in the private: declarations for a class
40 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
41  TypeName(const TypeName &); \
42  void operator=(const TypeName &)
43 
44 /********************************************************/
45 /*************** Debug String Formatting ****************/
46 /********************************************************/
47 
48 namespace gadgetlib2
49 {
50 // someday, if/when MSVC supports C++0x variadic templates, change FMT in
51 // release version to the following in order to increase efficiency: #define
52 // GADGETLIB2_FMT(...) ""
53 ::std::string GADGETLIB2_FMT(const char *format, ...);
54 
56 long safeConvert(const int64_t num);
57 
58 /********************************************************/
59 /******************* Error Handling *********************/
60 /********************************************************/
61 
62 // declare a function as never returning, to quiet down "control reaches end of
63 // non-void function" warnings
64 #if defined(_MSC_VER) // VisualC++
65 #define __noreturn _declspec(noreturn)
66 #elif defined(__GNUC__)
67 #define __noreturn __attribute__((noreturn))
68 #else
69 #define __noreturn
70 #endif
71 
78 {
79 public:
80  static void __noreturn fatalError(const ::std::string &msg);
81  static void __noreturn fatalError(const std::stringstream &msg);
82  static void printStacktrace();
83 };
84 
85 #define GADGETLIB_FATAL(msg) \
86  do { \
87  ::std::stringstream msgStream; \
88  msgStream << msg << " (In file " << __FILE__ << " line " << __LINE__ \
89  << ".)"; \
90  ErrorHandling::fatalError(msgStream.str()); \
91  } while (0)
92 
93 // TODO change GADGETLIB_ASSERT to not run in debug
94 #define GADGETLIB_ASSERT(predicate, msg) \
95  if (!(bool(predicate))) \
96  GADGETLIB_FATAL(msg);
97 
98 /********************************************************/
99 /****************** Basic Math **************************/
100 /********************************************************/
101 
102 double Log2(double n);
103 
104 // Calculates upper bound of Log2 of a number (number of bits needed to
105 // represent value)
106 unsigned int Log2ceil(uint64_t i);
107 
108 // Returns true iff the given number is a power of 2.
109 bool IsPower2(const long x);
110 
111 // Returns a^b when a can be a and b are INTEGERS.
112 // constexpr int64_t POW(int64_t base, int exponent) {
113 // return (int64_t) powl((long double)base, (long double)exponent);
114 //}
115 //#define POW(a,b) ((int64_t)(pow((float)(a),(int)(b))))
116 
117 // Returns 2^exponent
118 /*constexpr*/ inline int64_t POW2(int exponent)
119 {
120  // assert(exponent>=0);
121  return ((int64_t)1) << exponent;
122 }
123 
124 // Returns the ceiling of a when a is of type double.
125 /*constexpr*/ inline int64_t CEIL(double a) { return (int64_t)ceil(a); }
126 //#define CEIL(a) ((int64_t)ceil((double)(a)))
127 
128 using libff::UNUSED;
129 } // namespace gadgetlib2
130 
131 #endif // __infrastructure_HPP
gadgetlib2::Log2
double Log2(double n)
Definition: infrastructure.cpp:116
__noreturn
#define __noreturn
Definition: infrastructure.hpp:69
gadgetlib2::GADGETLIB2_FMT
::std::string GADGETLIB2_FMT(const char *format,...)
Definition: infrastructure.cpp:49
gadgetlib2::ErrorHandling::fatalError
static void __noreturn fatalError(const ::std::string &msg)
Definition: infrastructure.cpp:73
gadgetlib2::POW2
int64_t POW2(int exponent)
Definition: infrastructure.hpp:118
gadgetlib2::safeConvert
long safeConvert(const int64_t num)
Definition: infrastructure.cpp:57
gadgetlib2::CEIL
int64_t CEIL(double a)
Definition: infrastructure.hpp:125
gadgetlib2::ErrorHandling::printStacktrace
static void printStacktrace()
Definition: infrastructure.cpp:94
gadgetlib2::Log2ceil
unsigned int Log2ceil(uint64_t i)
Definition: infrastructure.cpp:120
gadgetlib2::IsPower2
bool IsPower2(const long x)
Returns true iff x is a power of 2.
Definition: infrastructure.cpp:130
gadgetlib2::ErrorHandling
Definition: infrastructure.hpp:77
gadgetlib2
Definition: adapters.cpp:15