Fabcoin Core  0.16.2
P2P Digital Currency
ida.h
Go to the documentation of this file.
1 // ida.h - written and placed in the public domain by Wei Dai
2 
5 
6 #ifndef CRYPTOPP_IDA_H
7 #define CRYPTOPP_IDA_H
8 
9 #include "cryptlib.h"
10 #include "mqueue.h"
11 #include "filters.h"
12 #include "channels.h"
13 #include "secblock.h"
14 #include "stdcpp.h"
15 #include "misc.h"
16 
18 
23 {
24 public:
25  RawIDA(BufferedTransformation *attachment=NULL)
26  : m_threshold (0), m_channelsReady(0), m_channelsFinished(0)
27  {Detach(attachment);}
28 
29  unsigned int GetThreshold() const {return m_threshold;}
30  void AddOutputChannel(word32 channelId);
31  void ChannelData(word32 channelId, const byte *inString, size_t length, bool messageEnd);
32  lword InputBuffered(word32 channelId) const;
33 
34  void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);
35  size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
36  {
37  if (!blocking)
38  throw BlockingInputOnly("RawIDA");
39  ChannelData(StringToWord<word32>(channel), begin, length, messageEnd != 0);
40  return 0;
41  }
42 
43 protected:
44  virtual void FlushOutputQueues();
45  virtual void OutputMessageEnds();
46 
47  unsigned int InsertInputChannel(word32 channelId);
48  unsigned int LookupInputChannel(word32 channelId) const;
49  void ComputeV(unsigned int);
50  void PrepareInterpolation();
51  void ProcessInputQueues();
52 
53  typedef std::map<word32, unsigned int> InputChannelMap;
54  InputChannelMap m_inputChannelMap;
55  InputChannelMap::iterator m_lastMapPosition;
56  std::vector<MessageQueue> m_inputQueues;
57  std::vector<word32> m_inputChannelIds, m_outputChannelIds, m_outputToInput;
58  std::vector<std::string> m_outputChannelIdStrings;
59  std::vector<ByteQueue> m_outputQueues;
61  unsigned int m_channelsReady, m_channelsFinished;
62  std::vector<SecBlock<word32> > m_v;
63  SecBlock<word32> m_u, m_w, m_y;
64 };
65 
71 class SecretSharing : public CustomFlushPropagation<Filter>
72 {
73 public:
75  SecretSharing(RandomNumberGenerator &rng, int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)
76  : m_rng(rng), m_ida(new OutputProxy(*this, true))
77  {
78  Detach(attachment);
79  IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("NumberOfShares", nShares)("AddPadding", addPadding));
80  }
81 
83  size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
84  bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) {return m_ida.Flush(hardFlush, propagation, blocking);}
85 
86 protected:
89  bool m_pad;
90 };
91 
97 class SecretRecovery : public RawIDA
98 {
99 public:
101  SecretRecovery(int threshold, BufferedTransformation *attachment=NULL, bool removePadding=true)
102  : RawIDA(attachment)
103  {IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("RemovePadding", removePadding));}
104 
106 
107 protected:
108  void FlushOutputQueues();
109  void OutputMessageEnds();
110 
111  bool m_pad;
112 };
113 
115 
122 {
123 public:
125  InformationDispersal(int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)
126  : m_ida(new OutputProxy(*this, true)), m_pad(false), m_nextChannel(0)
127  {
128  Detach(attachment);
129  IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("NumberOfShares", nShares)("AddPadding", addPadding));
130  }
131 
133  size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
134  bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) {return m_ida.Flush(hardFlush, propagation, blocking);}
135 
136 protected:
138  bool m_pad;
139  unsigned int m_nextChannel;
140 };
141 
148 {
149 public:
151  InformationRecovery(int threshold, BufferedTransformation *attachment=NULL, bool removePadding=true)
152  : RawIDA(attachment), m_pad(false)
153  {IsolatedInitialize(MakeParameters("RecoveryThreshold", threshold)("RemovePadding", removePadding));}
154 
156 
157 protected:
158  void FlushOutputQueues();
159  void OutputMessageEnds();
160 
161  bool m_pad;
163 };
164 
165 class PaddingRemover : public Unflushable<Filter>
166 {
167 public:
169  : m_possiblePadding(false), m_zeroCount(0) {Detach(attachment);}
170 
172  {CRYPTOPP_UNUSED(parameters); m_possiblePadding = false;}
173  size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
174 
175  // GetPossiblePadding() == false at the end of a message indicates incorrect padding
176  bool GetPossiblePadding() const {return m_possiblePadding;}
177 
178 private:
181 };
182 
184 
185 #endif
bool Flush(bool completeFlush, int propagation=-1, bool blocking=true)
Definition: simple.h:97
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: ida.h:171
std::vector< MessageQueue > m_inputQueues
Definition: ida.h:56
uint8_t byte
Definition: Common.h:57
std::vector< word32 > m_outputToInput
Definition: ida.h:57
Utility functions for the Crypto++ library.
RawIDA(BufferedTransformation *attachment=NULL)
Definition: ida.h:25
std::vector< SecBlock< word32 > > m_v
Definition: ida.h:62
Secret sharing and information dispersal base class.
Definition: ida.h:22
Filter class that is a proxy for a sink.
Definition: filters.h:903
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
ByteQueue m_queue
Definition: ida.h:162
Abstract base classes that provide a uniform interface to this library.
size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing on a channel.
Definition: ida.h:35
InputChannelMap m_inputChannelMap
Definition: ida.h:54
Interface for random number generators.
Definition: cryptlib.h:1188
std::vector< std::string > m_outputChannelIdStrings
Definition: ida.h:58
bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output, with signal propagation.
Definition: ida.h:84
PaddingRemover(BufferedTransformation *attachment=NULL)
Definition: ida.h:168
Interface for buffered transformations.
Definition: cryptlib.h:1352
bool GetPossiblePadding() const
Definition: ida.h:176
Interface for custom flush signals propagation.
Definition: simple.h:159
Classes for multiple named channels.
std::vector< ByteQueue > m_outputQueues
Definition: ida.h:59
bool m_possiblePadding
Definition: ida.h:179
RawIDA m_ida
Definition: ida.h:88
Classes and functions for secure memory allocations.
int m_threshold
Definition: ida.h:60
bool m_pad
Definition: ida.h:89
SecBlock< word32 > m_y
Definition: ida.h:63
void Detach(BufferedTransformation *newAttachment=NULL)
Replace an attached transformation.
Definition: filters.cpp:50
RawIDA m_ida
Definition: ida.h:137
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:498
RandomNumberGenerator & m_rng
Definition: ida.h:87
SecretSharing(RandomNumberGenerator &rng, int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)
Construct a SecretSharing.
Definition: ida.h:75
InformationDispersal(int threshold, int nShares, BufferedTransformation *attachment=NULL, bool addPadding=true)
Construct a InformationDispersal.
Definition: ida.h:125
unsigned int m_channelsReady
Definition: ida.h:61
unsigned int GetThreshold() const
Definition: ida.h:29
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: ida.cpp:253
lword m_zeroCount
Definition: ida.h:180
const NameValuePairs & g_nullNameValuePairs
An empty set of name-value pairs.
Definition: cryptlib.cpp:76
Data structure used to store byte strings.
Definition: queue.h:20
Implementation of BufferedTransformation&#39;s attachment interface.
Exception thrown by objects that have not implemented nonblocking input processing.
Definition: cryptlib.h:1470
Base class for unflushable filters.
Definition: simple.h:94
InputChannelMap::iterator m_lastMapPosition
Definition: ida.h:55
Rabin&#39;s Information Dispersal Algorithm.
Definition: ida.h:147
#define CRYPTOPP_UNUSED(x)
Definition: config.h:741
Provides auto signaling support.
Definition: simple.h:280
Implementation of BufferedTransformation&#39;s attachment interface.
Definition: filters.h:36
void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs)
Initialize or reinitialize this object, without signal propagation.
Definition: ida.cpp:247
#define NAMESPACE_END
Definition: config.h:201
SecretRecovery(int threshold, BufferedTransformation *attachment=NULL, bool removePadding=true)
Construct a SecretRecovery.
Definition: ida.h:101
std::vector< char * > parameters
Definition: boostTest.cpp:46
std::map< word32, unsigned int > InputChannelMap
Definition: ida.h:53
word64 lword
Definition: config.h:245
Shamir&#39;s Secret Sharing Algorithm.
Definition: ida.h:97
unsigned int word32
Definition: config.h:231
bool m_pad
Definition: ida.h:111
Multiple channels support for custom signal processing.
Definition: simple.h:215
a variant of Rabin&#39;s Information Dispersal Algorithm
Definition: ida.h:121
Shamir&#39;s Secret Sharing Algorithm.
Definition: ida.h:71
unsigned int m_nextChannel
Definition: ida.h:139
InformationRecovery(int threshold, BufferedTransformation *attachment=NULL, bool removePadding=true)
Construct a InformationRecovery.
Definition: ida.h:151
bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output, with signal propagation.
Definition: ida.h:134
Interface for retrieving values given their names.
Definition: cryptlib.h:279