Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

https://github.com/yacovm/ZKAT-VDP
20 March 2024, 19:29:05 UTC
  • Code
  • Branches (1)
  • Releases (0)
  • Visits
    • Branches
    • Releases
    • HEAD
    • refs/heads/main
    No releases to show
  • 48554d7
  • /
  • elgamal
  • /
  • elgamal.go
Raw File Download
Take a new snapshot of a software origin

If the archived software origin currently browsed is not synchronized with its upstream version (for instance when new commits have been issued), you can explicitly request Software Heritage to take a new snapshot of it.

Use the form below to proceed. Once a request has been submitted and accepted, it will be processed as soon as possible. You can then check its processing state by visiting this dedicated page.
swh spinner

Processing "take a new snapshot" request ...

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
  • revision
  • snapshot
origin badgecontent badge Iframe embedding
swh:1:cnt:d8a330bffd7d6737e2c9681cbe1a2cb43ab988da
origin badgedirectory badge Iframe embedding
swh:1:dir:480c77b62aa6e2a29b27bbc39096691e499e1c01
origin badgerevision badge
swh:1:rev:721f52b1b74f8e0aa60799bd1436865f8020258e
origin badgesnapshot badge
swh:1:snp:979a5772312db4ab61b637ed766bbc6a3f369470

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
  • revision
  • snapshot
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Tip revision: 721f52b1b74f8e0aa60799bd1436865f8020258e authored by yacovm on 14 August 2023, 13:43:45 UTC
Create README.md
Tip revision: 721f52b
elgamal.go
package elgamal

import (
	"crypto/rand"
	"golang.org/x/crypto/blake2b"
	"io"
	"math/big"

	"github.com/consensys/gnark-crypto/ecc/bn254/fr"
	"github.com/consensys/gnark-crypto/ecc/bn254/twistededwards"
)

const (
	sizeFr = fr.Bytes
	//sizePublicKey  = sizeFr
	//sizeSignature  = 2 * sizeFr
	//sizePrivateKey = 2*sizeFr + 32
)

var MessageMap = make(map[twistededwards.PointAffine]big.Int)

// PublicKey eddsa signature object
// cf https://en.wikipedia.org/wiki/EdDSA for notation
type PublicKey struct {
	A twistededwards.PointAffine
}

// PrivateKey private key of an eddsa instance
type PrivateKey struct {
	PublicKey PublicKey    // copy of the associated public key
	scalar    [sizeFr]byte // secret scalar, in big Endian
	randSrc   [32]byte     // source
}

func MessageMapInit() {
	c := twistededwards.GetEdwardsCurve()
	for i := 1; i < 100; i++ {
		P := &twistededwards.PointAffine{}
		P.ScalarMul(&c.Base, big.NewInt(int64(i)))
		MessageMap[*P] = *big.NewInt(int64(i))
	}
}

// GenerateKey generates a public and private key pair.
func GenerateKey(r io.Reader) (*PrivateKey, error) {
	c := twistededwards.GetEdwardsCurve()

	var pub PublicKey
	var priv PrivateKey
	// hash(h) = private_key || random_source, on 32 bytes each
	seed := make([]byte, 32)
	_, err := r.Read(seed)
	if err != nil {
		return nil, err
	}
	h := blake2b.Sum512(seed[:])
	for i := 0; i < 32; i++ {
		priv.randSrc[i] = h[i+32]
	}

	// prune the key
	// https://tools.ietf.org/html/rfc8032#section-5.1.5, key generation
	h[0] &= 0xF8
	h[31] &= 0x7F
	h[31] |= 0x40

	// reverse first bytes because setBytes interpret stream as big endian
	// but in eddsa specs s is the first 32 bytes in little endian
	for i, j := 0, sizeFr-1; i < sizeFr; i, j = i+1, j-1 {
		priv.scalar[i] = h[j]
	}

	var bScalar big.Int
	bScalar.SetBytes(priv.scalar[:])
	pub.A.ScalarMul(&c.Base, &bScalar)

	priv.PublicKey = pub

	return &priv, nil
}

// GenScalar returns a random scalar <= p.Order
func GenScalar(order *big.Int) *big.Int {
	r, _ := rand.Int(rand.Reader, order)
	return r
}

// Encrypt encrypts a message based on elgamal encryption.
// pubkey is Alice's public key used for encrypting the message. r is random scalar Bob generates.
func Encrypt(pubkey PublicKey, r *big.Int, msg *big.Int) (K, Ciph twistededwards.PointAffine) {

	curve := twistededwards.GetEdwardsCurve()

	var M, S twistededwards.PointAffine

	//msgBig := big.NewInt(int64(message))
	M.ScalarMul(&curve.Base, msg)

	// ElGamal-encrypt the point to produce ciphertext (K,C).
	K.ScalarMul(&curve.Base, r) // K = r * Base - Public key
	S.ScalarMul(&pubkey.A, r)   // S = k*A
	Ciph.Add(&S, &M)            // C = S + M

	return
}

// Decrypt decrypts cipher C using Alice's private key prive, and Bob's value K
func Decrypt(priv PrivateKey, K, C twistededwards.PointAffine) (msg big.Int) {

	var M, S twistededwards.PointAffine
	var bScalar big.Int
	bScalar.SetBytes(priv.scalar[:])

	// ElGamal-decrypt the ciphertext (K,C) to reproduce the message.
	S.ScalarMul(&K, &bScalar)
	S.Neg(&S)
	M.Add(&C, &S)

	msg = MessageMap[M]
	return
}

back to top

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API