Giter Club home page Giter Club logo

floppsy's Introduction

๐Ÿค floppsy

A tiny, simple and SMHasher-passing slow (200Mb/s) hash designed for floating point hardware. It's also non-deterministic (different values depend on architecture, due to quirks and differences in floating point implementations).

Link to the SUPERCOP ECRYPT benchmark for floppsy

demo

play with a demo online here

construction

constructed using floating point multiplication, division and addition.

based on continued and egyptian fractions.

no bit operations used in the making of this hash.

accolades

passes smhasher

see the results for all tests

also see an independent confirmation of these results

c source

FORCE_INLINE void q ( double * state, double key_val, 
         double numerator, double denominator )
{
  state[0] += numerator / denominator;
  state[0] = 1.0 / state[0];

  state[1] += key_val + M_PI;
  state[1] = numerator / state[1];
}

//---------
// round function : process the message 

FORCE_INLINE void round ( const uint8_t * msg, long len, 
            double * state ) 
{
  double numerator = 1.0;

  // Loop
  for( long i = 0; i < len; i++ ) {
    double val = (double)msg[i];
    double denominator = (M_E * val + i + 1) / state[1];

    q( state, val, numerator, denominator );

    numerator = denominator + 1;
  }
}

//---------
// setup function : setup the state

FORCE_INLINE void setup ( double * state, double init = 0 ) 
{
  state[0] += init != 0 ? pow(init + 1.0/init, 1.0/3) : 3.0;
  state[1] += init != 0 ? pow(init + 1.0/init, 1.0/7) : 1.0/7;
}

//---------
// floppsyhash
// with 64 bit continued egyptian fractions

void floppsyhash_64 ( const void * key, int len,
                   uint32_t seed, void * out )
{
  const uint8_t * data = (const uint8_t *)key;
  uint8_t buf [16];
  double * state = (double*)buf;
  uint32_t * state32 = (uint32_t*)buf;
  double seed32 = (double)seed;

  uint8_t * seedbuf;
  seedbuf = (uint8_t *)&seed;

  setup( state, seed32 );
  round( seedbuf, 4, state );
  round( data, len, state );

  uint8_t output [8];
  uint32_t * h = (uint32_t*)output;
  
  h[0] = state32[0] + state32[3];
  h[1] = state32[1] + state32[2];

  ((uint32_t*)out)[0] = h[0];
  ((uint32_t*)out)[1] = h[1];
} 

disclaimer

no claims are made regarding the security of this system.

get

npm i --save floppsy

use-cases

  • A slow hash for password hashing.
  • Hashing arrays of numbers (vectors, floating point).
  • As a basis for a cryptographic primitive (such as a PRNG, or symmetric cipher).

include

As a Node ES module:

import floppsy from 'floppsy';

As old style modules:

const floppsy = require('floppsy').default;

Using Snowpack in a web app:

import floppsy from './web_modules/floppsy.js';

api

Can produce digests of 32, 64 or 128 bits.

> f.hash('')
'7f5f8491f0b745bf'
> f.hash('', {bits:32})
'7016ca50'
> f.hash('', {bits:128})
'3f7f508e3fe034033ff0e269b0c66356'

Can also change output format:

  x.hash('',{out_format:'hex'}); // default
  x.hash('',{out_format:'binary'}); // binary string
  x.hash('',{out_format:'bytes'}); // Uint8Array
  x.hash('',{out_format:'uint32s'}); // Uint32Array

play with a demo online here

floppsy's People

Contributors

o0101 avatar rurban avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

floppsy's Issues

cpp implementation produces different results

We need to work out if the c++ implementation and the es implementation can produce the same results ( and how to get them to ), or if they cannot ( because of, say, differences in floating point arithmetic.

this is coming from tests using SMHasher ( specifically the verification test ).

Calculating the verification test value in JS gives me 39B945E5

While in the C++ version I get BB168BEF

A relevant SO question: https://stackoverflow.com/questions/44480529/is-double-arithmetic-the-same-in-cpp-and-javascript?noredirect=1#comment75955733_44480529

method signature is weird

hash("asdsada") // ok
hash([1,2,3,4.3432,5]) // ok
hash(["a","b","c"]) // NOT OK

I think array of chars ought to work...But it works to clarify this, and apply any fixes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.