Skip to content

Commit 7e5401e

Browse files
committed
feat: add support for zeroize in scalar type
1 parent e04cb90 commit 7e5401e

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ ff = "0.13"
2121
group = { version = "0.13", features = ["tests"] }
2222
pairing_lib = { version = "0.23", package = "pairing" }
2323
subtle = "2.2.1"
24+
zeroize = "1.8.2"
2425

2526
serde = { version = "1.0", features = ["derive"], optional = true }
2627
ec-gpu = { version = "0.2.0", optional = true }

src/scalar.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ use core::{
99
iter::{Product, Sum},
1010
ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
1111
};
12+
use std::{ptr, sync::atomic};
1213

1314
use blst::*;
1415
use byte_slice_cast::AsByteSlice;
1516
use ff::{Field, FieldBits, PrimeField, PrimeFieldBits};
1617
use rand_core::RngCore;
1718
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
19+
use zeroize::Zeroize;
1820

1921
/// Represents an element of the scalar field $\mathbb{F}_q$ of the BLS12-381 elliptic
2022
/// curve construction.
@@ -674,6 +676,20 @@ impl Scalar {
674676
}
675677
}
676678

679+
impl Zeroize for Scalar {
680+
/// Implementation based on the zeroize crate, which guarantees the value
681+
/// becomes 0 when the function is called by ensuring the compiler does not
682+
/// optimize the function away
683+
/// See <https://docs.rs/zeroize/latest/zeroize/#what-guarantees-does-this-crate-provide>
684+
/// for more details
685+
fn zeroize(&mut self) {
686+
unsafe {
687+
ptr::write_volatile(&mut self.0, blst_fr { l: [0u64; 4] });
688+
}
689+
atomic::compiler_fence(atomic::Ordering::SeqCst);
690+
}
691+
}
692+
677693
#[cfg(feature = "gpu")]
678694
impl ec_gpu::GpuName for Scalar {
679695
fn name() -> String {

0 commit comments

Comments
 (0)