Ejemplo n.º 1
0
vector<Ref<GenericGFPoly> > ReedSolomonDecoder::runEuclideanAlgorithm(Ref<GenericGFPoly> a,
                                                                      Ref<GenericGFPoly> b,
                                                                      int R) {
  // Assume a's degree is >= b's
  if (a->getDegree() < b->getDegree()) {
    Ref<GenericGFPoly> tmp = a;
    a = b;
    b = tmp;
  }

  Ref<GenericGFPoly> rLast(a);
  Ref<GenericGFPoly> r(b);
  Ref<GenericGFPoly> sLast(field->getOne());
  Ref<GenericGFPoly> s(field->getZero());
  Ref<GenericGFPoly> tLast(field->getZero());
  Ref<GenericGFPoly> t(field->getOne());


  // Run Euclidean algorithm until r's degree is less than R/2
  while (r->getDegree() >= R / 2) {
    Ref<GenericGFPoly> rLastLast(rLast);
    Ref<GenericGFPoly> sLastLast(sLast);
    Ref<GenericGFPoly> tLastLast(tLast);
    rLast = r;
    sLast = s;
    tLast = t;


    // Divide rLastLast by rLast, with quotient q and remainder r
    if (rLast->isZero()) {
      // Oops, Euclidean algorithm already terminated?
      throw ReedSolomonException("r_{i-1} was zero");
    }
    r = rLastLast;
    Ref<GenericGFPoly> q(field->getZero());
    int denominatorLeadingTerm = rLast->getCoefficient(rLast->getDegree());
    int dltInverse = field->inverse(denominatorLeadingTerm);
    while (r->getDegree() >= rLast->getDegree() && !r->isZero()) {
      int degreeDiff = r->getDegree() - rLast->getDegree();
      int scale = field->multiply(r->getCoefficient(r->getDegree()), dltInverse);
      q = q->addOrSubtract(field->buildMonomial(degreeDiff, scale));
      r = r->addOrSubtract(rLast->multiplyByMonomial(degreeDiff, scale));
    }

    s = q->multiply(sLast)->addOrSubtract(sLastLast);
    t = q->multiply(tLast)->addOrSubtract(tLastLast);

  }

  int sigmaTildeAtZero = t->getCoefficient(0);
  if (sigmaTildeAtZero == 0) {
    throw ReedSolomonException("sigmaTilde(0) was zero");
  }

  int inverse = field->inverse(sigmaTildeAtZero);
  Ref<GenericGFPoly> sigma(t->multiply(inverse));
  Ref<GenericGFPoly> omega(r->multiply(inverse));


#ifdef DEBUG
  cout << "t = " << *t << "\n";
  cout << "r = " << *r << "\n";
  cout << "sigma = " << *sigma << "\n";
  cout << "omega = " << *omega << "\n";
#endif

  vector<Ref<GenericGFPoly> > result(2);
  result[0] = sigma;
  result[1] = omega;
  return result;
}
CFX_PtrArray* CBC_ReedSolomonDecoder::RunEuclideanAlgorithm(
    CBC_ReedSolomonGF256Poly* a,
    CBC_ReedSolomonGF256Poly* b,
    int32_t R,
    int32_t& e) {
  if (a->GetDegree() < b->GetDegree()) {
    CBC_ReedSolomonGF256Poly* temp = a;
    a = b;
    b = temp;
  }
  CBC_ReedSolomonGF256Poly* rsg1 = a->Clone(e);
  BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
  CBC_AutoPtr<CBC_ReedSolomonGF256Poly> rLast(rsg1);
  CBC_ReedSolomonGF256Poly* rsg2 = b->Clone(e);
  BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
  CBC_AutoPtr<CBC_ReedSolomonGF256Poly> r(rsg2);
  CBC_ReedSolomonGF256Poly* rsg3 = m_field->GetOne()->Clone(e);
  BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
  CBC_AutoPtr<CBC_ReedSolomonGF256Poly> sLast(rsg3);
  CBC_ReedSolomonGF256Poly* rsg4 = m_field->GetZero()->Clone(e);
  BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
  CBC_AutoPtr<CBC_ReedSolomonGF256Poly> s(rsg4);
  CBC_ReedSolomonGF256Poly* rsg5 = m_field->GetZero()->Clone(e);
  BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
  CBC_AutoPtr<CBC_ReedSolomonGF256Poly> tLast(rsg5);
  CBC_ReedSolomonGF256Poly* rsg6 = m_field->GetOne()->Clone(e);
  BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
  CBC_AutoPtr<CBC_ReedSolomonGF256Poly> t(rsg6);
  while (r->GetDegree() >= R / 2) {
    CBC_AutoPtr<CBC_ReedSolomonGF256Poly> rLastLast = rLast;
    CBC_AutoPtr<CBC_ReedSolomonGF256Poly> sLastLast = sLast;
    CBC_AutoPtr<CBC_ReedSolomonGF256Poly> tLastlast = tLast;
    rLast = r;
    sLast = s;
    tLast = t;
    if (rLast->IsZero()) {
      e = BCExceptionR_I_1IsZero;
      BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
    }
    CBC_ReedSolomonGF256Poly* rsg7 = rLastLast->Clone(e);
    BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
    CBC_AutoPtr<CBC_ReedSolomonGF256Poly> rTemp(rsg7);
    r = rTemp;
    CBC_ReedSolomonGF256Poly* rsg8 = m_field->GetZero()->Clone(e);
    BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
    CBC_AutoPtr<CBC_ReedSolomonGF256Poly> q(rsg8);
    int32_t denominatorLeadingTerm = rLast->GetCoefficients(rLast->GetDegree());
    int32_t dltInverse = m_field->Inverse(denominatorLeadingTerm, e);
    BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
    while (r->GetDegree() >= rLast->GetDegree() && !(r->IsZero())) {
      int32_t degreeDiff = r->GetDegree() - rLast->GetDegree();
      int32_t scale =
          m_field->Multiply(r->GetCoefficients(r->GetDegree()), dltInverse);
      CBC_ReedSolomonGF256Poly* rsgp1 =
          m_field->BuildMonomial(degreeDiff, scale, e);
      BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
      CBC_AutoPtr<CBC_ReedSolomonGF256Poly> build(rsgp1);
      CBC_ReedSolomonGF256Poly* rsgp2 = q->AddOrSubtract(build.get(), e);
      BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
      CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp(rsgp2);
      q = temp;
      CBC_ReedSolomonGF256Poly* rsgp3 =
          rLast->MultiplyByMonomial(degreeDiff, scale, e);
      BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
      CBC_AutoPtr<CBC_ReedSolomonGF256Poly> multiply(rsgp3);
      CBC_ReedSolomonGF256Poly* rsgp4 = r->AddOrSubtract(multiply.get(), e);
      BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
      CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp3(rsgp4);
      r = temp3;
    }
    CBC_ReedSolomonGF256Poly* rsg9 = q->Multiply(sLast.get(), e);
    BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
    CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp1(rsg9);
    CBC_ReedSolomonGF256Poly* rsg10 = temp1->AddOrSubtract(sLastLast.get(), e);
    BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
    CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp2(rsg10);
    s = temp2;
    CBC_ReedSolomonGF256Poly* rsg11 = q->Multiply(tLast.get(), e);
    BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
    CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp5(rsg11);
    CBC_ReedSolomonGF256Poly* rsg12 = temp5->AddOrSubtract(tLastlast.get(), e);
    BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
    CBC_AutoPtr<CBC_ReedSolomonGF256Poly> temp6(rsg12);
    t = temp6;
  }
  int32_t sigmaTildeAtZero = t->GetCoefficients(0);
  if (sigmaTildeAtZero == 0) {
    e = BCExceptionIsZero;
    BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
  }
  int32_t inverse = m_field->Inverse(sigmaTildeAtZero, e);
  BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
  CBC_ReedSolomonGF256Poly* rsg13 = t->Multiply(inverse, e);
  BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
  CBC_AutoPtr<CBC_ReedSolomonGF256Poly> sigma(rsg13);
  CBC_ReedSolomonGF256Poly* rsg14 = r->Multiply(inverse, e);
  BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
  CBC_AutoPtr<CBC_ReedSolomonGF256Poly> omega(rsg14);
  CFX_PtrArray* temp = new CFX_PtrArray;
  temp->Add(sigma.release());
  temp->Add(omega.release());
  return temp;
}