Commit 1c00dc64 authored by mgiannopoulos's avatar mgiannopoulos
Browse files

Serialize/Deserialize publickey and secretkey

parent 808d15c8
......@@ -32,6 +32,7 @@ import edu.jhu.isi.CLSign.sign.Signature;
import edu.jhu.isi.CLSign.verify.Verify;
import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Pairing;
import it.unisa.dia.gas.jpbc.PairingParameters;
import it.unisa.dia.gas.plaf.jpbc.field.z.ZrElement;
import java.util.ArrayList;
......@@ -39,9 +40,10 @@ import java.util.List;
public class CLSign {
public static KeyPair keyGen(final int messageSize) {
final Pairing pairing = KeyGen.createPairing();
final PairingParameters params = KeyGen.createPairingParams();
final Pairing pairing = KeyGen.getPairing(params);
final SecretKey sk = KeyGen.createSecretKey(pairing, messageSize);
final PublicKey pk = KeyGen.createPublicKey(pairing, sk);
final PublicKey pk = KeyGen.createPublicKey(pairing, params, sk);
return new KeyPair(pk, sk);
}
......
......@@ -11,17 +11,15 @@ import java.util.List;
import java.util.stream.Collectors;
public class KeyGen {
public static PublicKey createPublicKey(final Pairing pairing, final SecretKey sk) {
public static PublicKey createPublicKey(final Pairing pairing, final PairingParameters params, final SecretKey sk) {
final Element generator = pairing.getG1().newRandomElement().getImmutable();
final Element generatorT = pairing.getGT().newRandomElement().getImmutable();
final Element X = generator.powZn(sk.getX());
final Element Y = generator.powZn(sk.getY());
final List<Element> Z = sk.getZ().stream()
.map(generator::powZn).collect(Collectors.toList());
final List<Element> W = sk.getZ().stream()
.map(Y::powZn).collect(Collectors.toList());
return new PublicKey(pairing, generator, generatorT,
X, Y, Z, W);
return new PublicKey(pairing, params, generator, X, Y, Z, W);
}
public static SecretKey createSecretKey(final Pairing pairing, final int messageSize) {
......@@ -41,4 +39,19 @@ public class KeyGen {
final PairingParameters params = pairingGenerator.generate();
return PairingFactory.getPairing(params);
}
public static PairingParameters createPairingParams() {
int rBits = 160;
int qBits = 512;
final TypeACurveGenerator pairingGenerator = new TypeACurveGenerator(rBits, qBits);
final PairingParameters params = pairingGenerator.generate();
return params;
}
public static Pairing getPairing(PairingParameters params) {
PairingFactory.getInstance().setReuseInstance(true);
PairingFactory.getInstance().setUsePBCWhenPossible(false);
return PairingFactory.getPairing(params);
}
}
......@@ -23,23 +23,24 @@ package edu.jhu.isi.CLSign.keygen;
import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Pairing;
import it.unisa.dia.gas.jpbc.PairingParameters;
import java.util.ArrayList;
import java.util.List;
public class PublicKey {
private Pairing pairing;
private PairingParameters parameters;
private Element generator;
private Element generatorT;
private Element X;
private Element Y;
private List<Element> Z = new ArrayList<>();
private List<Element> W = new ArrayList<>();
public PublicKey(final Pairing pairing, final Element generator, final Element generatorT, final Element x, final Element y, final List<Element> z, final List<Element> w) {
public PublicKey(final Pairing pairing, final PairingParameters parameters, final Element generator, final Element x, final Element y, final List<Element> z, final List<Element> w) {
this.pairing = pairing;
this.parameters = parameters;
this.generator = generator;
this.generatorT = generatorT;
X = x;
Y = y;
Z = z;
......@@ -50,6 +51,8 @@ public class PublicKey {
return pairing;
}
public PairingParameters getParameters() { return parameters; }
public Element getGenerator() {
return generator;
}
......
package edu.jhu.isi.CLSign.serialize;
import java.io.*;
public class SerializationUtils {
public static byte[] convertToBytes(Object object) throws IOException {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos)) {
out.writeObject(object);
return bos.toByteArray();
}
}
public static Object convertFromBytes(byte[] bytes) throws IOException, ClassNotFoundException {
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream in = new ObjectInputStream(bis)) {
return in.readObject();
}
}
}
package edu.jhu.isi.CLSign.serialize;
import edu.jhu.isi.CLSign.keygen.KeyGen;
import edu.jhu.isi.CLSign.keygen.PublicKey;
import edu.jhu.isi.CLSign.keygen.SecretKey;
import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Pairing;
import it.unisa.dia.gas.jpbc.PairingParameters;
import it.unisa.dia.gas.plaf.jpbc.field.z.ZrElement;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Serialize {
//---------- Secret Key ----------------------------------------------------------
public static byte[] serializeSecretKey(final SecretKey sk, final Pairing pairing) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream writer = new DataOutputStream(baos);
// write...
writeElement(writer, sk.getX(), pairing);
writeElement(writer, sk.getY(), pairing);
writer.writeInt(sk.getZ().size());
for(ZrElement zi : sk.getZ()) {
writeElement(writer, zi, pairing);
}
//----------------
writer.flush();
byte[] result = baos.toByteArray();
return result;
}
public static SecretKey deserializeSecretKey(final byte[] skBytes, final Pairing pairing) throws IOException, ClassNotFoundException {
ByteArrayInputStream bais = new ByteArrayInputStream(skBytes);
DataInputStream reader = new DataInputStream(bais);
// read..
ZrElement x = (ZrElement)readElement(reader, pairing);
ZrElement y = (ZrElement)readElement(reader, pairing);
int messageSize = reader.readInt();
final ZrElement[] z = new ZrElement[messageSize];
for(int i=0; i<messageSize; i++) {
z[i] = (ZrElement)readElement(reader, pairing);
}
return new SecretKey(x,y,z);
}
//------------------------------------------------------------------------------------
//---------- Public Key --------------------------------------------------------------
public static byte[] serializePublicKey(final PublicKey pk) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream writer = new DataOutputStream(baos);
// write...
//write pairing parameters
Pairing pairing = pk.getPairing();
byte[] params = SerializationUtils.convertToBytes(pk.getParameters());
writer.writeInt(params.length);
writer.write(params);
writeElement(writer, pk.getGenerator(), pairing);
writeElement(writer, pk.getX(), pairing);
writeElement(writer, pk.getY(), pairing);
writeElements(writer, pk.getZ(), pairing);
writeElements(writer, pk.getW(), pairing);
//----------------
writer.flush();
byte[] result = baos.toByteArray();
return result;
}
public static PublicKey deserializePublicKey(final byte[] pkBytes) throws IOException, ClassNotFoundException {
ByteArrayInputStream bais = new ByteArrayInputStream(pkBytes);
DataInputStream reader = new DataInputStream(bais);
// read..
//deserialize pairing parameters and get Pairing
int length = reader.readInt();
byte[] bytes = new byte[length];
reader.readFully(bytes);
PairingParameters params = (PairingParameters)SerializationUtils.convertFromBytes(bytes);
Pairing pairing = KeyGen.getPairing(params);
Element generator = readElement(reader, pairing);
Element X = readElement(reader, pairing);
Element Y = readElement(reader, pairing);
List<Element> Z = readElements(reader, pairing);
List<Element> W = readElements(reader, pairing);
return new PublicKey(pairing,params, generator, X, Y, Z, W);
}
//------------------------------------------------------------------------------------
private static Element readElement(DataInputStream reader, Pairing pairing) throws IOException {
int fieldIndex = reader.readInt();
int length = reader.readInt();
byte[] bytes = new byte[length];
reader.readFully(bytes);
Element e = (Element) pairing.getFieldAt(fieldIndex)
.newElementFromBytes(bytes)
.getImmutable();
return e;
}
private static List<Element> readElements(DataInputStream reader, Pairing pairing) throws IOException {
int messageSize = reader.readInt();
List<Element> elements = new ArrayList<>();
for(int i=0; i<messageSize; i++) {
Element element = readElement(reader, pairing);
elements.add(element);
}
return elements;
}
private static void writeElement(final DataOutputStream writer,
final Element element,
final Pairing pairing) throws IOException {
int fieldIndex = pairing.getFieldIndex(element.getField());
writer.writeInt(fieldIndex);
byte[] xBytes = element.toBytes();
writer.writeInt(xBytes.length);
writer.write(xBytes);
}
private static void writeElements(DataOutputStream writer, List<Element> elements, Pairing pairing) throws IOException {
writer.writeInt(elements.size());
for(Element e : elements) {
writeElement(writer, e, pairing);
}
}
}
package edu.jhu.isi.CLSign.serialize;
import edu.jhu.isi.CLSign.CLSign;
import edu.jhu.isi.CLSign.keygen.KeyPair;
import edu.jhu.isi.CLSign.keygen.PublicKey;
import edu.jhu.isi.CLSign.keygen.SecretKey;
import it.unisa.dia.gas.jpbc.Pairing;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class SerializeTest {
@Test
public void testSerializeSecretKey() throws Exception {
final int messageSize = 5;
final KeyPair keyPair = CLSign.keyGen(messageSize);
final PublicKey pk = keyPair.getPk();
final SecretKey sk = keyPair.getSk();
final Pairing pairing = pk.getPairing();
final byte[] skBytes = Serialize.serializeSecretKey(sk, pairing);
final SecretKey deserializedSk = Serialize.deserializeSecretKey(skBytes, pairing);
assertNotNull(pk);
assertNotNull(deserializedSk);
assertEquals(messageSize, deserializedSk.getZ().size());
assertEquals(messageSize, pk.getZ().size());
assertEquals(messageSize, pk.getW().size());
assertEquals(pk.getGenerator().powZn(deserializedSk.getX()), pk.getX());
assertEquals(pk.getGenerator().powZn(deserializedSk.getY()), pk.getY());
for (int i = 0; i < messageSize; i++) {
assertEquals(pk.getGenerator().powZn(deserializedSk.getZ(i)), pk.getZ(i));
assertEquals(pk.getY().powZn(deserializedSk.getZ(i)), pk.getW(i));
}
assertEquals(pk.getPairing().getG1(), pk.getPairing().getG2());
}
@Test
public void testSerializePublicKey() throws Exception {
final int messageSize = 5;
final KeyPair keyPair = CLSign.keyGen(messageSize);
final PublicKey pk = keyPair.getPk();
final SecretKey sk = keyPair.getSk();
final Pairing pairing = pk.getPairing();
final byte[] pkBytes = Serialize.serializePublicKey(pk);
final PublicKey deserializedPk = Serialize.deserializePublicKey(pkBytes);
assertNotNull(deserializedPk);
assertNotNull(sk);
assertEquals(messageSize, sk.getZ().size());
assertEquals(messageSize, deserializedPk.getZ().size());
assertEquals(messageSize, deserializedPk.getW().size());
assertEquals(deserializedPk.getGenerator().powZn(sk.getX()), deserializedPk.getX());
assertEquals(deserializedPk.getGenerator().powZn(sk.getY()), deserializedPk.getY());
for (int i = 0; i < messageSize; i++) {
assertEquals(deserializedPk.getGenerator().powZn(sk.getZ(i)), deserializedPk.getZ(i));
assertEquals(deserializedPk.getY().powZn(sk.getZ(i)), deserializedPk.getW(i));
}
assertEquals(deserializedPk.getPairing().getG1(), deserializedPk.getPairing().getG2());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment