Skip to content

Commit

Permalink
feat(compiler/clientlib): Allow decompression of seeded ciphertext be…
Browse files Browse the repository at this point in the history
…fore decryption
  • Loading branch information
BourgerieQuentin committed Aug 13, 2024
1 parent 89ef8d3 commit 53b5f01
Showing 1 changed file with 65 additions and 55 deletions.
120 changes: 65 additions & 55 deletions compilers/concrete-compiler/compiler/lib/Common/Transformers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,30 +195,45 @@ updateGateInfoAccordingValue(Message<concreteprotocol::GateInfo> &gate,
}
auto gateCiphertext = gateTypeInfo.getLweCiphertext();
auto gateCompression = gateCiphertext.getCompression();
if (gateCompression == concreteprotocol::Compression::SEED) {
auto valueReader = value.asReader();
auto valueTypeInfo = valueReader.getTypeInfo();
if (!valueTypeInfo.hasLweCiphertext()) {
return gate;
}
auto valueCiphertext = valueTypeInfo.getLweCiphertext();
auto valueCompression = valueCiphertext.getCompression();
if (valueCompression == concreteprotocol::Compression::NONE) {
// If the compression of transportValue is none and the gateInfo have
// compression we update the gateInfo to allow uncompressed transportValue
auto gateBuilder = gate.asBuilder();
gateBuilder.getTypeInfo().getLweCiphertext().setCompression(
concreteprotocol::Compression::NONE);
auto gateDimensions = gateBuilder.getRawInfo().getShape().getDimensions();
auto lweSize = gateCiphertext.getEncryption().getLweDimension() + 1;
gateDimensions.set(gateDimensions.size() - 1, lweSize);
auto concreteShapeDimensions = gateBuilder.getTypeInfo()
.getLweCiphertext()
.getConcreteShape()
.getDimensions();
concreteShapeDimensions.set(concreteShapeDimensions.size() - 1, lweSize);
return gateBuilder.asReader();
}
auto valueReader = value.asReader();
auto valueTypeInfo = valueReader.getTypeInfo();
if (!valueTypeInfo.hasLweCiphertext()) {
return gate;
}
auto valueCiphertext = valueTypeInfo.getLweCiphertext();
auto valueCompression = valueCiphertext.getCompression();
if (gateCompression == concreteprotocol::Compression::SEED &&
valueCompression == concreteprotocol::Compression::NONE) {
// If the compression of transportValue is none and the gateInfo have
// compression we update the gateInfo to allow uncompressed transportValue
auto gateBuilder = gate.asBuilder();
gateBuilder.getTypeInfo().getLweCiphertext().setCompression(
concreteprotocol::Compression::NONE);
auto gateDimensions = gateBuilder.getRawInfo().getShape().getDimensions();
auto lweSize = gateCiphertext.getEncryption().getLweDimension() + 1;
gateDimensions.set(gateDimensions.size() - 1, lweSize);
auto concreteShapeDimensions = gateBuilder.getTypeInfo()
.getLweCiphertext()
.getConcreteShape()
.getDimensions();
concreteShapeDimensions.set(concreteShapeDimensions.size() - 1, lweSize);
return gateBuilder.asReader();
}
if (gateCompression == concreteprotocol::Compression::NONE &&
valueCompression == concreteprotocol::Compression::SEED) {
// If the compression of transportValue is none and the gateInfo have
// compression we update the gateInfo to allow uncompressed transportValue
auto gateBuilder = gate.asBuilder();
gateBuilder.getTypeInfo().getLweCiphertext().setCompression(
concreteprotocol::Compression::SEED);
auto gateDimensions = gateBuilder.getRawInfo().getShape().getDimensions();
gateDimensions.set(gateDimensions.size() - 1, 3);
auto concreteShapeDimensions = gateBuilder.getTypeInfo()
.getLweCiphertext()
.getConcreteShape()
.getDimensions();
concreteShapeDimensions.set(concreteShapeDimensions.size() - 1, 3);
return gateBuilder.asReader();
}
return gate;
}
Expand Down Expand Up @@ -902,6 +917,23 @@ Result<Transformer> getSeededLweCiphertextDecompressionTransformer(
};
}

