Is there a need for a BigNumber library in Objective-C/Swift?


(Otto van Verseveld) #1

About I year ago I started, while Swift was still in beta development, a
github project containing a wrapper for the GNU Multiple Precision
Arithmetic library (see: https://github.com/githotto/osxgmp).

I haven't done much on it lately, also because some questions were not
answered in the GMP-community yet. I want to make it also suitable for iOS
development, since currently it compiles / runs only on OSX.

Do you think there is a need for this 'swift-extension' API/library and it
is worthwhile developing further such that the Swift language will have a
BigDecimal/BigInt library as being present in Java/C#?

Any thoughts or even help or suggestions on how to get it compiled for iOS
are very welcome!
Thanks in advance, kind regards,
Otto van Verseveld


(Etan Kissling) #2

Depends on what you want to achieve.

If it's really used for math, then yes, it's great to have such a library available.
Not many "real" use cases, though, imo, to warrant putting it into the core library atm.

For finance, I guess that's what's `NSDecimalNumber` is for.
This class is not that nice to use from Swift, though.

For crypto, you don't want to use a common BigInt library.
- The library doesn't provide any security guarantees. e.g. when buffers are cleared etc.
- Crypto typically involves fixed sizes (e.g. 256-bit). With common BigInts, you lose performance.
- Crypto is not possible to be implemented securely in pure Swift 2.
  The language lacks annotations to prevent compiler optimisations on a fine-grained level.
  i.e. timing invariance of comparisons etc is not guaranteed / no control about caching behaviour.
  Need to do that in Assembly to be safe against side channel attacks.
  Even with Assembly, you may lose to CPU microcode optimisations. Out of scope, though.

···

On 12 Dec 2015, at 12:37, Otto van Verseveld via swift-evolution <swift-evolution@swift.org> wrote:

About I year ago I started, while Swift was still in beta development, a github project containing a wrapper for the GNU Multiple Precision Arithmetic library (see: https://github.com/githotto/osxgmp).

I haven't done much on it lately, also because some questions were not answered in the GMP-community yet. I want to make it also suitable for iOS development, since currently it compiles / runs only on OSX.

Do you think there is a need for this 'swift-extension' API/library and it is worthwhile developing further such that the Swift language will have a BigDecimal/BigInt library as being present in Java/C#?

Any thoughts or even help or suggestions on how to get it compiled for iOS are very welcome!
Thanks in advance, kind regards,
Otto van Verseveld

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Dave Abrahams) #3

Related: I have been working for some time on a rewrite of all the integer types and protocols <https://github.com/apple/swift/blob/master/test/Prototypes/Integers.swift.gyb>. One goal of this effort is to enable operations on mixed integer types, which as you can see is partially completed. In-place arithmetic (anInt32 += aUInt64) is next. Another important goal is to make the integer protocols actually useful for writing generic code, instead of what they are today: implementation artifacts used only for code sharing. As another litmus test of the usefulness of the resulting protocols, the plan is to implement BigInt in terms of the generic operations defined on integers, and make BigInt itself conform to those protocols.

Using GMP would probably result in faster code, and after the protocols have proven themselves adequate for mixed-width/arbitrary-precision integer integer implementation, it might make sense to put a version of the same APIs based on GMP into the standard library.

-Dave

···

On Dec 12, 2015, at 3:37 AM, Otto van Verseveld via swift-evolution <swift-evolution@swift.org> wrote:

About I year ago I started, while Swift was still in beta development, a github project containing a wrapper for the GNU Multiple Precision Arithmetic library (see: https://github.com/githotto/osxgmp).

I haven't done much on it lately, also because some questions were not answered in the GMP-community yet. I want to make it also suitable for iOS development, since currently it compiles / runs only on OSX.

Do you think there is a need for this 'swift-extension' API/library and it is worthwhile developing further such that the Swift language will have a BigDecimal/BigInt library as being present in Java/C#?

Any thoughts or even help or suggestions on how to get it compiled for iOS are very welcome!