-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1343 from ZenithalHourlyRate:bgv-worst-noise-anal…
…ysis PiperOrigin-RevId: 725783991
- Loading branch information
Showing
27 changed files
with
1,387 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package( | ||
default_applicable_licenses = ["@heir//:license"], | ||
default_visibility = ["//visibility:public"], | ||
) | ||
|
||
cc_library( | ||
name = "NoiseByBoundCoeffModel", | ||
srcs = [ | ||
"NoiseByBoundCoeffModel.cpp", | ||
"NoiseByBoundCoeffModelAnalysis.cpp", | ||
], | ||
hdrs = [ | ||
"NoiseByBoundCoeffModel.h", | ||
], | ||
deps = [ | ||
":Noise", | ||
"@heir//lib/Analysis:Utils", | ||
"@heir//lib/Analysis/DimensionAnalysis", | ||
"@heir//lib/Analysis/LevelAnalysis", | ||
"@heir//lib/Analysis/NoiseAnalysis", | ||
"@heir//lib/Dialect/Mgmt/IR:Dialect", | ||
"@heir//lib/Dialect/Secret/IR:Dialect", | ||
"@heir//lib/Dialect/TensorExt/IR:Dialect", | ||
"@heir//lib/Parameters/BGV:Params", | ||
"@llvm-project//llvm:Support", | ||
"@llvm-project//mlir:ArithDialect", | ||
"@llvm-project//mlir:CallOpInterfaces", | ||
"@llvm-project//mlir:IR", | ||
"@llvm-project//mlir:Support", | ||
"@llvm-project//mlir:TensorDialect", | ||
], | ||
) | ||
|
||
cc_library( | ||
name = "Noise", | ||
srcs = ["Noise.cpp"], | ||
hdrs = [ | ||
"Noise.h", | ||
], | ||
deps = [ | ||
"@llvm-project//llvm:Support", | ||
"@llvm-project//mlir:IR", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "lib/Analysis/NoiseAnalysis/BGV/Noise.h" | ||
|
||
#include <cmath> | ||
#include <string> | ||
|
||
#include "llvm/include/llvm/Support/raw_ostream.h" // from @llvm-project | ||
#include "mlir/include/mlir/IR/Diagnostics.h" // from @llvm-project | ||
|
||
namespace mlir { | ||
namespace heir { | ||
namespace bgv { | ||
|
||
std::string NoiseState::toString() const { | ||
switch (noiseType) { | ||
case (NoiseType::UNINITIALIZED): | ||
return "NoiseState(uninitialized)"; | ||
case (NoiseType::SET): | ||
return "NoiseState(" + std::to_string(log(getValue()) / log(2)) + ") "; | ||
} | ||
} | ||
|
||
llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const NoiseState &noise) { | ||
return os << noise.toString(); | ||
} | ||
|
||
Diagnostic &operator<<(Diagnostic &diagnostic, const NoiseState &noise) { | ||
return diagnostic << noise.toString(); | ||
} | ||
|
||
} // namespace bgv | ||
} // namespace heir | ||
} // namespace mlir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#ifndef INCLUDE_ANALYSIS_NOISEANALYSIS_BGV_NOISE_H_ | ||
#define INCLUDE_ANALYSIS_NOISEANALYSIS_BGV_NOISE_H_ | ||
|
||
#include <optional> | ||
|
||
#include "llvm/include/llvm/Support/raw_ostream.h" // from @llvm-project | ||
#include "mlir/include/mlir/IR/Diagnostics.h" // from @llvm-project | ||
|
||
namespace mlir { | ||
namespace heir { | ||
namespace bgv { | ||
|
||
// This class could be shared among all noise models that tracks the noise by a | ||
// single value. Noise model could have different interpretation of the value. | ||
// In BGV world, most noise model just use a single value, either as bound or | ||
// as variance. | ||
class NoiseState { | ||
public: | ||
enum NoiseType { | ||
// A min value for the lattice, discarable when joined with anything else. | ||
UNINITIALIZED, | ||
// A known value for the lattice, when noise can be inferred. | ||
SET, | ||
}; | ||
|
||
static NoiseState uninitialized() { | ||
return NoiseState(NoiseType::UNINITIALIZED, std::nullopt); | ||
} | ||
static NoiseState of(double value) { | ||
return NoiseState(NoiseType::SET, value); | ||
} | ||
|
||
/// Create an integer value range lattice value. | ||
/// The default constructor must be equivalent to the "entry state" of the | ||
/// lattice, i.e., an uninitialized noise. | ||
NoiseState(NoiseType noiseType = NoiseType::UNINITIALIZED, | ||
std::optional<double> value = std::nullopt) | ||
: noiseType(noiseType), value(value) {} | ||
|
||
bool isKnown() const { return noiseType == NoiseType::SET; } | ||
|
||
bool isInitialized() const { return noiseType != NoiseType::UNINITIALIZED; } | ||
|
||
const double &getValue() const { | ||
assert(isKnown()); | ||
return *value; | ||
} | ||
|
||
bool operator==(const NoiseState &rhs) const { | ||
return noiseType == rhs.noiseType && value == rhs.value; | ||
} | ||
|
||
static NoiseState join(const NoiseState &lhs, const NoiseState &rhs) { | ||
// Uninitialized noises correspond to values that are not secret, | ||
// which may be the inputs to an encryption operation. | ||
if (lhs.noiseType == NoiseType::UNINITIALIZED) { | ||
return rhs; | ||
} | ||
if (rhs.noiseType == NoiseType::UNINITIALIZED) { | ||
return lhs; | ||
} | ||
|
||
assert(lhs.noiseType == NoiseType::SET && rhs.noiseType == NoiseType::SET); | ||
return NoiseState::of(std::max(lhs.getValue(), rhs.getValue())); | ||
} | ||
|
||
void print(llvm::raw_ostream &os) const { os << value; } | ||
|
||
std::string toString() const; | ||
|
||
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &os, | ||
const NoiseState &noise); | ||
|
||
friend Diagnostic &operator<<(Diagnostic &diagnostic, | ||
const NoiseState &noise); | ||
|
||
private: | ||
NoiseType noiseType; | ||
// notice that when level becomes large (e.g. 17), the max Q could be like | ||
// 3523 bits and could not be represented in double. | ||
std::optional<double> value; | ||
}; | ||
|
||
} // namespace bgv | ||
} // namespace heir | ||
} // namespace mlir | ||
|
||
#endif // INCLUDE_ANALYSIS_NOISEANALYSIS_BGV_NOISE_H_ |
Oops, something went wrong.