byte *ecc_decode(vlPoint toDecode, const char *passwd, vlPoint msg) { lunit *expt = NULL; lunit *logt = NULL; assert(toDecode != NULL); prng p; int i; vlPoint secret; byte *result; ByteArrayStream bas; byteStreamInit(&bas, (VL_SIZE) << 1); prng_init(&p); prng_set_secret_str(&p, passwd); prng_to_vlong(&p, secret); gfInit(&expt, &logt); assert(logt != NULL && expt != NULL); cpDecodeSecret(secret, toDecode, msg, expt, logt); for (i = 0; i < VL_SIZE; i++) { bas.writeShort(&bas, msg[i]); } gfQuit(expt, logt); result = bas.toArray(&bas); bas.dispose(&bas); prng_init(&p); vlClear(secret); return result; }
byte *getPubKey(const char *password, vlPoint publicKey) { lunit *expt = NULL; lunit *logt = NULL; vlPoint secret; prng p; int i; byte *result; ByteArrayStream bas; byteStreamInit(&bas, (VL_SIZE) << 1); prng_init(&p); prng_set_secret_str(&p, password); // 生成SHA-1的Password串 prng_to_vlong(&p, secret); // 将160bit的SHA-1转换成vlPoint gfInit(&expt, &logt); assert(logt != NULL && expt != NULL); cpMakePublicKey(publicKey, secret, expt, logt); for (i = 0; i < VL_SIZE; i++) { bas.writeShort(&bas, publicKey[i]); // 输入之后一定是BIG_ENDIAN } result = bas.toArray(&bas); bas.dispose(&bas); gfQuit(expt, logt); prng_init(&p); vlClear(secret); return result; }
byte *getRc4Key(const char *seed, vlPoint rc4Encode, vlPoint publicKey, vlPoint msg) { lunit *expt = NULL; lunit *logt = NULL; assert(rc4Encode != NULL); prng p; int i; byte *result; ByteArrayStream bas; byteStreamInit(&bas, (VL_SIZE) << 1); prng_init(&p); prng_set_secret_str(&p, seed); // seed 生成Rc4秘钥 prng_set_time(&p); prng_to_vlong(&p, rc4Encode); // 生成 写入 gfInit(&expt, &logt); cpEncodeSecret(publicKey, msg, rc4Encode, expt, logt); gfQuit(expt, logt); for (i = 0; i < VL_SIZE; i++) { bas.writeShort(&bas, rc4Encode[i]); } result = bas.toArray(&bas); bas.dispose(&bas); prng_init(&p); vlClear(rc4Encode); return result; }
byte *ecc_encode(vlPoint session, vlPoint publicKey, vlPoint msg) { lunit *expt = NULL; lunit *logt = NULL; assert(session != NULL && publicKey != NULL); prng p; int i; byte *result; ByteArrayStream bas; byteStreamInit(&bas, (VL_SIZE) << 1); prng_init(&p); gfInit(&expt, &logt); cpEncodeSecret(publicKey, msg, session, expt, logt); for (i = 0; i < VL_SIZE; i++) { bas.writeShort(&bas, msg[i]); } gfQuit(expt, logt); result = bas.toArray(&bas); bas.dispose(&bas); prng_init(&p); vlClear(publicKey); vlClear(session); return result; }
void RSCoder::Init(int ParSize) { RSCoder::ParSize=ParSize; // Store the number of recovery volumes. FirstBlockDone=false; gfInit(); pnInit(); }
RSCoder::RSCoder(int ParSize) { RSCoder::ParSize=ParSize; FirstBlockDone=false; gfInit(); pnInit(); }
RSCoder16::RSCoder16() { Decoding=false; ND=NR=NE=0; ValidFlags=NULL; MX=NULL; DataLog=NULL; DataLogSize=0; gfInit(); }
// Compute generator polynomial and super polynomial // (may instead be done at compile time): // rsGen(X) = prod(X - z^i) for i = 1 ... n-k // rsSup(X) = prod(X - z^i) for i = 0 ... m-1 // = X^m - 1 // ----------------------------------------------------------------------------- void rsInit() // ----------------------------------------------------------------------------- { printf("---------- rsInit\n"); gfInit(); // ---------- compute rsSup = X^m - 1 (only upper n-k coeffs) rsSup[RS_N_K - 1] = GF_1; for (int i=RS_N_K - 2; i>=0; i--) rsSup[i] = GF_0; // ---------- compute rsGen: gfExp T[RS_N_K + 1]; // tmp int nD1 = 0; gfExp* D1 = rsGen; int nD2; gfExp* D2 = T; D1[0] = GF_1; gfExp Z[2] = {GF_Z(1), 1}; // 1st root of rsGen(X): (X - z^1) for (int i=RS_N_K; i>0; i--) { nD2 = gfPolMul(D1, nD1, Z, 1, D2); Z[0]++; // (X - z^i) => (X - z^(i+1)) // swap D1 <-> D2: int s = nD1; gfExp* t = D1; D1 = D2; nD1 = nD2; D2 = t; nD2 = s; // result is in D1 } // copy rsGen = D1 (if not already identical) for (int i=nD1; i>=0; i--) { // an optimization in gfPolDiv1() requires (rsGen[i] != 0 for all i) // -> check here if (D1[i] == GF_0) { printf("!! BAD CONFIG: generator polynomial has 0-coefficient !!\n"); rsGen[0] = 1 / rsSup[0]; // throw DBZ-exception (trying to fool compiler) } rsGen[i] = D1[i]; } printPol("ini: rsSup", rsSup, RS_N_K - 1); printPol("ini: rsGen", rsGen, RS_N_K); }
// ----------------------------------------------------------------------------- int main () // ----------------------------------------------------------------------------- { // verify basic operations (mul, add, div): // a+b = b+a // a*b = b*a // a+(b+c) = (a+b)+c // a*(b*c) = (a*b)*c // a*c + b*c = (a+b)*c // a*(b/a) = b (if a != 0) gfInit(); for (int a=0; a<GF_N; a++) { for (int b=0; b<GF_N; b++) { if (gfAdd(a, b) != gfAdd(b, a)) return 1; if (gfMul(a, b) != gfMul(b, a)) return 2; if (gfMul(a, b) != gfMul(b, a)) return 2; for (int c=0; c<GF_N; c++) { if (gfAdd(a, gfAdd(b, c)) != gfAdd(gfAdd(a, b), c)) return 3; if (gfMul(a, gfMul(b, c)) != gfMul(gfMul(a, b), c)) return 4; if (gfAdd(gfMul(a, c), gfMul(b, c)) != gfMul(gfAdd(a, b), c)) return 5; } if (a != GF_0) if (gfMul(a, gfDiv(b, a)) != b) return 6; } } // verify polynomial operations: // A = BQ + R const int M = GF_N; // max deg gfExp A[M+1]; int nA; gfExp B[M+1]; int nB; gfExp Q[M+1]; int nQ; gfExp R1[M+2]; gfExp* R = R1 + 1; int nR; gfExp Z[M+1]; int nZ; gfExp Z1[2*M]; int nZ1; gfExp Z2[2*M]; int nZ2; gfExp* P = R; int nP; // alias gfExp* N = B; int nN; // alias gfExp* Y = Q; int nY; // alias gfExp Mem[8*(M+1) + 3]; // // -------------------- test polDiv, polMul, gfPolAdd(): -------------------- // for (int test=0; test<100000; test++) { // // // clear all -- should not be required: // // for (int i=0; i<=M; i++) // // A[i] = B[i] = Q[i] = R[i] = Z[i] = 0; // // R[-1] = 0; // nB = randInt(0, M); // nA = randInt(nB, M); // randPol(A, nA); // randPol(B, nB); // if (gfPolDeg(B, nB) == -1) // avoid dividing by B=0 // continue; // nB = polDiv(A, nA, B, nB, Q, &nQ, R, &nR); // nZ = gfPolMul(Q, nQ, B, nB, Z); // B * Q // if (nZ > nA) // return 7; // nZ = gfPolAdd(Z, nZ, R, nR, Z); // + R // if (! polCmp(Z, A, nZ, nA)) // return 8; // } // // -------------------- test gfPolEEA(): -------------------- // for (int test=0; test<100000; test++) { // nN = randInt(1, M); // nA = randInt(0, nN-1); // randPol(A, nA); // randPol(N, nN); // if (gfPolDeg(A, nA) == -1) // avoid dividing by A=0 // continue; // gfPolEEA(N, nN, A, nA, P, &nP, Q, &nQ, Mem); // nZ1 = gfPolMul(P, nP, N, nN, Z1); // nZ2 = gfPolMul(Q, nQ, A, nA, Z2); // if (! polCmp(Z1, Z2, nZ1, nZ2)) // return 9; // } // -------------------- test gfPolEvalSeq() against gfPolEval(): -------------------- for (int test=0; test<10000; test++) { nA = randInt(0, GF_N - 2); // limit of gfPolEvalSeq() nY = randInt(0, M); gfVec* Yv = Y; randPol(A, nA); gfExp x = GF_Z(1);//randE1(); gfPolEvalSeq(A, nA, Yv, nY, x); for (int i=0; i<=nY; i++) { gfExp y2 = gfPolEval(A, nA, x); gfExp y1 = gfV2E[Yv[nY-i]]; if (y2 != y1) return 10; x = gfMul(x, GF_Z(1)); } } return 0; }