bool SignatureValidate(PK_Signer &priv, PK_Verifier &pub, bool thorough = false) { bool pass = true, fail; fail = !pub.GetMaterial().Validate(GlobalRNG(), thorough ? 3 : 2) || !priv.GetMaterial().Validate(GlobalRNG(), thorough ? 3 : 2); pass = pass && !fail; cout << (fail ? "FAILED " : "passed "); cout << "signature key validation\n"; static const byte message[] = "test message"; const unsigned int messageLen = COUNTOF(message); SecByteBlock signature(priv.MaxSignatureLength()); size_t signatureLength = priv.SignMessage(GlobalRNG(), message, messageLen, signature); fail = !pub.VerifyMessage(message, messageLen, signature, signatureLength); pass = pass && !fail; cout << (fail ? "FAILED " : "passed "); cout << "signature and verification\n"; ++signature[0]; fail = pub.VerifyMessage(message, messageLen, signature, signatureLength); pass = pass && !fail; cout << (fail ? "FAILED " : "passed "); cout << "checking invalid signature" << endl; if (priv.MaxRecoverableLength() > 0) { signatureLength = priv.SignMessageWithRecovery(GlobalRNG(), message, messageLen, NULL, 0, signature); SecByteBlock recovered(priv.MaxRecoverableLengthFromSignatureLength(signatureLength)); DecodingResult result = pub.RecoverMessage(recovered, NULL, 0, signature, signatureLength); fail = !(result.isValidCoding && result.messageLength == messageLen && VerifyBufsEqual(recovered, message, messageLen)); pass = pass && !fail; cout << (fail ? "FAILED " : "passed "); cout << "signature and verification with recovery" << endl; ++signature[0]; result = pub.RecoverMessage(recovered, NULL, 0, signature, signatureLength); fail = result.isValidCoding; pass = pass && !fail; cout << (fail ? "FAILED " : "passed "); cout << "recovery with invalid signature" << endl; } return pass; }
PPMImage PerformAA(const PPMImage& original, const PPMImage& filtered) { NetPBMLoader loader; PPMImage recovered(original.getLength(), original.getWidth()); PPMImage maxVarDir(original.getLength(), original.getWidth()); PPMImage sobel(original.getLength(), original.getWidth()); for(int i = 0; i < recovered.getLength(); ++i) { for(int j = 0; j < recovered.getWidth(); ++j) { // don't treat border pixels if(i == 0 || j == 0 || i == recovered.getLength()-1 || j == recovered.getWidth()-1) { recovered(i,j) = filtered(i,j); continue; } PPMImage neighbors(3,3); PPMImage neighborsAliased(3,3); Vector3D pixel = original(i,j); Vector3D pixelAlias = filtered(i,j); // colors are changing most rapidly considering this direction Vector3D varDir; // the distance vector from the average value of neighbors Vector3D dAverage[9]; // the average value computed considering the neighbors Vector3D average; // parameters to compute recovery float alpha = 0; float beta = 0; float dp = 0; float Gd = 0; float ep = 0; float Ge = 0; // construct neighbors of aliased and original images for(int k = i-1; k <= i+1; ++k) { for(int l = j-1; l <= j+1; ++l) { neighbors((k-i)+1, (l-j)+1) = original(k,l); neighborsAliased((k-i)+1, (l-j)+1) = filtered(k,l); } } // compute the average for(int k = 0; k < neighbors.getLength(); ++k) { for(int l = 0; l < neighbors.getWidth(); ++l) { average += neighbors(k,l); } } average /= neighbors.getSize(); // compute the distance from the average color value for(int k = 0; k < neighbors.getLength(); ++k) { for(int l = 0; l < neighbors.getWidth(); ++l) { dAverage[k*neighbors.getLength()+l] = neighbors(k,l) - average; } } varDir = Vector3D(average.normalize() + EPSILON); EM(dAverage, 3, varDir, neighbors.getSize()); Vector2D maxColorPos; Vector2D minColorPos; // compute the two maximum colors along the straight line directed by varDir if(ExtremeColors(neighbors, varDir, maxColorPos, minColorPos)) { Vector3D ca = neighbors(minColorPos.x, minColorPos.y); Vector3D cb = neighbors(maxColorPos.x, maxColorPos.y); Vector3D cacb = cb - ca; Vector3D d; alpha = (pixel - ca).dot(cacb) / cacb.dot(cacb); d = pixel - (alpha * cacb) - ca; dp = d.length(); Gd = GAUSS(dp, SIGMA_D); } float eo = Sobel(neighbors); float ef = Sobel(neighborsAliased); ep = ef * eo; Ge = GAUSS(ep, SIGMA_E); Vector3D cbAliased = neighborsAliased(maxColorPos.x, maxColorPos.y); Vector3D caAliased = neighborsAliased(minColorPos.x, minColorPos.y); beta = Gd * (1- Ge); // final composition recovered(i,j) = beta * (caAliased * (1-alpha) + cbAliased * alpha) + (1-beta) * pixelAlias; maxVarDir(i,j) = varDir * 255; sobel(i,j) = Vector3D(sqrt(eo)); } } loader.savePPM(maxVarDir, "max_variance_direction"); loader.savePPM(sobel, "sobel"); return recovered; }