ctmap

package module
v0.0.0-...-400d224 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2019 License: BSD-3-Clause Imports: 1 Imported by: 0

README

ctmap

GoDoc Build Status Go Report Card

A constant-time key-value map experiment.

This is pretty stupid and not well tested or audited. It's just an experiment. I can't even really see why anyone would ever want to do this. If this seems like the solution to your problem, you probably have the wrong problem. 💁

If you don't know how to safely use the crypto/subtle package, you will never be able to use this package in a safe way.

This was loosely inspired by this article by Andy Wingo.

Documentation

Overview

Package ctmap implements a constant-time key-value map.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

type Map struct {
	// contains filtered or unexported fields
}

Map is a constant-time key-value map.

func New

func New(keySize, valSize int) *Map

New returns a new constant-time map with the given key and value sizes.

Every key and value must be of equal size.

For a constant-time equivalent of map[string]struct{}, use 0 for valSize.

func NewWithCapacity

func NewWithCapacity(keySize, valSize, capacity int) *Map

NewWithCapacity returns a new constant-time map with the given key and value sizes. It preallocates the map's backing storage, sized to fit capacity entries without reallocating.

Every key and value must be of equal size.

For a constant-time equivalent of map[string]struct{}, use 0 for valSize.

func (*Map) Add

func (m *Map) Add(key, val []byte)

Add appends a new entry to the map. It does not check for duplicates, nor does it handle them.

func (*Map) Contains

func (m *Map) Contains(key []byte) int

Contains determines if a key is present in the map in constant-time. It returns 1 if the key is present, 0 otherwise.

func (*Map) Delete

func (m *Map) Delete(key []byte) int

Delete removes an entry with a given key from the map. It returns 1 if an entry was removed, 0 otherwise. The removed entry is zeroed.

If the map contains multiple entries with the same key, only the first is removed.

WARNING: The interaction between Delete and other methods leaks timing information. In particular, if an entry was removed the size of the map is reduced and the time taken by other methods in this package will be reduced. If an entry was not removed, then the time taken does not change.

func (*Map) Len

func (m *Map) Len() int

Len returns the number of entries in the map. It does not account for duplicates.

func (*Map) Lookup

func (m *Map) Lookup(key, val []byte) int

Lookup finds the value associated with a key in constant-time. The value is copied, in constant-time, into val which must be the correct length. It returns 1 if the key was present, 0 otherwise.

If there are multiple entries matching key, only the first will be returned.

func (*Map) Range

func (m *Map) Range(f func(key, val []byte))

Range calls f for each entry in the map. f must be carefull not to leak any timing information about the contents of key.

If the map contains duplicate keys, f will be called once for each key.

The behaviour of the map is undefined if key or val are modified.

func (*Map) Rename

func (m *Map) Rename(oldKey, newKey []byte) int

Rename is like Replace but it only changes they key and leaves the value untouched.

If there are duplicate entries matching oldKey, only the first entry's key will be replaced.

func (*Map) Replace

func (m *Map) Replace(oldKey, newKey, val []byte) int

Replace replaces an existing entry in the map with a new entry in constant-time. It returns 1 if oldKey was present in the map and the entry replaced, 0 otherwise.

If there are duplicate entries matching oldKey, only the first entry will be replaced.

func (*Map) Set

func (m *Map) Set(key, val []byte) int

Set sets an existing map entry to val in constant-time. It returns 1 if the key was present and the val set, 0 otherwise.

If there are multiple entries with the same key, only the first entry will have it's value set to val.

Jump to

Keyboard shortcuts

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