Skip to content

Add SecretPoint. Improve SecretScalar#92

Open
maurges wants to merge 9 commits into
mfrom
zeroize-encoded
Open

Add SecretPoint. Improve SecretScalar#92
maurges wants to merge 9 commits into
mfrom
zeroize-encoded

Conversation

@maurges

@maurges maurges commented Jun 17, 2026

Copy link
Copy Markdown
Contributor
  • SecretPoint is like SecretScalar but for points
  • Add Zeroize instance to scalar and point byte representations
  • SecretPoint and SecretScalar provide methods to get secret bytes

maurges added 6 commits June 17, 2026 11:27
Signed-off-by: maurges <git@morj.men>
Signed-off-by: maurges <git@morj.men>
Signed-off-by: maurges <git@morj.men>
Signed-off-by: maurges <git@morj.men>
Signed-off-by: maurges <git@morj.men>
For some reason cargo build --no-default-features didn't fully check
every definition?

Signed-off-by: maurges <git@morj.men>
@maurges maurges marked this pull request as ready for review June 17, 2026 14:33
@maurges maurges requested a review from survived June 17, 2026 14:34
Comment thread generic-ec/src/non_zero/mod.rs
Comment thread generic-ec/src/secret_scalar/mod.rs Outdated
Comment thread generic-ec/src/secret_scalar/mod.rs Outdated
Comment thread generic-ec/src/non_zero/mod.rs
Comment thread generic-ec/src/encoded.rs Outdated
Comment on lines +193 to +197
pub(super) fn inner<T>(x: Secret<T>) -> T
where
T: zeroize::Zeroize + Clone,
{
(**x).clone()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The function signature suggest that it takes ownership of x and retrieves the wrapped value, where as in reality it just clones the wrapped value.

  • I think it should accept x: &Secret<T>. It's not only clearer, but also more efficient, as we don't need to increment/decrement Arc's reference counter
  • Another name could be a better fit. E.g. clone_inner?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

When I was implementing this, I was thinking about the non-Arc case: you take the secret value, take it out of the Zeroizing, and obtain a non-secret. But then I realized that you can't into_inner the Zeroizing struct, and can only clone from within, but I was commited to the function idea. Now I need to reflect on what I want from it.

Comment thread generic-ec/src/encoded.rs Outdated
Comment thread generic-ec/src/encoded.rs
}

/// Convert the representation into not-secret, dropping all guarantees
pub fn into_not_secret(self) -> EncodedPoint<E> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I like the naming. Probably, SecretScalar::as_ref() should be renamed into as_not_secret() to be clearer.

That would break the API, but we could deprecate as_ref and provide a new as_not_secret as the suggested replacement.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah I just realized that we can't do that, since we rely a lot on that &SecretScalar can be passed to a function that accepts x: impl AsRef<Scalar<E>>...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can add the method with this name at least

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Though its implementation will be as stupid as for the encoded one, with clone, because as I see I can't convert out of Zeroizing.

Unless we want to use unsafe transmute, which will work because Zeroizing is guaranteed to have the same representation

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Though its implementation will be as stupid as for the encoded one, with clone, because as I see I can't convert out of Zeroizing.

Why? You can reference inner data (via Deref on Zeroizing), you don't need to clone it

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this method would be a great addition. It would replicate existing AsRef implementation. We might consider in the future removing AsRef to force users using explicit method with "dangerous" name.

Comment thread generic-ec/src/encoded.rs Outdated
Comment thread generic-ec/src/encoded.rs

/// Obtain the reference to the encoded bytes. This bypasses all the secrecy
/// guarantees, so be careful when handling them
pub fn as_bytes(&self) -> &[u8] {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

in the spirit of into_not_secret method, I'm inclined to propose to rename this one to as_not_secret_bytes, though that becomes quite long (but maybe it's justified?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Maybe slight shortening to as_nonsecret_bytes? I was initially debating the method name between into_not_secret and into_nonsecret

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as_nonsecret_bytes sounds good to me!

Comment thread generic-ec/src/encoded.rs Outdated
maurges added 2 commits June 23, 2026 17:46
Signed-off-by: maurges <git@morj.men>
Signed-off-by: maurges <git@morj.men>
Comment thread generic-ec/src/secret.rs
@@ -0,0 +1,55 @@
//! Internals for both Secret structs, like `SecretScalar` and `SecretEncodedPoint`. They have a different representation based on the presence of `alloc`: either a reference-counted nonmoving one, or a moving value

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(there are more than 2 secret structs)

Suggested change
//! Internals for both Secret structs, like `SecretScalar` and `SecretEncodedPoint`. They have a different representation based on the presence of `alloc`: either a reference-counted nonmoving one, or a moving value
//! Internals for Secret structs, like `SecretScalar` and `SecretEncodedPoint`. They have a different representation based on the presence of `alloc`: either a reference-counted nonmoving one, or a moving value

Comment thread generic-ec/src/encoded.rs
}

/// Convert the representation into not-secret, dropping all guarantees
pub fn into_not_secret(self) -> EncodedPoint<E> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Though its implementation will be as stupid as for the encoded one, with clone, because as I see I can't convert out of Zeroizing.

Why? You can reference inner data (via Deref on Zeroizing), you don't need to clone it

Comment thread generic-ec/src/secret.rs
value_on_heap
}

pub fn inner<T>(x: Secret<T>) -> T

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

clone_inner feels like better name for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants