Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed compareLT and LessThanZero to take bitlength #428

Merged
merged 1 commit into from
Jun 21, 2024
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 @@ -72,20 +72,29 @@ default DRes<SInt> equals(DRes<SInt> x, DRes<SInt> y, int bitlength) {
/**
* Computes if x &lt; y.
*
* @param x the first input
* @param y the second input
* @param x the first input. Must be less than 2^{@code bitlength}
* @param y the second input. Must be less than 2^{@code bitlength}
* @param bitlength the amount of bits to do the comparison on. Must be less than or equal to
* the max bitlength allowed
* @param algorithm the algorithm to use
* @return A deferred result computing x &lt; y. Result will be either [1] (true) or [0] (false).
* @return A deferred result computing x' &lt; y'. Where x' and y' represent the {@code bitlength}
* least significant bits of x, respectively y. Result will be either [1] (true) or [0] (false).
*/
DRes<SInt> compareLT(DRes<SInt> x, DRes<SInt> y, Algorithm algorithm);
DRes<SInt> compareLT(DRes<SInt> x, DRes<SInt> y, int bitlength, Algorithm algorithm);

/**
* Call to {@link #compareLT(DRes, DRes, Algorithm)} with default comparison algorithm.
* Call to {@link #compareLT(DRes, DRes, int, Algorithm)} with default comparison algorithm.
*/
default DRes<SInt> compareLT(DRes<SInt> x, DRes<SInt> y) {
return compareLT(x, y, Algorithm.LOG_ROUNDS);
default DRes<SInt> compareLT(DRes<SInt> x, DRes<SInt> y, int bitlength) {
return compareLT(x, y, bitlength, Algorithm.LOG_ROUNDS);
}

/**
* Call to {@link #compareLT(DRes, DRes, int, Algorithm)} with default comparison algorithm,
* comparing all bits.
*/
DRes<SInt> compareLT(DRes<SInt> x, DRes<SInt> y);

/**
* Computes if the bit decomposition of an open value is less than the bit decomposition of a
* secret value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,29 @@ public DRes<SInt> compareLEQ(DRes<SInt> x, DRes<SInt> y) {
}

@Override
public DRes<SInt> compareLT(DRes<SInt> x, DRes<SInt> y, Algorithm algorithm) {
public DRes<SInt> compareLT(DRes<SInt> x, DRes<SInt> y, int bitlength, Algorithm algorithm) {
if (bitlength > builder.getBasicNumericContext().getMaxBitLength()) {
throw new IllegalArgumentException("The bitlength is more than allowed for elements.");
}
if (algorithm == Algorithm.LOG_ROUNDS) {
if (builder.getBasicNumericContext().getStatisticalSecurityParam() + builder
.getBasicNumericContext().getMaxBitLength() > builder.getBasicNumericContext()
.getModulus().bitLength()) {
if (builder.getBasicNumericContext().getStatisticalSecurityParam() + bitlength
> builder.getBasicNumericContext().getModulus().bitLength()) {
throw new IllegalArgumentException(
"The max bitlength plus the statistical security parameter overflows the size of the modulus.");
}
DRes<SInt> difference = builder.numeric().sub(x, y);
return builder.seq(new LessThanZero(difference));
return builder.seq(new LessThanZero(difference, bitlength));
} else {
throw new UnsupportedOperationException("Not implemented yet");
}
}

@Override
public DRes<SInt> compareLT(DRes<SInt> x, DRes<SInt> y) {
int maxBitLength = builder.getBasicNumericContext().getMaxBitLength();
return compareLT(x, y, maxBitLength);
}

@Override
public DRes<SInt> compareLTBits(BigInteger openValue, DRes<List<DRes<SInt>>> secretBits) {
return builder.seq(new BitLessThanOpen(openValue, secretBits));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,26 @@

/**
* Given secret value a, computes a &lt;? 0.
* https://www.researchgate.net/publication/225092133_Improved_Primitives_for_Secure_Multiparty_Integer_Computation
*/
public class LessThanZero implements Computation<SInt, ProtocolBuilderNumeric> {
// TODO add reference to protocol description

private final DRes<SInt> input;
private final int maxBitlength;

/**
* Constructs new {@link LessThanZero}.
*
* @param input input to compare to 0
* @param maxBitlength number of bits to compare
*/
public LessThanZero(DRes<SInt> input) {
public LessThanZero(DRes<SInt> input, int maxBitlength) {
this.input = input;
this.maxBitlength = maxBitlength;
}

@Override
public DRes<SInt> buildComputation(ProtocolBuilderNumeric builder) {
final int maxBitlength = builder.getBasicNumericContext().getMaxBitLength();
final int statisticalSecurity = builder.getBasicNumericContext()
.getStatisticalSecurityParam();
return builder.seq(seq -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public DRes<Pair<DRes<SInt>, DRes<SInt>>> buildComputation(ProtocolBuilderNumeri
DRes<List<DRes<SInt>>> bits = advancedNumeric.toBits(input, l);

// Sign bit (0 or 1)
DRes<SInt> signBit = new LessThanZero(input).buildComputation(par);
DRes<SInt> signBit =
new LessThanZero(input, par.getBasicNumericContext().getMaxBitLength())
.buildComputation(par);

return Pair.lazy(bits, signBit);
}).seq((seq, params) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public void test() {
List<DRes<SInt>> actualInner = new ArrayList<>(left.size());
for (int i = 0; i < left.size(); i++) {
actualInner.add(Comparison.using(builder).compareLT(left.get(i), right.get(i),
Comparison.Algorithm.LOG_ROUNDS));
builder.getBasicNumericContext().getMaxBitLength(), Comparison.Algorithm.LOG_ROUNDS));
}
DRes<List<DRes<BigInteger>>> opened = Collections.using(builder).openList(() -> actualInner);
return () -> opened.out().stream().map(DRes::out).collect(Collectors.toList());
Expand Down Expand Up @@ -437,7 +437,7 @@ public void test() throws Exception {
DRes<SInt> x = input.known(BigInteger.valueOf(3));
DRes<SInt> y = input.known(BigInteger.valueOf(5));
Comparison comparison = Comparison.using(builder);
DRes<SInt> result = comparison.compareLT(x, y, Algorithm.CONST_ROUNDS);
DRes<SInt> result = comparison.compareLT(x, y, 3, Algorithm.CONST_ROUNDS);
return input.open(result);
};
runApplication(app);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ public TestThread<ResourcePoolT, ProtocolBuilderNumeric> next() {
public void test() {
Application<List<BigInteger>, ProtocolBuilderNumeric> app = builder -> {
Numeric numeric = builder.numeric();
List<DRes<SInt>> inputs = openInputs.stream().map(numeric::known).collect(Collectors.toList());;
List<DRes<SInt>> inputs = openInputs.stream().map(numeric::known).collect(Collectors.toList());
List<DRes<SInt>> actualInner = new ArrayList<>(inputs.size());
for (DRes<SInt> input : inputs) {
actualInner.add(builder.seq(new LessThanZero(input)));
actualInner.add(builder.seq(
new LessThanZero(input, builder.getBasicNumericContext().getMaxBitLength())));
}
DRes<List<DRes<BigInteger>>> opened = Collections.using(builder).openList(DRes.of(actualInner));
return () -> opened.out().stream().map(DRes::out).collect(Collectors.toList());
Expand Down
Loading