LCOV - code coverage report
Current view: top level - cmdline - metro.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 45 45 100.0 %
Date: 2026-03-15 15:58:19 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2019 Andrea Mazzoleni
       3             :  *
       4             :  * This program is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 3 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * This program is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : /*
      19             :  * Derivative work from MetroHash128::Hash
      20             :  *
      21             :  * This version extends the original MetroHash128::Hash to accept a full 128-bit seed.
      22             :  *
      23             :  * Note: The original algorithm's behavior can be perfectly replicated by
      24             :  * setting the low and high part of the seed to the same 64-bit value.
      25             :  *
      26             :  * https://github.com/jandrewrogers/MetroHash/blob/master/src/metrohash128.cpp
      27             :  *
      28             :  * Copyright 2015-2018 J. Andrew Rogers
      29             :  *
      30             :  * Licensed under the Apache License, Version 2.0 (the "License");
      31             :  * you may not use this file except in compliance with the License.
      32             :  * You may obtain a copy of the License at
      33             :  *
      34             :  *     http://www.apache.org/licenses/LICENSE-2.0
      35             :  *
      36             :  * Unless required by applicable law or agreed to in writing, software
      37             :  * distributed under the License is distributed on an "AS IS" BASIS,
      38             :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      39             :  * See the License for the specific language governing permissions and
      40             :  * limitations under the License.
      41             :  */
      42             : 
      43             : static const uint64_t k0 = 0xC83A91E1;
      44             : static const uint64_t k1 = 0x8648DBDB;
      45             : static const uint64_t k2 = 0x7BDEC03B;
      46             : static const uint64_t k3 = 0x2F5870A5;
      47             : 
      48         369 : void MetroHash128(const void* data, size_t size, const uint8_t* seed, uint8_t* digest)
      49             : {
      50         369 :         const uint8_t* ptr = data;
      51             :         uint64_t v[4];
      52             : 
      53             :         /*
      54             :          * EXTENSION: Initialize primary state with 128-bit seed (seedL and seedH)
      55             :          * Ensures both halves influence the hash even for short messages (< 32 bytes)
      56             :          *
      57             :          * Original code was:
      58             :          * v[0] = (static_cast<uint64_t>(seed) - k0) * k3;
      59             :          * v[1] = (static_cast<uint64_t>(seed) + k1) * k2;
      60             :          */
      61         369 :         v[0] = (util_read64(seed) - k0) * k3;
      62         369 :         v[1] = (util_read64(seed + 8) + k1) * k2;
      63             : 
      64         369 :         if (size >= 32) {
      65             :                 /*
      66             :                  * EXTENSION: Initialize secondary state using both halves of the 128-bit seed
      67             :                  *
      68             :                  * Original code was:
      69             :                  * v[2] = (static_cast<uint64_t>(seed) + k0) * k2;
      70             :                  * v[3] = (static_cast<uint64_t>(seed) - k1) * k3;
      71             :                  */
      72         333 :                 v[2] = (util_read64(seed) + k0) * k2;
      73         333 :                 v[3] = (util_read64(seed + 8) - k1) * k3;
      74             : 
      75             :                 do {
      76      852876 :                         v[0] += util_read64(ptr) * k0; ptr += 8; v[0] = util_rotr64(v[0], 29) + v[2];
      77      852876 :                         v[1] += util_read64(ptr) * k1; ptr += 8; v[1] = util_rotr64(v[1], 29) + v[3];
      78      852876 :                         v[2] += util_read64(ptr) * k2; ptr += 8; v[2] = util_rotr64(v[2], 29) + v[0];
      79      852876 :                         v[3] += util_read64(ptr) * k3; ptr += 8; v[3] = util_rotr64(v[3], 29) + v[1];
      80      852876 :                         size -= 32;
      81      852876 :                 } while (size >= 32);
      82             : 
      83         333 :                 v[2] ^= util_rotr64(((v[0] + v[3]) * k0) + v[1], 21) * k1;
      84         333 :                 v[3] ^= util_rotr64(((v[1] + v[2]) * k1) + v[0], 21) * k0;
      85         333 :                 v[0] ^= util_rotr64(((v[0] + v[2]) * k0) + v[3], 21) * k1;
      86         333 :                 v[1] ^= util_rotr64(((v[1] + v[3]) * k1) + v[2], 21) * k0;
      87             :         }
      88             : 
      89         369 :         if (size >= 16) {
      90         132 :                 v[0] += util_read64(ptr) * k2; ptr += 8; v[0] = util_rotr64(v[0], 33) * k3;
      91         132 :                 v[1] += util_read64(ptr) * k2; ptr += 8; v[1] = util_rotr64(v[1], 33) * k3;
      92         132 :                 v[0] ^= util_rotr64((v[0] * k2) + v[1], 45) * k1;
      93         132 :                 v[1] ^= util_rotr64((v[1] * k3) + v[0], 45) * k0;
      94         132 :                 size -= 16;
      95             :         }
      96             : 
      97         369 :         if (size >= 8) {
      98         134 :                 v[0] += util_read64(ptr) * k2; ptr += 8; v[0] = util_rotr64(v[0], 33) * k3;
      99         134 :                 v[0] ^= util_rotr64((v[0] * k2) + v[1], 27) * k1;
     100         134 :                 size -= 8;
     101             :         }
     102             : 
     103         369 :         if (size >= 4) {
     104         132 :                 v[1] += util_read32(ptr) * k2; ptr += 4; v[1] = util_rotr64(v[1], 33) * k3;
     105         132 :                 v[1] ^= util_rotr64((v[1] * k3) + v[0], 46) * k0;
     106         132 :                 size -= 4;
     107             :         }
     108             : 
     109         369 :         if (size >= 2) {
     110         135 :                 v[0] += util_read16(ptr) * k2; ptr += 2; v[0] = util_rotr64(v[0], 33) * k3;
     111         135 :                 v[0] ^= util_rotr64((v[0] * k2) + v[1], 22) * k1;
     112         135 :                 size -= 2;
     113             :         }
     114             : 
     115         369 :         if (size >= 1) {
     116         133 :                 v[1] += util_read8(ptr) * k2; v[1] = util_rotr64(v[1], 33) * k3;
     117         133 :                 v[1] ^= util_rotr64((v[1] * k3) + v[0], 58) * k0;
     118             :         }
     119             : 
     120         369 :         v[0] += util_rotr64((v[0] * k0) + v[1], 13);
     121         369 :         v[1] += util_rotr64((v[1] * k1) + v[0], 37);
     122         369 :         v[0] += util_rotr64((v[0] * k2) + v[1], 13);
     123         369 :         v[1] += util_rotr64((v[1] * k3) + v[0], 37);
     124             : 
     125         369 :         util_write64(digest, v[0]);
     126         369 :         util_write64(digest + 8, v[1]);
     127         369 : }
     128             : 

Generated by: LCOV version 1.0