Я нашел эти коды, но я не понимаю значения некоторых кодов.Например, приведенные ниже коды.
while (strSources[0]->Pump(256))
{
for (unsigned int i=1; i<threshold; i++)
strSources[i]->Pump(256);
}
for (unsigned int i=0; i<threshold; i++)
strSources[i]->PumpAll();
, почему каждый Источник прокачивает 256 байтов за раз?почему бы не позволить им перекачать все данные один раз?
// Information Dispesal and Secret Sharing
bool TestSharing()
{
std::cout << "\nInformation Dispersal and Secret Sharing...\n\n";
static const unsigned int INFORMATION_SHARES = 128;
static const unsigned int SECRET_SHARES = 64;
static const unsigned int CHID_LENGTH = 4;
bool pass=true, fail=false;
// ********** Infrmation Dispersal **********//
for (unsigned int shares=3; shares<INFORMATION_SHARES; ++shares)
{
std::string message;
unsigned int len = GlobalRNG().GenerateWord32(4, 0xff);
unsigned int threshold = GlobalRNG().GenerateWord32(2, shares-1);
RandomNumberSource(GlobalRNG(), len, true, new StringSink(message));
ChannelSwitch *channelSwitch = NULLPTR;
StringSource source(message, false, new InformationDispersal(threshold,
shares, channelSwitch = new ChannelSwitch));
std::vector<std::string> strShares(shares);
vector_member_ptrs<StringSink> strSinks(shares);
std::string channel;
// ********** Create Shares
for (unsigned int i=0; i<shares; i++)
{
strSinks[i].reset(new StringSink(strShares[i]));
channel = WordToString<word32>(i);
strSinks[i]->Put((const byte *)channel.data(), CHID_LENGTH);
channelSwitch->AddRoute(channel, *strSinks[i], DEFAULT_CHANNEL);
}
source.PumpAll();
// ********** Randomize shares
GlobalRNG().Shuffle(strShares.begin(), strShares.end());
// ********** Recover secret
try
{
std::string recovered;
InformationRecovery recovery(threshold, new StringSink(recovered));
vector_member_ptrs<StringSource> strSources(threshold);
channel.resize(CHID_LENGTH);
for (unsigned int i=0; i<threshold; i++)
{
strSources[i].reset(new StringSource(strShares[i], false));
strSources[i]->Pump(CHID_LENGTH);
strSources[i]->Get((byte*)&channel[0], CHID_LENGTH);
strSources[i]->Attach(new ChannelSwitch(recovery, channel));
}
while (strSources[0]->Pump(256))
{
for (unsigned int i=1; i<threshold; i++)
strSources[i]->Pump(256);
}
for (unsigned int i=0; i<threshold; i++)
strSources[i]->PumpAll();
fail = (message != recovered);
}
catch (const Exception&)
{
fail = true;
}
pass &= !fail;
}
std::cout << (fail ? "FAILED:" : "passed:") << " " << INFORMATION_SHARES << "
information dispersals\n";
// ********** Secret Sharing **********//
for (unsigned int shares=3; shares<SECRET_SHARES; ++shares)
{
std::string message;
unsigned int len = GlobalRNG().GenerateWord32(4, 0xff);
unsigned int threshold = GlobalRNG().GenerateWord32(2, shares-1);
RandomNumberSource(GlobalRNG(), len, true, new StringSink(message));
ChannelSwitch *channelSwitch = NULLPTR;
StringSource source(message, false, new SecretSharing(GlobalRNG(),
threshold, shares, channelSwitch = new ChannelSwitch));
std::vector<std::string> strShares(shares);
vector_member_ptrs<StringSink> strSinks(shares);
std::string channel;
// ********** Create Shares
for (unsigned int i=0; i<shares; i++)
{
strSinks[i].reset(new StringSink(strShares[i]));
channel = WordToString<word32>(i);
strSinks[i]->Put((const byte *)channel.data(), CHID_LENGTH);
channelSwitch->AddRoute(channel, *strSinks[i], DEFAULT_CHANNEL);
}
source.PumpAll();
// ********** Randomize shares
GlobalRNG().Shuffle(strShares.begin(), strShares.end());
// ********** Recover secret
try
{
std::string recovered;
SecretRecovery recovery(threshold, new StringSink(recovered));
vector_member_ptrs<StringSource> strSources(threshold);
channel.resize(CHID_LENGTH);
for (unsigned int i=0; i<threshold; i++)
{
strSources[i].reset(new StringSource(strShares[i], false));
strSources[i]->Pump(CHID_LENGTH);
strSources[i]->Get((byte*)&channel[0], CHID_LENGTH);
strSources[i]->Attach(new ChannelSwitch(recovery, channel));
}
while (strSources[0]->Pump(256))
{
for (unsigned int i=1; i<threshold; i++)
strSources[i]->Pump(256);
}
for (unsigned int i=0; i<threshold; i++)
strSources[i]->PumpAll();
fail = (message != recovered);
}
catch (const Exception&)
{
fail = true;
}
pass &= !fail;
}
std::cout << (fail ? "FAILED:" : "passed:") << " " << SECRET_SHARES << "
secret sharings\n";
return pass;
}