All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.org.kotlincrypto.sponges.keccak.F400.kt Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2023 Matthew Nelson
 *
 * 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
 *
 *     https://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.
 **/
package org.kotlincrypto.sponges.keccak

import kotlin.experimental.and
import kotlin.experimental.inv
import kotlin.experimental.xor
import kotlin.jvm.JvmSynthetic

/**
 * [State] for Keccak-f[400]
 * */
public class F400: State {
    public constructor(): super(roundCount = 20, state = Array(P_LEN) { 0 })
    private constructor(state: F400): super(state.roundCount, state.state.copyOf())
    public override fun copy(): F400 = F400(this)

    @JvmSynthetic
    internal override fun  withContext(block: Context.() -> T): T = block(F400Context)

    private object F400Context: Context {
        private const val MASK: Int = 0xffff

        override fun and(a: Short, other: Short): Short = a and other
        override fun inv(a: Short): Short = a.inv()
        override fun xor(a: Short, other: Short): Short = a xor other
        override fun rotateLeft(a: Short, n: Int): Short {
            val bitCount = n % Short.SIZE_BITS
            return (
                ((a.toInt() and MASK) shl bitCount) or ((a.toInt() and MASK) ushr (Short.SIZE_BITS - bitCount))
            ).toShort()
        }
        override fun RC(index: Int): Short = RC[index].toShort()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy