基于字段扩展的三向公式

时间:2017-05-10 15:06:02

标签: c algorithm math field

我想编写一个在扩展字段中乘以2个多项式的程序。但结果并不匹配。

这是算法:    in section 4

和以前一样,我将2个多项式分成三部分。 下一步是:

P0 = A0B0                                          in F2[X]
P1 = (A0 + A1 + A2)(B0 + B1 + B2)                  in F2[X],
P2 = (A0 + A2 + α(A1 + A2))(B0 + B2 + α(B1 + B2)), in F4[X],
P3 = (A0 + A1 + α(A1 + A2))(B0 + B1 + α(B1 + B2)), in F4[X],
P4 = A2B2                                          in F2[X]

和结果:

 C = (P0 + X^(n/3)P4)(1 + X^n) + (P1 + (1 + α)(P2 + P3))(X^(n/3) + X^(2n/3) 
            +X^n)+α(P2 + P3)X^n + P2X^(2n/3) + P3X^(n/3)

这是我的代码:

void three_way(M_LONG A, M_LONG B, M_LONG &C)
{
   M_LONG P0, P1, P2, P3, P4, tmp,R1,R2,R3, m0, m1, m2, n0, n1, n2;
   M_LONG alpha;
   alpha[0] = 1; alpha[1] = 1;
   unsigned int mA = deg(A), mB = deg(B);
   unsigned int n = mA < mB ? mB : mA;
   int m = (n+1) / 3,h=0;
   int l = 0;
  if (m % 32)
  {
     l = m % 32; 
  }
  h = m/32;
  split3_pol(A, h, l, m0, m1, m2);
  split3_pol(B, h, l, n0, n1, n2);

  mul_pol(m0, n0, P0);//P0=m0*n0
  //P1=(m0+m1+m2)*(n0+n1+n2)
  add_pol(m0, m1, P2);
  add_pol(m2, P2, P2);

  add_pol(n0, n1, tmp);
  add_pol(n2, tmp, tmp);
  mul_pol(P2, tmp, P1);
  //P2 = (A0 + A2 + α(A1 + A2))(B0 + B2 + α(B1 + B2))
  add_pol(m0, m2, P3);
  add_pol(m1, m2, P4);//A1 + A2
  mul_pol(P4, alpha, R1);//α(A1 + A2)
  add_pol(P3, R1, P4);
  //m_shl(P2, 1);
  add_pol(n0, n2, P3);
  add_pol(n1, n2, P2);
  mul_pol(P2, alpha, R2);//α(B1 + B2)
  add_pol(P3, R2, tmp);

  mul_pol(tmp, P4, P2);
  //P3 = (A0 + A1 + α(A1 + A2))(B0 + B1 + α(B1 + B2))
  add_pol(m0, m1, P3);
  add_pol(P3, R1, tmp);

  add_pol(n0, n1, R3);
  add_pol(R2, R3, R3);

  mul_pol(R3, tmp, P3);
  //P4 = A2B2
  mul_pol(m2, n2, P4);
  M_LONG Xn, X2n, X3n, U1, U2, U3, U4, U5, U6, U7;
  int k = h + 1;
  m_zero(Xn, k); l = k * 2; m_zero(X2n, l); l += k; m_zero(X3n, l);
  m_setbit(Xn, m); k = 2 * m; m_setbit(X2n, k); k += m; m_setbit(X3n, k);
  while (Xn[Xn[0]] == 0)
     Xn[0]--;
  while (X2n[X2n[0]] == 0)
        X2n[0]--;
  while (X3n[X3n[0]] == 0)
     X3n[0]--;
//C = (P0 + Xn / 3P4)(1 + Xn) + (P1 + (1 + α)(P2 + P3))(Xn / 3 + X2n / 3 + Xn) + α(P2 + P3)Xn + P2X2n / 3 + P3Xn / 3

add_pol(P2, P3,U1);//U1=P2+P3
mul_pol(alpha, U1, U2);//U2 = αU1 (= α(P2 + P3)) 
//m_copy(U2, U1); m_shl(U2, 1);
//mul_pol(three, U1, U3);//U3 = (1 + α)U1 (= (1 + α)(P2 + P3))
add_pol(U1, U2, U3);
add_pol(P1, U3, U4);//U4 = P1 + U3 (= P1 + (1 + α)(P2 + P3))
//U5 = U4(Xn/3 + X2n/3 + X3n/3)
add_pol(Xn, X3n, tmp);
add_pol(X2n, tmp, tmp);
mul_pol(tmp, U4, U5);

 //U6 = P0 + Xn/3P4 (= P0 + Xn/3P4)
 mul_pol(Xn, P4, U6);
add_pol(P0, U6, U6);

//U7 = U6(1 + Xn) (= P0 + Xn/3P4)(1 + Xn))
mul_pol(U6, X3n, U7);
add_pol(U6, U7, U7);
add_pol(U7, U5, tmp);
mul_pol(X3n, U2, R1);
mul_pol(P2, X2n, R2);
mul_pol(P3, Xn, R3);
//m_shl(R3, 4);
add_pol(tmp, R1, C);
add_pol(R2, C, C);
add_pol(R3, C, C);
while (C[C[0]] == 0)
    C[0]--;
}

,结果如下:result

请告诉我哪里错了?

0 个答案:

没有答案