1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use super::Scalar;
use core::borrow::Borrow;
use elliptic_curve::{
ff::Field,
ops::Invert,
rand_core::{CryptoRng, RngCore},
subtle::CtOption,
};
#[cfg(feature = "zeroize")]
use elliptic_curve::zeroize::Zeroize;
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
pub struct BlindedScalar {
scalar: Scalar,
mask: Scalar,
}
impl BlindedScalar {
pub fn new(scalar: Scalar, rng: impl CryptoRng + RngCore) -> Self {
Self {
scalar,
mask: Scalar::random(rng),
}
}
}
impl Borrow<Scalar> for BlindedScalar {
fn borrow(&self) -> &Scalar {
&self.scalar
}
}
impl Invert for BlindedScalar {
type Output = Scalar;
fn invert(&self) -> CtOption<Scalar> {
(self.scalar * self.mask)
.invert_vartime()
.map(|s| s * self.mask)
}
}
#[cfg(feature = "zeroize")]
impl Zeroize for BlindedScalar {
fn zeroize(&mut self) {
self.scalar.zeroize();
self.mask.zeroize();
}
}
#[cfg(feature = "zeroize")]
impl Drop for BlindedScalar {
fn drop(&mut self) {
self.zeroize();
}
}