Skip to content

Commit

Permalink
Merge pull request #155 from cryptimeleon/develop
Browse files Browse the repository at this point in the history
New version
  • Loading branch information
JanBobolz authored Apr 14, 2022
2 parents e4a2313 + af882a8 commit 5355a17
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 17 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
group = 'org.cryptimeleon'
archivesBaseName = project.name
boolean isRelease = project.hasProperty("release")
version = '3.1.0' + (isRelease ? "" : "-SNAPSHOT")
version = '3.1.1' + (isRelease ? "" : "-SNAPSHOT")

sourceCompatibility = 1.8
targetCompatibility = 1.8
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/cryptimeleon/math/misc/BigIntegerTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,22 @@ public static Integer getExactInt(BigInteger num) {
throw new ArithmeticException("Integer value of BigInteger " + num + " is out of integer range");
return num.intValue();
}

/**
* Returns the long value corresponding to the given {@link BigInteger}, throwing a {@link ArithmeticException}
* if the value is out of bounds.
* <p>
* Implements the {@code longValueExact} method of {@code BigInteger} that is missing in the Android Sdk.
*
* @param num the {@code BigInteger} to convert
* @return the integer with the same value as {@code num}
*
* @throws ArithmeticException if the value of {@code num} is outside the supported long values
*/
public static Long getExactLong(BigInteger num) {
if (num.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0
|| num.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0)
throw new ArithmeticException("Long value of BigInteger " + num + " is out of long range");
return num.longValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public BigIntegerRepresentation(BigInteger n) {
this.n = n;
}

public BigIntegerRepresentation(int n) {
public BigIntegerRepresentation(long n) {
this.n = BigInteger.valueOf(n);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class StandaloneRepresentationHandler implements RepresentationHandler {

// it may be temping to add int.class etc. here, but it doesn't work because the ReprUtil assumes that everything
// that's not null is already set (and int is auto-initialized with 0)
private static final Class<?>[] supportedTypes = new Class[] {
StandaloneRepresentable.class, BigInteger.class, Integer.class, String.class, Boolean.class,
private static final Class<?>[] supportedTypes = new Class[]{
StandaloneRepresentable.class, BigInteger.class, Integer.class, Long.class, String.class, Boolean.class,
byte[].class, UUID.class, Enum.class
};
/**
Expand Down Expand Up @@ -68,6 +68,10 @@ public Object deserializeFromRepresentation(Representation repr, Function<String
return BigIntegerTools.getExactInt(repr.bigInt().get());
}

if (type.isAssignableFrom(Long.class) && repr instanceof BigIntegerRepresentation) {
return BigIntegerTools.getExactLong(repr.bigInt().get());
}

if (type.isAssignableFrom(String.class) && repr instanceof StringRepresentation) {
return repr.str().get();
}
Expand Down Expand Up @@ -119,6 +123,11 @@ public Representation serializeToRepresentation(Object value) {
return new BigIntegerRepresentation(integer);
}

if (value instanceof Long) {
Long longValue = (Long) value;
return new BigIntegerRepresentation(longValue);
}

if (value instanceof String) {
String string = (String) value;
return new StringRepresentation(string);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cryptimeleon.math.serialization.annotations;


import org.cryptimeleon.math.random.RandomGenerator;
import org.cryptimeleon.math.serialization.Representation;
import org.cryptimeleon.math.serialization.StandaloneRepresentable;
import org.cryptimeleon.math.structures.rings.Ring;
Expand All @@ -21,8 +22,19 @@ public class ReprUtilTest {
Zp.ZpElement zpelem;
@Represented
Foo foo;
@Represented
byte[] smallNumbers;
@Represented
Integer boringOldInteger;
@Represented
Long longNUmber;
@Represented
BigInteger reeeaaallyLongNumber;
@Represented
UUID veryUniqueNumber;

public static class Foo implements StandaloneRepresentable {

public static class Foo implements StandaloneRepresentable { //for testing restoration nof zpelem with a complicated restorer string
@Represented
Zp zp;

Expand Down Expand Up @@ -65,27 +77,47 @@ public int hashCode() {
}
}

@Test
public void testNestedMap() {
private void populate() {
Ring ring = new Zn(BigInteger.TEN);

HashMap<Map<String, RingElement>, List<String>> nestedMapOriginal = new HashMap<>();
nestedMap = new HashMap<>();
Map<String, RingElement> inner = new HashMap<>();
inner.put("testInner", ring.getUniformlyRandomElement());
nestedMap = nestedMapOriginal;

nestedMapOriginal.put(inner, Arrays.asList("testOuter", "testOuter2"));
nestedMap.put(inner, Arrays.asList("testOuter", "testOuter2"));

foo = new Foo(new Zp(BigInteger.valueOf(3)));
zpelem = Zp.valueOf(2, 3);

smallNumbers = RandomGenerator.getRandomBytes(3);
boringOldInteger = 23;
longNUmber = Long.MAX_VALUE;
reeeaaallyLongNumber = BigInteger.TEN.pow(100);
veryUniqueNumber = UUID.randomUUID();
}

@Test
public void testRestoration() {
Ring ring = new Zn(BigInteger.TEN);

Representation repr = ReprUtil.serialize(this);
nestedMap = null;
zpelem = null;
foo = null;
new ReprUtil(this).register(ring, "R").deserialize(repr);
ReprUtilTest deserialized = new ReprUtilTest();
new ReprUtil(deserialized).register(ring, "R").deserialize(repr);

assertEquals(deserialized, this);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ReprUtilTest that = (ReprUtilTest) o;
return Objects.equals(nestedMap, that.nestedMap) && Objects.equals(zpelem, that.zpelem) && Objects.equals(foo, that.foo) && Arrays.equals(smallNumbers, that.smallNumbers) && Objects.equals(boringOldInteger, that.boringOldInteger) && Objects.equals(longNUmber, that.longNUmber) && Objects.equals(reeeaaallyLongNumber, that.reeeaaallyLongNumber) && Objects.equals(veryUniqueNumber, that.veryUniqueNumber);
}

assertEquals(nestedMapOriginal, nestedMap);
assertEquals(Zp.valueOf(2,3), zpelem);
@Override
public int hashCode() {
int result = Objects.hash(nestedMap, zpelem, foo, boringOldInteger, longNUmber, reeeaaallyLongNumber, veryUniqueNumber);
result = 31 * result + Arrays.hashCode(smallNumbers);
return result;
}
}

0 comments on commit 5355a17

Please sign in to comment.