├── .gitattributes ├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md └── workflows │ └── github-pull-request-build.yml ├── .gitignore ├── .gitlab-ci.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── THANKS.md ├── bignum-serialization-kotlinx ├── README.md ├── build.gradle.kts └── src │ ├── commonMain │ └── kotlin │ │ └── com │ │ └── ionspin │ │ └── kotlin │ │ └── bignum │ │ └── serialization │ │ └── kotlinx │ │ ├── CollectedModules.kt │ │ ├── bigdecimal │ │ ├── BigDecimalArraySerializer.kt │ │ └── BigDecimalHumanReadableSerializer.kt │ │ └── biginteger │ │ ├── BigIntegerArraySerializer.kt │ │ └── BigIntegerHumanReadableSerializer.kt │ └── commonTest │ └── kotlin │ └── com │ └── ionspin │ └── kotlin │ └── bignum │ └── serialization │ └── kotlinx │ ├── QuickSetupTest.kt │ ├── bigdecimal │ ├── BigDecimalArraySerializationTest.kt │ └── BigDecimalHumanReadableSerializationTest.kt │ └── biginteger │ ├── BigIntegerArraySerializationTest.kt │ └── BigIntegerHumanReadableSerializationTest.kt ├── bignum ├── build.gradle.kts └── src │ ├── commonMain │ └── kotlin │ │ └── com │ │ └── ionspin │ │ └── kotlin │ │ └── bignum │ │ ├── BigNumber.kt │ │ ├── Endianness.kt │ │ ├── QuotientAndRemainder.kt │ │ ├── decimal │ │ ├── BigDecimal.kt │ │ ├── BigDecimalExtensions.kt │ │ └── DecimalMode.kt │ │ ├── integer │ │ ├── BigInteger.kt │ │ ├── BigInteger32Arithmetic.kt │ │ ├── BigIntegerArithmetic.kt │ │ ├── BigIntegerExtensions.kt │ │ ├── BigIntegerList63Arithmetic.kt │ │ ├── Configuration.kt │ │ ├── PlatformBigIntegerArithmetic.kt │ │ ├── PlatformWorkarounds.kt │ │ ├── Quadruple.kt │ │ ├── base32 │ │ │ └── BigInteger32Arithmetic.kt │ │ ├── base63 │ │ │ ├── BigInteger63LinkedListArithmetic.kt │ │ │ └── array │ │ │ │ └── BigInteger63Arithmetic.kt │ │ ├── concurrent │ │ │ └── Placeholder.kt │ │ └── util │ │ │ ├── ConversionUtils.kt │ │ │ ├── DigitUtil.kt │ │ │ └── VariousUtil.kt │ │ └── modular │ │ ├── ModularBigInteger.kt │ │ └── ModularBigIntegerExtensions.kt │ ├── commonTest │ └── kotlin │ │ └── com │ │ └── ionspin │ │ └── kotlin │ │ └── bignum │ │ ├── ArithmeticExtensions.kt │ │ ├── TestUtil.kt │ │ ├── WordTypealiasUtil.kt │ │ ├── decimal │ │ ├── BigDecimalComparisonTest.kt │ │ ├── BigDecimalCreationTest.kt │ │ ├── BigDecimalDecimalModeTests.kt │ │ ├── BigDecimalDivPowTest.kt │ │ ├── BigDecimalDivisionTest.kt │ │ ├── BigDecimalFactorialTest.kt │ │ ├── BigDecimalNarrowingTest.kt │ │ ├── BigDecimalReadmeTest.kt │ │ ├── BigDecimalRoundingTests.kt │ │ └── ReportedIssueReplicationTest.kt │ │ ├── integer │ │ ├── BigIntegerReadmeTest.kt │ │ ├── BigIntegerTest.kt │ │ ├── BitwiseTest.kt │ │ ├── ByteArrayConversionTest.kt │ │ ├── ConversionTest.kt │ │ ├── DebugTest.kt │ │ ├── MajorSystemErrorTest.kt │ │ ├── ParsingAndToStringTest.kt │ │ ├── arithmetic │ │ │ ├── BigInteger32ArithmeticAdditionTest.kt │ │ │ ├── BigInteger32ArithmeticSubtractionTest.kt │ │ │ ├── BigInteger32BitWiseTest.kt │ │ │ ├── BigInteger32MultiplicationTest.kt │ │ │ ├── DivisionBenchmark.kt │ │ │ ├── bigint63 │ │ │ │ ├── BigInteger63AdditionTest.kt │ │ │ │ ├── BigInteger63ArithmeticComparison.kt │ │ │ │ ├── BigInteger63ArithmeticDivisionTest.kt │ │ │ │ ├── BigInteger63BitwiseTest.kt │ │ │ │ ├── BigInteger63MultiplicationTest.kt │ │ │ │ └── BigInteger63SubtractionTest.kt │ │ │ └── bigint63list │ │ │ │ └── BigInteger63ListArithmeticDivisionTest.kt │ │ ├── benchmark │ │ │ ├── DivisionBenchmark.kt │ │ │ └── MultiplicationBenchmark.kt │ │ └── integer │ │ │ ├── BigIntegerBitwiseOperations.kt │ │ │ └── util │ │ │ └── EndianessTest.kt │ │ └── modular │ │ ├── ModularBigIntegerAddition.kt │ │ ├── ModularBigIntegerExponentiationTest.kt │ │ └── ModularBigIntegerReadmeTest.kt │ ├── iosMain │ └── kotlin │ │ └── com │ │ └── ionspin │ │ └── kotlin │ │ └── bignum │ │ └── integer │ │ ├── Placeholder │ │ └── util │ │ ├── block.kt │ │ └── runTest.kt │ ├── iosTest │ └── kotlin │ │ └── com │ │ └── ionspin │ │ └── kotlin │ │ └── bignum │ │ └── integer │ │ └── Placeholder │ ├── jsMain │ └── kotlin │ │ └── com │ │ └── ionspin │ │ └── kotlin │ │ └── bignum │ │ └── integer │ │ ├── Placeholder │ │ ├── PlatformBigIntegerArithmetic.kt │ │ └── PlatformWorkarounds.kt │ ├── jsTest │ └── kotlin │ │ └── com │ │ └── ionspin │ │ └── kotlin │ │ └── bignum │ │ └── integer │ │ └── Placeholder │ ├── jvmMain │ └── kotlin │ │ └── com │ │ └── ionspin │ │ └── kotlin │ │ └── bignum │ │ ├── decimal │ │ └── DecimalUtility.kt │ │ └── integer │ │ ├── BigInteger63Utility.kt │ │ ├── Placeholder │ │ ├── PlatformBigIntegerArithmetic.kt │ │ └── PlatformWorkarounds.kt │ ├── jvmTest │ └── kotlin │ │ └── com │ │ └── ionspin │ │ └── kotlin │ │ └── bignum │ │ ├── decimal │ │ ├── BigDecimalFloorCeilTest.kt │ │ ├── BigDecimalJvmTest.kt │ │ ├── BigDecimalNarrowingTest.kt │ │ ├── BigDecimalRoundingTest.kt │ │ ├── BigDecimalScaleTest.kt │ │ ├── BigDecimalToStringTest.kt │ │ └── ReportedIssuesTest.kt │ │ ├── integer │ │ ├── BigIntegerJvmTest.kt │ │ ├── ProfilerRunner.kt │ │ ├── base32 │ │ │ ├── BigInteger32JavaAdditionTest.kt │ │ │ ├── BigInteger32JavaBitwiseTest.kt │ │ │ ├── BigInteger32JavaDivisionTest.kt │ │ │ ├── BigInteger32JavaMultiplyTest.kt │ │ │ ├── BigInteger32JavaStringConversionTests.kt │ │ │ ├── BigInteger32JavaSubstractTest.kt │ │ │ ├── BigInteger32SqrtTest.kt │ │ │ ├── BigInteger32Utility.kt │ │ │ └── ByteArrayToAndFromTest.kt │ │ ├── base63 │ │ │ ├── BigInteger63ArithmeticDivisionTest.kt │ │ │ ├── BigInteger63GcdTest.kt │ │ │ ├── BigInteger63JavaAdditionTest.kt │ │ │ ├── BigInteger63JavaBaseConversionTest.kt │ │ │ ├── BigInteger63JavaBitwiseTest.kt │ │ │ ├── BigInteger63JavaDivisionTest.kt │ │ │ ├── BigInteger63JavaMultiplyTest.kt │ │ │ ├── BigInteger63JavaStringConversionTests.kt │ │ │ ├── BigInteger63SqrtTest.kt │ │ │ └── ByteArrayConversionTest.kt │ │ ├── base63List │ │ │ ├── BigInteger63ListGcdTest.kt │ │ │ ├── BigInteger63ListJavaAdditionTest.kt │ │ │ ├── BigInteger63ListJavaBaseConversionTest.kt │ │ │ ├── BigInteger63ListJavaBitwiseTest.kt │ │ │ ├── BigInteger63ListJavaDivisionTest.kt │ │ │ ├── BigInteger63ListJavaMultiplyTest.kt │ │ │ ├── BigInteger63ListJavaStringConversionTests.kt │ │ │ ├── BigIntegerList63SqrtTest.kt │ │ │ └── bigInteger63Utility.kt │ │ └── benchmark │ │ │ └── JavaMultiplicationBenchmark.kt │ │ └── modular │ │ ├── JvmModularBigIntegerExponentiationTest.kt │ │ ├── ModularBigIntegerDivisionTest.kt │ │ └── ModularBigIntegerMultiplication.kt │ ├── linuxMain │ └── kotlin │ │ └── com │ │ └── ionspin │ │ └── kotlin │ │ └── bignum │ │ └── integer │ │ └── Placeholder │ ├── linuxTest │ └── kotlin │ │ └── com │ │ └── ionspin │ │ └── kotlin │ │ └── bignum │ │ └── integer │ │ └── Placeholder │ ├── nativeMain │ └── kotlin │ │ └── com │ │ └── ionspin │ │ └── kotlin │ │ └── bignum │ │ └── integer │ │ ├── Placeholder │ │ ├── PlatformBigIntegerArithmetic.kt │ │ └── PlatformWorkarounds.kt │ ├── nativeTest │ └── kotlin │ │ └── com │ │ └── ionspin │ │ └── kotlin │ │ └── bignum │ │ └── integer │ │ └── Placeholder │ ├── wasmJsMain │ └── kotlin │ │ └── com │ │ └── ionspin │ │ └── kotlin │ │ └── bignum │ │ └── integer │ │ └── PlatformWorkarounds.wasmJs.kt │ ├── wasmJsTest │ └── kotlin │ │ └── com │ │ └── ionspin │ │ └── kotlin │ │ └── bignum │ │ └── TestUtil.wasmJs.kt │ └── wasmWasiMain │ └── kotlin │ └── com │ └── ionspin │ └── kotlin │ └── bignum │ └── integer │ └── PlatformWorkarounds.wasmWasi.kt ├── build.gradle.kts ├── buildSrc ├── build.gradle.kts └── src │ └── main │ └── kotlin │ └── Deps.kt ├── gradle.properties ├── gradle ├── verification-metadata.xml └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── kotlin-js-store └── yarn.lock ├── linuxBuild.sh ├── linuxBuildAndPublish.sh ├── linuxPublishToMaven.sh ├── macBuild.sh ├── macBuildAndPublish.sh ├── macPublishToMaven.sh ├── publishToMavenOneByOne.sh ├── settings.gradle.kts ├── windowsBuild.sh ├── windowsBuildAndPublish.sh └── windowsPublishToMaven.sh /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Operation 16 | 2. Values to reproduce issue 17 | 18 | **Expected behavior** 19 | A clear and concise description of what you expected to happen. 20 | 21 | **Platform** 22 | - [e.g. JVM, JS, Native, iOS, Linux] 23 | 24 | **If JS (please complete the following information):** 25 | - OS: [e.g. iOS] 26 | - Browser [e.g. chrome, safari] 27 | - Version [e.g. 22] 28 | 29 | **If Smartphone (please complete the following information):** 30 | - Device: [e.g. Google Pixel 3] 31 | - OS: [e.g. Android 9.0] 32 | - Browser [e.g. Chrome] 33 | - Version [e.g. 22] 34 | 35 | **Additional context** 36 | Add any other context about the problem here. 37 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/workflows/github-pull-request-build.yml: -------------------------------------------------------------------------------- 1 | 2 | name: Build project and run tests. 3 | run-name: ${{ github.actor }} pull request 4 | on: 5 | pull_request: 6 | branches: 7 | - main 8 | 9 | jobs: 10 | Build-And-Test-Linux: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: Setup java 14 | uses: actions/setup-java@v4 15 | with: 16 | distribution: 'temurin' 17 | java-version: '17' 18 | - name: Checkout 19 | uses: actions/checkout@v4 20 | - uses: browser-actions/setup-chrome@v1 21 | - run: CHROME_BIN=${{ steps.setup-chrome.outputs.chrome-path }} ./linuxBuild.sh 22 | Build-And-Test-Mac: 23 | runs-on: macos-latest 24 | steps: 25 | - name: Setup java 26 | uses: actions/setup-java@v4 27 | with: 28 | distribution: 'temurin' 29 | java-version: '17' 30 | - name: Checkout 31 | uses: actions/checkout@v4 32 | - uses: browser-actions/setup-chrome@v1 33 | id: setup-chrome 34 | - run: CHROME_BIN=${{ steps.setup-chrome.outputs.chrome-path }} ./macBuild.sh 35 | Build-And-Test-Windows: 36 | runs-on: windows-latest 37 | steps: 38 | - name: Setup java 39 | uses: actions/setup-java@v4 40 | with: 41 | distribution: 'temurin' 42 | java-version: '17' 43 | - name: Checkout 44 | uses: actions/checkout@v4 45 | - uses: msys2/setup-msys2@v2 46 | - uses: browser-actions/setup-chrome@v1 47 | id: setup-chrome 48 | - shell: msys2 {0} 49 | run: | 50 | ./windowsBuild.sh -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | */build 7 | build/ 8 | /captures 9 | /bignum/node_modules 10 | /buildSrc/out 11 | /bignum/node_modules 12 | zignum/ 13 | .kotlin/ 14 | -------------------------------------------------------------------------------- /.gitlab-ci.yml: -------------------------------------------------------------------------------- 1 | stages: 2 | - prepare 3 | - build 4 | - deploy 5 | 6 | workflow: 7 | rules: 8 | - if: $CI_EXTERNAL_PULL_REQUEST_IID 9 | - if: $CI_COMMIT_TAG 10 | - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH 11 | 12 | variables: 13 | GIT_SUBMODULE_STRATEGY: recursive 14 | CHROME_BIN: "chromium" 15 | 16 | simpleCheck: 17 | stage: prepare 18 | script: echo PREPARE 19 | tags: 20 | - linuxX64 21 | # ---- Linux builds ---- 22 | 23 | buildLinux: 24 | stage: build 25 | when: manual 26 | allow_failure: false 27 | script: 28 | - ./linuxBuild.sh 29 | tags: 30 | - linuxX64 31 | linuxPublishToSnapshot: 32 | stage: deploy 33 | script: 34 | - ./linuxBuildAndPublish.sh 35 | only: 36 | - main 37 | tags: 38 | - linuxX64 39 | 40 | 41 | # ---- Mac builds ---- 42 | 43 | buildMac: 44 | stage: build 45 | when: manual 46 | allow_failure: false 47 | script: 48 | - ./macBuild.sh 49 | tags: 50 | - macos 51 | 52 | macPublishToSnapshot: 53 | stage: deploy 54 | script: 55 | - ./macBuild.sh 56 | - ./macBuildAndPublish.sh 57 | only: 58 | - main 59 | tags: 60 | - macos 61 | 62 | # ---- Windows builds ---- 63 | 64 | buildWindows: 65 | stage: build 66 | when: manual 67 | allow_failure: false 68 | script: 69 | - $env:CHERE_INVOKING = 'yes' 70 | - C:\msys64\usr\bin\bash -lc "./windowsBuild.sh" 71 | tags: 72 | - windowsX64 73 | 74 | windowsPublishToSnapshot: 75 | stage: deploy 76 | script: 77 | - $env:CHERE_INVOKING = 'yes' 78 | - C:\msys64\usr\bin\bash -lc "./windowsBuildAndPublish.sh" 79 | only: 80 | - main 81 | tags: 82 | - windowsX64 83 | -------------------------------------------------------------------------------- /THANKS.md: -------------------------------------------------------------------------------- 1 | Special thanks to people for contributing to the library: 2 | 3 | - Steven K Olson (@skolson) 4 | - Or Noyman (@fullkomun) 5 | - Grégory Lureau (@glureau) 6 | -------------------------------------------------------------------------------- /bignum-serialization-kotlinx/src/commonMain/kotlin/com/ionspin/kotlin/bignum/serialization/kotlinx/CollectedModules.kt: -------------------------------------------------------------------------------- 1 | package com.ionspin.kotlin.bignum.serialization.kotlinx 2 | 3 | import com.ionspin.kotlin.bignum.decimal.BigDecimal 4 | import com.ionspin.kotlin.bignum.decimal.DecimalMode 5 | import com.ionspin.kotlin.bignum.integer.BigInteger 6 | import com.ionspin.kotlin.bignum.serialization.kotlinx.bigdecimal.BigDecimalArraySerializer 7 | import com.ionspin.kotlin.bignum.serialization.kotlinx.bigdecimal.BigDecimalHumanReadableSerializer 8 | import com.ionspin.kotlin.bignum.serialization.kotlinx.bigdecimal.DecimalModeSerializer 9 | import com.ionspin.kotlin.bignum.serialization.kotlinx.biginteger.BigIntegerArraySerializer 10 | import com.ionspin.kotlin.bignum.serialization.kotlinx.biginteger.BigIntegerHumanReadableSerializer 11 | import kotlinx.serialization.modules.SerializersModule 12 | 13 | /** 14 | * Created by Ugljesa Jovanovic 15 | * ugljesa.jovanovic@ionspin.com 16 | * on 11-Jul-2021 17 | */ 18 | val humanReadableSerializerModule = SerializersModule { 19 | contextual(BigInteger::class, BigIntegerHumanReadableSerializer) 20 | contextual(BigDecimal::class, BigDecimalHumanReadableSerializer) 21 | contextual(DecimalMode::class, DecimalModeSerializer) 22 | } 23 | 24 | val arrayBasedSerializerModule = SerializersModule { 25 | contextual(BigInteger::class, BigIntegerArraySerializer) 26 | contextual(BigDecimal::class, BigDecimalArraySerializer) 27 | contextual(DecimalMode::class, DecimalModeSerializer) 28 | } 29 | -------------------------------------------------------------------------------- /bignum-serialization-kotlinx/src/commonMain/kotlin/com/ionspin/kotlin/bignum/serialization/kotlinx/bigdecimal/BigDecimalArraySerializer.kt: -------------------------------------------------------------------------------- 1 | package com.ionspin.kotlin.bignum.serialization.kotlinx.bigdecimal 2 | 3 | import com.ionspin.kotlin.bignum.decimal.BigDecimal 4 | import com.ionspin.kotlin.bignum.decimal.DecimalMode 5 | import com.ionspin.kotlin.bignum.decimal.RoundingMode 6 | import com.ionspin.kotlin.bignum.integer.BigInteger 7 | import com.ionspin.kotlin.bignum.serialization.kotlinx.biginteger.BigIntegerArraySerializer 8 | import kotlinx.serialization.KSerializer 9 | import kotlinx.serialization.descriptors.SerialDescriptor 10 | import kotlinx.serialization.descriptors.buildClassSerialDescriptor 11 | import kotlinx.serialization.descriptors.element 12 | import kotlinx.serialization.encoding.CompositeDecoder 13 | import kotlinx.serialization.encoding.Decoder 14 | import kotlinx.serialization.encoding.Encoder 15 | import kotlinx.serialization.encoding.decodeStructure 16 | import kotlinx.serialization.encoding.encodeStructure 17 | import kotlinx.serialization.modules.SerializersModule 18 | 19 | /** 20 | * Created by Ugljesa Jovanovic 21 | * ugljesa.jovanovic@ionspin.com 22 | * on 04-Jul-2021 23 | */ 24 | object BigDecimalArraySerializer : KSerializer { 25 | 26 | 27 | override val descriptor: SerialDescriptor = buildClassSerialDescriptor("BigDecimalArray") { 28 | element("significand", BigIntegerArraySerializer.descriptor) 29 | element("exponent") 30 | element("decimalMode", DecimalModeSerializer.descriptor, isOptional = true) 31 | } 32 | 33 | override fun serialize(encoder: Encoder, value: BigDecimal) { 34 | encoder.encodeStructure(descriptor) { 35 | encodeSerializableElement(descriptor, 0, BigIntegerArraySerializer, value.significand) 36 | encodeLongElement(descriptor, 1, value.exponent) 37 | value.decimalMode?.let { 38 | encodeSerializableElement(descriptor, 2, DecimalModeSerializer, value.decimalMode!!) 39 | } 40 | 41 | } 42 | } 43 | 44 | override fun deserialize(decoder: Decoder): BigDecimal { 45 | return decoder.decodeStructure(descriptor) { 46 | var significand = BigInteger.ZERO 47 | var exponent = 0L 48 | var decimalMode : DecimalMode? = null 49 | while (true) { 50 | when (val index = decodeElementIndex(descriptor)) { 51 | 0 -> significand = decodeSerializableElement(descriptor, 0, BigIntegerArraySerializer) 52 | 1 -> exponent = decodeLongElement(descriptor, 1) 53 | 2 -> decimalMode = decodeSerializableElement(descriptor, 2, DecimalModeSerializer) 54 | CompositeDecoder.DECODE_DONE -> break 55 | else -> error("Unexpected index: $index") 56 | } 57 | } 58 | 59 | BigDecimal.fromBigIntegerWithExponent(significand, exponent, decimalMode) 60 | } 61 | } 62 | 63 | 64 | } 65 | 66 | object DecimalModeSerializer : KSerializer { 67 | 68 | override val descriptor: SerialDescriptor = buildClassSerialDescriptor("DecimalMode") { 69 | element("decimalPrecision") 70 | element("roundingMode") 71 | element("scale") 72 | } 73 | 74 | override fun serialize(encoder: Encoder, value: DecimalMode) { 75 | encoder.encodeStructure(descriptor) { 76 | encodeLongElement(descriptor, 0, value.decimalPrecision) 77 | encodeStringElement(descriptor, 1, value.roundingMode.name) 78 | encodeLongElement(descriptor, 2, value.scale) 79 | } 80 | } 81 | 82 | override fun deserialize(decoder: Decoder): DecimalMode { 83 | return decoder.decodeStructure(descriptor) { 84 | var decimalPrecision = -1L 85 | var roundingModeString = "" 86 | var scale = -1L 87 | while (true) { 88 | when (val index = decodeElementIndex(descriptor)) { 89 | 0 -> decimalPrecision = decodeLongElement(descriptor, 0) 90 | 1 -> roundingModeString = decodeStringElement(descriptor, 1) 91 | 2 -> scale = decodeLongElement(descriptor, 2) 92 | CompositeDecoder.DECODE_DONE -> break 93 | else -> error("Unexpected index: $index") 94 | } 95 | } 96 | 97 | DecimalMode(decimalPrecision, RoundingMode.valueOf(roundingModeString), scale) 98 | } 99 | } 100 | 101 | 102 | } 103 | 104 | val bigDecimalArraySerializerModule = SerializersModule { 105 | contextual(BigDecimal::class, BigDecimalArraySerializer) 106 | contextual(DecimalMode::class, DecimalModeSerializer) 107 | } 108 | -------------------------------------------------------------------------------- /bignum-serialization-kotlinx/src/commonMain/kotlin/com/ionspin/kotlin/bignum/serialization/kotlinx/bigdecimal/BigDecimalHumanReadableSerializer.kt: -------------------------------------------------------------------------------- 1 | package com.ionspin.kotlin.bignum.serialization.kotlinx.bigdecimal 2 | 3 | import com.ionspin.kotlin.bignum.decimal.BigDecimal 4 | import com.ionspin.kotlin.bignum.decimal.DecimalMode 5 | import kotlinx.serialization.KSerializer 6 | import kotlinx.serialization.descriptors.PrimitiveKind 7 | import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor 8 | import kotlinx.serialization.descriptors.SerialDescriptor 9 | import kotlinx.serialization.encoding.Decoder 10 | import kotlinx.serialization.encoding.Encoder 11 | import kotlinx.serialization.modules.SerializersModule 12 | 13 | /** 14 | * Created by Ugljesa Jovanovic 15 | * ugljesa.jovanovic@ionspin.com 16 | * on 04-Jul-2021 17 | */ 18 | object BigDecimalHumanReadableSerializer : KSerializer { 19 | 20 | override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("BigDecimal", PrimitiveKind.STRING) 21 | 22 | 23 | override fun serialize(encoder: Encoder, value: BigDecimal) { 24 | encoder.encodeString(value.toString(10)) 25 | } 26 | 27 | override fun deserialize(decoder: Decoder): BigDecimal { 28 | return BigDecimal.parseString(decoder.decodeString(), 10) 29 | } 30 | 31 | } 32 | 33 | val bigDecimalHumanReadableSerializerModule = SerializersModule { 34 | contextual(BigDecimal::class, BigDecimalHumanReadableSerializer) 35 | contextual(DecimalMode::class, DecimalModeSerializer) 36 | } 37 | -------------------------------------------------------------------------------- /bignum-serialization-kotlinx/src/commonMain/kotlin/com/ionspin/kotlin/bignum/serialization/kotlinx/biginteger/BigIntegerArraySerializer.kt: -------------------------------------------------------------------------------- 1 | package com.ionspin.kotlin.bignum.serialization.kotlinx.biginteger 2 | 3 | import com.ionspin.kotlin.bignum.integer.BigInteger 4 | import com.ionspin.kotlin.bignum.integer.Sign 5 | import kotlinx.serialization.ExperimentalSerializationApi 6 | import kotlinx.serialization.KSerializer 7 | import kotlinx.serialization.builtins.ArraySerializer 8 | import kotlinx.serialization.builtins.serializer 9 | import kotlinx.serialization.descriptors.SerialDescriptor 10 | import kotlinx.serialization.descriptors.buildClassSerialDescriptor 11 | import kotlinx.serialization.descriptors.element 12 | import kotlinx.serialization.descriptors.listSerialDescriptor 13 | import kotlinx.serialization.encoding.CompositeDecoder 14 | import kotlinx.serialization.encoding.Decoder 15 | import kotlinx.serialization.encoding.Encoder 16 | import kotlinx.serialization.encoding.decodeStructure 17 | import kotlinx.serialization.encoding.encodeStructure 18 | import kotlinx.serialization.modules.SerializersModule 19 | 20 | /** 21 | * Created by Ugljesa Jovanovic 22 | * ugljesa.jovanovic@ionspin.com 23 | * on 04-Jul-2021 24 | */ 25 | 26 | @OptIn(ExperimentalSerializationApi::class, ExperimentalUnsignedTypes::class) 27 | object BigIntegerArraySerializer : KSerializer { 28 | 29 | 30 | private val longArraySerializer = ArraySerializer(Long.serializer()) 31 | 32 | override val descriptor: SerialDescriptor = buildClassSerialDescriptor("BigIntegerArray") { 33 | element("magnitude", listSerialDescriptor(ULong.serializer().descriptor)) 34 | element("sign") 35 | } 36 | 37 | 38 | override fun serialize(encoder: Encoder, value: BigInteger) { 39 | val array = value.getBackingArrayCopy() 40 | encoder.encodeStructure(descriptor) { 41 | encodeSerializableElement(descriptor, 0, longArraySerializer, array.asLongArray().toTypedArray()) 42 | encodeStringElement(descriptor, 1, value.getSign().name) 43 | } 44 | 45 | } 46 | 47 | override fun deserialize(decoder: Decoder): BigInteger { 48 | return decoder.decodeStructure(descriptor) { 49 | var array = emptyArray() 50 | var signString = "" 51 | while (true) { 52 | when (val index = decodeElementIndex(descriptor)) { 53 | 0 -> array = decodeSerializableElement(descriptor, 0, longArraySerializer) 54 | 1 -> signString = decodeStringElement(descriptor, 1) 55 | CompositeDecoder.DECODE_DONE -> break 56 | else -> error("Unexpected index: $index") 57 | } 58 | } 59 | BigInteger.createFromWordArray(array.toLongArray().asULongArray(), Sign.valueOf(signString)) 60 | 61 | } 62 | 63 | } 64 | 65 | } 66 | 67 | val bigIntegerArraySerializerModule = SerializersModule { 68 | contextual(BigInteger::class, BigIntegerArraySerializer) 69 | } 70 | -------------------------------------------------------------------------------- /bignum-serialization-kotlinx/src/commonMain/kotlin/com/ionspin/kotlin/bignum/serialization/kotlinx/biginteger/BigIntegerHumanReadableSerializer.kt: -------------------------------------------------------------------------------- 1 | package com.ionspin.kotlin.bignum.serialization.kotlinx.biginteger 2 | 3 | import com.ionspin.kotlin.bignum.integer.BigInteger 4 | import kotlinx.serialization.KSerializer 5 | import kotlinx.serialization.descriptors.PrimitiveKind 6 | import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor 7 | import kotlinx.serialization.descriptors.SerialDescriptor 8 | import kotlinx.serialization.encoding.Decoder 9 | import kotlinx.serialization.encoding.Encoder 10 | import kotlinx.serialization.modules.SerializersModule 11 | 12 | /** 13 | * Created by Ugljesa Jovanovic 14 | * ugljesa.jovanovic@ionspin.com 15 | * on 04-Jul-2021 16 | * 17 | * Serializes big integer into base 10 human readable format. 18 | */ 19 | object BigIntegerHumanReadableSerializer : KSerializer { 20 | 21 | override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("BigInteger", PrimitiveKind.STRING) 22 | 23 | 24 | override fun serialize(encoder: Encoder, value: BigInteger) { 25 | encoder.encodeString(value.toString(10)) 26 | } 27 | 28 | override fun deserialize(decoder: Decoder): BigInteger { 29 | return BigInteger.parseString(decoder.decodeString(), 10) 30 | } 31 | 32 | } 33 | 34 | val bigIntegerhumanReadableSerializerModule = SerializersModule { 35 | contextual(BigInteger::class, BigIntegerHumanReadableSerializer) 36 | } 37 | -------------------------------------------------------------------------------- /bignum-serialization-kotlinx/src/commonTest/kotlin/com/ionspin/kotlin/bignum/serialization/kotlinx/QuickSetupTest.kt: -------------------------------------------------------------------------------- 1 | package com.ionspin.kotlin.bignum.serialization.kotlinx 2 | 3 | import com.ionspin.kotlin.bignum.decimal.BigDecimal 4 | import com.ionspin.kotlin.bignum.integer.BigInteger 5 | import kotlinx.serialization.Contextual 6 | import kotlinx.serialization.Serializable 7 | import kotlinx.serialization.decodeFromString 8 | import kotlinx.serialization.encodeToString 9 | import kotlinx.serialization.json.Json 10 | import kotlin.test.Test 11 | import kotlin.test.assertEquals 12 | 13 | /** 14 | * Created by Ugljesa Jovanovic 15 | * ugljesa.jovanovic@ionspin.com 16 | * on 11-Jul-2021 17 | */ 18 | class QuickSetupTest { 19 | 20 | val json = Json { 21 | serializersModule = arrayBasedSerializerModule // or humanReadableSerializerModule 22 | } 23 | 24 | @Serializable 25 | data class SomeDataHolder(@Contextual val bigInteger: BigInteger, @Contextual val bigDecimal: BigDecimal) 26 | 27 | @Test 28 | fun serializeAndDeserialize() { 29 | val bigInt = BigInteger.parseString("12345678901234567890") 30 | val bigDecimal = BigDecimal.parseString("1.234E-200") 31 | val someData = SomeDataHolder(bigInt, bigDecimal) 32 | val serialized = json.encodeToString(someData) 33 | println(serialized) 34 | val deserialized = json.decodeFromString(serialized) 35 | assertEquals(someData, deserialized) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /bignum-serialization-kotlinx/src/commonTest/kotlin/com/ionspin/kotlin/bignum/serialization/kotlinx/bigdecimal/BigDecimalArraySerializationTest.kt: -------------------------------------------------------------------------------- 1 | package com.ionspin.kotlin.bignum.serialization.kotlinx.bigdecimal 2 | 3 | import com.ionspin.kotlin.bignum.decimal.BigDecimal 4 | import com.ionspin.kotlin.bignum.decimal.DecimalMode 5 | import com.ionspin.kotlin.bignum.decimal.RoundingMode 6 | import kotlinx.serialization.Contextual 7 | import kotlinx.serialization.Serializable 8 | import kotlinx.serialization.decodeFromString 9 | import kotlinx.serialization.encodeToString 10 | import kotlinx.serialization.json.Json 11 | import kotlin.test.Test 12 | import kotlin.test.assertEquals 13 | 14 | /** 15 | * Created by Ugljesa Jovanovic 16 | * ugljesa.jovanovic@ionspin.com 17 | * on 04-Jul-2021 18 | */ 19 | class BigDecimalArraySerializationTest { 20 | 21 | @Test 22 | fun testSerialization() { 23 | val testBigDecimal = BigDecimal.parseString("1.000000000020000000000300000000004") 24 | val json = Json { 25 | serializersModule = bigDecimalArraySerializerModule 26 | } 27 | val serialized = json.encodeToString(testBigDecimal) 28 | println(serialized) 29 | val deserialized = json.decodeFromString(serialized) 30 | assertEquals(testBigDecimal, deserialized) 31 | } 32 | 33 | @Test 34 | fun testSerializationWithDecimalMode() { 35 | val withoutMode = BigDecimal.parseStringWithMode("1.000000000020000000000300000000004") 36 | val testBigDecimal = BigDecimal.fromBigDecimal(withoutMode, DecimalMode(13, RoundingMode.ROUND_HALF_TO_ODD)) 37 | val json = Json { 38 | serializersModule = bigDecimalArraySerializerModule 39 | } 40 | val serialized = json.encodeToString(testBigDecimal) 41 | println(serialized) 42 | val deserialized = json.decodeFromString(serialized) 43 | assertEquals(testBigDecimal, deserialized) 44 | } 45 | 46 | @Serializable 47 | data class BigDecimalArraySerializtionTestData(@Contextual val a : BigDecimal, @Contextual val b : BigDecimal) 48 | 49 | @Test 50 | fun testSomething() { 51 | val a = BigDecimal.parseString("1.000000000020000000000300000000004") 52 | val b = BigDecimal.parseString("-1.000000000020000000000300000000004") 53 | val testObject = BigDecimalArraySerializtionTestData(a, b) 54 | val json = Json { 55 | serializersModule = bigDecimalArraySerializerModule 56 | } 57 | val serialized = json.encodeToString(testObject) 58 | println(serialized) 59 | 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /bignum-serialization-kotlinx/src/commonTest/kotlin/com/ionspin/kotlin/bignum/serialization/kotlinx/bigdecimal/BigDecimalHumanReadableSerializationTest.kt: -------------------------------------------------------------------------------- 1 | package com.ionspin.kotlin.bignum.serialization.kotlinx.bigdecimal 2 | 3 | import com.ionspin.kotlin.bignum.decimal.BigDecimal 4 | import kotlinx.serialization.Contextual 5 | import kotlinx.serialization.Serializable 6 | import kotlinx.serialization.decodeFromString 7 | import kotlinx.serialization.encodeToString 8 | import kotlinx.serialization.json.Json 9 | import kotlin.test.Test 10 | import kotlin.test.assertEquals 11 | 12 | /** 13 | * Created by Ugljesa Jovanovic 14 | * ugljesa.jovanovic@ionspin.com 15 | * on 04-Jul-2021 16 | */ 17 | class BigDecimalHumanReadableSerializationTest { 18 | 19 | @Test 20 | fun testSerialization() { 21 | val testBigDecimal = BigDecimal.parseString("1.000000000020000000000300000000004") 22 | val json = Json { 23 | serializersModule = bigDecimalHumanReadableSerializerModule 24 | } 25 | val serialized = json.encodeToString(testBigDecimal) 26 | println(serialized) 27 | val deserialized = json.decodeFromString(serialized) 28 | assertEquals(testBigDecimal, deserialized) 29 | } 30 | 31 | @Serializable 32 | data class BigDecimalHumanReadableTestData(@Contextual val a : BigDecimal, @Contextual val b : BigDecimal) 33 | 34 | @Test 35 | fun testSomething() { 36 | val a = BigDecimal.parseString("1.000000000020000000000300000000004") 37 | val b = BigDecimal.parseString("-1.000000000020000000000300000000004") 38 | val testObject = BigDecimalHumanReadableTestData(a, b) 39 | val json = Json { 40 | serializersModule = bigDecimalHumanReadableSerializerModule 41 | } 42 | val serialized = json.encodeToString(testObject) 43 | println(serialized) 44 | 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /bignum-serialization-kotlinx/src/commonTest/kotlin/com/ionspin/kotlin/bignum/serialization/kotlinx/biginteger/BigIntegerArraySerializationTest.kt: -------------------------------------------------------------------------------- 1 | package com.ionspin.kotlin.bignum.serialization.kotlinx.biginteger 2 | 3 | import com.ionspin.kotlin.bignum.integer.BigInteger 4 | import kotlinx.serialization.Contextual 5 | import kotlinx.serialization.Serializable 6 | import kotlinx.serialization.decodeFromString 7 | import kotlinx.serialization.encodeToString 8 | import kotlinx.serialization.json.Json 9 | import kotlin.test.Test 10 | import kotlin.test.assertEquals 11 | 12 | /** 13 | * Created by Ugljesa Jovanovic 14 | * ugljesa.jovanovic@ionspin.com 15 | * on 04-Jul-2021 16 | */ 17 | class ArraySerializationTest { 18 | 19 | @Test 20 | fun testSerialization() { 21 | run { 22 | val testBigInteger = BigInteger.parseString("1000000000000000000000000000002000000000000000000000000000003") 23 | val json = Json { 24 | serializersModule = bigIntegerArraySerializerModule 25 | } 26 | val serialized = json.encodeToString(testBigInteger) 27 | println(serialized) 28 | val deserialized = json.decodeFromString(serialized) 29 | assertEquals(testBigInteger, deserialized) 30 | } 31 | run { 32 | val testBigInteger = BigInteger.parseString("-1000000000000000000000000000002000000000000000000000000000003") 33 | val json = Json { 34 | serializersModule = bigIntegerArraySerializerModule 35 | } 36 | val serialized = json.encodeToString(testBigInteger) 37 | println(serialized) 38 | val deserialized = json.decodeFromString(serialized) 39 | assertEquals(testBigInteger, deserialized) 40 | } 41 | 42 | } 43 | 44 | @Serializable 45 | data class BigIntegerArraySerializtionTestData(@Contextual val a : BigInteger, @Contextual val b : BigInteger) 46 | 47 | @Test 48 | fun testSomething() { 49 | val a = BigInteger.parseString("-1000000000000000000000000000002000000000000000000000000000003") 50 | val b = BigInteger.parseString("1000000000000000000000000000002000000000000000000000000000003") 51 | val testObject = BigIntegerArraySerializtionTestData(a, b) 52 | val json = Json { 53 | serializersModule = bigIntegerArraySerializerModule 54 | } 55 | val serialized = json.encodeToString(testObject) 56 | println(serialized) 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /bignum-serialization-kotlinx/src/commonTest/kotlin/com/ionspin/kotlin/bignum/serialization/kotlinx/biginteger/BigIntegerHumanReadableSerializationTest.kt: -------------------------------------------------------------------------------- 1 | package com.ionspin.kotlin.bignum.serialization.kotlinx.biginteger 2 | 3 | import com.ionspin.kotlin.bignum.integer.BigInteger 4 | import kotlinx.serialization.Contextual 5 | import kotlinx.serialization.Serializable 6 | import kotlinx.serialization.decodeFromString 7 | import kotlinx.serialization.encodeToString 8 | import kotlinx.serialization.json.Json 9 | import kotlin.test.Test 10 | import kotlin.test.assertEquals 11 | 12 | /** 13 | * Created by Ugljesa Jovanovic 14 | * ugljesa.jovanovic@ionspin.com 15 | * on 04-Jul-2021 16 | */ 17 | class BigIntegerHumanReadableSerializationTest { 18 | 19 | @Test 20 | fun testSerialization() { 21 | val testBigInteger = BigInteger.parseString("1000000000000000000000000000002000000000000000000000000000003") 22 | val json = Json { 23 | serializersModule = bigIntegerhumanReadableSerializerModule 24 | } 25 | val serialized = json.encodeToString(testBigInteger) 26 | println(serialized) 27 | val deserialized = json.decodeFromString(serialized) 28 | assertEquals(testBigInteger, deserialized) 29 | } 30 | 31 | @Serializable 32 | data class BigIntegerHumanReadableSerializtionTestData(@Contextual val a : BigInteger, @Contextual val b : BigInteger) 33 | 34 | @Test 35 | fun testSomething() { 36 | val a = BigInteger.parseString("-1000000000000000000000000000002000000000000000000000000000003") 37 | val b = BigInteger.parseString("1000000000000000000000000000002000000000000000000000000000003") 38 | val testObject = BigIntegerHumanReadableSerializtionTestData(a, b) 39 | val json = Json { 40 | serializersModule = bigIntegerhumanReadableSerializerModule 41 | } 42 | val serialized = json.encodeToString(testObject) 43 | println(serialized) 44 | 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/Endianness.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum 19 | 20 | /** 21 | * Created by Ugljesa Jovanovic 22 | * ugljesa.jovanovic@ionspin.com 23 | * on 22-Sep-2019 24 | */ 25 | enum class Endianness { 26 | BIG, LITTLE 27 | } 28 | -------------------------------------------------------------------------------- /bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/QuotientAndRemainder.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum 19 | 20 | import com.ionspin.kotlin.bignum.integer.BigInteger 21 | import com.ionspin.kotlin.bignum.modular.ModularBigInteger 22 | 23 | /** 24 | * Created by Ugljesa Jovanovic 25 | * ugljesa.jovanovic@ionspin.com 26 | * on 04-Apr-2019 27 | */ 28 | 29 | data class QuotientAndRemainder( 30 | val quotient: BigInteger, 31 | val remainder: BigInteger 32 | ) 33 | 34 | data class ModularQuotientAndRemainder(val quotient: ModularBigInteger, val remainder: ModularBigInteger) 35 | -------------------------------------------------------------------------------- /bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/decimal/DecimalMode.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.decimal 19 | 20 | /** 21 | * Created by Ugljesa Jovanovic 22 | * ugljesa.jovanovic@ionspin.com 23 | * on 23-Mar-2019 24 | */ 25 | 26 | enum class RoundingMode { 27 | /** 28 | * Towards negative infinity 29 | */ 30 | FLOOR, 31 | /** 32 | * Towards positive infinity 33 | */ 34 | CEILING, 35 | /** 36 | * Away from zero 37 | */ 38 | AWAY_FROM_ZERO, 39 | /** 40 | * Towards zero 41 | */ 42 | TOWARDS_ZERO, 43 | /** 44 | * Infinite decimalPrecision, and beyond 45 | */ 46 | NONE, 47 | /** 48 | * Round towards nearest integer, using away from zero as tie breaker when significant digit being rounded is 5 49 | */ 50 | ROUND_HALF_AWAY_FROM_ZERO, 51 | /** 52 | * Round towards nearest integer, using towards zero as tie breaker when significant digit being rounded is 5 53 | */ 54 | ROUND_HALF_TOWARDS_ZERO, 55 | /** 56 | * Round towards nearest integer, using towards infinity as tie breaker when significant digit being rounded is 5 57 | */ 58 | ROUND_HALF_CEILING, 59 | /** 60 | * Round towards nearest integer, using towards negative infinity as tie breaker when significant digit being rounded is 5 61 | */ 62 | ROUND_HALF_FLOOR, 63 | /** 64 | * Round towards nearest even integer 65 | */ 66 | ROUND_HALF_TO_EVEN, 67 | /** 68 | * Round towards neares odd integer 69 | */ 70 | ROUND_HALF_TO_ODD 71 | } 72 | 73 | /** 74 | * Decimal precision signifies how many digits will significand have. If decimal precision is 0 and RoundingMode is NONE 75 | * infinite precision is used. 76 | * @param decimalPrecision max number of digits allowed. Default 0 is unlimited precision. 77 | * @param roundingMode default RoundingMode.NONE is used with unlimited precision and no specified scale. 78 | * Otherwise specify mode that is used for rounding when decimalPrecision is exceeded, or when scale is in use. 79 | * @param scale is number of digits to the right of the decimal point. 80 | * When this is specified, a RoundingMode that is not RoundingMode.NONE is also required. 81 | * Scale cannot be greater than precision - 1. 82 | * If left to default = null, no scale will be used. Rounding and decimalPrecision apply. 83 | * Negative scale numbers are not supported. 84 | * Using scale will increase the precision to required number of digits. 85 | */ 86 | data class DecimalMode( 87 | val decimalPrecision: Long = 0, 88 | val roundingMode: RoundingMode = RoundingMode.NONE, 89 | val scale: Long = -1 90 | ) { 91 | 92 | init { 93 | if (decimalPrecision < 0) { 94 | throw ArithmeticException("Negative decimal precision is not allowed.") 95 | } 96 | } 97 | 98 | val isPrecisionUnlimited = decimalPrecision == 0L 99 | val usingScale = scale >= 0 100 | 101 | init { 102 | if (usingScale.not() && decimalPrecision == 0L && roundingMode != RoundingMode.NONE) { 103 | throw ArithmeticException("Rounding mode with 0 digits precision.") 104 | } 105 | if (scale < -1) { 106 | throw ArithmeticException("Negative Scale is unsupported.") 107 | } 108 | if (usingScale && roundingMode == RoundingMode.NONE) { 109 | throw ArithmeticException("Scale of $scale digits to the right of the decimal requires a RoundingMode that is not NONE.") 110 | } 111 | } 112 | 113 | companion object { 114 | /** 115 | * Default decimal mode, infinite precision, no rounding 116 | */ 117 | val DEFAULT = DecimalMode() 118 | 119 | /** 120 | * Example mode useful for US currency support with unlimited dollars, and no fractions of cents. 121 | * Note that prices, interest rate calculations, and lots of other usages of currency require 122 | * fractions of cents for accuracy, which requires a larger scale. 123 | * Arbitrarily chose really large precision 124 | */ 125 | val US_CURRENCY = DecimalMode(30, RoundingMode.ROUND_HALF_AWAY_FROM_ZERO, 2) 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/BigIntegerList63Arithmetic.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer 19 | 20 | /** 21 | * Interface defining big integer operations 22 | * 23 | * Created by Ugljesa Jovanovic 24 | * ugljesa.jovanovic@ionspin.com 25 | * on 10-Mar-2019 26 | */ 27 | 28 | interface BigIntegerList63Arithmetic { 29 | val ZERO: List 30 | val ONE: List 31 | val TWO: List 32 | val TEN: List 33 | 34 | val basePowerOfTwo: Int 35 | /** 36 | * Returns the number of leading zeros in a word 37 | */ 38 | fun numberOfLeadingZerosInAWord(value: ULong): Int 39 | 40 | /** 41 | * Number of bits needed to represent this number 42 | */ 43 | fun bitLength(value: List): Int 44 | 45 | /** 46 | * Number of consecutive zeros count from the right in binary representation 47 | */ 48 | fun trailingZeroBits(value: List): Int 49 | 50 | /** 51 | * Arithmetic shift left. Shifts the number to the left, by required places of bits, creating new words if necessary 52 | */ 53 | fun shiftLeft(operand: List, places: Int): List 54 | 55 | /** 56 | * Arithmetic shift right. Shifts the number to the right, by required places of bits, removing words that no longer relevant 57 | */ 58 | fun shiftRight(operand: List, places: Int): List 59 | 60 | /** 61 | * Compares two numbers 62 | * 63 | * @return -1 if first is bigger, 0 if equal, +1 if second is bigger 64 | */ 65 | fun compare(first: List, second: List): Int 66 | 67 | /** 68 | * Adds two big integers 69 | * @return result of add 70 | */ 71 | fun add(first: List, second: List): List 72 | 73 | /** 74 | * Subtracts two big integers 75 | * @return result of subtract 76 | */ 77 | fun subtract(first: List, second: List): List 78 | 79 | /** 80 | * Multiplies two big integers 81 | * @return result of multiply 82 | */ 83 | fun multiply(first: List, second: List): List 84 | 85 | /** 86 | * Divide two big integers 87 | * @return A pair representing quotient (first member of the pair) and remainder (second member of the pair) 88 | */ 89 | fun divide( 90 | first: List, 91 | second: List 92 | ): Pair, List> 93 | 94 | /** 95 | * Returns a integer reciprocal of this number such that 0 <= base ^ word - operand * reciprocal <= operand, 96 | * and remainder such that 0 < reciprocal < operand 97 | */ 98 | fun reciprocal(operand: List): Pair, List> 99 | 100 | /** 101 | * Exponentiation function 102 | * @return BigInteger result of exponentiation of number by exponent 103 | */ 104 | fun pow(base: List, exponent: Long): List 105 | 106 | fun sqrt(operand: List): Pair, List> 107 | 108 | fun gcd(first: List, second: List): List 109 | 110 | /** 111 | * Parse a string in a specific base into a big integer 112 | */ 113 | fun parseForBase(number: String, base: Int): List 114 | 115 | /** 116 | * return a string representation of big integer in a specific number base 117 | */ 118 | fun toString(operand: List, base: Int): String 119 | 120 | fun numberOfDecimalDigits(operand: List): Long 121 | 122 | fun fromULong(uLong: ULong): List 123 | fun fromUInt(uInt: UInt): List 124 | fun fromUShort(uShort: UShort): List 125 | fun fromUByte(uByte: UByte): List 126 | fun fromLong(long: Long): List 127 | fun fromInt(int: Int): List 128 | fun fromShort(short: Short): List 129 | fun fromByte(byte: Byte): List 130 | 131 | fun or(operand: List, mask: List): List 132 | fun xor(operand: List, mask: List): List 133 | fun and(operand: List, mask: List): List 134 | fun not(operand: List): List 135 | 136 | fun bitAt(operand: List, position: Long): Boolean 137 | fun setBitAt(operand: List, position: Long, bit: Boolean): List 138 | 139 | fun fromUByteArray(source: UByteArray): List 140 | fun fromByteArray(source: ByteArray): List 141 | 142 | fun toUByteArray(operand: List): UByteArray 143 | fun toByteArray(operand: List): ByteArray 144 | } 145 | -------------------------------------------------------------------------------- /bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/Configuration.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer 19 | 20 | import com.ionspin.kotlin.bignum.integer.base63.array.BigInteger63Arithmetic 21 | import kotlin.native.concurrent.SharedImmutable 22 | 23 | // import com.ionspin.kotlin.bignum.integer.base63.BigInteger63LinkedListArithmetic 24 | 25 | /** 26 | * Created by Ugljesa Jovanovic 27 | * ugljesa.jovanovic@ionspin.com 28 | * on 10-Mar-2019 29 | */ 30 | 31 | // typealias WordArray = List 32 | typealias WordArray = ULongArray 33 | 34 | typealias Word = ULong 35 | 36 | @SharedImmutable 37 | internal val chosenArithmetic: BigIntegerArithmetic = 38 | BigInteger63Arithmetic 39 | // internal val chosenArithmetic: BigIntegerArithmetic = BigInteger63LinkedListArithmetic 40 | 41 | // 42 | // object TypeHelper { 43 | // // val instance: WordArray = listOf() 44 | // val instance: WordArray = ulongArrayOf() 45 | // } 46 | -------------------------------------------------------------------------------- /bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/PlatformBigIntegerArithmetic.kt: -------------------------------------------------------------------------------- 1 | // /* 2 | // * Copyright 2019 Ugljesa Jovanovic 3 | // * 4 | // * Licensed under the Apache License, Version 2.0 (the "License"); 5 | // * you may not use this file except in compliance with the License. 6 | // * You may obtain a copy of the License at 7 | // * 8 | // * http://www.apache.org/licenses/LICENSE-2.0 9 | // * 10 | // * Unless required by applicable law or agreed to in writing, software 11 | // * distributed under the License is distributed on an "AS IS" BASIS, 12 | // * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // * See the License for the specific language governing permissions and 14 | // * limitations under the License. 15 | // * 16 | // */ 17 | // 18 | // package com.ionspin.kotlin.bignum.integer 19 | // 20 | // /** 21 | // * Created by Ugljesa Jovanovic 22 | // * ugljesa.jovanovic@ionspin.com 23 | // * on 10-Mar-2019 24 | // */ 25 | // 26 | // expect object PlatformBigIntegerArithmetic : BigIntegerArithmetic 27 | -------------------------------------------------------------------------------- /bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/PlatformWorkarounds.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer 19 | 20 | /** 21 | * Created by Ugljesa Jovanovic 22 | * ugljesa.jovanovic@ionspin.com 23 | * on 02-Jun-2019 24 | */ 25 | enum class Platform { 26 | JVM, NATIVE, JS, WASMJS 27 | } 28 | 29 | expect object RuntimePlatform { 30 | /** 31 | * We need to know if we are running on a platform that doesn't know how to tell decimal and integer apart. 32 | */ 33 | fun currentPlatform(): Platform 34 | } 35 | -------------------------------------------------------------------------------- /bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/Quadruple.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer 19 | 20 | /** 21 | * Created by Ugljesa Jovanovic 22 | * ugljesa.jovanovic@ionspin.com 23 | * on 09-Mar-2019 24 | */ 25 | data class Quadruple(val a: A, val b: B, val c: C, val d: D) 26 | 27 | data class Quintuple(val a: A, val b: B, val c: C, val d: D, val e: E) 28 | 29 | data class Sextuple(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F) 30 | -------------------------------------------------------------------------------- /bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/concurrent/Placeholder.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.concurrent 19 | 20 | /** 21 | * Created by Ugljesa Jovanovic 22 | * ugljesa.jovanovic@ionspin.com 23 | * on 17-Mar-2019 24 | */ 25 | suspend fun concurrentMultiply() { 26 | // if (BigInteger32Arithmetic.useCoroutines) { 27 | // val partialResults = second.mapIndexed { index, element -> 28 | // GlobalScope.async { 29 | // BigInteger32Arithmetic.multiply(first, element) shl (index * BigInteger32Arithmetic.basePowerOfTwo) 30 | // } 31 | // } 32 | // 33 | // 34 | // var result = uintArrayOf() 35 | // block { 36 | // partialResults.awaitAll() 37 | // result = partialResults.fold(UIntArray(0)) { acc, deferred -> 38 | // acc + (deferred.getCompleted()) 39 | // } 40 | // } 41 | // return result 42 | // } else { 43 | } 44 | -------------------------------------------------------------------------------- /bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/util/DigitUtil.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.util 19 | 20 | /** 21 | * Created by Ugljesa Jovanovic 22 | * ugljesa.jovanovic@ionspin.com 23 | * on 18-Mar-2019 24 | */ 25 | fun Char.toDigit(base: Int = 10): Int { 26 | // Check if number belongs to est used in base 27 | 28 | val digit = when (this) { 29 | in '0'..'9' -> (this - 48).toInt() 30 | in 'a'..'z' -> this - 'a' + 10 31 | in 'A'..'Z' -> this - 'A' + 10 32 | in '\uFF21'..'\uFF3A' -> this - '\uFF21' - 10 33 | in '\uFF41'..'\uFF5A' -> this - '\uFF41' - 10 34 | '.' -> throw NumberFormatException("Invalid digit for radix $this (Possibly a decimal value, which is not supported by BigInteger parser") 35 | else -> throw NumberFormatException("Invalid digit for radix $this") 36 | } 37 | if (digit < 0 || digit >= base) { 38 | throw NumberFormatException("$this is not a valid digit for number system with base $base") 39 | } 40 | return digit 41 | } 42 | -------------------------------------------------------------------------------- /bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/util/VariousUtil.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020. Ugljesa Jovanovic 3 | */ 4 | 5 | package com.ionspin.kotlin.bignum.integer.util 6 | 7 | /** 8 | * Created by Ugljesa Jovanovic 9 | * ugljesa.jovanovic@ionspin.com 10 | * on 17-Jun-2020 11 | */ 12 | fun Array.hexColumsPrint(chunk: Int = 16) { 13 | val printout = this.map { it.toString(16).padStart(2, '0') }.chunked(chunk) 14 | printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) } 15 | } 16 | 17 | fun UByteArray.hexColumsPrint(chunk: Int = 16) { 18 | val printout = this.map { it.toString(16).padStart(2, '0') }.chunked(chunk) 19 | printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) } 20 | } 21 | 22 | infix operator fun Char.times(count: Int): String { 23 | val stringBuilder = StringBuilder() 24 | for (i in 0 until count) { 25 | stringBuilder.append(this) 26 | } 27 | return stringBuilder.toString() 28 | } 29 | 30 | infix operator fun Char.times(count: Long): String { 31 | val stringBuilder = StringBuilder() 32 | for (i in 0 until count) { 33 | stringBuilder.append(this) 34 | } 35 | return stringBuilder.toString() 36 | } 37 | -------------------------------------------------------------------------------- /bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/modular/ModularBigIntegerExtensions.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.modular 19 | 20 | import com.ionspin.kotlin.bignum.BigNumber 21 | 22 | /** 23 | * Created by Ugljesa Jovanovic 24 | * ugljesa.jovanovic@ionspin.com 25 | * on 12-Aug-2019 26 | */ 27 | 28 | fun Long.toModularBigInteger(creator: BigNumber.Creator): ModularBigInteger { 29 | return creator.fromLong(this) 30 | } 31 | 32 | fun Int.toModularBigInteger(creator: BigNumber.Creator): ModularBigInteger { 33 | return creator.fromInt(this) 34 | } 35 | 36 | fun Short.toModularBigInteger(creator: BigNumber.Creator): ModularBigInteger { 37 | return creator.fromShort(this) 38 | } 39 | 40 | fun Byte.toModularBigInteger(creator: BigNumber.Creator): ModularBigInteger { 41 | return creator.fromByte(this) 42 | } 43 | 44 | fun String.toModularBigInteger(creator: BigNumber.Creator, base: Int = 10): ModularBigInteger { 45 | return creator.parseString(this, base) 46 | } 47 | 48 | // 49 | // -------- Basic type arithmetic operation extensions ------- 50 | // 51 | 52 | // --------- Addition ----------------- 53 | 54 | operator fun Long.plus(other: ModularBigInteger): ModularBigInteger { 55 | return this.toModularBigInteger(other.getCreator()) + other 56 | } 57 | 58 | operator fun Int.plus(other: ModularBigInteger): ModularBigInteger { 59 | return this.toModularBigInteger(other.getCreator()) + other 60 | } 61 | 62 | operator fun Short.plus(other: ModularBigInteger): ModularBigInteger { 63 | return this.toModularBigInteger(other.getCreator()) + other 64 | } 65 | 66 | operator fun Byte.plus(other: ModularBigInteger): ModularBigInteger { 67 | return this.toModularBigInteger(other.getCreator()) + other 68 | } 69 | 70 | // --------- Subtraction ----------------- 71 | 72 | operator fun Long.minus(other: ModularBigInteger): ModularBigInteger { 73 | return this.toModularBigInteger(other.getCreator()) - other 74 | } 75 | 76 | operator fun Int.minus(other: ModularBigInteger): ModularBigInteger { 77 | return this.toModularBigInteger(other.getCreator()) - other 78 | } 79 | 80 | operator fun Short.minus(other: ModularBigInteger): ModularBigInteger { 81 | return this.toModularBigInteger(other.getCreator()) - other 82 | } 83 | 84 | operator fun Byte.minus(other: ModularBigInteger): ModularBigInteger { 85 | return this.toModularBigInteger(other.getCreator()) - other 86 | } 87 | 88 | // --------- Multiplication ----------------- 89 | 90 | operator fun Long.times(other: ModularBigInteger): ModularBigInteger { 91 | return this.toModularBigInteger(other.getCreator()) * other 92 | } 93 | 94 | operator fun Int.times(other: ModularBigInteger): ModularBigInteger { 95 | return this.toModularBigInteger(other.getCreator()) * other 96 | } 97 | 98 | operator fun Short.times(other: ModularBigInteger): ModularBigInteger { 99 | return this.toModularBigInteger(other.getCreator()) * other 100 | } 101 | 102 | operator fun Byte.times(other: ModularBigInteger): ModularBigInteger { 103 | return this.toModularBigInteger(other.getCreator()) * other 104 | } 105 | 106 | // --------- Division ----------------- 107 | 108 | operator fun Long.div(other: ModularBigInteger): ModularBigInteger { 109 | return this.toModularBigInteger(other.getCreator()) / other 110 | } 111 | 112 | operator fun Int.div(other: ModularBigInteger): ModularBigInteger { 113 | return this.toModularBigInteger(other.getCreator()) / other 114 | } 115 | 116 | operator fun Short.div(other: ModularBigInteger): ModularBigInteger { 117 | return this.toModularBigInteger(other.getCreator()) / other 118 | } 119 | 120 | operator fun Byte.div(other: ModularBigInteger): ModularBigInteger { 121 | return this.toModularBigInteger(other.getCreator()) / other 122 | } 123 | 124 | // --------- Remainder ----------------- 125 | 126 | operator fun Long.rem(other: ModularBigInteger): ModularBigInteger { 127 | return this.toModularBigInteger(other.getCreator()) % other 128 | } 129 | 130 | operator fun Int.rem(other: ModularBigInteger): ModularBigInteger { 131 | return this.toModularBigInteger(other.getCreator()) % other 132 | } 133 | 134 | operator fun Short.rem(other: ModularBigInteger): ModularBigInteger { 135 | return this.toModularBigInteger(other.getCreator()) % other 136 | } 137 | 138 | operator fun Byte.rem(other: ModularBigInteger): ModularBigInteger { 139 | return this.toModularBigInteger(other.getCreator()) % other 140 | } 141 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/ArithmeticExtensions.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum 19 | 20 | import com.ionspin.kotlin.bignum.integer.base63.array.BigInteger63Arithmetic 21 | 22 | /** 23 | * Created by Ugljesa Jovanovic 24 | * ugljesa.jovanovic@ionspin.com 25 | * on 10-Mar-2019 26 | */ 27 | // TODO this breaks Kotln Native at the moment, since we are only releasing Array version, we'll hardcode it 28 | // Need to find a better solution than removal of zeroes at init time. 29 | 30 | fun ULongArray.removeLeadingZeroes(): ULongArray { 31 | // if ((TypeHelper.instance as Any) is ULongArray) { 32 | return BigInteger63Arithmetic.removeLeadingZeros(this) // as WordArray 33 | // } 34 | // if ((TypeHelper.instance as Any) is List<*>) { 35 | // return BigInteger63LinkedListArithmetic.removeLeadingZeros(this as List) as WordArray 36 | // } 37 | // throw RuntimeException("Invalid WordArray type") 38 | } 39 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/TestUtil.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum 19 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/WordTypealiasUtil.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum 19 | 20 | import com.ionspin.kotlin.bignum.integer.WordArray 21 | 22 | /** 23 | * Created by Ugljesa Jovanovic 24 | * ugljesa.jovanovic@ionspin.com 25 | * on 20-Oct-2019 26 | */ 27 | 28 | fun ULongArray.toProperType(): WordArray { 29 | // if ((TypeHelper.instance as Any) is ULongArray) { 30 | return this as WordArray 31 | // } 32 | // if ((TypeHelper.instance as Any) is List<*>) { 33 | // return this.toList() as WordArray 34 | // } 35 | // throw RuntimeException("Invalid WordArray type") 36 | } 37 | 38 | fun List.toProperType(): WordArray { 39 | // if ((TypeHelper.instance as Any) is ULongArray) { 40 | return this as WordArray 41 | // } 42 | // if ((TypeHelper.instance as Any) is List<*>) { 43 | // return this.toList() as WordArray 44 | // } 45 | // throw RuntimeException("Invalid WordArray type") 46 | } 47 | 48 | fun List.contentEquals(other: List): Boolean { 49 | return this == other 50 | } 51 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimalComparisonTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.decimal 19 | 20 | import kotlin.test.Test 21 | import kotlin.test.assertTrue 22 | 23 | /** 24 | * Created by Ugljesa Jovanovic 25 | * ugljesa.jovanovic@ionspin.com 26 | * on 10-Aug-2019 27 | */ 28 | 29 | class BigDecimalComparisonTest { 30 | @Test 31 | fun testDifferentPrecisionComparison() { 32 | val a = BigDecimal.fromIntWithExponent(200, 0) 33 | val b = BigDecimal.fromIntWithExponent(2, 0) 34 | assertTrue { a == b } 35 | } 36 | 37 | @Test 38 | fun testDifferentPrecisionComparison2() { 39 | val a = BigDecimal.fromIntWithExponent(2000000, 4) 40 | val b = BigDecimal.fromIntWithExponent(2000, 4) 41 | assertTrue { a == b } 42 | } 43 | 44 | @Test 45 | fun testDifferentPrecisionComparisonSamePrecision() { 46 | val a = BigDecimal.fromIntWithExponent(2000, 4) 47 | val b = BigDecimal.fromIntWithExponent(2000, 4) 48 | assertTrue { a == b } 49 | } 50 | 51 | @Test 52 | fun testHashCodeContract() { 53 | val a = BigDecimal.fromIntWithExponent(2000000, 4) 54 | val b = BigDecimal.fromIntWithExponent(2000, 4) 55 | assertTrue { a == b } 56 | assertTrue { a.hashCode() == b.hashCode() } 57 | } 58 | 59 | @Test 60 | fun testHashCodeContract2() { 61 | val a = BigDecimal.fromIntWithExponent(0, 4) 62 | val b = BigDecimal.fromIntWithExponent(0, 41) 63 | assertTrue { a == b } 64 | assertTrue { a.hashCode() == b.hashCode() } 65 | } 66 | 67 | @Test 68 | fun testHashCodeContractLong() { 69 | val a = BigDecimal.fromLongWithExponent(123400000000, -192) 70 | val b = BigDecimal.fromIntWithExponent(1234, -192) 71 | assertTrue { a == b } 72 | assertTrue { a.hashCode() == b.hashCode() } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimalDecimalModeTests.kt: -------------------------------------------------------------------------------- 1 | package com.ionspin.kotlin.bignum.decimal 2 | 3 | import kotlin.test.Test 4 | import kotlin.test.assertEquals 5 | import kotlin.test.assertFailsWith 6 | import kotlinx.coroutines.test.runTest 7 | 8 | class BigDecimalDecimalModeTests { 9 | 10 | @Test 11 | fun testInvalidPrecision() { 12 | assertFailsWith(ArithmeticException::class) { 13 | 1.toBigDecimal(decimalMode = DecimalMode(-1)) 14 | } 15 | } 16 | 17 | @Test 18 | fun testModePreservation() = runTest { 19 | run { 20 | val a = BigDecimal.fromInt(1, DecimalMode(scale = 5, roundingMode = RoundingMode.AWAY_FROM_ZERO)) 21 | val negated = -a 22 | assertEquals(a.decimalMode, negated.decimalMode) 23 | } 24 | 25 | run { 26 | val a = BigDecimal.fromInt(1, DecimalMode(scale = 5, roundingMode = RoundingMode.AWAY_FROM_ZERO)) 27 | val absolute = a.abs() 28 | assertEquals(a.decimalMode, absolute.decimalMode) 29 | } 30 | 31 | run { 32 | val a = BigDecimal.fromInt(1, DecimalMode(scale = 5, roundingMode = RoundingMode.AWAY_FROM_ZERO)) 33 | val negated = a.negate() 34 | assertEquals(a.decimalMode, negated.decimalMode) 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimalDivPowTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.decimal 19 | 20 | import kotlin.test.Test 21 | import kotlin.test.assertFailsWith 22 | import kotlin.test.assertTrue 23 | 24 | /** 25 | * Created by Ugljesa Jovanovic 26 | * ugljesa.jovanovic@ionspin.com 27 | * on 04-May-2019 28 | */ 29 | 30 | class BigDecimalDivPowTest { 31 | 32 | @Test 33 | fun testIntegerDivision() { 34 | assertTrue { 35 | val dividend = 4.123.toBigDecimal() 36 | val divisor = 2.toBigDecimal() 37 | val (quotient, remainder) = dividend divrem divisor 38 | 39 | quotient == 2.toBigDecimal() && remainder == 0.123.toBigDecimal() 40 | } 41 | } 42 | 43 | @Test 44 | fun testSpecificIntegerDivision() { 45 | assertTrue { 46 | val dividend = Short.MIN_VALUE.toBigDecimal() 47 | val divisor = 1.toBigDecimal() 48 | val (quotient, remainder) = dividend divrem divisor 49 | 50 | quotient == Short.MIN_VALUE.toBigDecimal() && remainder == 0.toBigDecimal() 51 | } 52 | } 53 | 54 | @Test 55 | fun testDecimalExponentiation() { 56 | assertTrue { 57 | val a = 2.toBigDecimal() 58 | val result = a.pow(2) 59 | result.compareTo(4) == 0 60 | } 61 | 62 | assertTrue { 63 | val a = 2.toBigDecimal() 64 | val result = a.pow(10) 65 | result.compareTo(1024) == 0 66 | } 67 | 68 | assertTrue { 69 | val a = 0.5.toBigDecimal() 70 | val result = a.pow(2) 71 | result.compareTo(0.25) == 0 72 | } 73 | 74 | assertTrue { 75 | val a = 4.toBigDecimal(decimalMode = DecimalMode(decimalPrecision = 10, roundingMode = RoundingMode.FLOOR)) 76 | val result = a.pow(-1) 77 | result.compareTo(0.25) == 0 78 | } 79 | 80 | assertTrue { 81 | val a = 4.toBigDecimal(decimalMode = DecimalMode(decimalPrecision = 10, roundingMode = RoundingMode.FLOOR)) 82 | val result = a.pow(-2) 83 | result.compareTo(1f / 16) == 0 84 | } 85 | 86 | assertFailsWith(ArithmeticException::class) { 87 | val a = 0.toBigDecimal() 88 | val result = a.pow(-1) 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimalFactorialTest.kt: -------------------------------------------------------------------------------- 1 | package com.ionspin.kotlin.bignum.decimal 2 | 3 | import com.ionspin.kotlin.bignum.integer.BigInteger 4 | import com.ionspin.kotlin.bignum.integer.toBigInteger 5 | import kotlin.test.Test 6 | import kotlin.test.assertEquals 7 | 8 | class BigDecimalFactorialTest { 9 | 10 | @Test 11 | fun factorialZeroTest() { 12 | assertEquals( 13 | BigInteger.parseString("1"), 14 | 0.toBigInteger().factorial() 15 | ) 16 | } 17 | 18 | @Test 19 | fun factorialOneTest() { 20 | assertEquals( 21 | BigInteger.parseString("1"), 22 | 1.toBigInteger().factorial() 23 | ) 24 | } 25 | 26 | @Test 27 | fun factorialNegativeOneTest() { 28 | assertEquals( 29 | BigInteger.parseString("-1"), 30 | (-1).toBigInteger().factorial() 31 | ) 32 | } 33 | 34 | @Test 35 | fun factorialNegativeFiveTest() { 36 | assertEquals( 37 | BigInteger.parseString("-120"), 38 | (-5).toBigInteger().factorial() 39 | ) 40 | } 41 | 42 | @Test 43 | fun factorialTest() { 44 | assertEquals( 45 | BigInteger.parseString("2432902008176640000"), 46 | 20.toBigInteger().factorial() 47 | ) 48 | } 49 | 50 | @Test 51 | fun factorialBigTest() { 52 | assertEquals( 53 | BigInteger.parseString("30414093201713378043612608166064768844377641568960512000000000000"), 54 | 50.toBigInteger().factorial() 55 | ) 56 | } 57 | 58 | @Test 59 | fun factorialVeryBigTest() { 60 | assertEquals( 61 | BigInteger.parseString("281710411438055027694947944226061159480056634330574206405101912752560026159795933451040286452340924018275123200000000000000000000"), 62 | 85.toBigInteger().factorial() 63 | ) 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/integer/BitwiseTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.arithmetic 19 | 20 | import com.ionspin.kotlin.bignum.integer.BigInteger 21 | import com.ionspin.kotlin.bignum.integer.toBigInteger 22 | import kotlin.test.Test 23 | import kotlin.test.assertEquals 24 | import kotlin.test.assertTrue 25 | 26 | /** 27 | * Created by Ugljesa Jovanovic 28 | * ugljesa.jovanovic@ionspin.com 29 | * on 23-Mar-2019 30 | */ 31 | 32 | class BitwiseTest { 33 | 34 | @Test 35 | fun testNegate() { 36 | val bigInt = BigInteger.fromInt(123) 37 | val negatedBigInt = -bigInt 38 | val expectedBigInt = BigInteger.fromInt(-123) 39 | 40 | assertTrue { negatedBigInt == expectedBigInt } 41 | } 42 | 43 | @Test 44 | fun numberOfDigitsTest() { 45 | val bigInteger = BigInteger.parseString("123456789012345678901234567890123456", 10) 46 | val numberOfDigits = bigInteger.numberOfDecimalDigits() 47 | val expectedDigits = 36L 48 | assertTrue { numberOfDigits == expectedDigits } 49 | } 50 | 51 | @Test 52 | fun numberOfDigitsBigTest() { 53 | var bigInteger = BigInteger.ONE 54 | for (i in 1..200L) { 55 | val numberOfDigits = bigInteger.numberOfDecimalDigits() 56 | bigInteger = bigInteger * 10 57 | assertTrue { numberOfDigits == i } 58 | } 59 | } 60 | 61 | @Test 62 | fun testBitAt() { 63 | assertTrue { 64 | val bigInt = 2.toBigInteger() 65 | bigInt.bitAt(0) == false && bigInt.bitAt(1) == true 66 | } 67 | assertTrue { 68 | val bigInt = Long.MAX_VALUE.toBigInteger() + 1 69 | (bigInt.bitAt(62) == false) && (bigInt.bitAt(63) == true) 70 | } 71 | assertTrue { 72 | val bigInt = ULong.MAX_VALUE.toBigInteger() + 1 73 | bigInt.bitAt(63) == false && bigInt.bitAt(64) == true 74 | } 75 | 76 | assertTrue { 77 | val bigInt = Long.MAX_VALUE.toBigInteger() 78 | (0..62L).fold(true) { acc, i -> 79 | acc and bigInt.bitAt(i) 80 | } 81 | } 82 | } 83 | 84 | @Test 85 | fun setBitAtTest() { 86 | assertTrue { 87 | val bigInt = BigInteger.fromInt(4) 88 | val result = bigInt.setBitAt(1, true) 89 | result == 6.toBigInteger() 90 | } 91 | 92 | assertTrue { 93 | val bigInt = BigInteger.fromInt(6) 94 | val result = bigInt.setBitAt(1, false) 95 | result == 4.toBigInteger() 96 | } 97 | 98 | assertTrue { 99 | val bigInt = BigInteger.fromLong(Long.MAX_VALUE) 100 | val result = bigInt.setBitAt(32, false) 101 | result == (Long.MAX_VALUE.toBigInteger() - 2.toBigInteger().pow(32)) 102 | } 103 | 104 | assertTrue { 105 | val bigInt = BigInteger.fromLong(Long.MAX_VALUE) - 2.toBigInteger().pow(32) 106 | val result = bigInt.setBitAt(32, true) 107 | result == (Long.MAX_VALUE.toBigInteger()) 108 | } 109 | } 110 | 111 | @Test 112 | fun bitLengthTestSimple() { 113 | assertTrue { 114 | val bigInt = BigInteger.fromInt(0) 115 | val result = bigInt.bitLength() 116 | result == 0 117 | } 118 | 119 | assertTrue { 120 | val bigInt = BigInteger.fromInt(1) 121 | val result = bigInt.bitLength() 122 | result == 1 123 | } 124 | 125 | assertTrue { 126 | val bigInt = BigInteger.fromInt(2) 127 | val result = bigInt.bitLength() 128 | result == 2 129 | } 130 | 131 | assertTrue { 132 | val bigInt = BigInteger.fromInt(4) 133 | val result = bigInt.bitLength() 134 | result == 3 135 | } 136 | } 137 | 138 | @Test 139 | fun bitLengthTest() { 140 | for (i in 0..4096) { 141 | val bigInt = BigInteger.ONE.shl(i) 142 | val bitLength = bigInt.bitLength() 143 | assertEquals(i + 1, bitLength) 144 | } 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/integer/DebugTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer 19 | 20 | /** 21 | * Created by Ugljesa Jovanovic 22 | * ugljesa.jovanovic@ionspin.com 23 | * on 24-Sep-2019 24 | */ 25 | 26 | class DebugTest 27 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/integer/MajorSystemErrorTest.kt: -------------------------------------------------------------------------------- 1 | package com.ionspin.kotlin.bignum.integer 2 | 3 | import kotlin.test.Test 4 | import kotlin.test.assertFalse 5 | 6 | /** 7 | * Procrastinating 8 | */ 9 | class MajorSystemErrorTest { 10 | private fun youThink(block: () -> Boolean) = assertFalse(block = block) 11 | 12 | private infix fun BigInteger.makes(second: BigInteger): Boolean = this == second 13 | private infix fun BigInteger.plus(second: BigInteger): BigInteger = this.plus(second) 14 | 15 | private val ONE = BigInteger.ONE 16 | private val SEVENSEVENSEVEN = 777.toBigInteger() 17 | private val TWO = BigInteger.TWO 18 | 19 | @Test 20 | fun canISeeAMajorSystemErrorInYou() { 21 | youThink { 22 | ONE plus SEVENSEVENSEVEN makes TWO 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/integer/ParsingAndToStringTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer 19 | 20 | import com.ionspin.kotlin.bignum.toProperType 21 | import kotlin.random.Random 22 | import kotlin.test.Test 23 | import kotlin.test.assertTrue 24 | 25 | /** 26 | * Created by Ugljesa Jovanovic 27 | * ugljesa.jovanovic@ionspin.com 28 | * on 16-Mar-2019 29 | */ 30 | 31 | class ParsingAndToStringTest { 32 | val seed = 1 33 | val random = Random(1) 34 | 35 | @Test 36 | fun testParsing() { 37 | parsingSingleTest("1234", 10) 38 | } 39 | 40 | fun parsingSingleTest(textNumber: String, base: Int) { 41 | assertTrue { 42 | val parsed = BigInteger.parseString(textNumber, base) 43 | val printed = parsed.toString(base) 44 | print(printed) 45 | 46 | textNumber == printed 47 | } 48 | } 49 | 50 | @Test 51 | fun toStringTest() { 52 | val bigInt = ulongArrayOf( 53 | 2357127997678045786UL, 54 | 9223372036854775806UL, 55 | 6618565566930092031UL, 56 | 3482571707102756671UL, 57 | 5561215897725336065UL, 58 | 7121810967379087079UL, 59 | 2244066548420960617UL, 60 | 2014538293531722329UL, 61 | 492133570377UL, 62 | 0UL 63 | ) 64 | val parsed = BigInteger.fromWordArray(bigInt.toProperType(), Sign.POSITIVE) 65 | parsed.toString() 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/integer/arithmetic/BigInteger32ArithmeticAdditionTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer 19 | 20 | import com.ionspin.kotlin.bignum.integer.base32.BigInteger32Arithmetic 21 | import kotlin.test.Test 22 | import kotlin.test.assertTrue 23 | 24 | /** 25 | * Created by Ugljesa Jovanovic 26 | * ugljesa.jovanovic@ionspin.com 27 | * on 09-Mar-2019 28 | */ 29 | 30 | class BigInteger32ArithmeticAdditionTest { 31 | 32 | @Test 33 | fun testAddition() { 34 | assertTrue { 35 | val a = uintArrayOf(1U) 36 | val b = uintArrayOf(2U) 37 | val c = BigInteger32Arithmetic.add(a, b) 38 | c[0] == 3U 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/integer/arithmetic/BigInteger32ArithmeticSubtractionTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer 19 | 20 | import com.ionspin.kotlin.bignum.integer.base32.BigInteger32Arithmetic 21 | import kotlin.test.Test 22 | import kotlin.test.assertTrue 23 | 24 | /** 25 | * Created by Ugljesa Jovanovic 26 | * ugljesa.jovanovic@ionspin.com 27 | * on 09-Mar-2019 28 | */ 29 | 30 | class BigInteger32ArithmeticSubtractionTest { 31 | 32 | @Test 33 | fun testAddition() { 34 | assertTrue { 35 | val a = uintArrayOf(10U, 20U) 36 | val b = uintArrayOf(15U, 5U) 37 | val c = BigInteger32Arithmetic.subtract(a, b) 38 | c[1] == 14U 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/integer/arithmetic/BigInteger32BitWiseTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.arithmetic 19 | 20 | import com.ionspin.kotlin.bignum.integer.base63.array.BigInteger63Arithmetic 21 | import kotlin.test.Ignore 22 | import kotlin.test.Test 23 | import kotlin.test.assertTrue 24 | 25 | /** 26 | * Created by Ugljesa Jovanovic 27 | * ugljesa.jovanovic@ionspin.com 28 | * on 24-Mar-2019 29 | */ 30 | 31 | class BigInteger32BitWiseTest { 32 | 33 | @Ignore 34 | @Test 35 | fun trailingZeroBitsTest() { 36 | assertTrue { 37 | val a = ulongArrayOf(64U) 38 | val count = BigInteger63Arithmetic.trailingZeroBits(a) 39 | count == 7 40 | } 41 | 42 | assertTrue { 43 | val a = ulongArrayOf(0U, 64U) 44 | val count = BigInteger63Arithmetic.trailingZeroBits(a) 45 | count == 70 46 | } 47 | 48 | assertTrue { 49 | val a = ulongArrayOf(0U, 64U, 0U) 50 | val count = BigInteger63Arithmetic.trailingZeroBits(a) 51 | count == 70 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/integer/arithmetic/DivisionBenchmark.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.arithmetic 19 | 20 | /** 21 | * Created by Ugljesa Jovanovic 22 | * ugljesa.jovanovic@ionspin.com 23 | * on 20-Apr-2019 24 | */ 25 | 26 | class DivisionBenchmark { 27 | 28 | // fun divideUsingReciprocal(dividend : ULongArray, divisor : ULongArray, expectedQuotient : ULongArray, expectedRemainder : ULongArray) { 29 | // val result = BigInteger63Arithmetic.reciprocalDivision(dividend, divisor) 30 | // } 31 | } 32 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/integer/arithmetic/bigint63/BigInteger63AdditionTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.arithmetic.bigint63 19 | 20 | import com.ionspin.kotlin.bignum.integer.base63.array.BigInteger63Arithmetic 21 | import kotlin.test.Test 22 | import kotlin.test.assertTrue 23 | 24 | /** 25 | * Created by Ugljesa Jovanovic 26 | * ugljesa.jovanovic@ionspin.com 27 | * on 20-Oct-2019 28 | */ 29 | 30 | class BigInteger63AdditionTest { 31 | @Test 32 | fun testAddition() { 33 | assertTrue { 34 | val a = ulongArrayOf(10U, 20U) 35 | val b = ulongArrayOf(15U, 5U) 36 | val c = BigInteger63Arithmetic.add(a, b) 37 | c[0] == 25UL && c[1] == 25UL 38 | } 39 | } 40 | 41 | @Test 42 | fun testAdditionWithLeadingZeros() { 43 | assertTrue { 44 | val a = ulongArrayOf(10U, 20U, 0U, 0U) 45 | val b = ulongArrayOf(15U, 5U, 0U, 0U, 0U) 46 | val c = BigInteger63Arithmetic.add(a, b) 47 | c[0] == 25UL && c[1] == 25UL 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/integer/arithmetic/bigint63/BigInteger63ArithmeticComparison.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.arithmetic.bigint63 19 | 20 | import com.ionspin.kotlin.bignum.integer.base63.array.BigInteger63Arithmetic 21 | import kotlin.test.Test 22 | import kotlin.test.assertTrue 23 | 24 | /** 25 | * Created by Ugljesa Jovanovic 26 | * ugljesa.jovanovic@ionspin.com 27 | * on 20-Oct-2019 28 | */ 29 | 30 | class BigInteger63ArithmeticComparison { 31 | @Test 32 | fun testComparisonWithLeadingZeros() { 33 | val first = ulongArrayOf(1U, 0U, 0U) 34 | val second = ulongArrayOf(2U, 0U, 0U) 35 | val comparison = BigInteger63Arithmetic.compare(first, second) 36 | assertTrue { comparison == -1 } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/integer/arithmetic/bigint63/BigInteger63BitwiseTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.arithmetic.bigint63 19 | 20 | import com.ionspin.kotlin.bignum.integer.base63.array.BigInteger63Arithmetic 21 | import kotlin.test.Test 22 | import kotlin.test.assertTrue 23 | 24 | /** 25 | * Created by Ugljesa Jovanovic 26 | * ugljesa.jovanovic@ionspin.com 27 | * on 17-Oct-2019 28 | */ 29 | 30 | class BigInteger63BitwiseTest { 31 | @Test 32 | fun testShiftLeft() { 33 | assertTrue { 34 | val a = ulongArrayOf(1U) 35 | val expected = ulongArrayOf(0U, 1U) 36 | val result = BigInteger63Arithmetic.shiftLeft(a, 63) 37 | expected.contentEquals(result) 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/integer/arithmetic/bigint63/BigInteger63MultiplicationTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.arithmetic.bigint63 19 | 20 | import com.ionspin.kotlin.bignum.integer.base63.array.BigInteger63Arithmetic 21 | import kotlin.test.Test 22 | import kotlin.test.assertTrue 23 | 24 | /** 25 | * Created by Ugljesa Jovanovic 26 | * ugljesa.jovanovic@ionspin.com 27 | * on 17-Oct-2019 28 | */ 29 | 30 | class BigInteger63MultiplicationTest { 31 | @Test 32 | fun testToomCook3() { 33 | assertTrue { 34 | val a = ulongArrayOf(0U, 0U, 1U) 35 | val b = ulongArrayOf(0U, 0U, 1U) 36 | val result = BigInteger63Arithmetic.toomCook3Multiply(a, b) 37 | val basecaseMultiply = BigInteger63Arithmetic.basecaseMultiply(a, b) 38 | result.contentEquals(basecaseMultiply) 39 | } 40 | 41 | assertTrue { 42 | val a = ulongArrayOf((0UL - 1UL) shr 1, (0UL - 1UL) shr 1, (0UL - 1UL) shr 1) 43 | val b = ulongArrayOf((0UL - 1UL) shr 1, (0UL - 1UL) shr 1, (0UL - 1UL) shr 1) 44 | val result = BigInteger63Arithmetic.toomCook3Multiply(a, b) 45 | val basecaseMultiply = BigInteger63Arithmetic.basecaseMultiply(a, b) 46 | // println(BigInteger63Arithmetic.toString(result, 10)) 47 | // println(BigInteger63Arithmetic.toString(basecaseMultiply, 10)) 48 | result.contentEquals(basecaseMultiply) 49 | } 50 | 51 | assertTrue { 52 | val a = ulongArrayOf(1U, 1U, 1U) 53 | val b = ulongArrayOf(1U, 0U, 0U) 54 | val result = BigInteger63Arithmetic.toomCook3Multiply(a, b) 55 | val basecaseMultiply = BigInteger63Arithmetic.basecaseMultiply(a, b) 56 | result.contentEquals(BigInteger63Arithmetic.removeLeadingZeros(basecaseMultiply)) 57 | } 58 | 59 | assertTrue { 60 | val a = ulongArrayOf(1U, 2U, 3U, 4U, 5U, 6U) 61 | val b = ulongArrayOf(1U, 2U, 3U, 4U, 5U, 6U) 62 | val result = BigInteger63Arithmetic.toomCook3Multiply(a, b) 63 | val basecaseMultiply = BigInteger63Arithmetic.basecaseMultiply(a, b) 64 | result.contentEquals(basecaseMultiply) 65 | } 66 | 67 | assertTrue { 68 | val a = ulongArrayOf(0U, 0U, 0U, 1U) 69 | val b = ulongArrayOf(0U, 0U, 0U, 1U) 70 | val result = BigInteger63Arithmetic.toomCook3Multiply(a, b) 71 | val basecaseMultiply = BigInteger63Arithmetic.basecaseMultiply(a, b) 72 | result.contentEquals(basecaseMultiply) 73 | } 74 | 75 | assertTrue { 76 | val a = ulongArrayOf(100U, 200U, 300U, 50U) 77 | val b = ulongArrayOf(301U, 201U, 101U, 40U) 78 | val result = BigInteger63Arithmetic.toomCook3Multiply(a, b) 79 | val basecaseMultiply = BigInteger63Arithmetic.basecaseMultiply(a, b) 80 | result.contentEquals(basecaseMultiply) 81 | } 82 | } 83 | 84 | @Test 85 | fun testKaratsubaSimple() { 86 | assertTrue { 87 | val a = ulongArrayOf(100UL, 200UL, 300UL, 50UL) 88 | val b = ulongArrayOf(301UL, 201UL, 101UL, 40UL) 89 | val result = BigInteger63Arithmetic.karatsubaMultiply(a, b) 90 | val basecaseMultiply = BigInteger63Arithmetic.basecaseMultiply(a, b) 91 | result.contentEquals(basecaseMultiply) 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/integer/arithmetic/bigint63/BigInteger63SubtractionTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.arithmetic.bigint63 19 | 20 | import com.ionspin.kotlin.bignum.integer.base63.array.BigInteger63Arithmetic 21 | import kotlin.test.Test 22 | import kotlin.test.assertTrue 23 | 24 | /** 25 | * Created by Ugljesa Jovanovic 26 | * ugljesa.jovanovic@ionspin.com 27 | * on 20-Oct-2019 28 | */ 29 | 30 | class BigInteger63SubtractionTest { 31 | @Test 32 | fun testAddition() { 33 | assertTrue { 34 | val a = ulongArrayOf(10U, 20U) 35 | val b = ulongArrayOf(15U, 5U) 36 | val c = BigInteger63Arithmetic.subtract(a, b) 37 | c[1] == 14UL 38 | } 39 | } 40 | 41 | @Test 42 | fun testAdditionWithLeadingZeros() { 43 | assertTrue { 44 | val a = ulongArrayOf(10U, 20U, 0U, 0U) 45 | val b = ulongArrayOf(15U, 5U, 0U, 0U, 0U) 46 | val c = BigInteger63Arithmetic.subtract(a, b) 47 | c[1] == 14UL 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/integer/integer/BigIntegerBitwiseOperations.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.integer 19 | 20 | import com.ionspin.kotlin.bignum.integer.BigInteger 21 | import kotlin.test.Test 22 | import kotlin.test.assertEquals 23 | 24 | /** 25 | * Created by Ugljesa Jovanovic 26 | * ugljesa.jovanovic@ionspin.com 27 | * on 01-Nov-2019 28 | */ 29 | class BigIntegerBitwiseOperations { 30 | @Test 31 | fun andWithZero() { 32 | val operand = BigInteger.parseString("11110000", 2) 33 | val mask = BigInteger.ZERO 34 | 35 | assertEquals(mask, operand and mask) 36 | assertEquals(mask, mask and operand) 37 | } 38 | 39 | @Test 40 | fun andBiggerThanLongMaxWithZero() { 41 | val operand = BigInteger.parseString("9223372036854775808", 10) 42 | val mask = BigInteger.ZERO 43 | 44 | assertEquals(mask, operand and mask) 45 | assertEquals(mask, mask and operand) 46 | } 47 | 48 | @Test 49 | fun orWithZero() { 50 | val operand = BigInteger.parseString("11110000", 2) 51 | val mask = BigInteger.ZERO 52 | 53 | assertEquals(operand, operand or mask) 54 | assertEquals(operand, mask or operand) 55 | } 56 | 57 | @Test 58 | fun orBiggerThanLongMaxWithZero() { 59 | val operand = BigInteger.parseString("9223372036854775808", 10) 60 | val mask = BigInteger.ZERO 61 | 62 | assertEquals(operand, operand or mask) 63 | assertEquals(operand, mask or operand) 64 | } 65 | 66 | @Test 67 | fun xorWithZero() { 68 | val operand = BigInteger.parseString("11110000", 2) 69 | val mask = BigInteger.ZERO 70 | val xorResult = operand xor mask 71 | println("Xor result: ${xorResult.toString(2)}") 72 | 73 | assertEquals(operand, xorResult) 74 | assertEquals(operand, mask xor operand) 75 | } 76 | 77 | @Test 78 | fun xorBiggerThanLongMaxWithZero() { 79 | val operand = BigInteger.parseString("9223372036854775808", 10) 80 | val mask = BigInteger.ZERO 81 | 82 | assertEquals(operand, operand xor mask) 83 | assertEquals(operand, mask xor operand) 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/integer/integer/util/EndianessTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020. Ugljesa Jovanovic 3 | */ 4 | 5 | package com.ionspin.kotlin.bignum.integer.integer.util 6 | 7 | import com.ionspin.kotlin.bignum.integer.util.mirrorBytes 8 | import kotlin.test.Test 9 | import kotlin.test.assertTrue 10 | 11 | /** 12 | * Created by Ugljesa Jovanovic 13 | * ugljesa.jovanovic@ionspin.com 14 | * on 14-Jul-2020 15 | */ 16 | class EndianessTest { 17 | 18 | @Test 19 | fun testBigToLittle() { 20 | assertTrue { 21 | val source = ubyteArrayOf( 22 | 0x00U, 0x11U, 0x22U, 0x33U, 0x44U, 0x55U, 0x66U, 0x77U, 0x88U, 0x99U, 0xAAU, 0xBBU, 0xCCU, 0xDDU, 0xEEU, 0xFFU 23 | ) 24 | val expected = ubyteArrayOf( 25 | 0x33U, 0x22U, 0x11U, 0x00U, 0x44U, 0x55U, 0x66U, 0x77U, 0x88U, 0x99U, 0xAAU, 0xBBU, 0xCCU, 0xDDU, 0xEEU, 0xFFU 26 | ) 27 | val target = source.copyOf() 28 | mirrorBytes(source, 0, 4, target, 0) 29 | expected.contentEquals(target) 30 | } 31 | 32 | assertTrue { 33 | val source = ubyteArrayOf( 34 | 0x00U, 0x11U, 0x22U, 0x33U, 0x44U, 0x55U, 0x66U, 0x77U, 0x88U, 0x99U, 0xAAU, 0xBBU, 0xCCU, 0xDDU, 0xEEU, 0xFFU 35 | ) 36 | val expected = ubyteArrayOf( 37 | 0x00U, 0x11U, 0x22U, 0x33U, 0xBBU, 0xAAU, 0x99U, 0x88U, 0x77U, 0x66U, 0x55U, 0x44U, 0xCCU, 0xDDU, 0xEEU, 0xFFU 38 | ) 39 | val target = source.copyOf() 40 | mirrorBytes(source, 4, 12, target, 4) 41 | expected.contentEquals(target) 42 | } 43 | 44 | assertTrue { 45 | val source = ubyteArrayOf( 46 | 0x00U, 0x11U, 0x22U, 0x33U, 0x44U, 0x55U, 0x66U, 0x77U, 0x88U, 0x99U, 0xAAU, 0xBBU, 0xCCU, 0xDDU, 0xEEU, 0xFFU 47 | ) 48 | val expected = ubyteArrayOf( 49 | 0xBBU, 0xAAU, 0x99U, 0x88U, 0x77U, 0x66U, 0x55U, 0x44U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U 50 | ) 51 | val target = UByteArray(16) 52 | mirrorBytes(source, 4, 12, target, 0) 53 | expected.contentEquals(target) 54 | } 55 | } 56 | 57 | @Test 58 | fun testLittleToBig() { 59 | assertTrue { 60 | val source = ubyteArrayOf( 61 | 0x33U, 0x22U, 0x11U, 0x00U, 0x44U, 0x55U, 0x66U, 0x77U, 0x88U, 0x99U, 0xAAU, 0xBBU, 0xCCU, 0xDDU, 0xEEU, 0xFFU 62 | ) 63 | val expected = ubyteArrayOf( 64 | 0x00U, 0x11U, 0x22U, 0x33U, 0x44U, 0x55U, 0x66U, 0x77U, 0x88U, 0x99U, 0xAAU, 0xBBU, 0xCCU, 0xDDU, 0xEEU, 0xFFU 65 | ) 66 | val target = source.copyOf() 67 | mirrorBytes(source, 0, 4, target, 0) 68 | expected.contentEquals(target) 69 | } 70 | 71 | assertTrue { 72 | val source = ubyteArrayOf( 73 | 0x00U, 0x11U, 0x22U, 0x33U, 0xBBU, 0xAAU, 0x99U, 0x88U, 0x77U, 0x66U, 0x55U, 0x44U, 0xCCU, 0xDDU, 0xEEU, 0xFFU 74 | ) 75 | val expected = ubyteArrayOf( 76 | 0x00U, 0x11U, 0x22U, 0x33U, 0x44U, 0x55U, 0x66U, 0x77U, 0x88U, 0x99U, 0xAAU, 0xBBU, 0xCCU, 0xDDU, 0xEEU, 0xFFU 77 | ) 78 | val target = source.copyOf() 79 | mirrorBytes(source, 4, 12, target, 4) 80 | expected.contentEquals(target) 81 | } 82 | 83 | assertTrue { 84 | val source = ubyteArrayOf( 85 | 0x00U, 0x11U, 0x22U, 0x33U, 0x44U, 0x55U, 0x66U, 0x77U, 0x88U, 0x99U, 0xAAU, 0xBBU, 0xCCU, 0xDDU, 0xEEU, 0xFFU 86 | ) 87 | val expected = ubyteArrayOf( 88 | 0xBBU, 0xAAU, 0x99U, 0x88U, 0x77U, 0x66U, 0x55U, 0x44U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U 89 | ) 90 | val target = UByteArray(16) 91 | mirrorBytes(source, 4, 12, target, 0) 92 | expected.contentEquals(target) 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/modular/ModularBigIntegerAddition.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.modular 19 | 20 | import kotlin.test.Test 21 | import kotlin.test.assertTrue 22 | 23 | /** 24 | * Created by Ugljesa Jovanovic 25 | * ugljesa.jovanovic@ionspin.com 26 | * on 22-Apr-2019 27 | */ 28 | 29 | class ModularBigIntegerAddition { 30 | @Test 31 | fun testAddition() { 32 | val creator = ModularBigInteger.creatorForModulo(10) 33 | val a = creator.fromInt(5) 34 | val b = creator.fromInt(7) 35 | val expected = creator.fromInt(2) 36 | testSingleAddition(a, b, expected) 37 | } 38 | 39 | fun testSingleAddition(a: ModularBigInteger, b: ModularBigInteger, expected: ModularBigInteger) { 40 | assertTrue { 41 | val c = a + b 42 | c == expected 43 | } 44 | } 45 | 46 | @Test 47 | fun testInverse() { 48 | val creator = ModularBigInteger.creatorForModulo(13) 49 | val a = creator.fromInt(7) 50 | assertTrue { -a == creator.fromInt(6) } 51 | assertTrue { a.negate() + a == creator.ZERO } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/modular/ModularBigIntegerExponentiationTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.modular 19 | 20 | import kotlin.test.Test 21 | import kotlin.test.assertEquals 22 | 23 | /** 24 | * Created by Ugljesa Jovanovic 25 | * ugljesa.jovanovic@ionspin.com 26 | * on 28-Jul-2019 27 | */ 28 | 29 | class ModularBigIntegerExponentiationTest { 30 | @Test 31 | fun testExponentiation() { 32 | val base = 5 33 | val modulus = 100 34 | val exponent = 3 35 | val creator = ModularBigInteger.creatorForModulo(modulus) 36 | val result = creator.fromInt(base).pow(exponent) 37 | assertEquals(result, creator.fromInt(25)) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/modular/ModularBigIntegerReadmeTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.modular 19 | 20 | import com.ionspin.kotlin.bignum.integer.BigInteger 21 | import com.ionspin.kotlin.bignum.integer.toBigInteger 22 | import kotlin.test.Test 23 | import kotlin.test.assertTrue 24 | 25 | /** 26 | * Created by Ugljesa Jovanovic 27 | * ugljesa.jovanovic@ionspin.com 28 | * on 22-Apr-2019 29 | */ 30 | 31 | class ModularBigIntegerReadmeTest { 32 | 33 | @Test 34 | fun createModularBigInteger() { 35 | val creator = ModularBigInteger.creatorForModulo(100) 36 | val modularBigInteger = creator.fromLong(150) 37 | println("ModularBigInteger: ${modularBigInteger.toStringWithModulo()}") 38 | assertTrue { 39 | modularBigInteger.residue == 50.toBigInteger() 40 | } 41 | } 42 | 43 | @Test 44 | fun calculateInverseOf3In63Base() { 45 | val expected = "3074457345618258603".toBigInteger() 46 | val base = BigInteger.ONE.shl(62) 47 | val creator = ModularBigInteger.creatorForModulo(base) 48 | 49 | val inverted3 = creator.fromInt(3).inverse() 50 | println("Res: ${inverted3.toBigInteger()}") 51 | assertTrue { 52 | expected.equals(inverted3.residue) 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /bignum/src/iosMain/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ionspin/kotlin-multiplatform-bignum/e2ec7901443ace456ca457fc70b9707b9e0af478/bignum/src/iosMain/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder -------------------------------------------------------------------------------- /bignum/src/iosMain/kotlin/com/ionspin/kotlin/bignum/integer/util/block.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.util 19 | -------------------------------------------------------------------------------- /bignum/src/iosMain/kotlin/com/ionspin/kotlin/bignum/integer/util/runTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.util 19 | -------------------------------------------------------------------------------- /bignum/src/iosTest/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ionspin/kotlin-multiplatform-bignum/e2ec7901443ace456ca457fc70b9707b9e0af478/bignum/src/iosTest/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder -------------------------------------------------------------------------------- /bignum/src/jsMain/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ionspin/kotlin-multiplatform-bignum/e2ec7901443ace456ca457fc70b9707b9e0af478/bignum/src/jsMain/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder -------------------------------------------------------------------------------- /bignum/src/jsMain/kotlin/com/ionspin/kotlin/bignum/integer/PlatformWorkarounds.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer 19 | 20 | /** 21 | * Created by Ugljesa Jovanovic 22 | * ugljesa.jovanovic@ionspin.com 23 | * on 02-Jun-2019 24 | */ 25 | actual object RuntimePlatform { 26 | 27 | actual fun currentPlatform(): Platform { 28 | return Platform.JS 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /bignum/src/jsTest/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ionspin/kotlin-multiplatform-bignum/e2ec7901443ace456ca457fc70b9707b9e0af478/bignum/src/jsTest/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder -------------------------------------------------------------------------------- /bignum/src/jvmMain/kotlin/com/ionspin/kotlin/bignum/decimal/DecimalUtility.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.decimal 19 | 20 | import com.ionspin.kotlin.bignum.integer.base63.toJavaBigInteger 21 | import kotlin.math.absoluteValue 22 | 23 | /** 24 | * Created by Ugljesa Jovanovic 25 | * ugljesa.jovanovic@ionspin.com 26 | * on 24-Mar-2019 27 | */ 28 | 29 | fun BigDecimal.toJavaBigDecimal(): java.math.BigDecimal { 30 | if (this.precision > Int.MAX_VALUE) { 31 | throw RuntimeException("Numbers with more digits than Int.MAX_VALUE cannot be converted to java BigDecimal") 32 | } 33 | if (this == BigDecimal.ZERO) { 34 | return java.math.BigDecimal.ZERO 35 | } 36 | return if (exponent > 0) { 37 | java.math.BigDecimal( 38 | this.significand.toJavaBigInteger(), 39 | (this.precision - this.exponent - 1).toInt() 40 | ) 41 | } else { 42 | java.math.BigDecimal( 43 | this.significand.toJavaBigInteger(), 44 | (this.precision + this.exponent.absoluteValue - 1).toInt() 45 | ) 46 | } 47 | // return java.math.BigDecimal(this.toStringExpanded()) 48 | } 49 | -------------------------------------------------------------------------------- /bignum/src/jvmMain/kotlin/com/ionspin/kotlin/bignum/integer/BigInteger63Utility.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.base63 19 | 20 | import java.math.BigInteger 21 | 22 | /** 23 | * Created by Ugljesa Jovanovic 24 | * ugljesa.jovanovic@ionspin.com 25 | * on 09-Mar-2019 26 | */ 27 | 28 | internal fun ULongArray.toJavaBigInteger(): BigInteger { 29 | return this.foldIndexed(BigInteger.valueOf(0)) { index, acc, digit -> 30 | acc.or(BigInteger(digit.toString(), 10).shiftLeft((index) * 63)) 31 | } 32 | } 33 | 34 | fun com.ionspin.kotlin.bignum.integer.BigInteger.toJavaBigInteger(): BigInteger { 35 | return (this.magnitude.toULongArray().toJavaBigInteger() * this.sign.toInt().toBigInteger()) 36 | } 37 | 38 | internal fun ULong.toJavaBigInteger(): BigInteger { 39 | return BigInteger(this.toString(10), 10) 40 | } 41 | -------------------------------------------------------------------------------- /bignum/src/jvmMain/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ionspin/kotlin-multiplatform-bignum/e2ec7901443ace456ca457fc70b9707b9e0af478/bignum/src/jvmMain/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder -------------------------------------------------------------------------------- /bignum/src/jvmMain/kotlin/com/ionspin/kotlin/bignum/integer/PlatformWorkarounds.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer 19 | 20 | /** 21 | * Created by Ugljesa Jovanovic 22 | * ugljesa.jovanovic@ionspin.com 23 | * on 02-Jun-2019 24 | */ 25 | actual object RuntimePlatform { 26 | actual fun currentPlatform(): Platform { 27 | return Platform.JVM 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimalFloorCeilTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.decimal 19 | 20 | import com.ionspin.kotlin.bignum.integer.toBigInteger 21 | import kotlin.test.assertTrue 22 | import org.junit.Test 23 | 24 | /** 25 | * Created by Ugljesa Jovanovic 26 | * ugljesa.jovanovic@ionspin.com 27 | * on 08-May-2019 28 | */ 29 | 30 | class BigDecimalFloorCeilTest { 31 | 32 | @Test 33 | fun testConversionToBigInteger() { 34 | assertTrue { 35 | val a = 12345.6789.toBigDecimal() 36 | val bigInt = a.toBigInteger() 37 | bigInt == 12345.toBigInteger() 38 | } 39 | 40 | assertTrue { 41 | val a = BigDecimal.fromLongWithExponent(123, 6) 42 | val bigInt = a.toBigInteger() 43 | bigInt == 1230000.toBigInteger() 44 | } 45 | } 46 | 47 | @Test 48 | fun testFloor() { 49 | assertTrue { 50 | val a = 12345.6789.toBigDecimal() 51 | val floor = a.floor() 52 | floor == 12345.toBigDecimal() 53 | } 54 | } 55 | 56 | @Test 57 | fun testFloorWithNegativeExponent() { 58 | assertTrue { 59 | val a = 0.123456.toBigDecimal() 60 | val floor = a.floor() 61 | floor == 0.toBigDecimal() 62 | } 63 | } 64 | 65 | @Test 66 | fun ceilFloor() { 67 | assertTrue { 68 | val a = 12345.6789.toBigDecimal() 69 | val ceil = a.ceil() 70 | ceil == 12346.toBigDecimal() 71 | } 72 | } 73 | 74 | @Test 75 | fun testCeilWithNegativeExponent() { 76 | assertTrue { 77 | val a = 0.123456.toBigDecimal() 78 | val ceil = a.ceil() 79 | 80 | ceil == 1.toBigDecimal() 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimalToStringTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.decimal 19 | 20 | import kotlin.test.assertTrue 21 | import org.junit.Test 22 | 23 | /** 24 | * Created by Ugljesa Jovanovic 25 | * ugljesa.jovanovic@ionspin.com 26 | * on 17-Dec-2019 27 | */ 28 | 29 | class BigDecimalToStringTest { 30 | 31 | @Test 32 | fun testPositiveToString() { 33 | 34 | assertTrue { 35 | val negativeExpanded = (0.6).toBigDecimal().toStringExpanded() 36 | negativeExpanded == "0.6" 37 | } 38 | 39 | assertTrue { 40 | val negativeExpanded = (6.0).toBigDecimal().toStringExpanded() 41 | negativeExpanded == "6" 42 | } 43 | 44 | assertTrue { 45 | val negativeExpanded = (6.1).toBigDecimal().toStringExpanded() 46 | negativeExpanded == "6.1" 47 | } 48 | 49 | assertTrue { 50 | val negativeExpanded = (60.0).toBigDecimal().toStringExpanded() 51 | negativeExpanded == "60" 52 | } 53 | 54 | assertTrue { 55 | val negativeExpanded = (60.1).toBigDecimal().toStringExpanded() 56 | negativeExpanded == "60.1" 57 | } 58 | 59 | assertTrue { 60 | val negativeExpanded = (660.661).toBigDecimal().toStringExpanded() 61 | negativeExpanded == "660.661" 62 | } 63 | } 64 | 65 | @Test 66 | fun testNegativeToString() { 67 | 68 | assertTrue { 69 | val negativeExpanded = (-0.6).toBigDecimal().toStringExpanded() 70 | negativeExpanded == "-0.6" 71 | } 72 | 73 | assertTrue { 74 | val negativeExpanded = (-6.0).toBigDecimal().toStringExpanded() 75 | negativeExpanded == "-6" 76 | } 77 | 78 | assertTrue { 79 | val negativeExpanded = (-6.1).toBigDecimal().toStringExpanded() 80 | negativeExpanded == "-6.1" 81 | } 82 | 83 | assertTrue { 84 | val negativeExpanded = (-60.0).toBigDecimal().toStringExpanded() 85 | negativeExpanded == "-60" 86 | } 87 | 88 | assertTrue { 89 | val negativeExpanded = (-60.1).toBigDecimal().toStringExpanded() 90 | negativeExpanded == "-60.1" 91 | } 92 | assertTrue { 93 | val negativeExpanded = (-660.661).toBigDecimal().toStringExpanded() 94 | negativeExpanded == "-660.661" 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/decimal/ReportedIssuesTest.kt: -------------------------------------------------------------------------------- 1 | package com.ionspin.kotlin.bignum.decimal 2 | 3 | import kotlin.test.assertEquals 4 | import org.junit.Test 5 | 6 | /** 7 | * Created by Ugljesa Jovanovic 8 | * ugljesa.jovanovic@ionspin.com 9 | * on 09-May-2021 10 | */ 11 | class ReportedIssuesTest { 12 | 13 | @Test 14 | fun multiplicationPrecisionLoss() { 15 | val a = "5.61".toBigDecimal().roundToDigitPositionAfterDecimalPoint(2, RoundingMode.ROUND_HALF_AWAY_FROM_ZERO) 16 | val b = "2.95".toBigDecimal().roundToDigitPositionAfterDecimalPoint(2, RoundingMode.ROUND_HALF_AWAY_FROM_ZERO) 17 | val aJava = a.toJavaBigDecimal() 18 | val bJava = b.toJavaBigDecimal() 19 | val javaRes = aJava.multiply(bJava).setScale(2, java.math.RoundingMode.HALF_UP) 20 | val res = a.multiply(b).roundToDigitPositionAfterDecimalPoint(2, RoundingMode.ROUND_HALF_AWAY_FROM_ZERO) 21 | assertEquals(res.toStringExpanded(), javaRes.toPlainString()) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/BigIntegerJvmTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer 19 | 20 | import com.ionspin.kotlin.bignum.integer.BigInteger.Companion.ZERO 21 | import com.ionspin.kotlin.bignum.integer.base63.toJavaBigInteger 22 | import com.ionspin.kotlin.bignum.modular.ModularBigInteger 23 | import com.ionspin.kotlin.bignum.toProperType 24 | import kotlin.random.Random 25 | import kotlin.random.nextULong 26 | import kotlin.test.assertTrue 27 | import kotlin.test.fail 28 | import kotlinx.coroutines.GlobalScope 29 | import kotlinx.coroutines.Job 30 | import kotlinx.coroutines.launch 31 | import kotlinx.coroutines.runBlocking 32 | import org.junit.Test 33 | 34 | /** 35 | * Created by Ugljesa Jovanovic 36 | * ugljesa.jovanovic@ionspin.com 37 | * on 22-Apr-2019 38 | */ 39 | 40 | class BigIntegerJvmTest { 41 | 42 | @Test 43 | fun testModInverse() { 44 | assertTrue { 45 | val a = BigInteger(11) 46 | val aInverse = a.modInverse(5.toBigInteger()) 47 | val aJavaInverse = a.toJavaBigInteger().modInverse(java.math.BigInteger.valueOf(5)) 48 | aInverse.toJavaBigInteger() == aJavaInverse 49 | } 50 | assertTrue { 51 | val a = BigInteger(12312354647) 52 | val aInverse = a.modInverse(121157920.toBigInteger()) 53 | val aJavaInverse = a.toJavaBigInteger().modInverse(java.math.BigInteger.valueOf(121157920)) 54 | aInverse.toJavaBigInteger() == aJavaInverse 55 | } 56 | } 57 | 58 | // TODO need to implement better testcase, need better coprime generation 59 | @Test 60 | fun testRandomModInverse() { 61 | val seed = 1 62 | var random = Random(seed) 63 | val jobList: MutableList = mutableListOf() 64 | for (i in 1..1000) { 65 | val length = random.nextInt(2, 50) 66 | val a = ULongArray(length) { 67 | random.nextULong() shr 1 68 | } 69 | 70 | val job = GlobalScope.launch { 71 | try { 72 | val aBigInt = BigInteger(a.toProperType(), Sign.POSITIVE) 73 | val aPrepared = if (aBigInt % 2 != ZERO) { 74 | aBigInt 75 | } else { 76 | aBigInt - 1 77 | } 78 | 79 | val bPrepared = aPrepared - 1 80 | testSingleModInverse(aPrepared, bPrepared) 81 | } catch (exception: Exception) { 82 | println("Failed on $length") 83 | exception.printStackTrace() 84 | } 85 | } 86 | jobList.add(job) 87 | } 88 | runBlocking { 89 | jobList.forEach { 90 | if (it.isCancelled) { 91 | fail("Some of the tests failed") 92 | } 93 | it.join() 94 | } 95 | } 96 | } 97 | 98 | fun testSingleModInverse(bigInteger: BigInteger, modulo: BigInteger) { 99 | assertTrue { 100 | val inverse = bigInteger.modInverse(modulo) 101 | val javaInverse = bigInteger.toJavaBigInteger().modInverse(modulo.toJavaBigInteger()) 102 | inverse.toJavaBigInteger().compareTo(javaInverse) == 0 103 | } 104 | } 105 | 106 | @Test 107 | fun testModPow() { 108 | val creator = ModularBigInteger.creatorForModulo(10) 109 | 110 | assertTrue { 111 | val a = creator.fromInt(-3) 112 | val aPow = a.pow(3) 113 | val javaBigIntPow = (-3).toBigInteger().toJavaBigInteger() 114 | .modPow( 115 | 3.toBigInteger().toJavaBigInteger(), 116 | 10.toBigInteger().toJavaBigInteger() 117 | ) 118 | aPow.residue.toJavaBigInteger().compareTo(javaBigIntPow) == 0 119 | } 120 | 121 | assertTrue { 122 | val a = creator.fromInt(3) 123 | val aPow = a.pow(3) 124 | val javaBigIntPow = (3).toBigInteger().toJavaBigInteger() 125 | .modPow( 126 | 3.toBigInteger().toJavaBigInteger(), 127 | 10.toBigInteger().toJavaBigInteger() 128 | ) 129 | aPow.residue.toJavaBigInteger().compareTo(javaBigIntPow) == 0 130 | } 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base32/BigInteger32JavaAdditionTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.base32 19 | 20 | import org.junit.Test 21 | 22 | /** 23 | * Created by Ugljesa Jovanovic 24 | * ugljesa.jovanovic@ionspin.com 25 | * on 17-Mar-2019 26 | */ 27 | 28 | class BigInteger32JavaAdditionTest { 29 | 30 | @Test 31 | fun `Test specific values`() { 32 | val a = uintArrayOf(1U) 33 | val b = uintArrayOf(2U) 34 | 35 | val sum = BigInteger32Arithmetic.add(a, b) 36 | println("Sum: $sum") 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base32/BigInteger32JavaBitwiseTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.base32 19 | 20 | import kotlin.random.Random 21 | import kotlin.random.nextUInt 22 | import kotlin.test.assertTrue 23 | import kotlin.test.fail 24 | import kotlinx.coroutines.GlobalScope 25 | import kotlinx.coroutines.Job 26 | import kotlinx.coroutines.launch 27 | import kotlinx.coroutines.runBlocking 28 | import org.junit.Test 29 | 30 | /** 31 | * Created by Ugljesa Jovanovic 32 | * ugljesa.jovanovic@ionspin.com 33 | * on 09-Mar-2019 34 | */ 35 | 36 | class BigInteger32JavaBitwiseTest { 37 | 38 | @Test 39 | fun `Test shift left`() { 40 | val seed = 1 41 | val random = Random(seed) 42 | 43 | val jobList: MutableList = mutableListOf() 44 | for (i in 1..30_000 step 5000) { 45 | val a = UIntArray(i) { random.nextUInt() } 46 | val b = random.nextInt(i) 47 | jobList.add( 48 | GlobalScope.launch { 49 | shiftLeftSingleTest(b, a) 50 | } 51 | ) 52 | } 53 | runBlocking { 54 | jobList.forEach { 55 | if (it.isCancelled) { 56 | fail("Some of the tests failed") 57 | } 58 | it.join() 59 | } 60 | } 61 | } 62 | 63 | fun shiftLeftSingleTest(places: Int, number: UIntArray) { 64 | assertTrue("Failed for $places and elements ${number.contentToString()}") { 65 | val a = number 66 | val result = BigInteger32Arithmetic.shiftLeft(a, places) 67 | val convertedResult = result.toJavaBigInteger() 68 | val bigIntResult = a.toJavaBigInteger() shl places 69 | convertedResult == bigIntResult 70 | } 71 | } 72 | 73 | @Test 74 | fun `Test shift right`() { 75 | val seed = 1 76 | val random = Random(seed) 77 | val jobList: MutableList = mutableListOf() 78 | for (i in 1..30_000 step 5000) { 79 | val a = UIntArray(i) { random.nextUInt() } 80 | val b = random.nextInt(i) 81 | jobList.add( 82 | GlobalScope.launch { 83 | shiftRightSingleTest(b, a) 84 | } 85 | ) 86 | } 87 | runBlocking { 88 | jobList.forEach { 89 | if (it.isCancelled) { 90 | fail("Some of the tests failed") 91 | } 92 | it.join() 93 | } 94 | } 95 | } 96 | 97 | @Test 98 | fun `Test shift right with specific values`() { 99 | val a = intArrayOf( 100 | 752029288, 101 | -964625924, 102 | 479674580, 103 | -1697013934, 104 | -1956440078, 105 | -1550357085 106 | ).toUIntArray() 107 | shiftRightSingleTest(128, number = a) 108 | } 109 | 110 | fun shiftRightSingleTest(places: Int, number: UIntArray) { 111 | assertTrue("Failed for $places and elements ${number.contentToString()}") { 112 | val result = BigInteger32Arithmetic.shiftRight(number, places) 113 | val convertedResult = result.toJavaBigInteger() 114 | val bigIntResult = number.toJavaBigInteger() shr places 115 | convertedResult == bigIntResult 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base32/BigInteger32JavaStringConversionTests.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.base32 19 | 20 | import java.math.BigInteger 21 | import kotlin.random.Random 22 | import kotlin.random.nextUInt 23 | import kotlin.test.assertTrue 24 | import kotlin.test.fail 25 | import kotlinx.coroutines.GlobalScope 26 | import kotlinx.coroutines.Job 27 | import kotlinx.coroutines.launch 28 | import kotlinx.coroutines.runBlocking 29 | import org.junit.Test 30 | 31 | /** 32 | * Created by Ugljesa Jovanovic 33 | * ugljesa.jovanovic@ionspin.com 34 | * on 16-Mar-2019 35 | */ 36 | 37 | class BigInteger32JavaStringConversionTests { 38 | 39 | @Test 40 | fun `Test parsing with sepcific values`() { 41 | testParsingSingleTest("1234", 10) 42 | } 43 | 44 | fun testParsingSingleTest(uIntArrayString: String, base: Int) { 45 | assertTrue { 46 | val parsed = BigInteger32Arithmetic.parseForBase(uIntArrayString, base) 47 | val javaBigIntParsed = BigInteger(uIntArrayString, base) 48 | 49 | parsed.toJavaBigInteger() == javaBigIntParsed 50 | } 51 | } 52 | 53 | @Test 54 | fun `Random toString test, in base 10`() { 55 | val seed = 1 56 | val random = Random(seed) 57 | val jobList: MutableList = mutableListOf() 58 | for (i in 1..Int.MAX_VALUE step 5001) { 59 | jobList.add( 60 | GlobalScope.launch { 61 | toStringSingleTest(uintArrayOf(random.nextUInt()), 10) 62 | } 63 | ) 64 | } 65 | runBlocking { 66 | jobList.forEach { 67 | if (it.isCancelled) { 68 | fail("Some of the tests failed") 69 | } 70 | it.join() 71 | } 72 | } 73 | } 74 | 75 | @Test 76 | fun `Random toString test, in random base less than 36`() { 77 | val seed = 1 78 | val random = Random(seed) 79 | val jobList: MutableList = mutableListOf() 80 | for (i in 1..Int.MAX_VALUE step 5001) { 81 | jobList.add( 82 | GlobalScope.launch { 83 | toStringSingleTest( 84 | uintArrayOf(random.nextUInt(), random.nextUInt()), 85 | random.nextInt(2, 36) 86 | ) // 36 is the max java bigint supports 87 | } 88 | ) 89 | } 90 | runBlocking { 91 | jobList.forEach { 92 | if (it.isCancelled) { 93 | fail("Some of the tests failed") 94 | } 95 | it.join() 96 | } 97 | } 98 | } 99 | 100 | @Test 101 | fun `Test toString with specific values`() { 102 | toStringSingleTest(uintArrayOf(1234U), 10) 103 | } 104 | 105 | fun toStringSingleTest(uIntArray: UIntArray, base: Int) { 106 | assertTrue { 107 | val result = BigInteger32Arithmetic.toString(uIntArray, base) 108 | val javaBigIntResult = uIntArray.toJavaBigInteger().toString(base) 109 | 110 | result == javaBigIntResult 111 | } 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base32/BigInteger32JavaSubstractTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.base32 19 | 20 | import java.math.BigInteger 21 | import java.time.Duration 22 | import java.time.LocalDateTime 23 | import kotlin.random.Random 24 | import kotlin.random.nextUInt 25 | import kotlin.test.assertTrue 26 | import kotlin.test.fail 27 | import kotlinx.coroutines.GlobalScope 28 | import kotlinx.coroutines.Job 29 | import kotlinx.coroutines.launch 30 | import kotlinx.coroutines.runBlocking 31 | import org.junit.Test 32 | 33 | /** 34 | * Created by Ugljesa Jovanovic 35 | * ugljesa.jovanovic@ionspin.com 36 | * on 09-Mar-2019 37 | */ 38 | 39 | class BigInteger32JavaSubtractTest { 40 | 41 | val basePower = 32 42 | 43 | @Test 44 | fun `Test subtraction with specific values`() { 45 | assertTrue { 46 | val a = uintArrayOf(10U, 20U) 47 | val b = uintArrayOf(15U, 5U) 48 | val c = BigInteger32Arithmetic.subtract(a, b) 49 | 50 | val resultBigInt = c.toJavaBigInteger() 51 | 52 | val aBigInt = a.toJavaBigInteger() 53 | val bBigInt = b.toJavaBigInteger() 54 | val cBigInt = aBigInt - bBigInt 55 | 56 | resultBigInt == cBigInt 57 | } 58 | } 59 | 60 | @Test 61 | fun `Test subtraction with random values`() { 62 | val seed = 1 63 | val random = Random(seed) 64 | val jobList: MutableList = mutableListOf() 65 | 66 | for (i in 1..Int.MAX_VALUE step 5001) { 67 | jobList.add( 68 | GlobalScope.launch { 69 | subtractSingleTest(random.nextUInt(), random.nextUInt(), random.nextUInt()) 70 | } 71 | ) 72 | } 73 | runBlocking { 74 | jobList.forEach { 75 | if (it.isCancelled) { 76 | fail("Some of the tests failed") 77 | } 78 | it.join() 79 | } 80 | } 81 | } 82 | 83 | @Test 84 | fun `Test subtraction with a large number of random values`() { 85 | val seed = 1 86 | val random = Random(seed) 87 | val numberOfElements = 15000 88 | println("Number of elements $numberOfElements") 89 | 90 | val lotOfElements = UIntArray(numberOfElements) { 91 | random.nextUInt() 92 | } 93 | subtractSingleTest(*lotOfElements) 94 | } 95 | 96 | fun subtractSingleTest(vararg elements: UInt) { 97 | assertTrue("Failed on ${elements.contentToString()}") { 98 | val time = false 99 | lateinit var lastTime: LocalDateTime 100 | lateinit var startTime: LocalDateTime 101 | 102 | if (time) { 103 | lastTime = LocalDateTime.now() 104 | startTime = lastTime 105 | } 106 | 107 | val first = 108 | elements.copyOfRange(0, elements.size / 2).fold(UIntArray(1) { 1U }) { acc, uInt -> 109 | BigInteger32Arithmetic.multiply(acc, uInt) 110 | } 111 | val second = elements.copyOfRange(elements.size / 2, elements.size - 1) 112 | .fold(UIntArray(1) { 1U }) { acc, uInt -> 113 | BigInteger32Arithmetic.multiply(acc, uInt) 114 | } 115 | val result = BigInteger32Arithmetic.subtract(first, second) 116 | 117 | if (time) { 118 | lastTime = LocalDateTime.now() 119 | println("Total time ${Duration.between(startTime, lastTime)}") 120 | startTime = lastTime 121 | } 122 | 123 | val convertedResult = result.toJavaBigInteger() 124 | val bigIntFirst = 125 | elements.copyOfRange(0, elements.size / 2).fold(BigInteger.ONE) { acc, uInt -> 126 | acc * BigInteger(uInt.toString(), 10) 127 | } 128 | val bigIntSecond = elements.copyOfRange(elements.size / 2, elements.size - 1) 129 | .fold(BigInteger.ONE) { acc, uInt -> 130 | acc * BigInteger(uInt.toString(), 10) 131 | } 132 | val bigIntResult = bigIntFirst - bigIntSecond 133 | if (time) { 134 | println("Result $convertedResult") 135 | println("Total time ${Duration.between(startTime, lastTime)}") 136 | } 137 | 138 | bigIntResult.abs() == convertedResult 139 | } 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base32/BigInteger32SqrtTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.base32 19 | 20 | import kotlin.random.Random 21 | import kotlin.test.assertTrue 22 | import org.junit.Ignore 23 | import org.junit.Test 24 | 25 | /** 26 | * Created by Ugljesa Jovanovic 27 | * ugljesa.jovanovic@ionspin.com 28 | * on 27-Apr-2019 29 | */ 30 | 31 | class BigInteger32SqrtTest { 32 | 33 | @Ignore("Need to improve basic divide algo before using it in sqrt") 34 | @Test 35 | fun testSpecificSqrt() { 36 | assertTrue { 37 | val operand = BigInteger32Arithmetic.parseForBase( 38 | "547162628152978179694572006333394526730648083587307585845689465425919940113687046992147981" + 39 | "70196777058540761287227012319014433594622337082294496524616700872104031016223448", 40 | 10 41 | ) 42 | val a = BigInteger32Arithmetic.sqrt(operand) 43 | println("Operand: ${BigInteger32Arithmetic.toString(operand, 10)}") 44 | println("Sqrt: ${BigInteger32Arithmetic.toString(a.first, 10)}") 45 | println("Remainder: ${BigInteger32Arithmetic.toString(a.second, 10)}") 46 | val resultSqrt = BigInteger32Arithmetic.parseForBase( 47 | "7397044194494028975732055495441088126843300784390793504636322150624655374658058636588", 48 | 10 49 | ) 50 | val resultRem = BigInteger32Arithmetic.subtract( 51 | operand, 52 | BigInteger32Arithmetic.multiply(resultSqrt, resultSqrt) 53 | ) 54 | 55 | resultSqrt.contentEquals(a.first) && resultRem.contentEquals(a.second) 56 | } 57 | 58 | assertTrue { 59 | val operand = BigInteger32Arithmetic.parseForBase("123456789", 10) 60 | val a = BigInteger32Arithmetic.sqrt(operand) 61 | println("Operand: ${BigInteger32Arithmetic.toString(operand, 10)}") 62 | println("Sqrt: ${BigInteger32Arithmetic.toString(a.first, 10)}") 63 | println("Remainder: ${BigInteger32Arithmetic.toString(a.second, 10)}") 64 | val resultSqrt = BigInteger32Arithmetic.parseForBase( 65 | "11111", 66 | 10 67 | ) 68 | val resultRem = BigInteger32Arithmetic.subtract( 69 | operand, 70 | BigInteger32Arithmetic.multiply(resultSqrt, resultSqrt) 71 | ) 72 | 73 | resultSqrt.contentEquals(a.first) 74 | } 75 | } 76 | 77 | @Test 78 | fun testSpecificSqrtInt() { 79 | val seed = 1 80 | val random = Random(seed) 81 | val a = UIntArray(1) { 144U } 82 | val sqrt = BigInteger32Arithmetic.sqrtInt(a) 83 | assertTrue { sqrt[0] == 12U } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base32/BigInteger32Utility.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.base32 19 | 20 | import java.math.BigInteger 21 | 22 | /** 23 | * Created by Ugljesa Jovanovic 24 | * ugljesa.jovanovic@ionspin.com 25 | * on 09-Mar-2019 26 | */ 27 | 28 | fun UIntArray.toJavaBigInteger(): BigInteger { 29 | return this.foldIndexed(BigInteger.valueOf(0)) { index, acc, digit -> 30 | acc.or(BigInteger(digit.toString(), 10).shiftLeft((index) * 32)) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base63/BigInteger63GcdTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.base63 19 | 20 | import com.ionspin.kotlin.bignum.integer.base63.array.BigInteger63Arithmetic 21 | import kotlin.random.Random 22 | import kotlin.random.nextULong 23 | import kotlin.test.assertTrue 24 | import kotlin.test.fail 25 | import kotlinx.coroutines.GlobalScope 26 | import kotlinx.coroutines.Job 27 | import kotlinx.coroutines.launch 28 | import kotlinx.coroutines.runBlocking 29 | import org.junit.Test 30 | 31 | /** 32 | * Created by Ugljesa Jovanovic 33 | * ugljesa.jovanovic@ionspin.com 34 | * on 27-Apr-2019 35 | */ 36 | 37 | class BigInteger63GcdTest { 38 | 39 | @Test 40 | fun testGcd() { 41 | val seed = 1 42 | val random = Random(seed) 43 | val jobList: MutableList = mutableListOf() 44 | 45 | for (i in 1..100) { 46 | 47 | val length = random.nextInt(2, 100) 48 | val a = ULongArray(length) { 49 | random.nextULong() shr 1 50 | } 51 | val divisorLength = random.nextInt(1, length) 52 | val b = ULongArray(divisorLength) { 53 | random.nextULong() shr 1 54 | } 55 | 56 | val job = GlobalScope.launch { 57 | testGcdSingle(a, b) 58 | } 59 | jobList.add(job) 60 | } 61 | runBlocking { 62 | jobList.forEach { 63 | if (it.isCancelled) { 64 | fail("Some of the tests failed") 65 | } 66 | it.join() 67 | } 68 | } 69 | } 70 | 71 | @Test 72 | fun debugBinaryGcd() { 73 | val first = ulongArrayOf(4U) 74 | val second = ulongArrayOf(6U) 75 | testGcdSingle(first, second) 76 | } 77 | 78 | fun testGcdSingle(first: ULongArray, second: ULongArray) { 79 | val a = BigInteger63Arithmetic.gcd(first, second) 80 | val aJavaBigInt = first.toJavaBigInteger().gcd(second.toJavaBigInteger()) 81 | assertTrue("Failed on ${first.toJavaBigInteger()} ${second.toJavaBigInteger()}") { 82 | a.toJavaBigInteger().compareTo(aJavaBigInt) == 0 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base63/BigInteger63JavaAdditionTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.base63 19 | 20 | import com.ionspin.kotlin.bignum.integer.base63.array.BigInteger63Arithmetic 21 | import kotlin.test.assertTrue 22 | import org.junit.Test 23 | 24 | /** 25 | * Created by Ugljesa Jovanovic 26 | * ugljesa.jovanovic@ionspin.com 27 | * on 17-Mar-2019 28 | */ 29 | /* 30 | 0 = 9223372036854775800 31 | 1 = 9223372036854775807 32 | 2 = 9223372036854775807 33 | 3 = 67108863 34 | 35 | + 8 36 | */ 37 | 38 | class BigInteger63JavaAdditionTest { 39 | 40 | @Test 41 | fun `Test specific values for addition`() { 42 | val first = ulongArrayOf(9223372036854775800UL, 9223372036854775807UL, 9223372036854775807UL, 67108863UL) 43 | val second = ulongArrayOf(8UL) 44 | val result = BigInteger63Arithmetic.add(first, second) 45 | 46 | val bigIntResult = first.toJavaBigInteger() + second.toJavaBigInteger() 47 | assertTrue { result.toJavaBigInteger() == bigIntResult } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base63/BigInteger63JavaBaseConversionTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.base63 19 | 20 | import com.ionspin.kotlin.bignum.integer.base32.toJavaBigInteger 21 | import com.ionspin.kotlin.bignum.integer.base63.array.BigInteger63Arithmetic 22 | import com.ionspin.kotlin.bignum.integer.base63.array.BigInteger63Arithmetic.baseMask 23 | import java.math.BigInteger 24 | import kotlin.random.Random 25 | import kotlin.random.nextUInt 26 | import kotlin.random.nextULong 27 | import kotlin.test.Test 28 | import kotlin.test.assertTrue 29 | 30 | /** 31 | * Created by Ugljesa Jovanovic 32 | * ugljesa.jovanovic@ionspin.com 33 | * on 10-Mar-2019 34 | */ 35 | 36 | class BigInteger63JavaBaseConversionTest { 37 | 38 | @Test 39 | fun test63To64Conversion() { 40 | assertTrue { 41 | val a = ulongArrayOf(1UL, 1UL, 1UL) 42 | val b = BigInteger63Arithmetic.convertTo64BitRepresentation(a) 43 | val aBigInt = a.toJavaBigInteger() 44 | val bBigInt = b.fromBase64toJavaBigInteger() 45 | 46 | aBigInt == bBigInt 47 | } 48 | 49 | assertTrue { 50 | val a = ulongArrayOf(0UL - 1UL, 0UL - 1UL, 0UL - 1UL, 0UL - 1UL, 0UL - 1UL, 0UL - 1UL, 1UL, 1UL) 51 | val b = BigInteger63Arithmetic.convertTo64BitRepresentation(a) 52 | val aBigInt = a.toJavaBigInteger() 53 | val bBigInt = b.fromBase64toJavaBigInteger() 54 | 55 | aBigInt == bBigInt 56 | } 57 | 58 | assertTrue { 59 | val seed = 1 60 | val random = Random(seed) 61 | val a = ULongArray(321) { random.nextULong() and baseMask } 62 | val b = BigInteger63Arithmetic.convertTo64BitRepresentation(a) 63 | val aBigInt = a.toJavaBigInteger() 64 | val bBigInt = b.fromBase64toJavaBigInteger() 65 | 66 | aBigInt == bBigInt 67 | } 68 | 69 | assertTrue { 70 | val a = ULongArray(129) { (0UL - 1UL) shr 1 } 71 | val b = BigInteger63Arithmetic.convertTo64BitRepresentation(a) 72 | val aBigInt = a.toJavaBigInteger() 73 | val bBigInt = b.fromBase64toJavaBigInteger() 74 | 75 | aBigInt == bBigInt 76 | } 77 | } 78 | 79 | @Test 80 | fun test63To32Conversion() { 81 | assertTrue { 82 | val a = ulongArrayOf(1UL, 1UL, 1UL) 83 | val b = BigInteger63Arithmetic.convertTo32BitRepresentation(a) 84 | val aBigInt = a.toJavaBigInteger() 85 | val bBigInt = b.toJavaBigInteger() 86 | 87 | aBigInt == bBigInt 88 | } 89 | } 90 | 91 | @Test 92 | fun test32To63Conversion() { 93 | assertTrue { 94 | val a = uintArrayOf(1U, 0U - 1U, 0U, 0U - 1U) 95 | val b = BigInteger63Arithmetic.convertFrom32BitRepresentation(a) 96 | val aBigInt = a.toJavaBigInteger() 97 | val bBigInt = b.toJavaBigInteger() 98 | 99 | aBigInt == bBigInt 100 | } 101 | 102 | assertTrue { 103 | val a = uintArrayOf(1U, 0U - 1U, 0U, 0U - 1U, 1U) 104 | val b = BigInteger63Arithmetic.convertFrom32BitRepresentation(a) 105 | val aBigInt = a.toJavaBigInteger() 106 | val bBigInt = b.toJavaBigInteger() 107 | 108 | aBigInt == bBigInt 109 | } 110 | 111 | assertTrue { 112 | val seed = 1 113 | val random = Random(seed) 114 | val a = UIntArray(352) { random.nextUInt() } 115 | val b = BigInteger63Arithmetic.convertFrom32BitRepresentation(a) 116 | val aBigInt = a.toJavaBigInteger() 117 | val bBigInt = b.toJavaBigInteger() 118 | 119 | aBigInt == bBigInt 120 | } 121 | } 122 | 123 | private fun ULongArray.fromBase64toJavaBigInteger(): BigInteger { 124 | return this.foldIndexed(BigInteger.valueOf(0)) { index, acc, digit -> 125 | acc.or(BigInteger(digit.toString(), 10).shiftLeft((index) * 64)) 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base63/BigInteger63JavaStringConversionTests.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.base63 19 | 20 | import com.ionspin.kotlin.bignum.integer.base63.array.BigInteger63Arithmetic 21 | import java.math.BigInteger 22 | import kotlin.random.Random 23 | import kotlin.random.nextULong 24 | import kotlin.test.assertTrue 25 | import kotlin.test.fail 26 | import kotlinx.coroutines.GlobalScope 27 | import kotlinx.coroutines.Job 28 | import kotlinx.coroutines.launch 29 | import kotlinx.coroutines.runBlocking 30 | import org.junit.Test 31 | 32 | /** 33 | * Created by Ugljesa Jovanovic 34 | * ugljesa.jovanovic@ionspin.com 35 | * on 16-Mar-2019 36 | */ 37 | 38 | class BigInteger63JavaStringConversionTests { 39 | 40 | @Test 41 | fun `Test parsing with sepcific values`() { 42 | // testParsingSingleTest("1234", 10) 43 | // testParsingSingleTest("922337203685477580799999999999990776627963145224192", 10) 44 | testParsingSingleTest("52656145834278593348959013841835216159447547700274555627155488768", 10) 45 | testParsingSingleTest("f", 16) 46 | } 47 | 48 | fun testParsingSingleTest(uIntArrayString: String, base: Int) { 49 | assertTrue { 50 | val parsed = BigInteger63Arithmetic.parseForBase(uIntArrayString, base) 51 | val javaBigIntParsed = BigInteger(uIntArrayString, base) 52 | 53 | parsed.toJavaBigInteger() == javaBigIntParsed 54 | } 55 | } 56 | 57 | @Test 58 | fun `Random toString test, in base 10`() { 59 | val seed = 1 60 | val random = Random(seed) 61 | val jobList: MutableList = mutableListOf() 62 | 63 | for (i in 1..Int.MAX_VALUE step 7001) { 64 | jobList.add( 65 | GlobalScope.launch { 66 | toStringSingleTest(ulongArrayOf(random.nextULong()), 10) 67 | } 68 | ) 69 | } 70 | runBlocking { 71 | jobList.forEach { 72 | if (it.isCancelled) { 73 | fail("Some of the tests failed") 74 | } 75 | it.join() 76 | } 77 | } 78 | } 79 | 80 | @Test 81 | fun `Random toString test, in random base less than 36`() { 82 | val seed = 1 83 | val random = Random(seed) 84 | val jobList: MutableList = mutableListOf() 85 | for (i in 1..Int.MAX_VALUE step 7001) { 86 | jobList.add( 87 | GlobalScope.launch { 88 | toStringSingleTest( 89 | ulongArrayOf(random.nextULong(), random.nextULong()), 90 | random.nextInt(2, 36) 91 | ) // 36 is the max java bigint supports 92 | } 93 | ) 94 | } 95 | 96 | runBlocking { 97 | jobList.forEach { 98 | if (it.isCancelled) { 99 | fail("Some of the tests failed") 100 | } 101 | it.join() 102 | } 103 | } 104 | } 105 | 106 | @Test 107 | fun `Test toString with specific values`() { 108 | toStringSingleTest(ulongArrayOf(1234U), 10) 109 | } 110 | 111 | fun toStringSingleTest(uLongArray: ULongArray, base: Int) { 112 | assertTrue { 113 | val result = BigInteger63Arithmetic.toString(uLongArray, base) 114 | val javaBigIntResult = uLongArray.toJavaBigInteger().toString(base) 115 | 116 | result == javaBigIntResult 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base63/BigInteger63SqrtTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.base63 19 | 20 | import com.ionspin.kotlin.bignum.integer.base63.array.BigInteger63Arithmetic 21 | import kotlin.random.Random 22 | import kotlin.random.nextULong 23 | import kotlin.test.assertTrue 24 | import org.junit.Test 25 | 26 | /** 27 | * Created by Ugljesa Jovanovic 28 | * ugljesa.jovanovic@ionspin.com 29 | * on 27-Apr-2019 30 | */ 31 | 32 | class BigInteger63SqrtTest { 33 | 34 | @Test 35 | fun testSpecificSqrt() { 36 | assertTrue { 37 | val seed = 1 38 | val random = Random(seed) 39 | val operand = ULongArray(9) { random.nextULong() shr 1 } 40 | val a = BigInteger63Arithmetic.sqrt(operand) 41 | println("Operand: ${BigInteger63Arithmetic.toString(operand, 10)}") 42 | println("Sqrt: ${BigInteger63Arithmetic.toString(a.first, 10)}") 43 | println("Remainder: ${BigInteger63Arithmetic.toString(a.second, 10)}") 44 | val resultSqrt = BigInteger63Arithmetic.parseForBase( 45 | "7397044194494028975732055495441088126843300784390793504636322150624655374658058636588", 46 | 10 47 | ) 48 | val resultRem = BigInteger63Arithmetic.subtract( 49 | operand, 50 | BigInteger63Arithmetic.multiply(resultSqrt, resultSqrt) 51 | ) 52 | 53 | resultSqrt.contentEquals(a.first) 54 | } 55 | 56 | assertTrue { 57 | val seed = 1 58 | val random = Random(seed) 59 | val operand = BigInteger63Arithmetic.parseForBase("123456789", 10) 60 | val a = BigInteger63Arithmetic.sqrt(operand) 61 | println("Operand: ${BigInteger63Arithmetic.toString(operand, 10)}") 62 | println("Sqrt: ${BigInteger63Arithmetic.toString(a.first, 10)}") 63 | println("Remainder: ${BigInteger63Arithmetic.toString(a.second, 10)}") 64 | val resultSqrt = BigInteger63Arithmetic.parseForBase( 65 | "11111", 66 | 10 67 | ) 68 | val resultRem = BigInteger63Arithmetic.subtract( 69 | operand, 70 | BigInteger63Arithmetic.multiply(resultSqrt, resultSqrt) 71 | ) 72 | 73 | resultSqrt.contentEquals(a.first) 74 | } 75 | } 76 | 77 | @Test 78 | fun testSpecificSqrtInt() { 79 | val seed = 1 80 | val random = Random(seed) 81 | val a = ULongArray(1) { 144U } 82 | val sqrt = BigInteger63Arithmetic.sqrtInt(a) 83 | assertTrue { sqrt[0] == 12UL } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base63List/BigInteger63ListGcdTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.base63List 19 | 20 | import com.ionspin.kotlin.bignum.integer.base63.BigInteger63LinkedListArithmetic 21 | import kotlin.random.Random 22 | import kotlin.random.nextULong 23 | import kotlin.test.assertTrue 24 | import kotlin.test.fail 25 | import kotlinx.coroutines.GlobalScope 26 | import kotlinx.coroutines.Job 27 | import kotlinx.coroutines.launch 28 | import kotlinx.coroutines.runBlocking 29 | import org.junit.Test 30 | 31 | /** 32 | * Created by Ugljesa Jovanovic 33 | * ugljesa.jovanovic@ionspin.com 34 | * on 27-Apr-2019 35 | */ 36 | 37 | class BigInteger63ListGcdTest() { 38 | 39 | @Test 40 | fun testGcd() { 41 | val seed = 1 42 | val random = Random(seed) 43 | val jobList: MutableList = mutableListOf() 44 | for (i in 1..1000) { 45 | 46 | val length = random.nextInt(2, 100) 47 | val a = List(length) { 48 | random.nextULong() shr 1 49 | } 50 | val divisorLength = random.nextInt(1, length) 51 | val b = List(divisorLength) { 52 | random.nextULong() shr 1 53 | } 54 | val job = GlobalScope.launch { 55 | try { 56 | testGcdSingle(a, b) 57 | } catch (exception: Exception) { 58 | println("Failed on $length $divisorLength") 59 | } 60 | } 61 | jobList.add(job) 62 | } 63 | runBlocking { 64 | jobList.forEach { 65 | if (it.isCancelled) { 66 | fail("Some of the tests failed") 67 | } 68 | it.join() 69 | } 70 | } 71 | } 72 | 73 | fun testGcdSingle(first: List, second: List) { 74 | val a = BigInteger63LinkedListArithmetic.gcd(first, second) 75 | val aJavaBigInt = first.toJavaBigInteger().gcd(second.toJavaBigInteger()) 76 | assertTrue { 77 | a.toJavaBigInteger().compareTo(aJavaBigInt) == 0 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base63List/BigInteger63ListJavaAdditionTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.base63List 19 | 20 | import com.ionspin.kotlin.bignum.integer.base63.BigInteger63LinkedListArithmetic 21 | import kotlin.test.assertTrue 22 | import org.junit.Test 23 | 24 | /** 25 | * Created by Ugljesa Jovanovic 26 | * ugljesa.jovanovic@ionspin.com 27 | * on 17-Mar-2019 28 | */ 29 | 30 | class BigInteger63ListJavaAdditionTest() { 31 | 32 | @Test 33 | fun `Test specific values for addition`() { 34 | val first = listOf(9223372036854775800UL, 9223372036854775807UL, 9223372036854775807UL, 67108863UL) 35 | val second = listOf(8UL) 36 | val result = BigInteger63LinkedListArithmetic.add(first, second) 37 | 38 | val bigIntResult = first.toJavaBigInteger() + second.toJavaBigInteger() 39 | assertTrue { result.toJavaBigInteger() == bigIntResult } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base63List/BigInteger63ListJavaBaseConversionTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.base63List 19 | 20 | import com.ionspin.kotlin.bignum.integer.base32.toJavaBigInteger 21 | import com.ionspin.kotlin.bignum.integer.base63.BigInteger63LinkedListArithmetic 22 | import com.ionspin.kotlin.bignum.integer.base63.BigInteger63LinkedListArithmetic.baseMask 23 | import java.math.BigInteger 24 | import kotlin.random.Random 25 | import kotlin.random.nextUInt 26 | import kotlin.random.nextULong 27 | import kotlin.test.Test 28 | import kotlin.test.assertTrue 29 | 30 | /** 31 | * Created by Ugljesa Jovanovic 32 | * ugljesa.jovanovic@ionspin.com 33 | * on 10-Mar-2019 34 | */ 35 | 36 | class BigInteger63ListJavaBaseConversionTest() { 37 | 38 | @Test 39 | fun test63To64Conversion() { 40 | assertTrue { 41 | val a = listOf(1UL, 1UL, 1UL) 42 | val b = BigInteger63LinkedListArithmetic.convertTo64BitRepresentation(a) 43 | val aBigInt = a.toJavaBigInteger() 44 | val bBigInt = b.fromBase64toJavaBigInteger() 45 | 46 | aBigInt == bBigInt 47 | } 48 | 49 | assertTrue { 50 | val a = listOf(0UL - 1UL, 0UL - 1UL, 0UL - 1UL, 0UL - 1UL, 0UL - 1UL, 0UL - 1UL, 1UL, 1UL) 51 | val b = BigInteger63LinkedListArithmetic.convertTo64BitRepresentation(a) 52 | val aBigInt = a.toJavaBigInteger() 53 | val bBigInt = b.fromBase64toJavaBigInteger() 54 | 55 | aBigInt == bBigInt 56 | } 57 | 58 | assertTrue { 59 | val seed = 1 60 | val random = Random(seed) 61 | val a = List(321) { random.nextULong() and baseMask } 62 | val b = BigInteger63LinkedListArithmetic.convertTo64BitRepresentation(a) 63 | val aBigInt = a.toJavaBigInteger() 64 | val bBigInt = b.fromBase64toJavaBigInteger() 65 | 66 | aBigInt == bBigInt 67 | } 68 | 69 | assertTrue { 70 | val a = List(129) { (0UL - 1UL) shr 1 } 71 | val b = BigInteger63LinkedListArithmetic.convertTo64BitRepresentation(a) 72 | val aBigInt = a.toJavaBigInteger() 73 | val bBigInt = b.fromBase64toJavaBigInteger() 74 | 75 | aBigInt == bBigInt 76 | } 77 | } 78 | 79 | @Test 80 | fun test63To32Conversion() { 81 | assertTrue { 82 | val a = listOf(1UL, 1UL, 1UL) 83 | val b = BigInteger63LinkedListArithmetic.convertTo32BitRepresentation(a) 84 | val aBigInt = a.toJavaBigInteger() 85 | val bBigInt = b.toJavaBigInteger() 86 | 87 | aBigInt == bBigInt 88 | } 89 | } 90 | 91 | @Test 92 | fun test32To63Conversion() { 93 | assertTrue { 94 | val a = uintArrayOf(1U, 0U - 1U, 0U, 0U - 1U) 95 | val b = BigInteger63LinkedListArithmetic.convertFrom32BitRepresentation(a) 96 | val aBigInt = a.toJavaBigInteger() 97 | val bBigInt = b.toJavaBigInteger() 98 | 99 | aBigInt == bBigInt 100 | } 101 | 102 | assertTrue { 103 | val a = uintArrayOf(1U, 0U - 1U, 0U, 0U - 1U, 1U) 104 | val b = BigInteger63LinkedListArithmetic.convertFrom32BitRepresentation(a) 105 | val aBigInt = a.toJavaBigInteger() 106 | val bBigInt = b.toJavaBigInteger() 107 | 108 | aBigInt == bBigInt 109 | } 110 | 111 | assertTrue { 112 | val seed = 1 113 | val random = Random(seed) 114 | val a = UIntArray(352) { random.nextUInt() } 115 | val b = BigInteger63LinkedListArithmetic.convertFrom32BitRepresentation(a) 116 | val aBigInt = a.toJavaBigInteger() 117 | val bBigInt = b.toJavaBigInteger() 118 | 119 | aBigInt == bBigInt 120 | } 121 | } 122 | 123 | private fun List.fromBase64toJavaBigInteger(): BigInteger { 124 | return this.foldIndexed(BigInteger.valueOf(0)) { index, acc, digit -> 125 | acc.or(BigInteger(digit.toString(), 10).shiftLeft((index) * 64)) 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base63List/BigInteger63ListJavaStringConversionTests.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.base63List 19 | 20 | import com.ionspin.kotlin.bignum.integer.base63.BigInteger63LinkedListArithmetic 21 | import java.math.BigInteger 22 | import kotlin.random.Random 23 | import kotlin.random.nextULong 24 | import kotlin.test.assertTrue 25 | import kotlin.test.fail 26 | import kotlinx.coroutines.GlobalScope 27 | import kotlinx.coroutines.Job 28 | import kotlinx.coroutines.launch 29 | import kotlinx.coroutines.runBlocking 30 | import org.junit.Test 31 | 32 | /** 33 | * Created by Ugljesa Jovanovic 34 | * ugljesa.jovanovic@ionspin.com 35 | * on 16-Mar-2019 36 | */ 37 | 38 | class BigInteger63ListJavaStringConversionTests() { 39 | 40 | @Test 41 | fun `Test parsing with sepcific values`() { 42 | // testParsingSingleTest("1234", 10) 43 | // testParsingSingleTest("922337203685477580799999999999990776627963145224192", 10) 44 | testParsingSingleTest("52656145834278593348959013841835216159447547700274555627155488768", 10) 45 | testParsingSingleTest("f", 16) 46 | } 47 | 48 | fun testParsingSingleTest(uIntArrayString: String, base: Int) { 49 | assertTrue { 50 | val parsed = BigInteger63LinkedListArithmetic.parseForBase(uIntArrayString, base) 51 | val javaBigIntParsed = BigInteger(uIntArrayString, base) 52 | 53 | parsed.toJavaBigInteger() == javaBigIntParsed 54 | } 55 | } 56 | 57 | @Test 58 | fun `Random toString test, in base 10`() { 59 | val seed = 1 60 | val random = Random(seed) 61 | val jobList: MutableList = mutableListOf() 62 | 63 | for (i in 1..Int.MAX_VALUE step 7001) { 64 | jobList.add( 65 | GlobalScope.launch { 66 | toStringSingleTest(listOf(random.nextULong()), 10) 67 | } 68 | ) 69 | } 70 | runBlocking { 71 | jobList.forEach { 72 | if (it.isCancelled) { 73 | fail("Some of the tests failed") 74 | } 75 | it.join() 76 | } 77 | } 78 | } 79 | 80 | @Test 81 | fun `Random toString test, in random base less than 36`() { 82 | val seed = 1 83 | val random = Random(seed) 84 | val jobList: MutableList = mutableListOf() 85 | for (i in 1..Int.MAX_VALUE step 7001) { 86 | jobList.add( 87 | GlobalScope.launch { 88 | toStringSingleTest( 89 | listOf(random.nextULong(), random.nextULong()), 90 | random.nextInt(2, 36) 91 | ) // 36 is the max java bigint supports 92 | } 93 | ) 94 | } 95 | 96 | runBlocking { 97 | jobList.forEach { 98 | if (it.isCancelled) { 99 | fail("Some of the tests failed") 100 | } 101 | it.join() 102 | } 103 | } 104 | } 105 | 106 | @Test 107 | fun `Test toString with specific values`() { 108 | toStringSingleTest(listOf(1234U), 10) 109 | } 110 | 111 | fun toStringSingleTest(operand: List, base: Int) { 112 | assertTrue { 113 | val result = BigInteger63LinkedListArithmetic.toString(operand, base) 114 | val javaBigIntResult = operand.toJavaBigInteger().toString(base) 115 | 116 | result == javaBigIntResult 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base63List/BigIntegerList63SqrtTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.base63List 19 | 20 | import com.ionspin.kotlin.bignum.integer.base63.BigInteger63LinkedListArithmetic 21 | import kotlin.random.Random 22 | import kotlin.random.nextULong 23 | import kotlin.test.assertTrue 24 | import org.junit.Test 25 | 26 | /** 27 | * Created by Ugljesa Jovanovic 28 | * ugljesa.jovanovic@ionspin.com 29 | * on 27-Apr-2019 30 | */ 31 | 32 | class BigIntegerList63SqrtTest() { 33 | 34 | @Test 35 | fun testSpecificSqrt() { 36 | assertTrue { 37 | val seed = 1 38 | val random = Random(seed) 39 | val operand = List(9) { random.nextULong() shr 1 } 40 | val a = BigInteger63LinkedListArithmetic.sqrt(operand) 41 | println("Operand: ${BigInteger63LinkedListArithmetic.toString(operand, 10)}") 42 | println("Sqrt: ${BigInteger63LinkedListArithmetic.toString(a.first, 10)}") 43 | println("Remainder: ${BigInteger63LinkedListArithmetic.toString(a.second, 10)}") 44 | val resultSqrt = BigInteger63LinkedListArithmetic.parseForBase( 45 | "7397044194494028975732055495441088126843300784390793504636322150624655374658058636588", 46 | 10 47 | ) 48 | val resultRem = BigInteger63LinkedListArithmetic.subtract( 49 | operand, 50 | BigInteger63LinkedListArithmetic.multiply(resultSqrt, resultSqrt) 51 | ) 52 | 53 | resultSqrt.equals(a.first) 54 | } 55 | 56 | assertTrue { 57 | val seed = 1 58 | val random = Random(seed) 59 | val operand = BigInteger63LinkedListArithmetic.parseForBase("123456789", 10) 60 | val a = BigInteger63LinkedListArithmetic.sqrt(operand) 61 | println("Operand: ${BigInteger63LinkedListArithmetic.toString(operand, 10)}") 62 | println("Sqrt: ${BigInteger63LinkedListArithmetic.toString(a.first, 10)}") 63 | println("Remainder: ${BigInteger63LinkedListArithmetic.toString(a.second, 10)}") 64 | val resultSqrt = BigInteger63LinkedListArithmetic.parseForBase( 65 | "11111", 66 | 10 67 | ) 68 | val resultRem = BigInteger63LinkedListArithmetic.subtract( 69 | operand, 70 | BigInteger63LinkedListArithmetic.multiply(resultSqrt, resultSqrt) 71 | ) 72 | 73 | resultSqrt.equals(a.first) 74 | } 75 | } 76 | 77 | @Test 78 | fun testSpecificSqrtInt() { 79 | val seed = 1 80 | val random = Random(seed) 81 | val a = List(1) { 144U } 82 | val sqrt = BigInteger63LinkedListArithmetic.sqrtInt(a) 83 | assertTrue { sqrt[0] == 12UL } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/integer/base63List/bigInteger63Utility.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer.base63List 19 | 20 | import java.math.BigInteger 21 | 22 | /** 23 | * Created by Ugljesa Jovanovic 24 | * ugljesa.jovanovic@ionspin.com 25 | * on 09-Mar-2019 26 | */ 27 | 28 | fun List.toJavaBigInteger(): BigInteger { 29 | return this.foldIndexed(BigInteger.valueOf(0)) { index, acc, digit -> 30 | acc.or(BigInteger(digit.toString(), 10).shiftLeft((index) * 63)) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/modular/JvmModularBigIntegerExponentiationTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.modular 19 | 20 | import com.ionspin.kotlin.bignum.integer.BigInteger 21 | import com.ionspin.kotlin.bignum.integer.Sign 22 | import com.ionspin.kotlin.bignum.integer.base63.toJavaBigInteger 23 | import com.ionspin.kotlin.bignum.toProperType 24 | import kotlin.random.Random 25 | import kotlin.random.nextULong 26 | import kotlin.test.Test 27 | import kotlin.test.assertTrue 28 | import kotlin.test.fail 29 | import kotlinx.coroutines.GlobalScope 30 | import kotlinx.coroutines.Job 31 | import kotlinx.coroutines.launch 32 | import kotlinx.coroutines.runBlocking 33 | 34 | /** 35 | * Created by Ugljesa Jovanovic 36 | * ugljesa.jovanovic@ionspin.com 37 | * on 28-Jul-2019 38 | */ 39 | 40 | class JvmModularBigIntegerExponentiationTest { 41 | @Test 42 | fun testRandomModularExponentiation() { 43 | val seed = 1 44 | val random = Random(seed) 45 | val jobList: MutableList = mutableListOf() 46 | for (i in 1..1000) { 47 | val aLength = random.nextInt(1, 500) 48 | val a = ULongArray(aLength) { 49 | random.nextULong() shr 1 50 | }.toProperType() 51 | 52 | val modulo = ULongArray(random.nextInt(1, 50)) { 53 | random.nextULong() shr 1 54 | }.toProperType() 55 | 56 | val creator = ModularBigInteger.creatorForModulo(BigInteger(modulo, Sign.POSITIVE)) 57 | 58 | val b = creator.fromInt(random.nextInt(500)) 59 | 60 | val job = GlobalScope.launch { 61 | try { 62 | val aMod = creator.fromBigInteger(BigInteger(a, Sign.POSITIVE)) 63 | singleDivisionTest(aMod, b) 64 | } catch (exception: Exception) { 65 | exception.printStackTrace() 66 | } 67 | } 68 | jobList.add(job) 69 | } 70 | runBlocking { 71 | jobList.forEach { 72 | if (it.isCancelled) { 73 | fail("Some of the tests failed") 74 | } 75 | it.join() 76 | } 77 | } 78 | } 79 | 80 | fun singleDivisionTest(a: ModularBigInteger, b: ModularBigInteger) { 81 | assertTrue { 82 | val result = a.pow(b) 83 | val javaResult = 84 | a.residue.toJavaBigInteger().modPow(b.residue.toJavaBigInteger(), b.modulus.toJavaBigInteger()) 85 | result.toBigInteger().toJavaBigInteger() == javaResult 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/modular/ModularBigIntegerDivisionTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.modular 19 | 20 | import com.ionspin.kotlin.bignum.integer.BigInteger 21 | import com.ionspin.kotlin.bignum.integer.Sign 22 | import com.ionspin.kotlin.bignum.integer.base63.toJavaBigInteger 23 | import com.ionspin.kotlin.bignum.toProperType 24 | import kotlin.random.Random 25 | import kotlin.random.nextULong 26 | import kotlin.test.assertTrue 27 | import kotlin.test.fail 28 | import kotlinx.coroutines.GlobalScope 29 | import kotlinx.coroutines.Job 30 | import kotlinx.coroutines.launch 31 | import kotlinx.coroutines.runBlocking 32 | import org.junit.Test 33 | 34 | /** 35 | * Created by Ugljesa Jovanovic 36 | * ugljesa.jovanovic@ionspin.com 37 | * on 22-Apr-2019 38 | */ 39 | 40 | class ModularBigIntegerDivisionTest { 41 | @Test 42 | fun testRandomModularDivision() { 43 | val seed = 1 44 | val random = Random(seed) 45 | val jobList: MutableList = mutableListOf() 46 | for (i in 1..1000) { 47 | val aLength = random.nextInt(2, 500) 48 | val a = ULongArray(aLength) { 49 | random.nextULong() shr 1 50 | }.toProperType() 51 | val b = ULongArray(random.nextInt(1, aLength)) { 52 | random.nextULong() shr 1 53 | }.toProperType() 54 | 55 | val modulo = ULongArray(random.nextInt(1, 500)) { 56 | random.nextULong() shr 1 57 | }.toProperType() 58 | val creator = ModularBigInteger.creatorForModulo(BigInteger(modulo, Sign.POSITIVE)) 59 | 60 | val job = GlobalScope.launch { 61 | try { 62 | val aMod = creator.fromBigInteger(BigInteger(a, Sign.POSITIVE)) 63 | val bMod = creator.fromBigInteger(BigInteger(b, Sign.POSITIVE)) 64 | if (aMod.checkIfDivisibleBoolean(aMod, bMod)) { 65 | singleDivisionTest(aMod, bMod) 66 | } 67 | } catch (exception: Exception) { 68 | exception.printStackTrace() 69 | } 70 | } 71 | jobList.add(job) 72 | } 73 | runBlocking { 74 | jobList.forEach { 75 | if (it.isCancelled) { 76 | fail("Some of the tests failed") 77 | } 78 | it.join() 79 | } 80 | } 81 | } 82 | 83 | @Test 84 | fun specificTest() { 85 | val creator = ModularBigInteger.creatorForModulo(15) 86 | val a = creator.fromInt(-10) 87 | val b = creator.fromInt(4) 88 | singleDivisionTest(a, b) 89 | } 90 | 91 | fun singleDivisionTest(a: ModularBigInteger, b: ModularBigInteger) { 92 | assertTrue { 93 | val result = a divrem b 94 | val javaResultQuotient = 95 | (a.residue.toJavaBigInteger() / b.residue.toJavaBigInteger()).mod(a.modulus.toJavaBigInteger()) 96 | val javaResultRemainder = 97 | (a.residue.toJavaBigInteger() % b.residue.toJavaBigInteger()).mod(a.modulus.toJavaBigInteger()) 98 | result.quotient.residue.toJavaBigInteger() == javaResultQuotient && 99 | result.remainder.residue.toJavaBigInteger() == javaResultRemainder 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /bignum/src/jvmTest/kotlin/com/ionspin/kotlin/bignum/modular/ModularBigIntegerMultiplication.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.modular 19 | 20 | import com.ionspin.kotlin.bignum.integer.BigInteger 21 | import com.ionspin.kotlin.bignum.integer.Sign 22 | import com.ionspin.kotlin.bignum.integer.base63.toJavaBigInteger 23 | import com.ionspin.kotlin.bignum.toProperType 24 | import kotlin.random.Random 25 | import kotlin.random.nextULong 26 | import kotlin.test.Test 27 | import kotlin.test.assertTrue 28 | import kotlin.test.fail 29 | import kotlinx.coroutines.GlobalScope 30 | import kotlinx.coroutines.Job 31 | import kotlinx.coroutines.launch 32 | import kotlinx.coroutines.runBlocking 33 | 34 | /** 35 | * Created by Ugljesa Jovanovic 36 | * ugljesa.jovanovic@ionspin.com 37 | * on 22-Apr-2019 38 | */ 39 | 40 | class ModularBigIntegerMultiplication { 41 | 42 | @Test 43 | fun testRandomModularMultiplication() { 44 | val seed = 1 45 | var random = Random(seed) 46 | val jobList: MutableList = mutableListOf() 47 | for (i in 1..1000) { 48 | val a = ULongArray(random.nextInt(1, 500)) { 49 | random.nextULong() shr 1 50 | }.toProperType() 51 | val b = ULongArray(random.nextInt(1, 500)) { 52 | random.nextULong() shr 1 53 | }.toProperType() 54 | 55 | val modulo = ULongArray(random.nextInt(1, 500)) { 56 | random.nextULong() shr 1 57 | }.toProperType() 58 | val creator = ModularBigInteger.creatorForModulo(BigInteger(modulo, Sign.POSITIVE)) 59 | 60 | val job = GlobalScope.launch { 61 | try { 62 | val aMod = creator.fromBigInteger(BigInteger(a, Sign.POSITIVE)) 63 | val bMod = creator.fromBigInteger(BigInteger(b, Sign.POSITIVE)) 64 | singleMultiplicationTest(aMod, bMod) 65 | } catch (exception: Exception) { 66 | exception.printStackTrace() 67 | } 68 | } 69 | jobList.add(job) 70 | } 71 | runBlocking { 72 | jobList.forEach { 73 | if (it.isCancelled) { 74 | fail("Some of the tests failed") 75 | } 76 | it.join() 77 | } 78 | } 79 | } 80 | 81 | @Test 82 | fun testRandomNegativeModularMultiplication() { 83 | val seed = 1 84 | var random = Random(seed) 85 | val jobList: MutableList = mutableListOf() 86 | for (i in 1..1000) { 87 | val a = ULongArray(random.nextInt(1, 500)) { 88 | random.nextULong() shr 1 89 | }.toProperType() 90 | val b = ULongArray(random.nextInt(1, 500)) { 91 | random.nextULong() shr 1 92 | }.toProperType() 93 | 94 | val modulo = ULongArray(random.nextInt(1, 500)) { 95 | random.nextULong() shr 1 96 | }.toProperType() 97 | val creator = ModularBigInteger.creatorForModulo(BigInteger(modulo, Sign.POSITIVE)) 98 | 99 | val job = GlobalScope.launch { 100 | try { 101 | val aMod = creator.fromBigInteger(BigInteger(a, Sign.POSITIVE)) 102 | val bMod = creator.fromBigInteger(BigInteger(b, Sign.NEGATIVE)) 103 | singleMultiplicationTest(aMod, bMod) 104 | } catch (exception: Exception) { 105 | exception.printStackTrace() 106 | } 107 | } 108 | jobList.add(job) 109 | } 110 | runBlocking { 111 | jobList.forEach { 112 | if (it.isCancelled) { 113 | fail("Some of the tests failed") 114 | } 115 | it.join() 116 | } 117 | } 118 | } 119 | 120 | fun singleMultiplicationTest(a: ModularBigInteger, b: ModularBigInteger) { 121 | assertTrue { 122 | val result = a * b 123 | val javaResult = 124 | (a.residue.toJavaBigInteger() * b.residue.toJavaBigInteger()).mod(a.modulus.toJavaBigInteger()) 125 | result.residue.toJavaBigInteger() == javaResult 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /bignum/src/linuxMain/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ionspin/kotlin-multiplatform-bignum/e2ec7901443ace456ca457fc70b9707b9e0af478/bignum/src/linuxMain/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder -------------------------------------------------------------------------------- /bignum/src/linuxTest/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ionspin/kotlin-multiplatform-bignum/e2ec7901443ace456ca457fc70b9707b9e0af478/bignum/src/linuxTest/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder -------------------------------------------------------------------------------- /bignum/src/nativeMain/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ionspin/kotlin-multiplatform-bignum/e2ec7901443ace456ca457fc70b9707b9e0af478/bignum/src/nativeMain/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder -------------------------------------------------------------------------------- /bignum/src/nativeMain/kotlin/com/ionspin/kotlin/bignum/integer/PlatformWorkarounds.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | package com.ionspin.kotlin.bignum.integer 19 | 20 | /** 21 | * Created by Ugljesa Jovanovic 22 | * ugljesa.jovanovic@ionspin.com 23 | * on 02-Jun-2019 24 | */ 25 | actual object RuntimePlatform { 26 | actual fun currentPlatform(): Platform { 27 | return Platform.NATIVE 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /bignum/src/nativeTest/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ionspin/kotlin-multiplatform-bignum/e2ec7901443ace456ca457fc70b9707b9e0af478/bignum/src/nativeTest/kotlin/com/ionspin/kotlin/bignum/integer/Placeholder -------------------------------------------------------------------------------- /bignum/src/wasmJsMain/kotlin/com/ionspin/kotlin/bignum/integer/PlatformWorkarounds.wasmJs.kt: -------------------------------------------------------------------------------- 1 | package com.ionspin.kotlin.bignum.integer 2 | 3 | actual object RuntimePlatform { 4 | /** 5 | * We need to know if we are running on a platform that doesn't know how to tell decimal and integer apart. 6 | */ 7 | actual fun currentPlatform(): Platform { 8 | return Platform.WASMJS 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /bignum/src/wasmJsTest/kotlin/com/ionspin/kotlin/bignum/TestUtil.wasmJs.kt: -------------------------------------------------------------------------------- 1 | package com.ionspin.kotlin.bignum 2 | -------------------------------------------------------------------------------- /bignum/src/wasmWasiMain/kotlin/com/ionspin/kotlin/bignum/integer/PlatformWorkarounds.wasmWasi.kt: -------------------------------------------------------------------------------- 1 | package com.ionspin.kotlin.bignum.integer 2 | 3 | actual object RuntimePlatform { 4 | /** 5 | * We need to know if we are running on a platform that doesn't know how to tell decimal and integer apart. 6 | */ 7 | actual fun currentPlatform(): Platform = Platform.WASMJS 8 | } 9 | -------------------------------------------------------------------------------- /build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | 19 | buildscript { 20 | 21 | repositories { 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | classpath("org.jetbrains.dokka:dokka-gradle-plugin:${Versions.dokkaPlugin}") 27 | } 28 | } 29 | 30 | allprojects { 31 | repositories { 32 | mavenCentral() 33 | maven { 34 | url = uri("https://oss.sonatype.org/content/repositories/snapshots") 35 | } 36 | } 37 | } 38 | 39 | plugins { 40 | kotlin("multiplatform") version Versions.kotlin apply false 41 | } 42 | 43 | group = "com.ionspin.kotlin" 44 | 45 | 46 | 47 | repositories { 48 | mavenCentral() 49 | } 50 | -------------------------------------------------------------------------------- /buildSrc/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | 19 | plugins { 20 | `kotlin-dsl` 21 | } 22 | 23 | buildscript { 24 | repositories { 25 | mavenCentral() 26 | } 27 | } 28 | 29 | repositories { 30 | mavenCentral() 31 | } 32 | -------------------------------------------------------------------------------- /buildSrc/src/main/kotlin/Deps.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | object Versions { 19 | val kotlinCoroutines = "1.9.0-RC" 20 | val kotlin = "2.0.0" 21 | val kotlinSerialization = "1.7.1" 22 | val dokkaPlugin = "1.9.10" 23 | } 24 | 25 | val projectVersion = "0.3.11-SNAPSHOT" 26 | 27 | object Deps { 28 | 29 | object Common { 30 | val stdLib = "stdlib-common" 31 | val test = "test-common" 32 | val testAnnotation = "test-annotations-common" 33 | val coroutines = "org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.kotlinCoroutines}" 34 | val testCoroutines = "org.jetbrains.kotlinx:kotlinx-coroutines-test:${Versions.kotlinCoroutines}" 35 | val kotlinxSerialization = "org.jetbrains.kotlinx:kotlinx-serialization-json:${Versions.kotlinSerialization}" 36 | val bignum = "com.ionspin.kotlin:bignum:$projectVersion" 37 | } 38 | 39 | object Js { 40 | val stdLib = "stdlib-js" 41 | val test = "test-js" 42 | val coroutines = "org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.kotlinCoroutines}" 43 | val serialization = "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:${Versions.kotlinSerialization}" 44 | } 45 | 46 | object Jvm { 47 | val stdLib = "stdlib-jdk8" 48 | val test = "test" 49 | val testJUnit = "test-junit" 50 | val reflection = "reflect" 51 | val coroutinesCore = "org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.kotlinCoroutines}" 52 | val serialization = "org.jetbrains.kotlinx:kotlinx-serialization-runtime:${Versions.kotlinSerialization}" 53 | val coroutinesTest = "org.jetbrains.kotlinx:kotlinx-coroutines-test:${Versions.kotlinCoroutines}" 54 | } 55 | 56 | object iOs { 57 | val serialization = "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:${Versions.kotlinSerialization}" 58 | val coroutines = "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:${Versions.kotlinCoroutines}" 59 | } 60 | 61 | object Native { 62 | val serialization = "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:${Versions.kotlinSerialization}" 63 | val coroutines = "org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.kotlinCoroutines}" 64 | } 65 | 66 | object WasmJs { 67 | val stdLib = "stdlib-wasm-js" 68 | val test = "test-wasm-js" 69 | } 70 | 71 | object Project { 72 | val bignum = ":bignum" 73 | } 74 | } 75 | 76 | object PluginsDeps { 77 | object PluginVersions { 78 | val spotlessVersion = "5.14.0" 79 | val kotlinxSerialization = Versions.kotlin 80 | } 81 | 82 | 83 | val multiplatform = "multiplatform" 84 | val node = "com.github.node-gradle.node" 85 | val mavenPublish = "maven-publish" 86 | val signing = "signing" 87 | val dokka = "org.jetbrains.dokka" 88 | val spotless = "com.diffplug.spotless" 89 | val kotlinxSerialization = "plugin.serialization" 90 | } 91 | 92 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2019 Ugljesa Jovanovic 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | # 17 | org.gradle.parallel=true 18 | kotlin.code.style=official 19 | org.gradle.jvmargs=-Xmx5g 20 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ionspin/kotlin-multiplatform-bignum/e2ec7901443ace456ca457fc70b9707b9e0af478/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2019 Ugljesa Jovanovic 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | # 17 | distributionBase=GRADLE_USER_HOME 18 | distributionPath=wrapper/dists 19 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip 20 | zipStoreBase=GRADLE_USER_HOME 21 | zipStorePath=wrapper/dists 22 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS="-Xmx64m" 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /linuxBuild.sh: -------------------------------------------------------------------------------- 1 | if [[ -v "GITHUB_ACTION" ]]; then 2 | echo "Running without verification" 3 | ./gradlew build --dependency-verification lenient || exit 1 4 | else 5 | echo "Running with verification" 6 | ./gradlew build || exit 1 7 | fi 8 | exit 0 9 | -------------------------------------------------------------------------------- /linuxBuildAndPublish.sh: -------------------------------------------------------------------------------- 1 | ./gradlew build \ 2 | bignum:publishJvmPublicationToSnapshotRepository \ 3 | bignum:publishJsPublicationToSnapshotRepository \ 4 | bignum:publishKotlinMultiplatformPublicationToSnapshotRepository \ 5 | bignum:publishLinuxX64PublicationToSnapshotRepository \ 6 | bignum:publishLinuxArm64PublicationToSnapshotRepository \ 7 | bignum:publishAndroidNativeX64PublicationToSnapshotRepository \ 8 | bignum:publishAndroidNativeX86PublicationToSnapshotRepository \ 9 | bignum:publishAndroidNativeArm32PublicationToSnapshotRepository \ 10 | bignum:publishAndroidNativeArm64PublicationToSnapshotRepository \ 11 | bignum:publishWasmJsPublicationToSnapshotRepository \ 12 | bignum:publishWasmWasiPublicationToSnapshotRepository || exit 1 13 | 14 | ./gradlew \ 15 | bignum-serialization-kotlinx:publishJsPublicationToSnapshotRepository \ 16 | bignum-serialization-kotlinx:publishJvmPublicationToSnapshotRepository \ 17 | bignum-serialization-kotlinx:publishJsPublicationToSnapshotRepository \ 18 | bignum-serialization-kotlinx:publishKotlinMultiplatformPublicationToSnapshotRepository \ 19 | bignum-serialization-kotlinx:publishLinuxX64PublicationToSnapshotRepository \ 20 | bignum-serialization-kotlinx:publishLinuxArm64PublicationToSnapshotRepository \ 21 | bignum-serialization-kotlinx:publishAndroidNativeX64PublicationToSnapshotRepository \ 22 | bignum-serialization-kotlinx:publishAndroidNativeX86PublicationToSnapshotRepository \ 23 | bignum-serialization-kotlinx:publishAndroidNativeArm32PublicationToSnapshotRepository \ 24 | bignum-serialization-kotlinx:publishAndroidNativeArm64PublicationToSnapshotRepository \ 25 | bignum-serialization-kotlinx:publishWasmJsPublicationToSnapshotRepository \ 26 | bignum-serialization-kotlinx:publishWasmWasiPublicationToSnapshotRepository || exit 1 27 | exit 0 28 | -------------------------------------------------------------------------------- /linuxPublishToMaven.sh: -------------------------------------------------------------------------------- 1 | ./gradlew build \ 2 | bignum:publishJvmPublicationToMavenRepository \ 3 | bignum:publishJsPublicationToMavenRepository \ 4 | bignum:publishKotlinMultiplatformPublicationToMavenRepository \ 5 | bignum:publishLinuxX64PublicationToMavenRepository \ 6 | bignum:publishLinuxArm64PublicationToMavenRepository \ 7 | bignum:publishAndroidNativeX64PublicationToMavenRepository \ 8 | bignum:publishAndroidNativeX86PublicationToMavenRepository \ 9 | bignum:publishAndroidNativeArm32PublicationToMavenRepository \ 10 | bignum:publishAndroidNativeArm64PublicationToMavenRepository \ 11 | bignum:publishWasmJsPublicationToMavenRepository \ 12 | bignum:publishWasmWasiPublicationToMavenRepository || exit 1 13 | 14 | ./gradlew \ 15 | bignum-serialization-kotlinx:publishJsPublicationToMavenRepository \ 16 | bignum-serialization-kotlinx:publishJvmPublicationToMavenRepository \ 17 | bignum-serialization-kotlinx:publishJsPublicationToMavenRepository \ 18 | bignum-serialization-kotlinx:publishKotlinMultiplatformPublicationToMavenRepository \ 19 | bignum-serialization-kotlinx:publishLinuxX64PublicationToMavenRepository \ 20 | bignum-serialization-kotlinx:publishLinuxArm64PublicationToMavenRepository \ 21 | bignum-serialization-kotlinx:publishAndroidNativeX64PublicationToMavenRepository \ 22 | bignum-serialization-kotlinx:publishAndroidNativeX86PublicationToMavenRepository \ 23 | bignum-serialization-kotlinx:publishAndroidNativeArm32PublicationToMavenRepository \ 24 | bignum-serialization-kotlinx:publishAndroidNativeArm64PublicationToMavenRepository \ 25 | bignum-serialization-kotlinx:publishWasmJsPublicationToMavenRepository \ 26 | bignum-serialization-kotlinx:publishWasmWasiPublicationToMavenRepository || exit 1 27 | exit 0 -------------------------------------------------------------------------------- /macBuild.sh: -------------------------------------------------------------------------------- 1 | if [[ -n "$GITHUB_ACTION" ]]; then 2 | echo "Running without verification" 3 | ./gradlew build --dependency-verification lenient -x watchosX64Test -x jsBrowserTest -x wasmJsBrowserTest || exit 1 4 | else 5 | echo "Running with verification" 6 | ./gradlew build -x watchosX64Test -x jsBrowserTest -x wasmJsBrowserTest || exit 1 7 | fi 8 | 9 | exit 0 10 | -------------------------------------------------------------------------------- /macBuildAndPublish.sh: -------------------------------------------------------------------------------- 1 | ./gradlew build -x watchosX64Test publishIosArm64PublicationToSnapshotRepository publishIosX64PublicationToSnapshotRepository \ 2 | publishMacosX64PublicationToSnapshotRepository \ 3 | publishTvosArm64PublicationToSnapshotRepository publishTvosX64PublicationToSnapshotRepository \ 4 | publishWatchosArm32PublicationToSnapshotRepository publishWatchosArm64PublicationToSnapshotRepository \ 5 | publishWatchosX64PublicationToSnapshotRepository \ 6 | publishMacosArm64PublicationToSnapshotRepository publishIosSimulatorArm64PublicationToSnapshotRepository \ 7 | publishTvosSimulatorArm64PublicationToSnapshotRepository publishWatchosSimulatorArm64PublicationToSnapshotRepository -x jsBrowserTest -x wasmJsBrowserTest || exit 1 8 | exit 0 9 | -------------------------------------------------------------------------------- /macPublishToMaven.sh: -------------------------------------------------------------------------------- 1 | ./gradlew build -x watchosX64Test \ 2 | publishIosArm64PublicationToMavenRepository \ 3 | publishIosX64PublicationToMavenRepository \ 4 | publishMacosX64PublicationToMavenRepository \ 5 | publishTvosArm64PublicationToMavenRepository \ 6 | publishTvosX64PublicationToMavenRepository \ 7 | publishWatchosArm32PublicationToMavenRepository \ 8 | publishWatchosArm64PublicationToMavenRepository \ 9 | publishWatchosX64PublicationToMavenRepository \ 10 | publishMacosArm64PublicationToMavenRepository \ 11 | publishIosSimulatorArm64PublicationToMavenRepository \ 12 | publishTvosSimulatorArm64PublicationToMavenRepository \ 13 | publishWatchosSimulatorArm64PublicationToMavenRepository -x jsBrowserTest -x wasmJsBrowserTest || exit 1 14 | exit 0 -------------------------------------------------------------------------------- /publishToMavenOneByOne.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ./gradlew publishJsPublicationToMavenRepository || exit 1 4 | ./gradlew publishJvmPublicationToMavenRepository || exit 1 5 | ./gradlew publishKotlinMultiplatformPublicationToMavenRepository || exit 1 6 | ./gradlew publishLinuxArm32HfpPublicationToMavenRepository || exit 1 7 | ./gradlew publishLinuxArm64PublicationToMavenRepository || exit 1 8 | ./gradlew publishLinuxPublicationToMavenRepository || exit 1 9 | ./gradlew publishMetadataPublicationToMavenRepository || exit 1 10 | -------------------------------------------------------------------------------- /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Ugljesa Jovanovic 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | pluginManagement { 19 | repositories { 20 | mavenCentral() 21 | gradlePluginPortal() 22 | } 23 | resolutionStrategy { 24 | eachPlugin { 25 | if (requested.id.id == "kotlin-multiplatform") { 26 | useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${requested.version}") 27 | } 28 | } 29 | } 30 | } 31 | rootProject.name = "KotlinBigInteger" 32 | include("bignum") 33 | include("bignum-serialization-kotlinx") 34 | 35 | -------------------------------------------------------------------------------- /windowsBuild.sh: -------------------------------------------------------------------------------- 1 | if [[ -n "$GITHUB_ACTION" ]]; then 2 | echo "Running without verification" 3 | ./gradlew build --dependency-verification lenient -x spotlessKotlinCheck -x spotlessKotlinGradleCheck -x jsBrowserTest -x wasmJsBrowserTest || exit 1 4 | else 5 | echo "Running with verification" 6 | ./gradlew build -x spotlessKotlinCheck -x spotlessKotlinGradleCheck -x jsBrowserTest -x wasmJsBrowserTest || exit 1 7 | fi 8 | 9 | ./gradlew --stop 10 | exit 0 11 | -------------------------------------------------------------------------------- /windowsBuildAndPublish.sh: -------------------------------------------------------------------------------- 1 | ./gradlew --no-daemon build bignum:publishMingwX64PublicationToSnapshotRepository -x spotlessKotlinCheck -x spotlessKotlinGradleCheck -x jsBrowserTest -x wasmJsBrowserTest || exit 1 2 | ./gradlew --no-daemon bignum-serialization-kotlinx:publishMingwX64PublicationToSnapshotRepository -x spotlessKotlinCheck -x spotlessKotlinGradleCheck -x jsBrowserTest -x wasmJsBrowserTest || exit 1 3 | ./gradlew --stop 4 | exit 0 5 | -------------------------------------------------------------------------------- /windowsPublishToMaven.sh: -------------------------------------------------------------------------------- 1 | ./gradlew --no-daemon publishMingwX64PublicationToMavenRepository -x spotlessKotlinCheck -x spotlessKotlinGradleCheck -x jsBrowserTest -x wasmJsBrowserTest 2 | ./gradlew --stop 3 | --------------------------------------------------------------------------------