Skip to content

Use BitLength in constant expressions when const_fn is stable #346

Closed

Description

@briansmith

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.

Activity

  1. pietro commented on Jan 15, 2019

    @pietro
    Contributor

    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.

  2. briansmith commented on Jan 16, 2019

    @briansmith
    OwnerAuthor

    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.

  3. pietro commented on Jan 16, 2019

    @pietro
    Contributor

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

  4. briansmith commented on Jan 16, 2019

    @briansmith
    OwnerAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions