sumhash

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2022 License: MIT Imports: 5 Imported by: 4

README

Sumhash

A Go implementation of Algorand’s subset-sum hash function. The library exports the subset sum hash function via a hash.Hash interface.

Install

go get github.com/algorand/go-sumhash

Alternatively the same can be achieved if you use import in a package:

import "github.com/algorand/go-sumhash"

and run go get without parameters.

Usage

Construct a sumhash instance with block size of 512.

package main

import (
	"fmt"

	"github.com/algorand/go-sumhash"
)

func main() {
	h := sumhash.New512(nil)
	input := []byte("sumhash input")
	_, _ = h.Write(input)

	sum := h.Sum(nil)
	fmt.Printf("subset sum hash value: %X", sum)
}

Testing

go test ./...

Spec

The specification of the function as well as the security parameters can be found here

Documentation

Overview

Package sumhash implements the subset-sum hash function.

Index

Constants

View Source
const Sumhash512DigestBlockSize = 64

Sumhash512DigestBlockSize is the block size, in bytes, of the sumhash hash function

View Source
const Sumhash512DigestSize = 64

Sumhash512DigestSize The size in bytes of the sumhash checksum

Variables

This section is empty.

Functions

func BlockSize

func BlockSize(c Compressor) int

BlockSize returns the block size in bytes

func New

func New(c Compressor, salt []byte) hash.Hash

New returns a new hash.Hash computing a sumhash checksum. If salt is nil, then hash.Hash computes a hash output in unsalted mode. Otherwise, salt should be BlockSize(c) bytes, and the hash is computed in salted mode. the context returned by this function reference the salt argument. any changes might affect the hash calculation

func New512

func New512(salt []byte) hash.Hash

New512 creates a new sumhash512 context that computes a sumhash checksum. The output of the hash function is 64 bytes (512 bits). If salt is nil, then hash.Hash computes a hash output in unsalted mode. Otherwise, salt should be 64 bytes, and the hash is computed in salted mode. the context returned by this function reference the salt argument. any changes might affect the hash calculation

Types

type Compressor

type Compressor interface {
	Compress(dst []byte, input []byte)
	InputLen() int  // len(input)
	OutputLen() int // len(dst)
}

Compressor represents the compression function which is performed on a message

var SumhashCompressor Compressor

SumhashCompressor is a matrix derived from a seed which is used by the sumhash512 interface. In order the gain speed, this matrix can be used to compress input which have exactly size of InputLen()

type LookupTable

type LookupTable [][][256]uint64

LookupTable is the precomputed sums from a matrix for every possible byte of input. Its dimensions are [n]m/8[256]uint64.

func (LookupTable) Compress

func (A LookupTable) Compress(dst []byte, msg []byte)

Compress performs the compression algorithm on a message and output into dst

func (LookupTable) InputLen

func (A LookupTable) InputLen() int

InputLen returns the valid length of a message in bytes

func (LookupTable) OutputLen

func (A LookupTable) OutputLen() int

OutputLen returns the output len in bytes of the compression function

type Matrix

type Matrix [][]uint64

Matrix is the n-by-m sumhash matrix A with elements in Z_q where q=2^64

func RandomMatrix

func RandomMatrix(rand io.Reader, n int, m int) (Matrix, error)

RandomMatrix generates a random sumhash matrix by reading from rand. n is the number of rows in the matrix and m is the number of bits in the input message. m must be a multiple of 8.

func RandomMatrixFromSeed

func RandomMatrixFromSeed(seed []byte, n int, m int) (Matrix, error)

RandomMatrixFromSeed creates a random-looking matrix to be used for the sumhash function using the seed bytes. n and m are the rows and columns of the matrix respectively

func (Matrix) Compress

func (A Matrix) Compress(dst []byte, msg []byte)

Compress performs the compression algorithm on a message and output into dst

func (Matrix) InputLen

func (A Matrix) InputLen() int

InputLen returns the valid length of a message in bytes

func (Matrix) LookupTable

func (A Matrix) LookupTable() LookupTable

LookupTable generates a lookuptable used to increase hash calculation performance

func (Matrix) OutputLen

func (A Matrix) OutputLen() int

OutputLen returns the output len in bytes of the compression function

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL