void longHandDiv(unsigned long long b, unsigned long long a, unsigned long long* quo, unsigned long long* rem, int isLastStep) // b divided by a = quotient: quo and remainder: rem { //find degrees of the polynomials int deg_a = findDeg(a); int deg_b = findDeg(b); /*printf("deg(a) = %d\n", deg_a); printf("deg(b) = %d\n", deg_b);*/ unsigned long long dividend = b; //initialize the masks unsigned long long mask_a = (unsigned long long)255 << (deg_a*8); unsigned long long mask_dividend = (unsigned long long)255 << (deg_b*8); unsigned char firstByte_a = ((mask_a) & a) >> (deg_a*8); unsigned char nxtByte_dividend = 0; unsigned char tempQuo = 0; unsigned long long temp = 0; int i=0; for(i=0; i<deg_b-deg_a+1; i++) { nxtByte_dividend = ((mask_dividend) & dividend) >> ((deg_b - i)*8); //find the next byte in b, form the left. /*printf("byte a = %02x\n", firstByte_a); printf("byte b = %02x\n", nxtByte_dividend); printf("inverse(a) = %02x\n", inverseBitCoeff(firstByte_a)); printf("bigDot(inv(a), nxtByte_b) = %02x\n", bigDot(inverseBitCoeff(firstByte_a), nxtByte_dividend));*/ if(isLastStep == 1 && i==deg_b-deg_a) //we want remainder to be {01} { tempQuo = (bigDot(inverseBitCoeff(firstByte_a), nxtByte_dividend^1)); } else { tempQuo = (bigDot(inverseBitCoeff(firstByte_a), nxtByte_dividend)); } *quo = ((*quo) << 8) | tempQuo; //store quotient in 'quo' temp = (unsigned long long)(circleX(tempQuo, (unsigned int)a)) << ((deg_b - deg_a - i)*8); dividend = dividend ^ temp; mask_dividend = mask_dividend >> 8; /*printf("quo = %02x\n", tempQuo); printf("temp = %010llx\n", temp); printf("dividend = %010llx\n", dividend);*/ } *rem = dividend; //printf("%llx / %llx = quo: %llx rem: %llx\n", b, a, *quo, *rem); }
unsigned char inverseBitCoeff(unsigned char byte) //brute force method to find inverse of polynomial with bit coefficients { if(byte == 0) return 0; int i=0; for(i=0; i<256; i++) if(circleX(byte, (unsigned char)i) == 1) return i; return 0; //control will never reach this statement. }
void computeModprod(char* poly1, char* poly2) { //check if length of hexstring is 8 if(strlen(poly1) != 8 || strlen(poly2) != 8) { fprintf(stderr, "Either poly1 or poly2 or both are not represented by a hexstring of length 8.\n"); return; } else { int k=0; for(k=0; k<8; k++) //check if poly1 contains valid hex symbols { if(poly1[k] < 48 || (poly1[k] > 57 && poly1[k] < 97) || poly1[k] > 102) { fprintf(stderr, "poly1 is not in hexstring format.\n"); return; } } for(k=0; k<8; k++) //check if poly2 contains valid hex symbols { if(poly2[k] < 48 || (poly2[k] > 57 && poly2[k] < 97) || poly2[k] > 102) { fprintf(stderr, "poly2 is not in hexstring format.\n"); return; } } } //convert hexstring to int unsigned int a = convertHexToInt(poly1); unsigned int b = convertHexToInt(poly2); unsigned result = circleX(a, b); //print results unsigned char d3 = (result & (255<<24)) >> 24; unsigned char d2 = (result & (255<<16)) >> 16; unsigned char d1 = (result & (255<<8)) >> 8; unsigned char d0 = (result & 255); printf("{%c%c}{%c%c}{%c%c}{%c%c} CIRCLEX {%c%c}{%c%c}{%c%c}{%c%c} = {%02x}{%02x}{%02x}{%02x}\n", poly1[0], poly1[1], poly1[2], poly1[3], poly1[4], poly1[5], poly1[6], poly1[7], poly2[0], poly2[1], poly2[2], poly2[3], poly2[4], poly2[5], poly2[6], poly2[7], d3, d2, d1, d0); }
void mixColumns(unsigned char state[4][4], unsigned int P) { unsigned int s = 0, mask = (unsigned int)255 << 24; int col = 0, row = 3; for(col=0; col<4; col++) { mask = (unsigned int)255 << 24; s = word(state[3][col], state[2][col], state[1][col], state[0][col]); //construct a 32 bit word from one column in the state array. s = circleX(P, s); //now separate 8 bits and put them back into the column for(row=3; row>=0; row--) { state[row][col] = (s & mask) >> (row*8); mask = mask >> 8; } } }
// ----------------------------------------------------------------------------- // We should just subclass QwtPlotGrid and do all the drawing there. // ----------------------------------------------------------------------------- void StatsGenODFWidget::drawODFPlotGrid(QwtPlot* plot) { // Draw the accepted Inverse Pole Grid Style // Draw the Horizontal and Vertical Lines that form the central cross m_PlotGrid = new QwtPlotMarker(); m_PlotGrid->attach(plot); m_PlotGrid->setLineStyle(QwtPlotMarker::Cross); m_PlotGrid->setLinePen(QPen(Qt::darkMagenta, 1, Qt::SolidLine)); m_PlotGrid->setXValue(0); m_PlotGrid->setYValue(0); // Draw the outer Circle QwtArray<double> circleX(900); // 900 because our plots are hard set to 450 pixels QwtArray<double> circleY(900); float inc = 2.0f / 449.0f; for(int i = 0; i < 450; ++i) { circleX[i] = 1.0 - (i * inc); circleX[450 + i] = -1.0 + (i * inc); circleY[i] = sqrt(1.0 - (circleX[i] * circleX[i])); circleY[450 + i] = -circleY[i]; } m_CircleGrid = new QwtPlotCurve; #if QWT_VERSION >= 0x060000 m_CircleGrid->setSamples(circleX, circleY); #else m_CircleGrid->setData(circleX, circleY); #endif QColor c = QColor(Qt::darkMagenta); c.setAlpha(255); m_CircleGrid->setPen(QPen(c)); m_CircleGrid->setStyle(QwtPlotCurve::Lines); m_CircleGrid->attach(plot); // Draw the Rotated Central cross { QwtArray<double> rotCrossX(2); QwtArray<double> rotCrossY(2); rotCrossX[0] = 0.7071067811f; rotCrossY[0] = sqrt(1.0 - (rotCrossX[0] * rotCrossX[0])); rotCrossX[1] = -0.7071067811f; rotCrossY[1] = -sqrt(1.0 - (rotCrossX[1] * rotCrossX[1])); m_RotCross0 = new QwtPlotCurve; #if QWT_VERSION >= 0x060000 m_RotCross0->setSamples(rotCrossX, rotCrossY); #else m_RotCross0->setData(rotCrossX, rotCrossY); #endif QColor c = QColor(Qt::darkMagenta); c.setAlpha(200); m_RotCross0->setPen(QPen(c)); m_RotCross0->setStyle(QwtPlotCurve::Lines); m_RotCross0->attach(plot); } { QwtArray<double> rotCrossX(2); QwtArray<double> rotCrossY(2); rotCrossX[0] = 0.7071067811f; rotCrossY[0] = -sqrt(1.0 - (rotCrossX[0] * rotCrossX[0])); rotCrossX[1] = -0.7071067811f; rotCrossY[1] = sqrt(1.0 - (rotCrossX[1] * rotCrossX[1])); m_RotCross1 = new QwtPlotCurve; #if QWT_VERSION >= 0x060000 m_RotCross1->setSamples(rotCrossX, rotCrossY); #else m_RotCross1->setData(rotCrossX, rotCrossY); #endif QColor c = QColor(Qt::darkMagenta); c.setAlpha(200); m_RotCross1->setPen(QPen(c)); m_RotCross1->setStyle(QwtPlotCurve::Lines); m_RotCross1->attach(plot); } }