forked from nayuki/Bitcoin-Cryptography-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldInt.hpp
More file actions
106 lines (59 loc) · 2.98 KB
/
Copy pathFieldInt.hpp
File metadata and controls
106 lines (59 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Bitcoin cryptography library
* Copyright (c) Project Nayuki
*
* https://www.nayuki.io/page/bitcoin-cryptography-library
* https://github.com/nayuki/Bitcoin-Cryptography-Library
*/
#pragma once
#include <cstdint>
#include "Uint256.hpp"
/*
* An unsigned 256-bit integer modulo a specific prime number, for Bitcoin and secp256k1.
* The input and output values of each method are always in the range [0, MODULUS).
*
* Some behaviors are specific to FieldInt (such as reciprocal()), while others are
* the same as Uint256 (such as comparisons). The number representation format is
* the same as Uint256. It is illegal to set the value to be greater than or equal
* to MODULUS; undefined behavior will result. Instances of this class are mutable.
*/
class FieldInt final : private Uint256 {
public: using Uint256::NUM_WORDS;
/*---- Fields ----*/
public: using Uint256::value;
/*---- Constructors ----*/
// Constructs a FieldInt from the given 64-character hexadecimal string. Not constant-time.
// If the syntax of the string is invalid, then an assertion will fail.
public: explicit FieldInt(const char *str);
// Constructs a FieldInt from the given Uint256, reducing it as necessary.
// Constant-time with respect to the given value.
public: explicit FieldInt(const Uint256 &val);
/*---- Arithmetic methods ----*/
// Adds the given number into this number, modulo the prime. Constant-time with respect to both values.
public: void add(const FieldInt &other);
// Subtracts the given number from this number, modulo the prime. Constant-time with respect to both values.
public: void subtract(const FieldInt &other);
// Doubles this number, modulo the prime. Constant-time with respect to this value.
public: void multiply2();
// Squares this number, modulo the prime. Constant-time with respect to this value.
public: void square();
// Multiplies the given number into this number, modulo the prime. Constant-time with respect to both values.
public: void multiply(const FieldInt &other);
// Computes the multiplicative inverse of this number with respect to the modulus.
// If this number is zero, the reciprocal is zero. Constant-time with respect to this value.
public: void reciprocal();
/*---- Miscellaneous methods ----*/
public: void replace(const FieldInt &other, std::uint32_t enable);
public: using Uint256::getBigEndianBytes;
/*---- Equality and inequality operators ----*/
public: bool operator==(const FieldInt &other) const;
public: bool operator!=(const FieldInt &other) const;
public: bool operator<(const FieldInt &other) const;
public: bool operator<=(const FieldInt &other) const;
public: bool operator>(const FieldInt &other) const;
public: bool operator>=(const FieldInt &other) const;
private: bool operator<(const Uint256 &other) const;
private: bool operator>=(const Uint256 &other) const;
/*---- Class constants ----*/
private: static const Uint256 MODULUS; // Prime number
};