utils

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SaltSize is based on blake2b256
	SaltSize = 32
)

Variables

View Source
var (
	// ErrLessOrEqualBig2 is returned if the field order is less than or equal to 2
	ErrLessOrEqualBig2 = errors.New("less 2")
	//ErrExceedMaxRetry is returned if we retried over times
	ErrExceedMaxRetry = errors.New("exceed max retries")
	//ErrInvalidInput is returned if the input is invalid
	ErrInvalidInput = errors.New("invalid input")
	//ErrLargeRank is returned if the rank is too large
	ErrLargeRank = errors.New("large rank")
	//ErrLargeThreshold is returned if the threshold is too large
	ErrLargeThreshold = errors.New("large threshold")
	// ErrNotInRange is returned if the value is not in the given range.
	ErrNotInRange = errors.New("not in range")
	// ErrLargerFloor is returned if the floor is larger than ceil.
	ErrLargerFloor = errors.New("larger floor")
	// ErrEmptySlice is returned if the length of slice is zero.
	ErrEmptySlice = errors.New("empty slice")
	// ErrSmallThreshold is returned if the threshold < 2.
	ErrSmallThreshold = errors.New("threshold < 2")
	// ErrSmallSafePrime is returned if the safePrime < 2^10.
	ErrSmallSafePrime = errors.New("safe-prime size must be at least 10-bit")
)
View Source
var File_github_com_getamis_alice_crypto_utils_message_proto protoreflect.FileDescriptor

Functions

func BinaryMul

func BinaryMul(c uint8, input []byte) []byte

func BitsToBytes

func BitsToBytes(input []uint8) ([]byte, error)

func BytesToBits

func BytesToBits(data []byte) []uint8

func EnsureFieldOrder

func EnsureFieldOrder(fieldOrder *big.Int) error

EnsureFieldOrder ensures the field order should be more than 2.

func EnsureRank

func EnsureRank(rank uint32, threshold uint32) error

EnsureRank ensures the rank+1 should be smaller than threshold.

func EnsureThreshold

func EnsureThreshold(threshold uint32, n uint32) error

EnsureThreshold ensures the threshold should be smaller than or equal to n.

func EulerFunction

func EulerFunction(primeFactor []*big.Int) (*big.Int, error)

EulerFunction :(Special case) Assume that N is square-free and primeFactor consists of prime integers. Formula: N = prod_i P_i, the output is prod_i (P_i -1). TODO: general case.

func ExtendHashOutput

func ExtendHashOutput(salt, message []byte, outputByteLength int) []byte

The hash result is Hash(salt + "," + message +"," + "0") | Hash(salt + "," + message + "," + "1") | .... | Hash(salt + "," + message + "," + "n-1"). Here n := Ceil (outputByteLength/32)

func ExtnedHashOuput

func ExtnedHashOuput(salt, message []byte, outputBitLength int) []byte

Let n := outputBitLength / 256. The hash result is Hash(salt + "," + message +"," + "0") | Hash(salt + "," + message + "," + "1") | .... | Hash(salt + "," + message + "," + "n") | the remainder part.

func FastMod3

func FastMod3(number *big.Int) int

This is a algorithm to get number % 3. The velocity of this function is faster than new(bigInt).mod(number, 3).

func Gcd

func Gcd(a *big.Int, b *big.Int) *big.Int

Gcd calculates greatest common divisor (GCD) via Euclidean algorithm

func GenRandomBytes

func GenRandomBytes(size int) ([]byte, error)

GenRandomBytes generates a random byte array with indicating the legnth.

func GetAnyMsg

func GetAnyMsg(bs ...[]byte) []proto.Message

func HashBytesToInt

func HashBytesToInt(salt []byte, bs ...[]byte) (*big.Int, error)

func HashProtos

func HashProtos(salt []byte, msgs ...proto.Message) ([]byte, error)

HashProtos hashes a slice of message.

func HashProtosRejectSampling

func HashProtosRejectSampling(fieldOrder *big.Int, msgs ...proto.Message) (*big.Int, []byte, error)

func HashProtosToInt

func HashProtosToInt(salt []byte, msgs ...proto.Message) (*big.Int, error)

Waring: The follwing function only work in S256 and P256, because the output of blake2b is 32 byte. HashProtosToInt hashes a slice of message to an integer.

func InRange

func InRange(checkValue *big.Int, floor *big.Int, ceil *big.Int) error

InRange checks if the checkValue is in [floor, ceil).

func IsRelativePrime

func IsRelativePrime(a *big.Int, b *big.Int) bool

IsRelativePrime returns if a and b are relative primes

func Lcm

func Lcm(a, b *big.Int) (*big.Int, error)

Lcm calculates find Least Common Multiple https://rosettacode.org/wiki/Least_common_multiple#Go

func RandomAbsoluteRangeInt

func RandomAbsoluteRangeInt(n *big.Int) (*big.Int, error)

RandomInt generates a random number in [-n, n].

func RandomAbsoluteRangeIntBySeed

func RandomAbsoluteRangeIntBySeed(salt []byte, message []byte, q *big.Int) *big.Int

RandomAbsoluteRangeIntBySeed generates a random number in (-2^q.bit, 2^q.bit) with seed.

func RandomCoprimeInt

func RandomCoprimeInt(n *big.Int) (*big.Int, error)

RandomCoprimeInt generates a random relative prime number in [2, n)

func RandomInt

func RandomInt(n *big.Int) (*big.Int, error)

RandomInt generates a random number in [0, n).

func RandomPositiveInt

func RandomPositiveInt(n *big.Int) (*big.Int, error)

RandomPositiveInt generates a random number in [1, n).

func RandomPrime

func RandomPrime(bits int) (*big.Int, error)

RandomPrime generates a random prime number with bits size

func ReverseByte

func ReverseByte(s []byte) []byte

func ScalarMul

func ScalarMul(bit uint8, input []uint8) []uint8

func Xor

func Xor(bigArray, smallArray []byte) []byte

Types

type Hash

type Hash struct {
	Msgs []*anypb.Any `protobuf:"bytes,1,rep,name=msgs,proto3" json:"msgs,omitempty"`
	// contains filtered or unexported fields
}

func (*Hash) Descriptor deprecated

func (*Hash) Descriptor() ([]byte, []int)

Deprecated: Use Hash.ProtoReflect.Descriptor instead.

func (*Hash) GetMsgs

func (x *Hash) GetMsgs() []*anypb.Any

func (*Hash) ProtoMessage

func (*Hash) ProtoMessage()

func (*Hash) ProtoReflect

func (x *Hash) ProtoReflect() protoreflect.Message

func (*Hash) Reset

func (x *Hash) Reset()

func (*Hash) String

func (x *Hash) String() string

type SafePrime

type SafePrime struct {
	P *big.Int
	Q *big.Int
}

p, q are primes and p = 2*q+1

func GenerateRandomSafePrime

func GenerateRandomSafePrime(rand io.Reader, pbits int) (*SafePrime, error)

The algorithm appears in the paper Safe Prime Generation with a Combined Sieve https://eprint.iacr.org/2003/186.pdf safe prime: p = 2q+1, where p and q are both primes.

Jump to

Keyboard shortcuts

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