Skip to content

Commit

Permalink
Big refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cygnusv committed Feb 2, 2016
1 parent ed7877e commit 6cd09f7
Show file tree
Hide file tree
Showing 4 changed files with 371 additions and 0 deletions.
62 changes: 62 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nics.crypto.ntrureencrypt
</groupId>
<artifactId>ntrureencrypt</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>NTRUReEncrypt</name>
<url>https://github.com/cygnusv/ntrureencrypt</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.sf.ntru</groupId>
<artifactId>ntru</artifactId>
<version>1.2</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>nics.crypto.ntrureencrypt.TestNTRUReEncrypt</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
194 changes: 194 additions & 0 deletions src/main/java/nics/crypto/ntrureencrypt/NTRUReEncrypt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package nics.crypto.ntrureencrypt;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import net.sf.ntru.encrypt.EncryptionKeyPair;
import net.sf.ntru.encrypt.EncryptionParameters;
import net.sf.ntru.encrypt.EncryptionPrivateKey;
import net.sf.ntru.encrypt.EncryptionPublicKey;
import net.sf.ntru.encrypt.NtruEncrypt;
import net.sf.ntru.polynomial.IntegerPolynomial;
import net.sf.ntru.polynomial.Polynomial;

/**
*
* @author David Nuñez <dnunez (at) lcc.uma.es>
*/
public class NTRUReEncrypt {


EncryptionParameters params;
NtruEncrypt ntru;

private IntegerPolynomial one;

static boolean out = true;

/**
* Constructs a new instance with a set of encryption parameters.
*
* @param params encryption parameters
*/
public NTRUReEncrypt(EncryptionParameters params) {
ntru = new NtruEncrypt(params);
this.params = params;

one = new IntegerPolynomial(params.N);
one.coeffs[0] = 1;
}

private Polynomial generateBlindingPolynomial(byte[] seed) throws Exception {

// for(Method m : ntru.getClass().getDeclaredMethods()){
// System.out.println(m.getName());
// }
Method m = ntru.getClass().getDeclaredMethod("generateBlindingPoly", byte[].class);
m.setAccessible(true);
return (Polynomial) m.invoke(ntru, seed);
}

private static IntegerPolynomial extractH(EncryptionPublicKey pub) throws Exception {
Field f = EncryptionPublicKey.class.getDeclaredField("h");
f.setAccessible(true);
return ((IntegerPolynomial) f.get(pub)).toIntegerPolynomial();
}

public EncryptionKeyPair generateKeyPair() {
return this.ntru.generateKeyPair();
}



public ReEncryptionKey generateReEncryptionKey(EncryptionPrivateKey pA, EncryptionPrivateKey pB) throws Exception {

IntegerPolynomial fA = privatePolynomial(pA);
IntegerPolynomial fB = privatePolynomial(pB);

return new ReEncryptionKey(fA, fB, params.q);
}

public ReEncryptionKey generateReEncryptionKey(EncryptionKeyPair pA, EncryptionKeyPair pB) throws Exception {
return generateReEncryptionKey(pA.getPrivate(), pB.getPrivate());
}

public IntegerPolynomial encrypt(EncryptionPublicKey pub, IntegerPolynomial m) throws Exception {

Polynomial r = generateBlindingPolynomial(generateSeed()); // oid es cualquier cosa

out("r = " + Arrays.toString(r.toIntegerPolynomial().coeffs));

IntegerPolynomial h = extractH(pub);

out("h = " + Arrays.toString(h.coeffs));

IntegerPolynomial e = r.mult(h);

out("e = " + Arrays.toString(e.coeffs));

e.add(m);

out("e = " + Arrays.toString(e.coeffs));

e.ensurePositive(params.q);

out("e = " + Arrays.toString(e.coeffs));

return e;

}

public IntegerPolynomial reEncrypt(ReEncryptionKey rk, IntegerPolynomial c) throws Exception {

Polynomial r = generateBlindingPolynomial(generateSeed()); // oid es cualquier cosa



IntegerPolynomial ruido = r.toIntegerPolynomial();
ruido.mult(3);
ruido.modCenter(params.q);


IntegerPolynomial c_prime = c.mult(rk.rk);
c_prime.add(ruido);
c_prime.ensurePositive(params.q);

return c_prime;

}

public IntegerPolynomial decrypt(EncryptionPrivateKey priv, IntegerPolynomial c) throws Exception {

IntegerPolynomial f = privatePolynomial(priv);

IntegerPolynomial a = c.toIntegerPolynomial().mult(f);

a.modCenter(params.q);

IntegerPolynomial m = a.toIntegerPolynomial();

m.mod3();

return m;

}

public static void out(String s) {
if (out) {
System.out.println(s);
}
}

public IntegerPolynomial message(byte[] msg) {
// Crea un mensaje aleatorio con dm 0's, dm 1's y dm -1's.
IntegerPolynomial m = new IntegerPolynomial(params.N);
Random rand = new SecureRandom(msg);
ArrayList<Integer> list = new ArrayList<Integer>();

int dm0 = 106; // params.dm0; FIXED FROM ees1171ep1

while (list.size() < dm0 * 3) {
Integer i = rand.nextInt(params.N);
if (!list.contains(i)) {
list.add(i);
}
}
for (int j = 0; j < dm0; j++) {
m.coeffs[list.get(j)] = 0;
}
for (int j = dm0; j < 2 * dm0; j++) {
m.coeffs[list.get(j)] = -1;
}
for (int j = 2 * dm0; j < 3 * dm0; j++) {
m.coeffs[list.get(j)] = 1;
}
out("m = " + Arrays.toString(m.coeffs));
return m;
}

public static IntegerPolynomial extractF(EncryptionPrivateKey priv) throws Exception {
Field f = EncryptionPrivateKey.class.getDeclaredField("t");
f.setAccessible(true);
return ((Polynomial) f.get(priv)).toIntegerPolynomial();
}

public IntegerPolynomial privatePolynomial(EncryptionPrivateKey priv) throws Exception {

IntegerPolynomial f = extractF(priv);
f.mult(3);
f.add(one);

return f;
}

private byte[] generateSeed() {
return new byte[]{1, 2, 3, 4};
}
}
30 changes: 30 additions & 0 deletions src/main/java/nics/crypto/ntrureencrypt/ReEncryptionKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package nics.crypto.ntrureencrypt;

