GithubHelp home page GithubHelp logo

Field Trait API UX about lambdaworks HOT 3 OPEN

lambdaclass avatar lambdaclass commented on August 12, 2024
Field Trait API UX

from lambdaworks.

Comments (3)

unbalancedparentheses avatar unbalancedparentheses commented on August 12, 2024 1

I wouldn't suffice but it would make things simpler. Let's create an issue to migrate to core.

from lambdaworks.

schouhy avatar schouhy commented on August 12, 2024

leveraging Add and its friends was our approach too at first. We hit a snag when we implemented complex formulas for field extensions and elliptic curves. We noticed that unless the operations are defined for all combinations of (&F, &F), (F, &F), (&F, F), (F, F), the formulas end up being very unreadable because of explosions of parenthesis and & symbols. The syntax to add all those trait bounds seemed very convoluted, specially for the (&T, &T) case where we need to ask for a type T to have implemented Add for &T. Also, for fields, all the operations only make sense when OutputType equal to F, which made the syntax also way more verbose.
We came up with this solution that in our opinion made things simple for the trait code and for the users of the trait to implement it. And formulas can be read easily.
All of the std::ops are implemented off the operations in the trait now. I wonder if changing those to core::ops would suffice to have support for no_std easier. But I'm not familiar with that.

from lambdaworks.

gluax avatar gluax commented on August 12, 2024

Regarding the complexity around Add for the different combinations, it can be complex, but rust has tools to make it easier.

We could leverage macro_rules like most libraries, including how rust implements the trait for its core types(u8, u16, etc.). Or, if we are particularly inspired, we could write a proc macro for something like #[derive(Add)] at the expense of compile time.

Despite the extra work, it's better in the long term if we ever decide to layer traits. For example, let's say we want to define a Hash2Curve trait which requires some mathematical operations on Field types. Without it having the operations on the Field trait itself, we'd have to define Hash2Curve in a way like:

pub trait Hash2Curve {
  type Field: Field + Add<Rhs = Self::Field> + ...;

  ...
}

We could not define those trait bounds on the trait, but then type inference within the compiler or specific use cases would deny the usage of those traits. This means we could leverage the .add method defined on the Field trait, but ergonomically this leaves much to be desired.

from lambdaworks.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.