Explorar o código

feat: add `GaussSamp`

Enrico Bottazzi hai 9 meses
pai
achega
a60b38a101
Modificáronse 5 ficheiros con 19 adicións e 17 borrados
  1. 1 1
      examples/trapdoor.rs
  2. 1 1
      src/DCRTPoly.cc
  3. 13 11
      src/Trapdoor.cc
  4. 2 3
      src/Trapdoor.h
  5. 2 1
      src/lib.rs

+ 1 - 1
examples/trapdoor.rs

@@ -15,5 +15,5 @@ fn main() {
 
     let k = 68; // to calculate
 
-    ffi::DCRTPolyGaussSamp(n.try_into().unwrap(), k, base);
+    let _res = ffi::DCRTPolyGaussSamp(n.try_into().unwrap(), k, &trapdoor, &u, base);
 }

+ 1 - 1
src/DCRTPoly.cc

@@ -26,7 +26,7 @@ std::unique_ptr<DCRTPolyImpl> DCRTPolyGenFromDug(const ILDCRTParams& params)
 {   
     std::shared_ptr<ILDCRTParams> params_ptr = std::make_shared<ILDCRTParams>(params);
     typename DCRTPolyImpl::DugType dug;
-    return std::make_unique<DCRTPolyImpl>(dug, params_ptr, Format::COEFFICIENT);
+    return std::make_unique<DCRTPolyImpl>(dug, params_ptr, Format::EVALUATION);
 }
 
 } // openfhe

+ 13 - 11
src/Trapdoor.cc

@@ -1,5 +1,4 @@
 #include "Trapdoor.h"
-#include "DCRTPoly.h"
 #include "openfhe/core/lattice/trapdoor.h"
 #include "openfhe/core/lattice/dgsampling.h"
 
@@ -27,22 +26,25 @@ std::unique_ptr<TrapdoorOutput> DCRTPolyTrapdoorGen(
     });
 }
 
-void DCRTPolyGaussSamp(size_t n, size_t k, base)
+std::unique_ptr<Matrix> DCRTPolyGaussSamp(size_t n, size_t k, const TrapdoorOutput& trapdoor, const DCRTPolyImpl& u, int64_t base)
 {
     DCRTPolyImpl::DggType dgg(lbcrypto::SIGMA);
 
     double c = (base + 1) * lbcrypto::SIGMA;
     double s = lbcrypto::SPECTRAL_BOUND(n, k, base);
     DCRTPolyImpl::DggType dggLargeSigma(sqrt(s * s - c * c));
-}
 
-// [x] n
-// [x] k
-// [ ] trapPair.first
-// [ ] trapPair.second
-// [ ] u
-// [x] dgg 
-// [x] dggLargeSigma
-// [x] base
+    auto result = lbcrypto::RLWETrapdoorUtility<lbcrypto::DCRTPoly>::GaussSamp(
+        n,
+        k,
+        trapdoor.m,
+        trapdoor.tp,
+        u,
+        dgg,
+        dggLargeSigma,
+        base
+    );
 
+    return std::make_unique<Matrix>(std::move(result));
+}
 } // openfhe 

+ 2 - 3
src/Trapdoor.h

@@ -3,6 +3,7 @@
 #include "openfhe/core/lattice/hal/lat-backend.h"
 #include "openfhe/core/math/matrix.h"
 #include "openfhe/core/lattice/trapdoor.h"
+#include "DCRTPoly.h"
 
 namespace openfhe
 {
@@ -23,8 +24,6 @@ struct TrapdoorOutput
     int64_t base,
     bool balanced);
 
-// [[nodiscard]]
-void DCRTPolyGaussSamp(size_t n, size_t k, int64_t base);
-    
+[[nodiscard]] std::unique_ptr<Matrix> DCRTPolyGaussSamp(size_t n, size_t k, const TrapdoorOutput& trapdoor, const DCRTPolyImpl& u, int64_t base);
     
 } // openfhe

+ 2 - 1
src/lib.rs

@@ -209,6 +209,7 @@ pub mod ffi
         type MapFromIndexToEvalKey;
         type MapFromStringToMapFromIndexToEvalKey;
         type MapFromStringToVectorOfEvalKeys;
+        type Matrix;
         type Params;
         type ParamsBFVRNS;
         type ParamsBGVRNS;
@@ -1148,7 +1149,7 @@ pub mod ffi
     {   
         // Generator functions
         fn DCRTPolyTrapdoorGen(params: &ILDCRTParams, base: i64, balanced: bool) -> UniquePtr<TrapdoorOutput>;
-        fn DCRTPolyGaussSamp(n: usize, k: usize, base: i64);
+        fn DCRTPolyGaussSamp(n: usize, k: usize, trapdoor: &TrapdoorOutput, u: &DCRTPolyImpl, base: i64) -> UniquePtr<Matrix>;
     }
 }