word

package
v1.2.24 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Copyright 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 distributed 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.

SPDX-License-Identifier: Apache-2.0

Copyright 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 distributed 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.

SPDX-License-Identifier: Apache-2.0

Copyright 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 distributed 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.

SPDX-License-Identifier: Apache-2.0

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByteWidth

func ByteWidth(bitwidth uint) uint

ByteWidth returns the least number of bytes required to store an element of the given width.

func ByteWidth16

func ByteWidth16(value uint16) uint

ByteWidth16 returns the bytewidth of the given uint16 value.

func ByteWidth32

func ByteWidth32(value uint32) uint

ByteWidth32 returns the bytewidth of the given uint64 value.

func ByteWidth64

func ByteWidth64(value uint64) uint

ByteWidth64 returns the bytewidth of the given uint64 value.

func TrimLeadingZeros

func TrimLeadingZeros(bytes []byte) []byte

TrimLeadingZeros any leading zeros from this array

func Uint64

func Uint64[W Word[W]](value uint64) W

Uint64 constructs a word from a given uint64 value.

Types

type BigEndian

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

BigEndian captures the notion of an array of bytes represented in big endian form. This is really just a wrapper for convenience, and to help clarify the underlying byte order.

func NewBigEndian

func NewBigEndian(bytes []byte) BigEndian

NewBigEndian constructs a new big endian byte array.

func (BigEndian) Add

func (p BigEndian) Add(o BigEndian) BigEndian

Add implementation for field.Element interface

func (BigEndian) BigInt

func (p BigEndian) BigInt() *big.Int

BigInt implementation for word.Word interface.

func (BigEndian) ByteWidth

func (p BigEndian) ByteWidth() uint

ByteWidth implementation for the Word interface.

func (BigEndian) Bytes

func (p BigEndian) Bytes() []byte

Bytes implementation for Word interface.

func (BigEndian) Cmp

func (p BigEndian) Cmp(o BigEndian) int

Cmp implements a comparison by regarding the word as an unsigned integer.

func (BigEndian) Cmp64

func (p BigEndian) Cmp64(o uint64) int

Cmp64 implementation for Word interface.

func (BigEndian) Equals

func (p BigEndian) Equals(o BigEndian) bool

Equals implementation for the hash.Hasher interface.

func (BigEndian) FitsWithin

func (p BigEndian) FitsWithin(bitwidth uint) bool

FitsWithin implementation for Word interface.

func (*BigEndian) GobDecode

func (p *BigEndian) GobDecode(data []byte) error

GobDecode a previously encoded option

func (*BigEndian) GobEncode

func (p *BigEndian) GobEncode() (data []byte, err error)

GobEncode a big endian word. This allows it to be marshalled into a binary form.

func (BigEndian) Hash

func (p BigEndian) Hash() uint64

Hash implementation for the hash.Hasher interface.

func (BigEndian) Inverse

func (p BigEndian) Inverse() BigEndian

Inverse implementation for field.Element interface

func (BigEndian) IsOne

func (p BigEndian) IsOne() bool

IsOne implementation for field.Element interface

func (BigEndian) IsZero

func (p BigEndian) IsZero() bool

IsZero implementation for the Word interface

func (BigEndian) Modulus

func (p BigEndian) Modulus() *big.Int

Modulus implementation for field.Element interface

func (BigEndian) Mul

func (p BigEndian) Mul(o BigEndian) BigEndian

Mul implementation for field.Element interface

func (BigEndian) PutBytes

func (p BigEndian) PutBytes(bytes []byte) []byte

PutBytes implementation for Word interface.

func (BigEndian) Rsh

func (p BigEndian) Rsh(n uint) BigEndian

Rsh performs a right bitshift of a given amount on this value.

func (BigEndian) SetBytes

func (p BigEndian) SetBytes(bytes []byte) BigEndian

SetBytes implementation for Word interface.

func (BigEndian) SetUint64

func (p BigEndian) SetUint64(value uint64) BigEndian

SetUint64 implementation for Word interface.

func (BigEndian) String

func (p BigEndian) String() string

func (BigEndian) Sub

func (p BigEndian) Sub(o BigEndian) BigEndian

Sub implementation for field.Element interface

func (BigEndian) Text

func (p BigEndian) Text(base int) string

Text returns a string representation of this word in a given base.

func (BigEndian) Uint64

func (p BigEndian) Uint64() uint64

Uint64 implementation for Word interface.

type DynamicWord

type DynamicWord[T any] interface {
	Word[T]
	// Return minimal number of bytes required to store this word.  This can be
	// defined as the length of bytes of this word, with all leading zero bytes
	// removed.  For example, 0x1010 has a length of 2, 0x0010 has a length of 1
	// whilst 0x0000 has a byte length of 0.  Observe that, if the word is
	// encoded (e.g. in Montgomerry form), then this is the length of the
	// encoded bytes.
	ByteWidth() uint
	// Write contents of this word into given byte array.  If the given byte
	// array is not big enough, a new array is allocated and returned.
	PutBytes([]byte) []byte
}

DynamicWord is a word which has a dynamically sized representation, rather than a fixed-size representation. In particular, the dynamic word representing zero is always the empty byte array.

type Word

type Word[T any] interface {
	fmt.Stringer
	// Return the value of this word as a big integer.
	BigInt() *big.Int
	// Cmp returns 1 if x > y, 0 if x = y, and -1 if x < y.
	Cmp(y T) int
	// Cmp64 returns 1 if x > y, 0 if x = y, and -1 if x < y.
	Cmp64(y uint64) int
	// Check whether two items are equal (or not).
	Equals(T) bool
	// Check whether this value fits within the given bitwidth.
	FitsWithin(uint) bool
	// Return a suitable hashcode.
	Hash() uint64
	// Returns the raw bytes of this word.  Observe that, if the word is encoded
	// (e.g. in Montgomerry form), then the *encoded* bytes are returned.
	Bytes() []byte
	// Check whether this word is zero, or not.
	IsZero() bool
	// Initialise this word from a set of raw bytes.
	SetBytes([]byte) T
	// Set this word to a uint64 value
	SetUint64(uint64) T
	// Returns value of word as an unsigned integer (truncated for 64bits).
	Uint64() uint64
}

Word abstracts a sequence of n bits.

Jump to

Keyboard shortcuts

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