sm2elgamal

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2023 License: MIT Imports: 15 Imported by: 0

README

Partially Homomorphic Encryption, EC-ElGamal with SM2

test Documentation GitHub go.mod Go version (branch) Release

本实验性实现是EC-ElGamal with SM2的半同态加密(Partially Homomorphic Encryption, PHE), 支持uint32 或者 int32类型。

  • 密文同态加法,如果结果溢出(uint32/int32),则解密时抛异常;
  • 密文同态减法,如果结果为负数(如果是uint32),则解密时抛异常;
  • 密文标量乘法,如果结果溢出(uint32/int32),则解密时抛异常;

解密的时候采用Shank的大步小步(Giant Step, Baby Step)算法,小步值缓存于map中,大概65M的大小(332^21), 经“压缩”后,大概15M左右的大小(72^21),uint32/int32共享同一个查找表。

参考资料

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrOverflow = fmt.Errorf("the value is overflow")

Functions

func DecryptInt32

func DecryptInt32(priv *sm2.PrivateKey, ciphertext *Ciphertext) (int32, error)

DecryptInt32 decrypts ciphertext to int32, if the value overflow, it returns ErrOverflow. The negative value will be slower than positive value.

func DecryptUint32

func DecryptUint32(priv *sm2.PrivateKey, ciphertext *Ciphertext) (uint32, error)

DecryptUint32 decrypts ciphertext to uint32, if the value overflow, it returns ErrOverflow.

func Marshal

func Marshal(c *Ciphertext) ([]byte, error)

Marshal converts the ciphertext to ASN.1 DER form.

Types

type Ciphertext

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

Ciphertext sturcture represents EL-Gamal ecnryption result.

func EncryptInt32

func EncryptInt32(random io.Reader, pub *ecdsa.PublicKey, m int32) (*Ciphertext, error)

EncryptInt32 encrypts m with the publickey.

func EncryptUint32

func EncryptUint32(random io.Reader, pub *ecdsa.PublicKey, m uint32) (*Ciphertext, error)

EncryptUint32 encrypts m with the publickey.

func Unmarshal

func Unmarshal(der []byte) (*Ciphertext, error)

Unmarshal parses ciphertext in ASN.1 DER form.

func (*Ciphertext) Add

func (ret *Ciphertext) Add(c1, c2 *Ciphertext) *Ciphertext

Add returns c1 + c2.

func (*Ciphertext) ScalarMultInt32

func (ret *Ciphertext) ScalarMultInt32(c *Ciphertext, m int32) *Ciphertext

ScalarMultInt32 scalar mutiples the ciphertext with m.

func (*Ciphertext) ScalarMultUint32

func (ret *Ciphertext) ScalarMultUint32(c *Ciphertext, m uint32) *Ciphertext

ScalarMultUint32 scalar mutiples the ciphertext with m.

func (*Ciphertext) Sub

func (ret *Ciphertext) Sub(c1, c2 *Ciphertext) *Ciphertext

Sub returns c1 - c2.

func (*Ciphertext) Sum added in v0.2.0

func (ret *Ciphertext) Sum(values ...*Ciphertext) *Ciphertext

Sum returns cumulative sum value

type PrivateKey added in v0.2.0

type PrivateKey interface {
	// GetCurve returns this private key's Curve
	GetCurve() elliptic.Curve
	// GetD returns this private key's value
	GetD() *big.Int
}

PrivateKey is an interface for elgamal decription requirement abstraction

type TwistedElgamal added in v0.2.0

type TwistedElgamal struct {
	Curve elliptic.Curve
	X, Y  *big.Int // H
}

TwistedElgamal is a struct for Twisted Elagaml context which contains H and curve information.

func FromPrivateKey added in v0.2.0

func FromPrivateKey(priv *TwistedPrivateKey) *TwistedElgamal

FromPrivateKey creates related SM2 Twisted Elgamal context.

func NewTwistedElgamal added in v0.2.0

func NewTwistedElgamal(rand io.Reader) (*TwistedElgamal, error)

NewTwistedElgamal creates one SM2 Twisted Elgamal context.

func (*TwistedElgamal) EncryptInt32 added in v0.2.0

func (te *TwistedElgamal) EncryptInt32(random io.Reader, pub *ecdsa.PublicKey, m int32) (*Ciphertext, error)

EncryptInt32 encrypts m with the publickey.

func (*TwistedElgamal) EncryptUint32 added in v0.2.0

func (te *TwistedElgamal) EncryptUint32(random io.Reader, pub *ecdsa.PublicKey, m uint32) (*Ciphertext, error)

EncryptUint32 encrypts m with the publickey.

func (*TwistedElgamal) GenerateKey added in v0.2.0

func (te *TwistedElgamal) GenerateKey(rand io.Reader) (*TwistedPrivateKey, error)

GenerateKey generates a public and private key pair.

type TwistedPrivateKey added in v0.2.0

type TwistedPrivateKey struct {
	ecdsa.PrivateKey
}

TwistedPrivateKey is a struct for Twisted private key, its public key can't be derived from D value without H.

func (*TwistedPrivateKey) DecryptInt32 added in v0.2.0

func (priv *TwistedPrivateKey) DecryptInt32(ciphertext *Ciphertext) (int32, error)

DecryptInt32 decrypts ciphertext to int32, if the value overflow, it returns ErrOverflow. The negative value will be slower than positive value.

func (*TwistedPrivateKey) DecryptUint32 added in v0.2.0

func (priv *TwistedPrivateKey) DecryptUint32(ciphertext *Ciphertext) (uint32, error)

DecryptUint32 decrypts ciphertext to uint32, if the value overflow, it returns ErrOverflow.

func (*TwistedPrivateKey) EncryptInt32 added in v0.2.0

func (priv *TwistedPrivateKey) EncryptInt32(random io.Reader, m int32) (*Ciphertext, error)

EncryptInt32 encrypts m with the publickey.

func (*TwistedPrivateKey) EncryptUint32 added in v0.2.0

func (priv *TwistedPrivateKey) EncryptUint32(random io.Reader, m uint32) (*Ciphertext, error)

EncryptInt32 encrypts m with the publickey.

func (*TwistedPrivateKey) Equal added in v0.2.0

func (priv *TwistedPrivateKey) Equal(x crypto.PrivateKey) bool

func (*TwistedPrivateKey) GetCurve added in v0.2.0

func (priv *TwistedPrivateKey) GetCurve() elliptic.Curve

func (*TwistedPrivateKey) GetD added in v0.2.0

func (priv *TwistedPrivateKey) GetD() *big.Int

Jump to

Keyboard shortcuts

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