Skip to content

Commit

Permalink
Merge pull request Art3misOne#2 from Art3misOne/Art3misOne-patch-1
Browse files Browse the repository at this point in the history
Separated error term from private key
  • Loading branch information
Art3misOne authored Oct 9, 2017
2 parents cc5723f + 6e910cf commit 84c31ac
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 40 deletions.
3 changes: 3 additions & 0 deletions RlweConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class Constants {
protected static final int Q_TIMES_16 = 196624;

protected static final int numRecDataBytes = 256;

protected static final byte FOURIER = 0;
protected static final byte ORDINARY = 1;

protected static final Felm[] OMEGA = new Felm[] {
new Felm (1), new Felm (49), new Felm (2401), new Felm (7048), new Felm (1260),
Expand Down
11 changes: 0 additions & 11 deletions RlweKeyExchange.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,6 @@ public byte[] initAgreement (RlwePrivateKey kI, RlwePublicKey kR, byte[] rdata)
}


/*
* At a high level, 4 coefficients are used per bit of shared key produced. Computing
* reconciliation data involves finding the closest lattice vector to those 4 coefficients (as a
* vector) and computing the discretized difference between those coefficients and the closest
* lattice vector. This discretized difference is the reconciliation data vector, r. It is assumed
* that translating a lattice point by r will move it closer to the correct lattice point. The
* function helpRec uses an algorithm for the closest vector problem to find r, and the function
* rec computes reconciliation using r. The reconciliation data is 2 bits per coefficient so this
* is compressed before being sent to reduce bandwidth.
*/

private byte[] helpRec (RingElt v) {
int i, j, k, x, rbit, norm;
int[] v0 = new int[4];
Expand Down
58 changes: 31 additions & 27 deletions RlweKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class RlwePublicKey {


public RlwePublicKey (RlwePrivateKey k, RingElt a) {
key = a.pointwiseMultAdd(k.getS (), k.getE ());
RingElt e = Sample.getSample ();
e.ntt ();
key = a.pointwiseMultAdd (k.getS (), e);
}


Expand All @@ -43,7 +45,7 @@ public RingElt getKey () {
return key;
}


public byte[] serialize () {
return key.toByteArray();
}
Expand All @@ -57,26 +59,22 @@ public int hashcode () {

class RlwePrivateKey {
private RingElt s;
private RingElt e;

private byte domain;

public RlwePrivateKey (RingElt sIn, RingElt eIn) {
public RlwePrivateKey (RingElt sIn) {
s = new RingElt (sIn);
e = new RingElt (eIn);
}


public RlwePrivateKey () {
s = Sample.getSample ();
e = Sample.getSample ();
domain = Constants.ORDINARY;
}


public RlwePrivateKey (byte[] inBytes) {
// Reconstruct a private key from a byte array assuming s and e are the same size.
int len = inBytes.length;
s = new RingElt (Arrays.copyOfRange (inBytes, 0, len / 2));
e = new RingElt (Arrays.copyOfRange (inBytes, len / 2, len));
domain = inBytes[0];
s = new RingElt (Arrays.copyOfRange (inBytes, 1, inBytes.length));
}


Expand All @@ -85,36 +83,36 @@ public RingElt getS () {
}


public RingElt getE () {
return e;
}


public void toFourierDomain () {
s.ntt();
e.ntt();
if (domain == Constants.ORDINARY) {
s.ntt();
domain = Constants.FOURIER;
}
}


public void fromFourierDomain () {
s.nttInv();
e.nttInv();
if (domain == Constants.FOURIER) {
s.nttInv();
domain = Constants.ORDINARY;
}
}


public byte[] serialize () {
byte[] sba = s.toByteArray ();
byte[] eba = e.toByteArray ();
byte[] r = new byte[sba.length + eba.length];
System.arraycopy (sba, 0, r, 0, sba.length);
System.arraycopy (eba, 0, r, sba.length, eba.length);
return r;
byte[] sba = s.toByteArray();
byte[] ba = new byte[sba.length + 1];

ba[0] = domain;
System.arraycopy (sba, 0, ba, 1, sba.length);

return ba;
}
}


class RlweKeyPair {
private final RlwePublicKey pubKey;
private RlwePublicKey pubKey;
private final RlwePrivateKey privKey;


Expand Down Expand Up @@ -147,4 +145,10 @@ public RlwePrivateKey getPrivateKey () {
public RlwePublicKey getPublicKey () {
return pubKey;
}


// Generate a new public key with the same private key but new error term
public void genNewPubKey (RingElt a) {
pubKey = new RlwePublicKey (privKey, a);
}
}
2 changes: 0 additions & 2 deletions RlweTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ public static void main (String[] args) {
/*
System.out.println ("Initiator private key: ");
System.out.println ("\t s = " + keysI.getPrivateKey().getS());
System.out.println ("\t e = " + keysI.getPrivateKey().getE() + "\n");
System.out.println ("Initiator public key: ");
System.out.println ("\t k = " + keysI.getPublicKey().getKey() + "\n");
System.out.println ("Responder private key: ");
System.out.println ("\t s = " + keysR.getPrivateKey().getS());
System.out.println ("\t e = " + keysR.getPrivateKey().getE() + "\n");
System.out.println ("Responder public key: ");
System.out.println ("\t k = " + keysR.getPublicKey().getKey() + "\n");
Expand Down

0 comments on commit 84c31ac

Please sign in to comment.