MCPcopy Create free account
hub / github.com/assaultcube/AC / ed25519test

Function ed25519test

source/src/crypto.cpp:648–689  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

646#ifndef STANDALONE
647#ifdef _DEBUG
648void ed25519test(char *vectorfilename) // check ed25519 functions with test vectors from http://ed25519.cr.yp.to/python/sign.input
649{
650 path(vectorfilename);
651 stopwatch watch;
652 int vectorfilesize = 0;
653 char *vectorfile = loadfile(vectorfilename, &vectorfilesize), *b;
654 if(!vectorfile) { conoutf("could not read test vector file %s", vectorfilename); return; }
655 conoutf("loaded %d bytes from %s", vectorfilesize, vectorfilename);
656 watch.start();
657 int lines = 0, inputerrors = 0, pubfail = 0, signfail = 0, verifyfail = 0, verifyfail2 = 0;
658 for(char *l = strtok_r(vectorfile, "\n", &b); l; l = strtok_r(NULL, "\n", &b))
659 { // a line consists of 32 bytes private key, 32 byte public key, ":", 32 byte public key (again), ":", message, ":", signed message, ":"
660 char *vprivpub = l, *vpub = strchr(l, ':'), *vmsg, *vsmsg, hextemp[65];
661 int linelen = strlen(l), msglen = 0;
662 uchar privpub[64], pub[32], *msg = new uchar[linelen], *smsg = new uchar[linelen], temp[64];
663 lines++;
664 if(!vpub || !(vmsg = strchr(vpub + 1, ':')) || !(vsmsg = strchr(vmsg + 1, ':')) ||
665 hex2bin(privpub, vprivpub, 64) != 64 || hex2bin(pub, ++vpub, 32) != 32 || (msglen = hex2bin(msg, ++vmsg, linelen)) < 0 || hex2bin(smsg, ++vsmsg, linelen) != (msglen + 64) ||
666 memcmp(msg, smsg + 64, msglen) || memcmp(privpub + 32, pub, 32)) { inputerrors++; continue; } // line does not match template
667 ed25519_pubkey_from_private(temp, privpub);
668 if(strncmp(bin2hex(hextemp, temp, 32), vpub, 64)) { pubfail++; continue; } // private and public key don't match
669 ed25519_sign(msg, &linelen, msg, msglen, privpub);
670 if(linelen != msglen + 64 || memcmp(msg, smsg, linelen)) { signfail++; continue; } // newly signed message doesn't match vector data
671 if(!ed25519_sign_check(smsg, msglen + 64, pub)) { verifyfail++; continue; } // verification of signed message failed
672 smsg[msglen % 64]++;
673 if(ed25519_sign_check(smsg, msglen + 64, pub)) { verifyfail2++; continue; } // verification of altered signed message didn't fail (...but should've)
674 smsg[msglen % 64]--;
675 if(msglen)
676 {
677 smsg[42 % msglen]++;
678 if(ed25519_sign_check(smsg, msglen + 64, pub)) { verifyfail2++; continue; } // verification of altered signed message didn't fail (...but should've)
679 smsg[42 % msglen]--;
680 }
681 pub[msglen % 32]++;
682 if(ed25519_sign_check(smsg, msglen + 64, pub)) { verifyfail2++; continue; } // verification of altered signed message didn't fail (...but should've)
683 delete[] smsg;
684 delete[] msg;
685 }
686 DELETEA(vectorfile);
687 conoutf("processed %d lines in %d milliseconds", lines, watch.elapsed());
688 conoutf("%d vector file errors, %d private-pubkey mismatches, %d sign fails, %d verify fails, %d missed verify fails", inputerrors, pubfail, signfail, verifyfail, verifyfail2);
689}
690COMMAND(ed25519test, "s");
691
692void ed25519speedtest() // check speed of ed25519 functions

Callers

nothing calls this directly

Calls 11

pathFunction · 0.85
loadfileFunction · 0.85
strtok_rFunction · 0.85
hex2binFunction · 0.85
bin2hexFunction · 0.85
ed25519_signFunction · 0.85
ed25519_sign_checkFunction · 0.85
elapsedMethod · 0.80
conoutfFunction · 0.70
startMethod · 0.45

Tested by

no test coverage detected