From f5b3eb6b923bf26eb6b21c2422c738c88f2cc1f9 Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Thu, 21 May 2020 14:56:28 +0200 Subject: [PATCH] Model argon2Matrix --- .../keyderivation/argon2/Argon2Matrix.kt | 48 ++++++++++++++++ .../crypto/hash/argon/Argon2MatrixTest.kt | 55 +++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2Matrix.kt create mode 100644 multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/argon/Argon2MatrixTest.kt diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2Matrix.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2Matrix.kt new file mode 100644 index 0000000..a90a0ac --- /dev/null +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2Matrix.kt @@ -0,0 +1,48 @@ +/* + * Copyright 2019 Ugljesa Jovanovic + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@file:Suppress("EXPERIMENTAL_API_USAGE") + +package com.ionspin.kotlin.crypto.keyderivation.argon2 + +/** + * Created by Ugljesa Jovanovic + * ugljesa.jovanovic@ionspin.com + * on 21-May-2020 + */ + class Argon2Matrix(val columnCount: Int, val rowCount: Int) { + + internal val storage: UByteArray = UByteArray(columnCount * rowCount * 1024) + + operator fun get(rowPosition: Int, columnPosition: Int, inBlockPosition: Int) : UByte { + return storage[rowPosition * (columnCount - 1) * 1024 + columnPosition * 1024 + inBlockPosition] + } + + operator fun get(rowPosition: Int, columnPosition: Int) : UByteArray { + println("Expensive.") + return storage.copyOfRange( + rowPosition * (columnCount - 1) * 1024 + columnPosition * 1024, + rowPosition * (columnCount - 1) * 1024 + columnPosition * 1024 + 1024 + ) + } + + operator fun get(rowPosition: Int) : Array { + return Array(columnCount) { + this.get(rowPosition, it) + } + + } +} \ No newline at end of file diff --git a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/argon/Argon2MatrixTest.kt b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/argon/Argon2MatrixTest.kt new file mode 100644 index 0000000..a6aa3e5 --- /dev/null +++ b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/argon/Argon2MatrixTest.kt @@ -0,0 +1,55 @@ +/* + * Copyright 2019 Ugljesa Jovanovic + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@file:Suppress("EXPERIMENTAL_API_USAGE", "EXPERIMENTAL_UNSIGNED_LITERALS") + +package com.ionspin.kotlin.crypto.hash.argon + +import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Matrix +import com.ionspin.kotlin.crypto.util.hexColumsPrint +import kotlin.test.Test +import kotlin.test.assertTrue + +/** + * Created by Ugljesa Jovanovic + * ugljesa.jovanovic@ionspin.com + * on 21-May-2020 + */ +class Argon2MatrixTest { + val zeroesBlock = UByteArray(1024) { 0U } + val onesBlock = UByteArray(1024) { 1U } + val twosBlock = UByteArray(1024) { 2U } + val threesBlock = UByteArray(1024) { 3U } + + + @Test + fun debugTest() { + val argon2Matrix = Argon2Matrix(2, 2) + (zeroesBlock + onesBlock + twosBlock + threesBlock).copyInto(argon2Matrix.storage) + println(argon2Matrix[0, 0, 0]) + println(argon2Matrix[1, 0, 0]) + println(argon2Matrix[2, 0, 0]) + println(argon2Matrix[3, 0, 0]) + argon2Matrix.storage.hexColumsPrint(1024) + assertTrue { + argon2Matrix[0, 0, 0] == 0U.toUByte() && + argon2Matrix[1, 0, 0] == 1U.toUByte() && + argon2Matrix[2, 0, 0] == 2U.toUByte() && + argon2Matrix[3, 0, 0] == 3U.toUByte() + + } + } +} \ No newline at end of file