I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit 6615586a authored by str4d's avatar str4d
Browse files

Add benchmarks for ElGamal

parent f9b8a5ec
No related branches found
No related tags found
No related merge requests found
package net.i2p.crypto;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import net.i2p.I2PAppContext;
import net.i2p.data.PrivateKey;
import net.i2p.data.PublicKey;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS)
@Fork(1)
@State(Scope.Benchmark)
public class ElGamalBenchmarks {
I2PAppContext ctx = I2PAppContext.getGlobalContext();
PublicKey pubkey;
PrivateKey privkey;
byte[] origPT;
byte[] origCT;
@Setup
public void prepare() {
Object pair[] = KeyGenerator.getInstance().generatePKIKeypair();
pubkey = (PublicKey) pair[0];
privkey = (PrivateKey) pair[1];
origPT = new byte[222];
ctx.random().nextBytes(origPT);
origCT = ctx.elGamalEngine().encrypt(origPT, pubkey);
}
@Benchmark
public Object[] keygen() {
return KeyGenerator.getInstance().generatePKIKeypair();
}
@Benchmark
public byte[] encrypt() {
return ctx.elGamalEngine().encrypt(origPT, pubkey);
}
@Benchmark
public byte[] decrypt() {
return ctx.elGamalEngine().decrypt(origCT, privkey);
}
public static void main(String args[]) throws RunnerException {
Options opt = new OptionsBuilder()
.include(ElGamalBenchmarks.class.getSimpleName())
.build();
new Runner(opt).run();
}
}
......@@ -250,63 +250,4 @@ public final class ElGamalEngine {
+ Base64.encode(rv), new Exception("Doesn't match"));
return null;
}
/****
public static void main(String args[]) {
long eTime = 0;
long dTime = 0;
long gTime = 0;
int numRuns = 100;
if (args.length > 0) try {
numRuns = Integer.parseInt(args[0]);
} catch (NumberFormatException nfe) { // nop
}
try {
Thread.sleep(30 * 1000);
} catch (InterruptedException ie) { // nop
}
RandomSource.getInstance().nextBoolean();
I2PAppContext context = new I2PAppContext();
System.out.println("Running " + numRuns + " times");
for (int i = 0; i < numRuns; i++) {
long startG = Clock.getInstance().now();
Object pair[] = KeyGenerator.getInstance().generatePKIKeypair();
long endG = Clock.getInstance().now();
PublicKey pubkey = (PublicKey) pair[0];
PrivateKey privkey = (PrivateKey) pair[1];
byte buf[] = new byte[128];
RandomSource.getInstance().nextBytes(buf);
long startE = Clock.getInstance().now();
byte encr[] = context.elGamalEngine().encrypt(buf, pubkey);
long endE = Clock.getInstance().now();
byte decr[] = context.elGamalEngine().decrypt(encr, privkey);
long endD = Clock.getInstance().now();
eTime += endE - startE;
dTime += endD - endE;
gTime += endG - startG;
if (!DataHelper.eq(decr, buf)) {
System.out.println("PublicKey : " + DataHelper.toString(pubkey.getData(), pubkey.getData().length));
System.out.println("PrivateKey : " + DataHelper.toString(privkey.getData(), privkey.getData().length));
System.out.println("orig : " + DataHelper.toString(buf, buf.length));
System.out.println("d(e(orig) : " + DataHelper.toString(decr, decr.length));
System.out.println("orig.len : " + buf.length);
System.out.println("d(e(orig).len : " + decr.length);
System.out.println("Not equal!");
System.exit(0);
} else {
System.out.println("*Run " + i + " is successful, with encr.length = " + encr.length + " [E: "
+ (endE - startE) + " D: " + (endD - endE) + " G: " + (endG - startG) + "]\n");
}
}
System.out.println("\n\nAll " + numRuns + " tests successful, average encryption time: " + (eTime / numRuns)
+ " average decryption time: " + (dTime / numRuns) + " average key generation time: "
+ (gTime / numRuns));
}
****/
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment