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

Remove variable time computations #245

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 0 additions & 10 deletions crates/fhe-math/benches/ntt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,10 @@ pub fn ntt_benchmark(c: &mut Criterion) {
|b| b.iter(|| op.forward(&mut a)),
);

group.bench_function(
BenchmarkId::new("forward_vt", format!("{vector_size}/{p_nbits}")),
|b| b.iter(|| unsafe { op.forward_vt(a.as_mut_ptr()) }),
);

group.bench_function(
BenchmarkId::new("backward", format!("{vector_size}/{p_nbits}")),
|b| b.iter(|| op.backward(&mut a)),
);

group.bench_function(
BenchmarkId::new("backward_vt", format!("{vector_size}/{p_nbits}")),
|b| b.iter(|| unsafe { op.backward_vt(a.as_mut_ptr()) }),
);
}
}

Expand Down
103 changes: 15 additions & 88 deletions crates/fhe-math/benches/rq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,14 @@ fn create_group(c: &mut Criterion, name: String) -> BenchmarkGroup<WallTime> {
}

macro_rules! bench_op {
($c: expr, $name:expr, $op:expr, $vt:expr) => {{
let name = if $vt {
format!("{}_vt", $name)
} else {
$name.to_string()
};
let mut group = create_group($c, name);
($c: expr, $name:expr, $op:expr) => {{
let mut group = create_group($c, $name.to_string());
let mut rng = thread_rng();

for degree in DEGREE {
let ctx = Arc::new(Context::new(&MODULI[..1], *degree).unwrap());
let p = Poly::random(&ctx, Representation::Ntt, &mut rng);
let mut q = Poly::random(&ctx, Representation::Ntt, &mut rng);
if $vt {
unsafe { q.allow_variable_time_computations() }
}
let q = Poly::random(&ctx, Representation::Ntt, &mut rng);

group.bench_function(
BenchmarkId::from_parameter(format!("{}/{}", degree, ctx.modulus().bits())),
Expand All @@ -54,22 +46,13 @@ macro_rules! bench_op {
}

macro_rules! bench_op_unary {
($c: expr, $name:expr, $op:expr, $vt:expr) => {{
let name = if $vt {
format!("{}_vt", $name)
} else {
$name.to_string()
};
let mut group = create_group($c, name);
($c: expr, $name:expr, $op:expr) => {{
let mut group = create_group($c, $name.to_string());
let mut rng = thread_rng();

for degree in DEGREE {
let ctx = Arc::new(Context::new(&MODULI[..1], *degree).unwrap());
let p = Poly::random(&ctx, Representation::Ntt, &mut rng);
let mut q = Poly::random(&ctx, Representation::Ntt, &mut rng);
if $vt {
unsafe { q.allow_variable_time_computations() }
}

group.bench_function(
BenchmarkId::from_parameter(format!("{}/{}", degree, ctx.modulus().bits())),
Expand All @@ -82,22 +65,14 @@ macro_rules! bench_op_unary {
}

macro_rules! bench_op_assign {
($c: expr, $name:expr, $op:expr, $vt:expr) => {{
let name = if $vt {
format!("{}_vt", $name)
} else {
$name.to_string()
};
let mut group = create_group($c, name);
($c: expr, $name:expr, $op:expr) => {{
let mut group = create_group($c, $name.to_string());
let mut rng = thread_rng();

for degree in DEGREE {
let ctx = Arc::new(Context::new(&MODULI[..1], *degree).unwrap());
let mut p = Poly::random(&ctx, Representation::Ntt, &mut rng);
let mut q = Poly::random(&ctx, Representation::Ntt, &mut rng);
if $vt {
unsafe { q.allow_variable_time_computations() }
}
let q = Poly::random(&ctx, Representation::Ntt, &mut rng);

group.bench_function(
BenchmarkId::from_parameter(format!("{}/{}", degree, ctx.modulus().bits())),
Expand All @@ -110,15 +85,13 @@ macro_rules! bench_op_assign {
}

pub fn rq_op_benchmark(c: &mut Criterion) {
for vt in [false, true] {
bench_op!(c, "rq_add", <&Poly>::add, vt);
bench_op_assign!(c, "rq_add_assign", Poly::add_assign, vt);
bench_op!(c, "rq_sub", <&Poly>::sub, vt);
bench_op_assign!(c, "rq_sub_assign", Poly::sub_assign, vt);
bench_op!(c, "rq_mul", <&Poly>::mul, vt);
bench_op_assign!(c, "rq_mul_assign", Poly::mul_assign, vt);
bench_op_unary!(c, "rq_neg", <&Poly>::neg, vt);
}
bench_op!(c, "rq_add", <&Poly>::add);
bench_op_assign!(c, "rq_add_assign", Poly::add_assign);
bench_op!(c, "rq_sub", <&Poly>::sub);
bench_op_assign!(c, "rq_sub_assign", Poly::sub_assign);
bench_op!(c, "rq_mul", <&Poly>::mul);
bench_op_assign!(c, "rq_mul_assign", Poly::mul_assign);
bench_op_unary!(c, "rq_neg", <&Poly>::neg);
}

pub fn rq_dot_product(c: &mut Criterion) {
Expand Down Expand Up @@ -235,52 +208,6 @@ pub fn rq_benchmark(c: &mut Criterion) {
});
},
);

p.change_representation(Representation::Ntt);
q.change_representation(Representation::Ntt);

unsafe {
q.allow_variable_time_computations();
q.change_representation(Representation::NttShoup);

group.bench_function(
BenchmarkId::new(
"mul_shoup_vt",
format!("{}/{}", degree, ctx.modulus().bits()),
),
|b| {
b.iter(|| p *= &q);
},
);

p.allow_variable_time_computations();

group.bench_function(
BenchmarkId::new(
"change_representation/PowerBasis_to_Ntt_vt",
format!("{}/{}", degree, ctx.modulus().bits()),
),
|b| {
b.iter(|| {
p.override_representation(Representation::PowerBasis);
p.change_representation(Representation::Ntt)
});
},
);

group.bench_function(
BenchmarkId::new(
"change_representation/Ntt_to_PowerBasis_vt",
format!("{}/{}", degree, ctx.modulus().bits()),
),
|b| {
b.iter(|| {
p.override_representation(Representation::Ntt);
p.change_representation(Representation::PowerBasis)
});
},
);
}
}
}

Expand Down
8 changes: 0 additions & 8 deletions crates/fhe-math/benches/zq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ pub fn zq_benchmark(c: &mut Criterion) {
b.iter(|| q.add_vec(&mut a, &c));
});

group.bench_function(BenchmarkId::new("add_vec_vt", vector_size), |b| unsafe {
b.iter(|| q.add_vec_vt(&mut a, &c));
});

group.bench_function(BenchmarkId::new("sub_vec", vector_size), |b| {
b.iter(|| q.sub_vec(&mut a, &c));
});
Expand All @@ -36,10 +32,6 @@ pub fn zq_benchmark(c: &mut Criterion) {
b.iter(|| q.mul_vec(&mut a, &c));
});

group.bench_function(BenchmarkId::new("mul_vec_vt", vector_size), |b| unsafe {
b.iter(|| q.mul_vec_vt(&mut a, &c));
});

group.bench_function(BenchmarkId::new("mul_shoup_vec", vector_size), |b| {
b.iter(|| q.mul_shoup_vec(&mut a, &c, &c_shoup));
});
Expand Down
14 changes: 2 additions & 12 deletions crates/fhe-math/src/ntt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,12 @@ mod tests {
for _ in 0..ntests {
let mut a = q.random_vec(size, &mut rng);
let a_clone = a.clone();
let mut b = a.clone();

op.forward(&mut a);
assert_ne!(a, a_clone);

unsafe { op.forward_vt(b.as_mut_ptr()) }
assert_eq!(a, b);

op.backward(&mut a);
assert_eq!(a, a_clone);

unsafe { op.backward_vt(b.as_mut_ptr()) }
assert_eq!(a, b);
}
}
}
Expand All @@ -91,11 +84,8 @@ mod tests {
let mut a_lazy = a.clone();

op.forward(&mut a);

unsafe {
op.forward_vt_lazy(a_lazy.as_mut_ptr());
q.reduce_vec(&mut a_lazy);
}
op.forward_lazy(&mut a_lazy);
q.reduce_vec(&mut a_lazy);

assert_eq!(a, a_lazy);
}
Expand Down
Loading
Loading