void skipjack_dec( iu8* v, iu8* k ) { iu8 kidx=8, counter=32; #ifdef INLINE iu8 tmp; #endif #ifdef DEBUG printf( "skipjack_dec( %.2hX %.2hX %.2hX %.2hX %.2hX %.2hX %.2hX %.2hX, %.2hX %.2hX %.2hX %.2hX %.2hX %.2hX %.2hX %.2hX %.2hX %.2hX )\n", v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], k[0], k[1], k[2], k[3], k[4], k[4], k[6], k[7], k[8], k[9]); #endif while( counter>24 ) { ginv( v+2, k, kidx ); rule_binv( v, counter ); #ifndef INLINE kidx += 6; if( kidx>=10 ) kidx-=10; #endif counter--; } while( counter>16 ) { rule_ainv( v, counter ); ginv( v, k, kidx ); #ifndef INLINE kidx += 6; if( kidx>=10 ) kidx-=10; #endif counter--; } while( counter>8 ) { ginv( v+2, k, kidx ); rule_binv( v, counter ); #ifndef INLINE kidx += 6; if( kidx>=10 ) kidx-=10; #endif counter--; } while( counter>0 ) { rule_ainv( v, counter ); ginv( v, k, kidx ); #ifndef INLINE kidx += 6; if( kidx>=10 ) kidx-=10; #endif counter--; } }
static trace_data * init_trace(trace_data *T, GEN S, nflift_t *L, GEN q) { long e = gexpo(S), i,j, l,h; GEN qgood, S1, invd; if (e < 0) return NULL; /* S = 0 */ qgood = int2n(e - 32); /* single precision check */ if (cmpii(qgood, q) > 0) q = qgood; S1 = gdivround(S, q); if (gcmp0(S1)) return NULL; invd = ginv(itor(L->den, DEFAULTPREC)); T->dPinvS = gmul(L->iprk, S); l = lg(S); h = lg(T->dPinvS[1]); T->PinvSdbl = (double**)cgetg(l, t_MAT); init_dalloc(); for (j = 1; j < l; j++) { double *t = dalloc(h * sizeof(double)); GEN c = gel(T->dPinvS,j); pari_sp av = avma; T->PinvSdbl[j] = t; for (i=1; i < h; i++) t[i] = rtodbl(mpmul(invd, gel(c,i))); avma = av; } T->d = L->den; T->P1 = gdivround(L->prk, q); T->S1 = S1; return T; }
void skipjack_dec( iu8* v, iu8* k ) { iu8 kidx=8, counter=32; #ifdef INLINE iu8 tmp; #endif while( counter>24 ) { ginv( v+2, k, kidx ); rule_binv( v, counter ); #ifndef INLINE kidx += 6; if( kidx>=10 ) kidx-=10; #endif counter--; } while( counter>16 ) { rule_ainv( v, counter ); ginv( v, k, kidx ); #ifndef INLINE kidx += 6; if( kidx>=10 ) kidx-=10; #endif counter--; } while( counter>8 ) { ginv( v+2, k, kidx ); rule_binv( v, counter ); #ifndef INLINE kidx += 6; if( kidx>=10 ) kidx-=10; #endif counter--; } while( counter>0 ) { rule_ainv( v, counter ); ginv( v, k, kidx ); #ifndef INLINE kidx += 6; if( kidx>=10 ) kidx-=10; #endif counter--; } }
void debug_check_syndrome (void) { int i; for (i = 0; i < 3; i++) { fprintf(stderr, " inv log S[%d]/S[%d] = %d\n", i, i+1, glog[gmult(synBytes[i], ginv(synBytes[i+1]))]); } }
int correct_errors_erasures (unsigned char codeword[], int csize, int nerasures, int erasures[]) { int r, i, j, err; /* If you want to take advantage of erasure correction, be sure to set NErasures and ErasureLocs[] with the locations of erasures. */ NErasures = nerasures; for (i = 0; i < NErasures; i++) ErasureLocs[i] = erasures[i]; Modified_Berlekamp_Massey(); Find_Roots(); if ((NErrors <= NPAR) && NErrors > 0) { /* first check for illegal error locs */ for (r = 0; r < NErrors; r++) { if (ErrorLocs[r] >= csize) { if (RS_DEBUG) fprintf(stderr, "Error loc i=%d outside of codeword length %d\n", i, csize); return(0); } } for (r = 0; r < NErrors; r++) { int num, denom; i = ErrorLocs[r]; /* evaluate Omega at alpha^(-i) */ num = 0; for (j = 0; j < MAXDEG; j++) num ^= gmult(Omega[j], gexp[((255-i)*j)%255]); /* evaluate Lambda' (derivative) at alpha^(-i) ; all odd powers disappear */ denom = 0; for (j = 1; j < MAXDEG; j += 2) { denom ^= gmult(Lambda[j], gexp[((255-i)*(j-1)) % 255]); } err = gmult(num, ginv(denom)); if (RS_DEBUG) fprintf(stderr, "Error magnitude %#x at loc %d\n", err, csize-i); codeword[csize-i-1] ^= err; } return(1); } else { if (RS_DEBUG && NErrors) fprintf(stderr, "Uncorrectable codeword\n"); return(0); } }
void debug_check_syndrome (void) { #ifdef NEVER int i; for (i = 0; i < 3; i++) { printf(" inv log S[%d]/S[%d] = %d\n", i, i+1, glog[gmult(synBytes[i], ginv(synBytes[i+1]))]); } #endif }
static void bestlift_init(long a, GEN nf, GEN pr, GEN C, nflift_t *L) { const long D = 100; const double alpha = ((double)D-1) / D; /* LLL parameter */ const long d = degpol(nf[1]); pari_sp av = avma; GEN prk, PRK, B, GSmin, pk; pari_timer ti; TIMERstart(&ti); if (!a) a = (long)bestlift_bound(C, d, alpha, pr_norm(pr)); for (;; avma = av, a<<=1) { if (DEBUGLEVEL>2) fprintferr("exponent: %ld\n",a); PRK = prk = idealpows(nf, pr, a); pk = gcoeff(prk,1,1); /* reduce size first, "scramble" matrix */ PRK = lllintpartial_ip(PRK); /* now floating point reduction is fast */ PRK = lllint_fp_ip(PRK, 4); PRK = lllint_i(PRK, D, 0, NULL, NULL, &B); if (!PRK) { PRK = prk; GSmin = pk; } /* nf = Q */ else { pari_sp av2 = avma; GEN S = invmat( get_R(PRK) ), BB = GS_norms(B, DEFAULTPREC); GEN smax = gen_0; long i, j; for (i=1; i<=d; i++) { GEN s = gen_0; for (j=1; j<=d; j++) s = gadd(s, gdiv( gsqr(gcoeff(S,i,j)), gel(BB,j))); if (gcmp(s, smax) > 0) smax = s; } GSmin = gerepileupto(av2, ginv(gmul2n(smax, 2))); } if (gcmp(GSmin, C) >= 0) break; } if (DEBUGLEVEL>2) fprintferr("for this exponent, GSmin = %Z\nTime reduction: %ld\n", GSmin, TIMER(&ti)); L->k = a; L->den = L->pk = pk; L->prk = PRK; L->iprk = ZM_inv(PRK, pk); L->GSmin= GSmin; L->prkHNF = prk; init_proj(L, gel(nf,1), gel(pr,1)); }
static real ginvae(arglist *al) /* like ginv, but enrolling At_reset and At_exit */ { static int nginv; AmplExports *ae = al->AE; if (AEInext < AEIlast) { AEInext->n = ++nginv; AEInext->ae = ae; at_reset(At_reset, AEInext); at_exit(At_end, AEInext++); } return ginv(al); }
void CReedSolomon::Modified_Berlekamp_Massey (void) { int L, L2, k, d; int psi [MAX_LENGTH], psi2 [MAX_LENGTH], D [MAX_LENGTH]; int gamma [MAX_LENGTH]; int iMaxLength = m_iCorrectCodeSize * 2; /* initialize Gamma, the erasure locator polynomial */ init_gamma (gamma); /* initialize to z */ copy_poly (D, gamma); mul_z_poly (D); copy_poly (psi, gamma); k = -1; L = m_NErasures; for (int n = m_NErasures; n < m_iCorrectCodeSize; n ++) { d = compute_discrepancy (psi, m_synBytes, L, n); if (d != 0) { /* psi2 = psi - d*D */ for (int i = 0; i < iMaxLength; i++) { psi2 [i] = psi [i] ^ gmult (d, D [i]); } if (L < (n - k)) { L2 = n - k; k = n - L; /* D = scale_poly(ginv(d), psi); */ for (int i = 0; i < iMaxLength; i ++) { D [i] = gmult (psi [i], ginv (d)); } L = L2; } /* psi = psi2 */ for (int i = 0; i < iMaxLength; i ++) { psi [i] = psi2 [i]; } } mul_z_poly (D); } for (int i = 0; i < MAX_LENGTH; i ++) { m_Lambda [i] = psi [i]; } compute_modified_omega (); }
/* From Cain, Clark, "Error-Correction Coding For Digital Communications", pp. 216. */ void Modified_Berlekamp_Massey (void) { int n, L, L2, k, d, i; int psi[MAXDEG], psi2[MAXDEG], D[MAXDEG]; int gamma[MAXDEG]; /* initialize Gamma, the erasure locator polynomial */ init_gamma(gamma); /* initialize to z */ copy_poly(D, gamma); mul_z_poly(D); copy_poly(psi, gamma); k = -1; L = NErasures; for (n = NErasures; n < NPAR; n++) { d = compute_discrepancy(psi, synBytes, L, n); if (d != 0) { /* psi2 = psi - d*D */ for (i = 0; i < MAXDEG; i++) psi2[i] = psi[i] ^ gmult(d, D[i]); if (L < (n-k)) { L2 = n-k; k = n-L; /* D = scale_poly(ginv(d), psi); */ for (i = 0; i < MAXDEG; i++) D[i] = gmult(psi[i], ginv(d)); L = L2; } /* psi = psi2 */ for (i = 0; i < MAXDEG; i++) psi[i] = psi2[i]; } mul_z_poly(D); } for(i = 0; i < MAXDEG; i++) Lambda[i] = psi[i]; compute_modified_omega(); }
int unwhiten(int letter,int row,int column) { int highbits; // save the high 3 bits of the letter byte, which indicate whether it's data or check highbits=letter&-32; if (debugwhiten) printf("r%d c%d %c->",row,column,(letter&31)+'@'); letter=bitctunrot[letter&31]-'@'; if (debugwhiten) printf("%c->",(letter&31)+'@'); letter='_'-bitctrot[oddmul(letter,invoddmul[row&31])]; letter&=31; if (debugwhiten) printf("%c->",(letter&31)+'@'); letter=gmult(letter,ginv(column+1))-column-1; if (debugwhiten) printf("%c\n",(letter&31)+'@'); return (letter&31)|highbits; }
int CReedSolomon::correct_errors_erasures (unsigned char codeword [], int csize, int nerasures, int erasures []) { int i, err; m_NErasures = nerasures; for (i = 0; i < m_NErasures; i ++) { m_ErasureLocs [i] = erasures [i]; } Modified_Berlekamp_Massey (); Find_Roots (); if ((m_NErrors <= m_iCorrectCodeSize) && m_NErrors > 0) { /* first check for illegal error locs */ for (int r = 0; r < m_NErrors; r ++) { if (m_ErrorLocs [r] >= csize) { return(0); } } for (int r = 0; r < m_NErrors; r ++) { int num, denom; i = m_ErrorLocs [r]; /* evaluate m_Omega at alpha^(-i) */ num = 0; for (int j = 0; j < MAX_LENGTH; j ++) { num ^= gmult (m_Omega [j], byExpToInt [((255 - i) * j) % 255]); } /* evaluate m_Lambda' (derivative) at alpha^(-i) ; all odd powers disappear */ denom = 0; for (int j = 1; j < MAX_LENGTH; j += 2) { denom ^= gmult (m_Lambda [j], byExpToInt [((255 - i) * (j - 1)) % 255]); } err = gmult (num, ginv (denom)); codeword [csize - i - 1] ^= err; } return 1; } else { return 0; } }