import net.sf.ntru.polynomial.IntegerPolynomial;

/**
*
* @author David Nuñez <dnunez (at) lcc.uma.es>
*/
public class ReEncryptionKey {


public IntegerPolynomial rk;



public ReEncryptionKey(IntegerPolynomial fA, IntegerPolynomial fB, int q) {

IntegerPolynomial fBinv = fB.toIntegerPolynomial().invertFq(q);

rk = fA.toIntegerPolynomial().mult(fBinv);


}

}
85 changes: 85 additions & 0 deletions src/main/java/nics/crypto/ntrureencrypt/TestNTRUReEncrypt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package nics.crypto.ntrureencrypt;

import java.util.Arrays;
import net.sf.ntru.encrypt.EncryptionKeyPair;
import net.sf.ntru.encrypt.EncryptionParameters;
import net.sf.ntru.polynomial.IntegerPolynomial;

/**
*
* @author David Nuñez <dnunez (at) lcc.uma.es>
*/
public class TestNTRUReEncrypt {

static EncryptionParameters[] eps = {
EncryptionParameters.EES1087EP2, //0
EncryptionParameters.EES1087EP2_FAST, //1
EncryptionParameters.EES1171EP1, // 2
EncryptionParameters.EES1171EP1_FAST, // 3
EncryptionParameters.EES1499EP1, // 4
EncryptionParameters.EES1499EP1_FAST, // 5
EncryptionParameters.APR2011_439, // 6
EncryptionParameters.APR2011_439_FAST, // 7
EncryptionParameters.APR2011_743, // 8
EncryptionParameters.APR2011_743_FAST // 9
};

public static void main(String[] args) throws Exception {
test1();
test2();
}

public static void test1() throws Exception {

EncryptionParameters ep = eps[3]; // EES1171EP1_FAST

NTRUReEncrypt ntruReEnc = new NTRUReEncrypt(ep);

EncryptionKeyPair kpA = ntruReEnc.generateKeyPair();

IntegerPolynomial m = ntruReEnc.message(new byte[]{12,34,56});

IntegerPolynomial c = ntruReEnc.encrypt(kpA.getPublic(), m);

IntegerPolynomial m2 = ntruReEnc.decrypt(kpA.getPrivate(), c);


if (Arrays.equals(m.coeffs, m2.coeffs)) {
System.out.println("Test 1 OK!");
} else {
System.out.println("Test 1 Failed!");
}
}

public static void test2() throws Exception {

EncryptionParameters ep = eps[3]; // EES1171EP1_FAST

NTRUReEncrypt ntruReEnc = new NTRUReEncrypt(ep);

EncryptionKeyPair kpA = ntruReEnc.generateKeyPair();

IntegerPolynomial m = ntruReEnc.message(new byte[]{12,34,56});

IntegerPolynomial c = ntruReEnc.encrypt(kpA.getPublic(), m);

EncryptionKeyPair kpB = ntruReEnc.generateKeyPair();

ReEncryptionKey rk = ntruReEnc.generateReEncryptionKey(kpA, kpB);

IntegerPolynomial cB = ntruReEnc.reEncrypt(rk, c);

IntegerPolynomial m2 = ntruReEnc.decrypt(kpB.getPrivate(), cB);


if (Arrays.equals(m.coeffs, m2.coeffs)) {
System.out.println("Test 2 OK!");
} else {
System.out.println("Test 2 Failed!");
}
}
}

0 comments on commit 6cd09f7

Please sign in to comment.