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

feat(zeroize): added zeorizing to Felt #100

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Trantorian1
Copy link

This is feature-gated behind the zeroing flag

Pull Request type

  • Feature

What is the current behavior?

Felt provides no option to zeroize its memory, which can be an issue when dealing with sensitive cryptographic information such as private keys.

Resolves: #99

What is the new behavior?

  • Implemented zeroizing for Felt through the zeroize crate. While zeroize is not used directly, Felt implements manually the Zeroize trait in the hopes of making this behavior more 'standard'. This has been gated behind the zeroing feature.

Note

Contrarily to zeroize, this is done using safe rust by leveraging core::mem::take to replace the in-memory representation of a felt with its default value of Felt::ZERO.

Does this introduce a breaking change?

No. While the zeroing feature has been added to default, zeroing has not been implemented on drop, both because this would interfere with the Copy trait of Felt but also because it seems like it should be up to the end user to determine when they are dealing with sensitive information.

This is feature-gated behind the `zeroing` flag
@@ -44,6 +46,7 @@ serde = ["alloc", "dep:serde"]
prime-bigint = ["dep:lazy_static"]
num-traits = []
papyrus-serialization = ["std"]
zeroing = ["zeroize"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zeroing = ["dep:zeroize"]
in ordr to disable the implicit feature.
https://doc.rust-lang.org/cargo/reference/features.html#optional-dependencies

Currently you have two features available
zeroing the one you defined
and
zeroize from the default behaviour of optional = true

@@ -44,6 +46,7 @@ serde = ["alloc", "dep:serde"]
prime-bigint = ["dep:lazy_static"]
num-traits = []
papyrus-serialization = ["std"]
zeroing = ["zeroize"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for this types of feature that just depend on one single crate, I tend to name the feature by the crate name. Here zeroize rather than zeroing. Wdyt?

@@ -1062,6 +1062,18 @@ mod errors {
}
}

mod zeroing {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't really need to create a new module here. Or if you do, it should be behind the feature flag. Here it will happen in the path, even if it is empty, when the feature is not enabled

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend to make it a file. zeroize.rs. And put mod import behind the feature flag in felt/mod.rs

use super::*;

#[cfg(feature = "zeroing")]
impl zeroize::Zeroize for Felt {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +1860 to +1868
#[cfg(feature = "zeroing")]
#[test]
fn zeroing_felt() {
use zeroize::Zeroize;

let mut felt = Felt::from_hex_unchecked("0x01");
felt.zeroize();
assert_eq!(felt, Felt::ZERO);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this tests to the new file too

@jbcaron jbcaron added the enhancement New feature or request label Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug(felt): Felt is not zeroized on drop
3 participants