/*
 * Copyright 2022 ConsenSys Software Inc.
 *
 * 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 dis-
 * tributed 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 dafnyevm;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Arrays;

import org.junit.jupiter.api.Test;

import dafnyevm.util.Bytes;
import dafnyevm.util.Precompiles;
import evmtools.util.Hex;

public class PrecompiledTests {
    // ==============================================================
    // Blake2bf
    // ==============================================================
//
//    @Test
//    public void test_blake() {
//        byte[] bytes = new byte[8];
//        for(long i = 0;i!=Long.MAX_VALUE;++i) {
//            Precompiles.toLittleEndianBytes(i, 0, bytes);
//            assertEquals(i,Precompiles.fromLittleEndian64(bytes, 0));
//        }
//    }

    @Test
  public void test_single_blake() {
      byte[] bytes = new byte[8];
      long i = 2147483648L;
      Bytes.toLittleEndianBytes(i, 0, bytes);
      assertEquals(i,Bytes.fromLittleEndian64(bytes, 0));
  }

    @Test
    public void test_blake_01() {
        // Test vector 1
        byte[] input = Hex.toBytes(
                "0x00000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001");
        byte[] output = new byte[0];
        assertArrayEquals(output,Precompiles.blake2f(input));
    }

    @Test
    public void test_blake_02() {
        // Test vector 1
        byte[] input = Hex.toBytes(
                "0x000000000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001");
        byte[] output = new byte[0];
        assertArrayEquals(output,Precompiles.blake2f(input));
    }

    @Test
    public void test_blake_03() {
        // Test vector 1
        byte[] input = Hex.toBytes(
                "0x0000000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000002");
        byte[] output = new byte[0];
        assertArrayEquals(output,Precompiles.blake2f(input));
    }

    @Test
    public void test_blake_04() {
        // Test vector 4
        byte[] input = Hex.toBytes(
                "0x0000000048c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001");
        byte[] output = Hex.toBytes("0x08c9bcf367e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d282e6ad7f520e511f6c3e2b8c68059b9442be0454267ce079217e1319cde05b");
        assertArrayEquals(output,Precompiles.blake2f(input));
    }

    @Test
    public void test_blake_05() {
        // Test vector 5
        byte[] input = Hex.toBytes(
                "0x0000000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001");
        byte[] output = Hex.toBytes(
                "0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923");
        assertArrayEquals(output, Precompiles.blake2f(input));
    }

    @Test
    public void test_blake_06() {
        // Test vector 6
        byte[] input = Hex.toBytes(
                "0x0000000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000");
        byte[] output = Hex.toBytes(
                "0x75ab69d3190a562c51aef8d88f1c2775876944407270c42c9844252c26d2875298743e7f6d5ea2f2d3e8d226039cd31b4e426ac4f2d3d666a610c2116fde4735");
        assertArrayEquals(output, Precompiles.blake2f(input));
    }

    @Test
    public void test_blake_07() {
        // Test vector 7
        byte[] input = Hex.toBytes(
                "0x0000000148c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001");
        byte[] output = Hex.toBytes(
                "0xb63a380cb2897d521994a85234ee2c181b5f844d2c624c002677e9703449d2fba551b3a8333bcdf5f2f7e08993d53923de3d64fcc68c034e717b9293fed7a421");
        assertArrayEquals(output, Precompiles.blake2f(input));
    }

//    Commented out because it takes a long time to run due to the number of
//    rounds. However, it should still pass!
//    @Test
//    public void test_blake_08() {
//        // Test vector 8
//        byte[] input = Hex.toBytes(
//                "0xffffffff48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001");
//        byte[] output = Hex.toBytes(
//                "0xfc59093aafa9ab43daae0e914c57635c5402d8e3d2130eb9b3cc181de7f0ecf9b22bf99a7815ce16419e200e01846e6b5df8cc7703041bbceb571de6631d2615");
//        assertArrayEquals(output, Precompiles.blake2f(input));
//    }
}
