mirror of
https://github.com/arnaucube/fhe-study.git
synced 2026-01-24 04:33:52 +01:00
add methods for encoding constants for ct-pt-multiplication
This commit is contained in:
@@ -61,6 +61,16 @@ impl TGLWE {
|
||||
let pt = pt.mul_div_round(p, u64::MAX);
|
||||
Rq::from_vec_u64(¶m.pt(), pt.coeffs().iter().map(|c| c.0).collect())
|
||||
}
|
||||
/// encodes the given message as a TGLWE constant/public value, for using it
|
||||
/// in ct-pt-multiplication.
|
||||
pub fn new_const(param: &Param, m: &Rq) -> Tn {
|
||||
debug_assert_eq!(param.t, m.param.q);
|
||||
// don't scale up m, set the Tn element directly from m's coefficients
|
||||
Tn {
|
||||
param: param.ring,
|
||||
coeffs: m.coeffs().iter().map(|c_i| T64(c_i.v)).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
/// encrypts with the given SecretKey (instead of PublicKey)
|
||||
pub fn encrypt_s(rng: impl Rng, param: &Param, sk: &SecretKey, p: &Tn) -> Result<Self> {
|
||||
@@ -310,11 +320,7 @@ mod tests {
|
||||
let m1 = Rq::rand_u64(&mut rng, msg_dist, ¶m.pt())?;
|
||||
let m2 = Rq::rand_u64(&mut rng, msg_dist, ¶m.pt())?;
|
||||
let p1: Tn = TGLWE::encode(¶m, &m1);
|
||||
// don't scale up p2, set it directly from m2
|
||||
let p2: Tn = Tn {
|
||||
param: param.ring,
|
||||
coeffs: m2.coeffs().iter().map(|c_i| T64(c_i.v)).collect(),
|
||||
};
|
||||
let p2: Tn = TGLWE::new_const(¶m, &m2); // as constant/public value
|
||||
|
||||
let c1 = TGLWE::encrypt(&mut rng, ¶m, &pk, &p1)?;
|
||||
|
||||
|
||||
@@ -61,6 +61,12 @@ impl TLWE {
|
||||
let p = p.mul_div_round(param.t, u64::MAX);
|
||||
Rq::from_vec_u64(¶m.pt(), p.coeffs().iter().map(|c| c.0).collect())
|
||||
}
|
||||
/// encodes the given message as a TLWE constant/public value, for using it
|
||||
/// in ct-pt-multiplication.
|
||||
pub fn new_const(param: &Param, m: &Rq) -> T64 {
|
||||
debug_assert_eq!(param.t, m.param.q);
|
||||
T64(m.coeffs()[0].v)
|
||||
}
|
||||
|
||||
// encrypts with the given SecretKey (instead of PublicKey)
|
||||
pub fn encrypt_s(rng: impl Rng, param: &Param, sk: &SecretKey, p: &T64) -> Result<Self> {
|
||||
@@ -400,8 +406,7 @@ mod tests {
|
||||
let m1 = Rq::rand_u64(&mut rng, msg_dist, ¶m.pt())?;
|
||||
let m2 = Rq::rand_u64(&mut rng, msg_dist, ¶m.pt())?;
|
||||
let p1: T64 = TLWE::encode(¶m, &m1);
|
||||
// don't scale up p2, set it directly from m2
|
||||
let p2: T64 = T64(m2.coeffs()[0].v);
|
||||
let p2: T64 = TLWE::new_const(¶m, &m2); // as constant/public value
|
||||
|
||||
let c1 = TLWE::encrypt(&mut rng, ¶m, &pk, &p1)?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user