aboutsummaryrefslogtreecommitdiff
path: root/src/crypto/muhash.h
diff options
context:
space:
mode:
authorFabian Jahr <fjahr@protonmail.com>2020-12-20 22:23:47 +0100
committerFabian Jahr <fjahr@protonmail.com>2020-12-20 23:27:58 +0100
commit0b4d290bf5b0a4d156c523431bf89aaa9ffe92e5 (patch)
tree676d832b0e7b0319c3bebe818b1a87eab87fd38a /src/crypto/muhash.h
parent589f958662a2dcaacdb9a66f1088c74828a39577 (diff)
downloadbitcoin-0b4d290bf5b0a4d156c523431bf89aaa9ffe92e5.tar.xz
crypto: Add Num3072 implementation
Num3072 is a specialized bignum implementation used in MuHash3072. Co-authored-by: Pieter Wuille <pieter.wuille@gmail.com>
Diffstat (limited to 'src/crypto/muhash.h')
-rw-r--r--src/crypto/muhash.h62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/crypto/muhash.h b/src/crypto/muhash.h
new file mode 100644
index 0000000000..e5f6f0464f
--- /dev/null
+++ b/src/crypto/muhash.h
@@ -0,0 +1,62 @@
+// Copyright (c) 2017-2020 The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#ifndef BITCOIN_CRYPTO_MUHASH_H
+#define BITCOIN_CRYPTO_MUHASH_H
+
+#if defined(HAVE_CONFIG_H)
+#include <config/bitcoin-config.h>
+#endif
+
+#include <serialize.h>
+#include <uint256.h>
+
+#include <stdint.h>
+
+class Num3072
+{
+private:
+ void FullReduce();
+ bool IsOverflow() const;
+ Num3072 GetInverse() const;
+
+public:
+
+#ifdef HAVE___INT128
+ typedef unsigned __int128 double_limb_t;
+ typedef uint64_t limb_t;
+ static constexpr int LIMBS = 48;
+ static constexpr int LIMB_SIZE = 64;
+#else
+ typedef uint64_t double_limb_t;
+ typedef uint32_t limb_t;
+ static constexpr int LIMBS = 96;
+ static constexpr int LIMB_SIZE = 32;
+#endif
+ limb_t limbs[LIMBS];
+
+ // Sanity check for Num3072 constants
+ static_assert(LIMB_SIZE * LIMBS == 3072, "Num3072 isn't 3072 bits");
+ static_assert(sizeof(double_limb_t) == sizeof(limb_t) * 2, "bad size for double_limb_t");
+ static_assert(sizeof(limb_t) * 8 == LIMB_SIZE, "LIMB_SIZE is incorrect");
+
+ // Hard coded values in MuHash3072 constructor and Finalize
+ static_assert(sizeof(limb_t) == 4 || sizeof(limb_t) == 8, "bad size for limb_t");
+
+ void Multiply(const Num3072& a);
+ void Divide(const Num3072& a);
+ void SetToOne();
+ void Square();
+
+ Num3072() { this->SetToOne(); };
+
+ SERIALIZE_METHODS(Num3072, obj)
+ {
+ for (auto& limb : obj.limbs) {
+ READWRITE(limb);
+ }
+ }
+};
+
+#endif // BITCOIN_CRYPTO_MUHASH_H