Skip to content

Why does ConditionallySelectable require Copy? #56

Closed

Description

@nickray

I'm working on an embedded versions of ed25519, and considering whether to use your library, or roll my own. I'd like to have more control over what is copied.

On the one hand, I think ConditionallySelectable could use Clone as trait bound, and then later in the provided method conditional_swap explicitly clone (instead of the implict copy in the derefence). AFAIK, Copy types would always implement Clone in the same way, so this would strictly extend applicability of the trait. If you agree, I can PR this, it's just two changed lines.

On the other hand, the implementation I am currently using, which is a transliteration of the TweetNaCl code, does not use any copy or clone at all:

type Limbs = [i64; 16];

#[derive(Clone,Debug,Default)]
pub struct FieldElement(pub Limbs);

pub fn conditional_swap(p: &mut FieldElement, q: &mut FieldElement, b: i64) {
    let mask: i64 = !(b - 1);
    for (pi, qi) in p.0.iter_mut().zip(q.0.iter_mut()) {
        let t = mask & (*pi ^ *qi);
        *pi ^= t;
        *qi ^= t;
    }
}

Therefore, I'm not sure I agree with deriving conditional swap from select. What is the rationale here, besides minimal implementation cost?

EDIT: I see you overwrite the provided implementation for integral types, so I guess the idea is for users to do so as well for their own types?

Activity

  1. hdevalence commented on Nov 15, 2019

    @hdevalence
    Contributor

    The Copy trait is a marker that the value can be duplicated by copying bits, whereas Clone can perform arbitrary logic (a "deep copy"). We require Copy because it doesn't really make sense to do constant-time operations on non-Copyable types.

    Your example does use Copy, just not explicitly (e.g., in the line let t = mask & (*pi ^ *qi)).

    As you note in the edit, the provided implementations are there for implementation convenience, but implementors can override them if they wish.

  2. nickray commented on Nov 15, 2019

    @nickray
    Author

    Yes, it's a marker trait, but it also (when implemented e.g. on newtypes) cause the compiler to change behaviour and implicitly copy things around, eliminating compile time checks. So not just "can" but also "does". (Yes, in my example copies are also done, but of individual entries, not the entire type.)

    As noted, I assume all your current use cases that are Copy implement Clone also, in exactly the same way - no deep copy. Do they?

    As it stands, I have the choice of making my types Copy or not using your library. Do you have some (heuristic...) argument on why I should do the former?

    Also, do you foresee actual harm in replacing the Copy with a Clone bound (given it would trivially extend the applicability of the library)?

  3. hdevalence commented on Nov 15, 2019

    @hdevalence
    Contributor

    The heuristic reason why your types should be Copy is that constant-time operations implemented using bit operations should be performed on types which are bit-copyable.

    You are of course free to not use this library if it doesn't suit your needs, but I don't think we will be relaxing the Copy bound.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions