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

Use BitLength in constant expressions when const_fn is stable #346

Closed
briansmith opened this issue Nov 14, 2016 · 4 comments
Closed

Use BitLength in constant expressions when const_fn is stable #346

briansmith opened this issue Nov 14, 2016 · 4 comments

Comments

@briansmith
Copy link
Owner

Example before:

        pub static $NAME: agreement::Algorithm = agreement::Algorithm {
            i: ec::AgreementAlgorithmImpl {
                public_key_len: 1 + (2 * (($bits + 7) / 8)),
                elem_and_scalar_len: ($bits + 7) / 8,
                [...]
            },
        };

Example after:

        pub static $NAME: agreement::Algorithm = agreement::Algorithm {
            i: ec::AgreementAlgorithmImpl {
                public_key_len: 1 + (2 * $bits.as_usize_bytes()),
                elem_and_scalar_len: $bits.as_usize_bytes(),
                [...]
            },
        };

This depends on rust-lang/rust#24111 since as_usize_bytes() and the constructors for BitLength must be constant functions.

@pietro
Copy link
Contributor

pietro commented Jan 15, 2019

I did some work on this on this branch. I created a new function as_usize_bytes, but on #350 you mention using as_bytes_rounded_up which already exists, well as_usize_bytes_rounded_up does. If you're ok with the current code I'll look for the bits<->bytes conversions.

@briansmith
Copy link
Owner Author

I created a new function as_usize_bytes

I don't have a strong preference here. Generally I prefer the code to avoid panic! so as_usize_bytes_rounded_up variant seems better with that in mind, because it is clear it will never panic and it is clear that it rounds. Maybe in the future we can get type-level programming in Rust such that we can restrict the input type to a multiple of 8. Alternatively, we could flip the definitions around so that the xxx_BITS variables are defined to have value 8 * xxx_BYTES. I don't have strong feelings either way there.

@pietro
Copy link
Contributor

pietro commented Jan 16, 2019

I pushed a commit using as_usize_bytes_rounded_up. It will break no_heap builds though.

@briansmith
Copy link
Owner Author

I pushed a commit using as_usize_bytes_rounded_up. It will break no_heap builds though.

IIRC, that function is marked #[cfg(feature = "use_heap")] only because it is only used by other functions that are marked that way. In other words, it is right to remove the #[cfg(feature = "use_heap")] if that is needed to make the no-heap builds work.

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

No branches or pull requests

2 participants