bool IndexPCalculator::GetSchurMatrix(const SmartPtr<const SchurData>& B, SmartPtr<Matrix>& S) { DBG_START_METH("IndexPCalculator::GetSchurMatrix", dbg_verbosity); bool retval = true; Number* S_values; if (!IsValid(S)) { if ( B==data_A() ) { SmartPtr<DenseSymMatrixSpace> S_sym_space = new DenseSymMatrixSpace(B->GetNRowsAdded()); SmartPtr<DenseSymMatrix> dS = new DenseSymMatrix(GetRawPtr(S_sym_space)); S_values = dS->Values(); S = GetRawPtr(dS); } else { SmartPtr<DenseGenMatrixSpace> S_sym_space = new DenseGenMatrixSpace(B->GetNRowsAdded(), B->GetNRowsAdded()); SmartPtr<DenseGenMatrix> dS = new DenseGenMatrix(GetRawPtr(S_sym_space)); S_values = dS->Values(); S = GetRawPtr(dS); } } else { // Try DenseGenMatrix - if NULL, try DenseSymMatrix SmartPtr<DenseGenMatrix> dS_gen = dynamic_cast<DenseGenMatrix*>(GetRawPtr(S)); if (!IsValid(dS_gen)) { SmartPtr<DenseSymMatrix> dS_sym = dynamic_cast<DenseSymMatrix*>(GetRawPtr(S)); S_values = dS_sym->Values(); } else { S_values = dS_gen->Values(); } } /* DenseGenMatrix* dS = static_cast<DenseGenMatrix*>(&S); DBG_ASSERT(dynamic_cast<const DenseGenMatrix*>(&S)); */ // Check whether data_A was changed from the outside if (ncols_!=data_A()->GetNRowsAdded()) { ncols_ = data_A()->GetNRowsAdded(); ComputeP(); } /* DBG_ASSERT(dS->NRows()==dS->NCols()); DBG_ASSERT(dS->NRows()==data_A()->GetNRowsAdded()); */ std::vector<Index> indices; std::vector<Number> factors; // Compute S = B^T*P from indices, factors and P const std::vector<Index>* data_A_idx = dynamic_cast<const IndexSchurData*>(GetRawPtr(data_A()))->GetColIndices(); const std::vector<Index>* data_B_idx = dynamic_cast<const IndexSchurData*>(GetRawPtr(B))->GetColIndices(); Index col_count = 0; for (std::vector<Index>::const_iterator a_it=data_A_idx->begin(); a_it!=data_A_idx->end(); ++a_it) { cols_[*a_it]->GetSchurMatrixRows(data_B_idx, S_values+col_count*ncols_); col_count++; } return retval; }
/* * ComputeF: Compute the DES f function and store the result in fout. */ static void ComputeF(bool fout[32], bool R[32], bool roundKey[56]) { bool expandedBlock[48], subkey[48], sout[32]; int i,k; /* Expand R into 48 bits using the E expansion */ ComputeExpansionE(expandedBlock, R); DumpBin("expanded E", expandedBlock, 48); /* Convert the roundKey into the subkey using PC2 */ ComputePC2(subkey, roundKey); DumpBin("subkey", subkey, 48); /* XOR the subkey onto the expanded block */ for (i = 0; i < 48; i++) expandedBlock[i] ^= subkey[i]; /* Divide expandedBlock into 6-bit chunks and do S table lookups */ for (k = 0; k < 8; k++) ComputeS_Lookup(k, sout+4*k, expandedBlock+6*k); /* To complete the f() calculation, do permutation P on the S table output */ ComputeP(fout, sout); }