Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
Public Member Functions | Friends | List of all members
gadgetlib2::FElem Class Reference

#include <variable.hpp>

Public Member Functions

 FElem (const FElemInterface &elem)
 
void promoteToFieldType (FieldType type)
 
 FElem ()
 
 FElem (const long n)
 
 FElem (const int i)
 
 FElem (const size_t n)
 
 FElem (const Fp &elem)
 
 FElem (const FElem &src)
 
FElemoperator= (const FElem &other)
 
FElemoperator= (FElem &&other)
 
FElemoperator= (const long i)
 
::std::string asString () const
 
FieldType fieldType () const
 
bool operator== (const FElem &other) const
 
FElemoperator*= (const FElem &other)
 
FElemoperator+= (const FElem &other)
 
FElemoperator-= (const FElem &other)
 
FElem operator- () const
 
FElem inverse (const FieldType &fieldType)
 
long asLong () const
 
int getBit (unsigned int i, const FieldType &fieldType)
 

Friends

class GadgetLibAdapter
 
FElem power (const FElem &base, long exponent)
 
::std::ostream & operator<< (::std::ostream &os, const FElem &elem)
 

Detailed Description

A wrapper class for field elements. Can hold any derived type of FieldElementInterface

Definition at line 101 of file variable.hpp.

Constructor & Destructor Documentation

◆ FElem() [1/7]

gadgetlib2::FElem::FElem ( const FElemInterface elem)
explicit

Definition at line 43 of file variable.cpp.

43 : elem_(elem.clone()) {}

◆ FElem() [2/7]

gadgetlib2::FElem::FElem ( )

Definition at line 44 of file variable.cpp.

44 : elem_(new FConst(0)) {}
Here is the caller graph for this function:

◆ FElem() [3/7]

gadgetlib2::FElem::FElem ( const long  n)

Definition at line 45 of file variable.cpp.

45 : elem_(new FConst(n)) {}

◆ FElem() [4/7]

gadgetlib2::FElem::FElem ( const int  i)

Definition at line 46 of file variable.cpp.

46 : elem_(new FConst(i)) {}

◆ FElem() [5/7]

gadgetlib2::FElem::FElem ( const size_t  n)

Definition at line 47 of file variable.cpp.

47 : elem_(new FConst(n)) {}

◆ FElem() [6/7]

gadgetlib2::FElem::FElem ( const Fp elem)

Definition at line 48 of file variable.cpp.

48 : elem_(new R1P_Elem(elem)) {}

◆ FElem() [7/7]

gadgetlib2::FElem::FElem ( const FElem src)

Definition at line 49 of file variable.cpp.

49 : elem_(src.elem_->clone()) {}

Member Function Documentation

◆ asLong()

long gadgetlib2::FElem::asLong ( ) const
inline

Definition at line 140 of file variable.hpp.

140 { return elem_->asLong(); }
Here is the caller graph for this function:

◆ asString()

::std::string gadgetlib2::FElem::asString ( ) const
inline

Definition at line 127 of file variable.hpp.

127 { return elem_->asString(); }
Here is the caller graph for this function:

◆ fieldType()

FieldType gadgetlib2::FElem::fieldType ( ) const
inline

Definition at line 128 of file variable.hpp.

128 { return elem_->fieldType(); }
Here is the caller graph for this function:

◆ getBit()

int gadgetlib2::FElem::getBit ( unsigned int  i,
const FieldType fieldType 
)

Definition at line 128 of file variable.cpp.

129 {
131  if (this->fieldType() == fieldType) {
132  return elem_->getBit(i);
133  } else {
135  "Attempted to extract bits from incompatible field type.");
136  }
137 }
Here is the call graph for this function:

◆ inverse()

FElem gadgetlib2::FElem::inverse ( const FieldType fieldType)

Definition at line 122 of file variable.cpp.