Result<ArgTransformer> getDecompressionTransformer(
const Message<concreteprotocol::LweCiphertextEncryptionInfo> &info) {

return [=](TransportValue transportVal) -> Result<Value> {
auto value = Value::fromRawTransportValue(transportVal);
auto compression = transportVal.asReader()
.getTypeInfo()
.getLweCiphertext()
.getCompression();
if (compression == concreteprotocol::Compression::SEED) {
OUTCOME_TRY(auto d, getSeededLweCiphertextDecompressionTransformer(info));
return d(value);
}
return value;
};
}

Result<ArgTransformer> TransformerFactory::getLweCiphertextArgTransformer(
Message<concreteprotocol::GateInfo> gateInfo, bool useSimulation) {
if (!gateInfo.asReader().getTypeInfo().hasLweCiphertext()) {
Expand All @@ -910,20 +942,9 @@ Result<ArgTransformer> TransformerFactory::getLweCiphertextArgTransformer(
}

/// Generating the decompression transformer.
Transformer decompressionTransformer;
auto lweCiphertextInfo = gateInfo.asReader().getTypeInfo().getLweCiphertext();
auto compression = lweCiphertextInfo.getCompression();
if (compression == concreteprotocol::Compression::NONE || useSimulation) {
OUTCOME_TRY(decompressionTransformer, getNoneDecompressionTransformer());
} else if (compression == concreteprotocol::Compression::SEED) {
OUTCOME_TRY(decompressionTransformer,
getSeededLweCiphertextDecompressionTransformer(
lweCiphertextInfo.getEncryption()));
} else {
return StringError(
"Only none compression is currently supported for lwe ciphertext "
"currently.");
}
OUTCOME_TRY(auto decompressionTransformer,
getDecompressionTransformer(lweCiphertextInfo.getEncryption()));

// Generating the verifier.
TransportValueVerifier verify;
Expand All @@ -935,13 +956,7 @@ Result<ArgTransformer> TransformerFactory::getLweCiphertextArgTransformer(

return [=](TransportValue transportVal) -> Result<Value> {
OUTCOME_TRYV(verify(transportVal));
auto value = Value::fromRawTransportValue(transportVal);
if (transportVal.asReader()
.getTypeInfo()
.getLweCiphertext()
.getCompression() == concreteprotocol::Compression::NONE)
return value;
return decompressionTransformer(value);
return decompressionTransformer(transportVal);
};
}

Expand Down Expand Up @@ -1001,15 +1016,10 @@ Result<OutputTransformer> TransformerFactory::getLweCiphertextOutputTransformer(
}

/// Generating the decompression transformer.
Transformer decompressionTransformer;
if (gateInfo.asReader().getTypeInfo().getLweCiphertext().getCompression() ==
concreteprotocol::Compression::NONE) {
OUTCOME_TRY(decompressionTransformer, getNoneDecompressionTransformer());
} else {
return StringError(
"Only none compression is currently supported for lwe ciphertext "
"currently.");
}
auto encryptionInfo =
gateInfo.asReader().getTypeInfo().getLweCiphertext().getEncryption();
OUTCOME_TRY(auto decompressionTransformer,
getDecompressionTransformer(encryptionInfo));

/// Generating the decryption transformer.
Transformer decryptionTransformer;
Expand Down Expand Up @@ -1056,8 +1066,8 @@ Result<OutputTransformer> TransformerFactory::getLweCiphertextOutputTransformer(

return [=](TransportValue transportVal) -> Result<Value> {
OUTCOME_TRYV(verify(transportVal));
return decodingTransformer(decryptionTransformer(
decompressionTransformer(Value::fromRawTransportValue(transportVal))));
OUTCOME_TRY(auto value, decompressionTransformer(transportVal));
return decodingTransformer(decryptionTransformer(value));
};
}

Expand Down

0 comments on commit 53b5f01

Please sign in to comment.