Skip to content

Commit

Permalink
secret-sharing/src/poly: Restrict add/sub/mul assign std ops
Browse files Browse the repository at this point in the history
The AddAssign, SubAssign, and MulAssign functions can now be used
only when the prime field supports zeroization. This ensures that
any leftover data from heap reallocation, when the right-hand-side
polynomial has more coefficients than the left-hand-side, is zeroized.

An alternative solution is to remove these functions, but this
could lead to performance drawbacks.
  • Loading branch information
peternose committed Nov 6, 2024
1 parent d98b318 commit 0d475b1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Empty file added .changelog/5928.trivial.md
Empty file.
26 changes: 20 additions & 6 deletions secret-sharing/src/poly/univariate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ where

impl<F> AddAssign for Polynomial<F>
where
F: PrimeField,
F: PrimeField + Zeroize,
{
#[inline]
fn add_assign(&mut self, rhs: Polynomial<F>) {
Expand All @@ -248,9 +248,16 @@ where

impl<F> AddAssign<&Polynomial<F>> for Polynomial<F>
where
F: PrimeField,
F: PrimeField + Zeroize,
{
fn add_assign(&mut self, rhs: &Polynomial<F>) {
if self.a.capacity() < rhs.a.len() {
let mut a = Vec::with_capacity(rhs.a.len());
a.extend_from_slice(&self.a);
self.a.zeroize();
self.a = a;
}

let min_len = min(self.a.len(), rhs.a.len());

for i in 0..min_len {
Expand Down Expand Up @@ -321,7 +328,7 @@ where

impl<F> SubAssign for Polynomial<F>
where
F: PrimeField,
F: PrimeField + Zeroize,
{
#[inline]
fn sub_assign(&mut self, rhs: Polynomial<F>) {
Expand All @@ -331,9 +338,16 @@ where

impl<F> SubAssign<&Polynomial<F>> for Polynomial<F>
where
F: PrimeField,
F: PrimeField + Zeroize,
{
fn sub_assign(&mut self, rhs: &Polynomial<F>) {
if self.a.capacity() < rhs.a.len() {
let mut a = Vec::with_capacity(rhs.a.len());
a.extend_from_slice(&self.a);
self.a.zeroize();
self.a = a;
}

let min_len = min(self.a.len(), rhs.a.len());

for i in 0..min_len {
Expand Down Expand Up @@ -510,7 +524,7 @@ where

impl<F> Sum for Polynomial<F>
where
F: PrimeField,
F: PrimeField + Zeroize,
{
fn sum<I: Iterator<Item = Polynomial<F>>>(iter: I) -> Polynomial<F> {
let mut sum = Polynomial::zero(0);
Expand All @@ -521,7 +535,7 @@ where

impl<'a, F> Sum<&'a Polynomial<F>> for Polynomial<F>
where
F: PrimeField,
F: PrimeField + Zeroize,
{
fn sum<I: Iterator<Item = &'a Polynomial<F>>>(iter: I) -> Polynomial<F> {
let mut sum = Polynomial::zero(0);
Expand Down

0 comments on commit 0d475b1

Please sign in to comment.