Interaction between classes from different curves should fail:
k256.Point.BASE.add(p256.Point.BASE)
For this purpose we want to use instanceof operator, which is fast and works during runtime
Different calls of curve() would return different classes -
curve(params) !== curve(params): if somebody decided to monkey-patch their curve,
it won't affect others
TypeScript can't infer types for classes created inside a function. Classes is one instance of nominative types in TypeScript and interfaces only check for shape, so it's hard to create unique type for every function call.
We can use generic types via some param, like curve opts, but that would:
1. Enable interaction between curve(params) and curve(params) (curves of same params)
which is hard to debug.
2. Params can be generic and we can't enforce them to be constant value:
if somebody creates curve from non-constant params,
it would be allowed to interact with other curves with non-constant params
Design rationale for types
k256.Point.BASE.add(p256.Point.BASE)
instanceof
operator, which is fast and works during runtimecurve()
would return different classes -curve(params) !== curve(params)
: if somebody decided to monkey-patch their curve, it won't affect othersTypeScript can't infer types for classes created inside a function. Classes is one instance of nominative types in TypeScript and interfaces only check for shape, so it's hard to create unique type for every function call.
We can use generic types via some param, like curve opts, but that would: 1. Enable interaction between
curve(params)
andcurve(params)
(curves of same params) which is hard to debug. 2. Params can be generic and we can't enforce them to be constant value: if somebody creates curve from non-constant params, it would be allowed to interact with other curves with non-constant paramsTODO: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#unique-symbol