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

Raw File Download

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
content badge
swh:1:cnt:627519db3bac9b3be08eeb751a76c4949c8aeca9

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
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
package deltacircuit

import (
	//"crypto/subtle"

	tedwards "github.com/consensys/gnark-crypto/ecc/twistededwards"
	"github.com/consensys/gnark/frontend"
	"github.com/consensys/gnark/std/algebra/twistededwards"
	"github.com/consensys/gnark/std/math/bits"
	eddsa "github.com/consensys/gnark/std/signature/eddsa"

	"github.com/consensys/gnark/std/hash/mimc"
)

type Point struct {
	X, Y frontend.Variable
}

type deltaCircuit struct {

	// Random value agreed upon with the census
	Coin0 frontend.Variable
	Coin1 frontend.Variable
	Xi    frontend.Variable
	CMXi  frontend.Variable `gnark:",public"`

	// private value hidden by LDP
	ID     frontend.Variable
	LDPVal frontend.Variable
	Delta  Point `gnark:",public"` // Delta = Encrypt(LDP(ID,Xi))

	curveID tedwards.ID

	// Variables used for the elgamal encryption
	RNDscalar frontend.Variable
	CensusPK  eddsa.PublicKey `gnark:",public"` // Public key of the census

	ApkList               []frontend.Variable
	RegAuthorityPK        eddsa.PublicKey `gnark:",public"`
	RegAuthoritySignature eddsa.Signature
}

// Define declares the circuit logic. The compiler then produces a list of constraints
// which must be satisfied (valid witness) in order to create a valid zk-SNARK
func (circuit *deltaCircuit) Define(api frontend.API) error {

	// Create the encryption circuit
	curve, err := twistededwards.NewEdCurve(api, circuit.curveID)
	if err != nil {
		return err
	}

	hfunc, err := mimc.NewMiMC(api)
	if err != nil {
		return err
	}
	hfunc.Write(circuit.Xi)
	result := hfunc.Sum()

	api.AssertIsEqual(result, circuit.CMXi)

	ldpval, _ := LDP(api, circuit.Coin0, circuit.Coin1, circuit.Xi, circuit.ID)

	api.AssertIsEqual(ldpval, circuit.LDPVal)

	err = Encrypt(curve, circuit.RNDscalar, circuit.CensusPK, ldpval, circuit.Delta)
	if err != nil {
		return err
	}

	hfunc.Reset()
	hfunc.Write(circuit.ApkList[:]...)
	hfunc.Write(circuit.ID)
	signdata := hfunc.Sum()

	// Verify sign_R
	mimcSign, err := mimc.NewMiMC(api)
	if err != nil {
		return err
	}
	err = eddsa.Verify(curve, circuit.RegAuthoritySignature, signdata, circuit.RegAuthorityPK, &mimcSign)
	if err != nil {
		return err
	}

	return nil
}

func LDP(api frontend.API, coin0, coin1, xi, msg frontend.Variable) (frontend.Variable, error) {

	coins := bits.ToBinary(api, xi)
	api.AssertIsEqual(coins[0], coin0)
	api.AssertIsEqual(coins[1], coin1)

	// Create the LDP circuit
	c0 := api.IsZero(coins[0]) // Check first coin toss
	c1 := api.IsZero(coins[1]) // Check second coin toss

	ldp := api.Select(c0, msg, c1) // Calculate Random Response result

	return ldp, nil

}

// Encrypt creates the circuit matching the elgamal encryption
func Encrypt(curve twistededwards.Curve, r frontend.Variable, pubkey eddsa.PublicKey, msg frontend.Variable, delta Point) error {

	base := twistededwards.Point{
		X: curve.Params().Base[0],
		Y: curve.Params().Base[1],
	}

	// project the message on to the curve
	M := curve.ScalarMul(base, msg)
	curve.AssertIsOnCurve(M)

	// ElGamal-encrypt the point to produce ciphertext (K,C).
	//K := curve.ScalarMul(base, r) // K = r * Base - Public key

	S := curve.ScalarMul(pubkey.A, r) // S = r*A
	curve.AssertIsOnCurve(S)

	Cipher := curve.Add(S, M) // C = S + M

	curve.API().AssertIsEqual(Cipher.X, delta.X)
	curve.API().AssertIsEqual(Cipher.Y, delta.Y)

	return nil
}

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