123 {
125  return FElem(*(elem_->inverse()));
126 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator*=()

FElem & gadgetlib2::FElem::operator*= ( const FElem other)

Definition at line 101 of file variable.cpp.

102 {
103  promoteToFieldType(other.fieldType());
104  *elem_ *= *other.elem_;
105  return *this;
106 }
Here is the call graph for this function:

◆ operator+=()

FElem & gadgetlib2::FElem::operator+= ( const FElem other)

Definition at line 108 of file variable.cpp.

109 {
110  promoteToFieldType(other.fieldType());
111  *elem_ += *other.elem_;
112  return *this;
113 }
Here is the call graph for this function:

◆ operator-()

FElem gadgetlib2::FElem::operator- ( ) const
inline

Definition at line 133 of file variable.hpp.

134  {
135  FElem retval(0);
136  retval -= FElem(*elem_);
137  return retval;
138  }
Here is the call graph for this function:

◆ operator-=()

FElem & gadgetlib2::FElem::operator-= ( const FElem other)

Definition at line 115 of file variable.cpp.

116 {
117  promoteToFieldType(other.fieldType());
118  *elem_ -= *other.elem_;
119  return *this;
120 }
Here is the call graph for this function:

◆ operator=() [1/3]

FElem & gadgetlib2::FElem::operator= ( const FElem other)

Definition at line 51 of file variable.cpp.

52 {
53  if (fieldType() == other.fieldType() || fieldType() == AGNOSTIC) {
54  elem_ = other.elem_->clone();
55  } else if (other.fieldType() != AGNOSTIC) {
56  GADGETLIB_FATAL("Attempted to assign field element of incorrect type");
57  } else {
58  *elem_ = dynamic_cast<FConst *>(other.elem_.get())->asLong();
59  }
60  return *this;
61 }
Here is the call graph for this function:

◆ operator=() [2/3]

FElem& gadgetlib2::FElem::operator= ( const long  i)
inline

Definition at line 122 of file variable.hpp.

123  {
124  *elem_ = i;
125  return *this;
126  }

◆ operator=() [3/3]

FElem & gadgetlib2::FElem::operator= ( FElem &&  other)

Definition at line 63 of file variable.cpp.

64 {
65  if (fieldType() == other.fieldType() || fieldType() == AGNOSTIC) {
66  elem_ = ::std::move(other.elem_);
67  } else if (other.elem_->fieldType() != AGNOSTIC) {
69  "Attempted to move assign field element of incorrect type");
70  } else {
71  *elem_ = dynamic_cast<FConst *>(other.elem_.get())->asLong();
72  }
73  return *this;
74 }
Here is the call graph for this function:

◆ operator==()

bool gadgetlib2::FElem::operator== ( const FElem other) const
inline

Definition at line 129 of file variable.hpp.

129 { return *elem_ == *other.elem_; }

◆ promoteToFieldType()

void gadgetlib2::FElem::promoteToFieldType ( FieldType  type)

Helper method. When doing arithmetic between a constant and a field specific element we want to "promote" the constant to the same field. This function changes the unique_ptr to point to a field specific element with the same value as the constant which it held.

Definition at line 86 of file variable.cpp.

87 {
88  if (!fieldMustBePromotedForArithmetic(this->fieldType(), type)) {
89  return;
90  }
91  if (type == R1P) {
92  const FConst *fConst = dynamic_cast<FConst *>(elem_.get());
94  fConst != NULL, "Cannot convert between specialized field types.");
95  elem_.reset(new R1P_Elem(fConst->asLong()));
96  } else {
97  GADGETLIB_FATAL("Attempted to promote to unknown field type");
98  }
99 }
Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Function Documentation

◆ GadgetLibAdapter

friend class GadgetLibAdapter
friend

Definition at line 150 of file variable.hpp.

◆ operator<<

::std::ostream& operator<< ( ::std::ostream &  os,
const FElem elem 
)
friend

Definition at line 144 of file variable.hpp.

146  {
147  return os << elem.elem_->asString();
148  }

◆ power

FElem power ( const FElem base,
long  exponent 
)
friend

Definition at line 139 of file variable.cpp.

140 { // TODO .cpp
141  FElem retval(base);
142  retval.elem_->power(exponent);
143  return retval;
144 }

The documentation for this class was generated from the following files:
gadgetlib2::FElem::promoteToFieldType
void promoteToFieldType(FieldType type)
Definition: variable.cpp:86
gadgetlib2::FElem::FElem
FElem()
Definition: variable.cpp:44
gadgetlib2::AGNOSTIC
@ AGNOSTIC
Definition: variable.hpp:37
gadgetlib2::FElem::asLong
long asLong() const
Definition: variable.hpp:140
GADGETLIB_FATAL
#define GADGETLIB_FATAL(msg)
Definition: infrastructure.hpp:85
gadgetlib2::FElem::fieldType
FieldType fieldType() const
Definition: variable.hpp:128
gadgetlib2::fieldMustBePromotedForArithmetic
bool fieldMustBePromotedForArithmetic(const FieldType &lhsField, const FieldType &rhsField)
Definition: variable.cpp:76
GADGETLIB_ASSERT
#define GADGETLIB_ASSERT(predicate, msg)
Definition: infrastructure.hpp:94
gadgetlib2::R1P
@ R1P
Definition: variable.hpp:37