Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,18 @@ public ExponentExpressionVector truncate(int newLength) {
return new ExponentExpressionVector(super.truncate(newLength));
}

@Override
public ExponentExpressionVector concatenate(Vector<? extends ExponentExpr> secondPart) {
return new ExponentExpressionVector(super.concatenate(secondPart));
}

@Override
public ExponentExpressionVector append(ExponentExpr valueToAppend) {
return new ExponentExpressionVector(super.append(valueToAppend));
}

@Override
public ExponentExpressionVector prepend(ExponentExpr valueToPrepend) {
return new ExponentExpressionVector(super.prepend(valueToPrepend));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ public GroupElementExpressionVector pad(GroupElementExpression valueToPadWith, i
return new GroupElementExpressionVector(super.pad(valueToPadWith, desiredLength));
}

@Override
public GroupElementExpressionVector append(GroupElementExpression valueToAppend) {
return new GroupElementExpressionVector(super.append(valueToAppend));
}

@Override
public GroupElementExpressionVector prepend(GroupElementExpression valueToPrepend) {
return new GroupElementExpressionVector(super.prepend(valueToPrepend));
}

@Override
public GroupElementExpressionVector replace(int index, GroupElementExpression substitute) {
return new GroupElementExpressionVector(super.replace(index, substitute));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,19 @@ public Vector<X> pad(X valueToPadWith, int desiredLength) {
return instantiateWithSafeArray(result);
}

public Vector<X> append(X valueToAppend) {
ArrayList<X> result = new ArrayList<>(values);
result.add(valueToAppend);
return instantiateWithSafeArray(result);
}

public Vector<X> prepend(X valueToPrepend) {
ArrayList<X> result = new ArrayList<>();
result.add(valueToPrepend);
result.addAll(values);
return instantiateWithSafeArray(result);
}

public Vector<X> replace(int index, X substitute) {
ArrayList<X> result = new ArrayList<>(values);
result.set(index, substitute);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.cryptimeleon.math.structures.groups;

import org.cryptimeleon.math.expressions.bool.BooleanExpression;
import org.cryptimeleon.math.expressions.exponent.ExponentExpr;
import org.cryptimeleon.math.expressions.group.GroupElementConstantExpr;
import org.cryptimeleon.math.expressions.group.GroupElementExpression;
Expand Down Expand Up @@ -151,6 +152,42 @@ default GroupElementConstantExpr expr() {
return new GroupElementConstantExpr(this);
}

/**
* Returns an expression of the form "this == expr".
* This is meant to write down an expression, usually only useful if you want to express something that depends on variables that become known later.
* If you just want to compare two {@link GroupElement}s, just use {@link #equals(Object)}.
*
* @param expr an expression to compare this group element to
* @return an expression the evaluates to true (for some variable instantiation) if this is equal to expr.
*/
default BooleanExpression isEqualTo(GroupElementExpression expr) {
return expr().isEqualTo(expr);
}

/**
* Returns an expression of the form "this == expr".
* This is meant to write down an expression, usually only useful if you want to express something that depends on variables that become known later.
* If you just want to compare two {@link GroupElement}s, just use {@link #equals(Object)}.
*
* @param expr an expression to compare this group element to
* @return an expression the evaluates to true (for some variable instantiation) if this is equal to expr.
*/
default BooleanExpression isEqualTo(String expr) {
return expr().isEqualTo(expr);
}

/**
* Returns an expression of the form "this == other".
* This is meant to write down an expression, usually only useful if you want to express something that depends on variables that become known later.
* If you just want to compare two {@link GroupElement}s, just use {@link #equals(Object)}.
*
* @param other another group element to compare this group element to
* @return an expression the evaluates to true iff this.equals(other).
*/
default BooleanExpression isEqualTo(GroupElement other) {
return expr().isEqualTo(other);
}

/**
* Advises the {@code GroupElement} to prepare it for later {@code pow()} calls.
* This will take some time and should only be done ahead of time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,21 @@ public GroupElementVector truncate(int newLength) {
return new GroupElementVector(super.truncate(newLength));
}

@Override
public GroupElementVector concatenate(Vector<? extends GroupElement> secondPart) {
return new GroupElementVector(super.concatenate(secondPart));
}

@Override
public GroupElementVector append(GroupElement valueToAppend) {
return new GroupElementVector(super.append(valueToAppend));
}

@Override
public GroupElementVector prepend(GroupElement valueToPrepend) {
return new GroupElementVector(super.prepend(valueToPrepend));
}

public static GroupElementVector fromStream(Stream<? extends GroupElement> stream) {
return fromStreamPlain(stream, GroupElementVector::instantiateWithSafeArray);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,21 @@ public RingElementVector truncate(int newLength) {
return new RingElementVector(super.truncate(newLength));
}

@Override
public RingElementVector concatenate(Vector<? extends RingElement> secondPart) {
return new RingElementVector(super.concatenate(secondPart));
}

@Override
public RingElementVector append(RingElement valueToAppend) {
return new RingElementVector(super.append(valueToAppend));
}

@Override
public RingElementVector prepend(RingElement valueToPrepend) {
return new RingElementVector(super.prepend(valueToPrepend));
}

public ProductRingElement asElementInProductRing() {
return new ProductRingElement(values);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,29 @@ public static BigInteger[] decomposeIntoDigits(BigInteger number, BigInteger bas

return result;
}

/**
* Decomposes a given number into digits with the given base.
* <p>
* For example, for base = 2, this does bit decomposition.
*
* @return an array {@code A} containing values {@code A[i] < base} such that
* \(\sum_i{ \text{A}[i] \cdot \text{base}^i} = \text{number}\)
*/
public static BigInteger[] decomposeIntoDigits(BigInteger number, long base) {
return decomposeIntoDigits(number, BigInteger.valueOf(base));
}

/**
* Decomposes a given number into the given number of digits with the given base.
* <p>
* For example, for base = 2, this does bit decomposition.
*
* @return an array {@code A} containing values {@code A[i] < base} such that
* \(\sum_i{ \text{A}[i] \cdot \text{base}^i} = \text{number}\)
* @throws IllegalArgumentException if {@code numDigits} is not enough to represent the given number
*/
public static BigInteger[] decomposeIntoDigits(BigInteger number, long base, int numDigits) {
return decomposeIntoDigits(number, BigInteger.valueOf(base), numDigits);
}
}
55 changes: 55 additions & 0 deletions src/main/java/org/cryptimeleon/math/structures/rings/zn/Zn.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.cryptimeleon.math.structures.rings.zn;

import org.cryptimeleon.math.expressions.bool.BooleanExpression;
import org.cryptimeleon.math.expressions.bool.ExponentEqualityExpr;
import org.cryptimeleon.math.expressions.exponent.ExponentConstantExpr;
import org.cryptimeleon.math.expressions.exponent.ExponentExpr;
import org.cryptimeleon.math.hash.ByteAccumulator;
import org.cryptimeleon.math.hash.UniqueByteRepresentable;
import org.cryptimeleon.math.random.RandomGenerator;
Expand Down Expand Up @@ -169,6 +172,10 @@ public ZnElement add(Element e) {
return createZnElementUnsafe(result);
}

public ExponentExpr add(ExponentExpr e) {
return asExponentExpression().add(e);
}

@Override
public ZnElement neg() {
return v.equals(BigInteger.ZERO) ? this : createZnElementUnsafe(n.subtract(v));
Expand All @@ -183,6 +190,10 @@ public ZnElement sub(Element e) {
return createZnElementUnsafe(result);
}

public ExponentExpr sub(ExponentExpr e) {
return asExponentExpression().sub(e);
}

@Override
public ZnElement mul(Element e) {
checkSameModulus(e);
Expand All @@ -199,6 +210,10 @@ public ZnElement mul(long k) {
return mul(BigInteger.valueOf(k));
}

public ExponentExpr mul(ExponentExpr e) {
return asExponentExpression().mul(e);
}

@Override
public ZnElement pow(BigInteger k) {
return createZnElementUnsafe(v.modPow(k, n));
Expand All @@ -209,6 +224,10 @@ public ZnElement pow(long k) {
return pow(BigInteger.valueOf(k));
}

public ExponentExpr pow(ExponentExpr e) {
return asExponentExpression().pow(e);
}

@Override
public ZnElement inv() throws UnsupportedOperationException {
try {
Expand Down Expand Up @@ -295,6 +314,42 @@ public ExponentConstantExpr asExponentExpression() {
return new ExponentConstantExpr(this);
}

/**
* Returns an expression "this = other".
* This is for building expressions, i.e. only useful if unknown variables are involved.
* To compare two {@linkplain ZnElement}s normally, just use {@link #equals(Object)}.
*/
public ExponentEqualityExpr isEqualTo(ExponentExpr other) {
return asExponentExpression().isEqualTo(other);
}

/**
* Returns an expression "this = other".
* This is for building expressions, i.e. only useful if unknown variables are involved.
* To compare two {@linkplain ZnElement}s normally, just use {@link #equals(Object)}.
*/
public ExponentEqualityExpr isEqualTo(ZnElement other) {
return isEqualTo(other.asExponentExpression());
}

/**
* Returns an expression "this = other mod n".
* This is for building expressions, i.e. only useful if unknown variables are involved.
* To compare two {@linkplain ZnElement}s normally, just use {@link #equals(Object)}.
*/
public ExponentEqualityExpr isEqualTo(BigInteger other) {
return isEqualTo(valueOf(other));
}

/**
* Returns an expression "this = other mod n".
* This is for building expressions, i.e. only useful if unknown variables are involved.
* To compare two {@linkplain ZnElement}s normally, just use {@link #equals(Object)}.
*/
public ExponentEqualityExpr isEqualTo(long other) {
return isEqualTo(valueOf(other));
}

@Override
public BigInteger asInteger() throws UnsupportedOperationException {
return v;
Expand Down