Exemplo n.º 1
0
////////////////////////////////////////////////////////////////////////////////
// Due to SWIG complications, passing in a value by reference really isn't
// feasible. Therefore, we'll use int64 and pass back -1 if we don't find a
// nonce.
int64_t BlockHeader::findNonce(const char* inDiffStr)
{
   const BinaryData playHeader(serialize());
   const CryptoPP::Integer minBDiff("FFFF0000000000000000000000000000000000000000000000000000h");
   const CryptoPP::Integer inDiff(inDiffStr);

   if(inDiff > minBDiff) {
      cout << "Difficulty " << inDiffStr << " is too high for Bitcoin (bdiff)." << endl;
   }
   else {
      volatile bool stopNow=false;

      std::mutex lockSolution;
      bool hasSolution=false;

      const auto computer = [&] (uint32_t startAt, uint32_t stopAt)->int64_t
      {
         BinaryData hashResult(32);
         for(uint32_t nonce=startAt; nonce<stopAt; nonce++)
         {
            *(uint32_t*)(playHeader.getPtr()+76) = nonce;
            BtcUtils::getHash256_NoSafetyCheck(playHeader.getPtr(), HEADER_SIZE,
                                               hashResult);
            const CryptoPP::Integer hashRes((hashResult.swapEndian()).getPtr(),
                                            hashResult.getSize());

            if(hashRes < inDiff)
            {
               unique_lock<mutex> l(lockSolution);
               cout << "NONCE FOUND! " << nonce << endl;
               unserialize(playHeader);
               cout << "Raw Header: " << serialize().toHexStr() << endl;
               pprint();
               cout << "Hash:       " << hashResult.toHexStr() << endl;
               hasSolution=true;
               stopNow=true;
               return nonce;
            }

            if (stopNow)
            {
               break;
            }

            if(startAt==0 && nonce % 10000000 == 0)
            {
               cout << ".";
               cout.flush();
            }
         }

         //needs a return val for windows to build
         return -1;
      };

      const unsigned numThreads = thread::hardware_concurrency();
      vector<thread> threads;
      threads.reserve(numThreads);

      for (unsigned i=0; i < numThreads; i++)
      {
         threads.emplace_back(
            computer,
            (uint32_t)(-1)/numThreads*i,
            (uint32_t)(-1)/numThreads*(i+1)
         );
      }
      for (unsigned i=0; i < numThreads; i++)
         threads[i].join();

      if (!hasSolution) {
         cout << "No nonce found!" << endl;
      }
      // We have to change the coinbase script, recompute merkle root, and then
      // can cycle through all the nonces again.
   }

   // If we've landed here for one reason or another, we've failed. Return 0.
   return -1;
}
Exemplo n.º 2
0
SigNat sign(PublicKey group, RogueKey rogue, Secret secret, Cert cert, Message msg)
{
  char bsn[MAX_CHAR_ARRAY_LENGTH], m[MAX_CHAR_ARRAY_LENGTH];
  char error[MAX_CHAR_ARRAY_LENGTH],c_h[MAX_CHAR_ARRAY_LENGTH];
  char hash[MAX_CHAR_ARRAY_LENGTH], inner[MAX_CHAR_ARRAY_LENGTH];
  BigInt n_v, c, sv, sf0, sf1, se, see, sew, sr, ser, sw;
  Origin b;
  BigInt n,gPrime,g,h,r0,r1,s;
  BigInt biga,e, p2;
  BigInt f0,f1,v;
  Tuple wr,rf0rf1,rwrr,rewrer;
  BigInt bigt1, bigt2, temp1, temp2, temp3, temp4, temp5, temp6, temp7, w, r;
  BigInt zeta,bign_V,rf0,rf1, rv, bigt2inv, tilde_TPrime2,n_t;
  BigInt tilde_T1t, tilde_rf, tilde_N_V,rw,rr,re,ree,rew, rer,hinv, tilde_T1, tilde_T2;
  Hash hashPub, hashHost ,hashTpm1, hashTpm2;
  Responses ss;
  SigNat signat;

  strcpy(bsn,msg.bsn); strcpy(m,msg.m);
  b = msg.b; n_v = msg.n_v;
  if(noOfBits(n_v) > hash_bits){
    strcpy(error,"n_v out of bounds");
    Abort(error);
  }
  n=group.n; gPrime=group.gPrime; g=group.g; h=group.h;
  r0=group.r0; r1=group.r1; s=group.s;
  
  biga = cert.biga; e = cert.e;
  f0 = secret.f; f1=secret.s; v=secret.t;
  wr = pick2(rsa_modulus_bits+distribution_bits);
  w = wr.r; r = wr.m;
  bigt1 = BImodMult(biga,BImodPower(h,wr.r,n),n);
  
  temp1 = BIpower(g,w); temp2 = BIpower(h,e); temp3 = BIpower(gPrime,r);
  temp4 = BImul(temp1, temp2); temp5 = BImul(temp4, temp3);
  bigt2 = BImod(temp5,n);
  
  zeta = base(bsn,rogue);
  bign_V = tag(rogue,zeta,f0,f1);

  rv = pick(random_bits+distribution_bits+hash_bits);
  rf0rf1 = pick2(halfkeyBits+distribution_bits+hash_bits);
  rf0 = rf0rf1.r; rf1 = rf0rf1.m;
  
  temp1 = BIpower(r0,rf0); temp2 = BIpower(r1,rf1); temp3 = BIpower(s,rv);
  temp4 = BImul(temp1, temp2); temp5 = BImul(temp4, temp3);
  tilde_T1t = BImod(temp5,n);

  temp1 = BIshiftLeft(rf1,halfkeyBits);
  temp2 = BIadd(rf0, temp1);
  tilde_rf = BImod(temp2,rogue.rho);
  tilde_N_V = BImodPower(zeta,tilde_rf,rogue.bigGamma);
  
  re = pick(prime_random_bits+distribution_bits+hash_bits);
  ree = pick(2*prime_total_bits+distribution_bits+hash_bits+1);
  rwrr = pick2(rsa_modulus_bits+2*distribution_bits+hash_bits);
  rw = rwrr.r; rr = rwrr.m;
  rewrer = pick2(prime_total_bits+rsa_modulus_bits+2*distribution_bits+hash_bits+1);
  rew = rewrer.r; rer = rewrer.m;
  hinv = BImodInv(h,n);

  temp1 = tilde_T1t; temp2 = BIpower(bigt1,re); temp3 = BIpower(hinv,rew);
  temp4 = BImul(temp1, temp2); temp5 = BImul(temp4, temp3);
  tilde_T1 = BImod(temp5,n);

  temp1 = BIpower(g,rw); temp2 = BIpower(h,re); temp3 = BIpower(gPrime,rr);
  temp4 = BImul(temp1, temp2); temp5 = BImul(temp4, temp3);
  tilde_T2 = BImod(temp5,n);
  
  bigt2inv = BImodInv(bigt2,n);
  temp1 = BIpower(bigt2inv,re); temp2 = BIpower(g,rew); temp3 = BIpower(h,ree); temp4 = BIpower(gPrime,rer);
  temp5 = BImul(temp1, temp2); temp6 = BImul(temp5, temp3); temp7 = BImul(temp6, temp4);
  tilde_TPrime2 = BImod(temp7,n);

  //hash public inputs
  hashPub = getSha1Hash();
  addBI(&hashPub,group.n); addBI(&hashPub,group.g); addBI(&hashPub,group.gPrime); addBI(&hashPub,group.h);
  addBI(&hashPub,group.r0); addBI(&hashPub,group.r1); addBI(&hashPub,group.s); addBI(&hashPub,group.z);
  addBI(&hashPub,rogue.gamma); addBI(&hashPub,rogue.bigGamma); addBI(&hashPub,rogue.rho);
  addBI(&hashPub,zeta); addBI(&hashPub,bigt1); addBI(&hashPub,bigt2); addBI(&hashPub,bign_V);
  strcpy(hash,hashResult(hashPub));

  //hash host inputs
  hashHost = getSha1Hash();
  addBI(&hashHost,tilde_T1); addBI(&hashHost,tilde_T2); addBI(&hashHost,tilde_TPrime2);
  addBI(&hashHost,tilde_N_V); addBI(&hashHost,n_v);
  strcpy(c_h,hashResult(hashHost));

  n_t = pick(distribution_bits);
  
  hashTpm1 = getSha1Hash();
  addBytes(&hashTpm1,c_h); 
  addBI(&hashTpm1,n_t); 
  strcpy(inner,hashResult(hashTpm1));
  hashTpm2 = getSha1Hash();
  addBytes(&hashTpm2,inner); 
  if(b == Card)
    addByte(&hashTpm2,0);
  else if (b == Verifier)
    addByte(&hashTpm2,1);
  else
    ;
  addBytes(&hashTpm2, m);
  c = BItoNumber(hashResult(hashTpm2));
  sv = rval(rv,v,c);
  sf0 = rval(rf0,f0,c);
  sf1 = rval(rf1,f1,c);
  p2 = BIshiftLeft(BItoNumber("1"),prime_total_bits-1);
  se = rval(re,BIsub(e, p2),c);
  see = rval(ree,BImul(e,e),c);
  sw = rval(rw,w,c);
  sew = rval(rew,BImul(e,w),c);
  sr = rval(rr,r,c);
  ser = rval(rer,BImul(e,r),c);

  ss.sv = sv; 
  ss.sf0 = sf0; 
  ss.sf1 = sf1; 
  ss.se = se; 
  ss.see = see; 
  ss.sw = sw; 
  ss.sew = sew; 
  ss.sr = sr; 
  ss.ser = ser;
  
  signat.zeta = zeta;
  signat.bigt1 = bigt1;
  signat.bigt2 = bigt2;
  signat.bign_V = bign_V;
  signat.c = c;
  signat.n_t = n_t;
  signat.ss = ss;

  return signat;
}