123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- #include "Scalar.hpp"
- extern const scalar_t bn_n;
- Scalar::Scalar()
- {
- element = 0;
- }
- Scalar::Scalar(const scalar_t& input)
- {
- set(input);
- }
- Scalar::Scalar(mpz_class input)
- {
- element = input;
- }
- void Scalar::set(const scalar_t& input)
- {
- std::stringstream buffer;
- std::string temp;
- buffer << std::hex << input[3] << input[2] << input[1] << input[0];
- buffer >> temp;
-
- element.set_str(temp, 16);
- }
- void Scalar::set(mpz_class input)
- {
- element = input;
- }
- void Scalar::set_random()
- {
- scalar_t temp;
-
- scalar_setrandom(temp, bn_n);
- set(temp);
- }
- Scalar Scalar::operator+(const Scalar& b) const
- {
- mpz_class temp = element + b.element;
- mpz_mod(temp.get_mpz_t(), temp.get_mpz_t(), mpz_bn_n.get_mpz_t());
- return Scalar(temp);
- }
- Scalar Scalar::operator-(const Scalar& b) const
- {
- mpz_class temp = element - b.element;
- mpz_mod(temp.get_mpz_t(), temp.get_mpz_t(), mpz_bn_n.get_mpz_t());
- return Scalar(temp);
- }
- Scalar Scalar::operator*(const Scalar& b) const
- {
- mpz_class temp = element * b.element;
- mpz_mod(temp.get_mpz_t(), temp.get_mpz_t(), mpz_bn_n.get_mpz_t());
- return Scalar(temp);
- }
- Scalar Scalar::operator/(const Scalar& b) const
- {
- mpz_class temp;
- mpz_invert(temp.get_mpz_t(), b.element.get_mpz_t(), mpz_bn_n.get_mpz_t());
- temp *= element;
- mpz_mod(temp.get_mpz_t(), temp.get_mpz_t(), mpz_bn_n.get_mpz_t());
- return Scalar(temp);
- }
- Scalar& Scalar::operator++()
- {
- element++;
- mpz_mod(element.get_mpz_t(), element.get_mpz_t(), mpz_bn_n.get_mpz_t());
- return *this;
- }
- Scalar Scalar::operator++(int)
- {
- Scalar retval = *this;
-
- element++;
- mpz_mod(element.get_mpz_t(), element.get_mpz_t(), mpz_bn_n.get_mpz_t());
- return retval;
- }
- Scalar& Scalar::operator--()
- {
- element--;
- mpz_mod(element.get_mpz_t(), element.get_mpz_t(), mpz_bn_n.get_mpz_t());
- return *this;
- }
- Scalar Scalar::operator--(int)
- {
- Scalar retval = *this;
-
- element--;
- mpz_mod(element.get_mpz_t(), element.get_mpz_t(), mpz_bn_n.get_mpz_t());
- return retval;
- }
- curvepoint_fp_t Scalar::operator*(const curvepoint_fp_t& b) const
- {
- curvepoint_fp_t retval;
- curvepoint_fp_scalarmult_vartime(retval, b, element.to_scalar_t().expose());
- return retval;
- }
- twistpoint_fp2_t Scalar::operator*(const twistpoint_fp2_t& b) const
- {
- twistpoint_fp2_t retval;
- twistpoint_fp2_scalarmult_vartime(retval, b, element.to_scalar_t().expose());
- return retval;
- }
- fp12e_t Scalar::operator*(const fp12e_t& b) const
- {
- fp12e_t retval;
- fp12e_pow_vartime(retval, b, element.to_scalar_t().expose());
- return retval;
- }
- Bipoint<curvepoint_fp_t> Scalar::operator*(const Bipoint<curvepoint_fp_t>& b) const
- {
- return b * *this;
- }
- Bipoint<twistpoint_fp2_t> Scalar::operator*(const Bipoint<twistpoint_fp2_t>& b) const
- {
- return b * *this;
- }
- Quadripoint Scalar::operator*(const Quadripoint& b) const
- {
- return b * *this;
- }
- bool Scalar::operator==(const Scalar& b) const
- {
- return element == b.element;
- }
- bool Scalar::operator!=(const Scalar& b) const
- {
- return element != b.element;
- }
- Scalar::SecretScalar::SecretScalar()
- {
- element = {0,0,0,0};
- }
- Scalar::SecretScalar::SecretScalar(const Scalar& input)
- {
- set(input.element);
- }
- Scalar::SecretScalar::SecretScalar(mpz_class input)
- {
- set(input);
- }
- const scalar_t& Scalar::SecretScalar::expose() const
- {
- return element;
- }
- void Scalar::SecretScalar::set(mpz_class input)
- {
- std::stringstream buffer;
- char temp[17];
- buffer << std::setfill('0') << std::setw(64) << input.get_string(16);
- for (int i = 3; i >= 0; i--)
- {
- buffer.get(temp, 17);
- element[i] = strtoull(temp, NULL, 16);
- }
- }
-
- Scalar::SecretScalar Scalar::to_scalar_t() const
- {
- return SecretScalar(element);
- }
- std::ostream& operator<<(std::ostream& os, const Scalar& output)
- {
- os << output.element;
- return os;
- }
|