Tuesday, November 16, 2010

Hashing a message into an elliptic curve group

I wanted a hash function (h) defined as follows for an implementation that I'm working on these days:

h : {0,1}* → G

This means that h maps a bit string of any length to a point of the the elliptic curve group G.

These sort of functions are quite common in literature and I came up with a quick fix solution for one :-) (and I'm sure there are much better implementations out there).

Here's the code:


import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Pairing;
import it.unisa.dia.gas.plaf.jpbc.pairing.CurveParams;
import it.unisa.dia.gas.plaf.jpbc.pairing.PairingFactory;

import java.io.InputStream;
import java.math.BigInteger;

import org.bouncycastle.crypto.digests.SHA256Digest;

public class Digest {

private Element gElem = null;
private Pairing pairing = null;

public Digest() throws Exception {
CurveParams curveParams = new CurveParams();
curveParams.load(Digest.class.getClassLoader().getResourceAsStream("a_181_603.properties"));

//Load private key
InputStream fis = Digest.class.getClassLoader().getResourceAsStream("keys/digest/g");
byte[] tmp = new byte[1024];
int read = fis.read(tmp);

byte[] gBytes = new byte[read];
System.arraycopy(tmp, 0, gBytes, 0, read);

this.pairing = PairingFactory.getPairing(curveParams);
this.gElem = this.pairing.getG1().newElement();
this.gElem.setFromBytes(gBytes);
}


public byte[] createHash(byte[] data) throws Exception {

//Create a sha 256 of the message
SHA256Digest dgst = new SHA256Digest();
dgst.reset();
dgst.update(data, 0, data.length);
int digestSize = dgst.getDigestSize();
byte[] hash = new byte[digestSize];
dgst.doFinal(hash, 0);

BigInteger val = new BigInteger(hash);

BigInteger order = this.pairing.getG1().getOrder();
BigInteger fact = val.mod(order);

Element gen = this.gElem.duplicate();
Element mul = gen.mul(fact);
return mul.toBytes();
}

}


This uses the jPBC library.

3 comments:

John Alert said...

I have read your blog its very attractive and impressive. I like it your blog.


JavaEE Training in Chennai JavaEE Training in Chennai

Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

Java Online Training Java Online Training Core Java 8 Training in Chennai Java 8 Training in Chennai

John Alert said...

Java Training Institutes Java Training Institutes
Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Java Spring Hibernate Training Institutes in Chennai | Hibernate Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

Hibernate Online Training Hibernate Online Training Hibernate Training in Chennai Hibernate Training in Chennai Java Online Training Java Online Training Hibernate Training Institutes in ChennaiHibernate Training Institutes in Chennai

Anu Sri said...

Good post. Keep sharing such a new topic.

php training in chennai