File: elgamal_test.go

package info (click to toggle)
golang-github-bwesterb-go-ristretto 1.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 628 kB
  • sloc: ansic: 1,915; asm: 278; makefile: 3
file content (37 lines) | stat: -rw-r--r-- 803 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package ristretto_test

import (
	"bytes"
	"fmt"

	"github.com/bwesterb/go-ristretto"
)

func Example() {
	// Generate an El'Gamal keypair
	var secretKey ristretto.Scalar
	var publicKey ristretto.Point

	secretKey.Rand()                     // generate a new secret key
	publicKey.ScalarMultBase(&secretKey) // compute public key

	// El'Gamal encrypt a random curve point p into a ciphertext-pair (c1,c2)
	var p ristretto.Point
	var r ristretto.Scalar
	var c1 ristretto.Point
	var c2 ristretto.Point
	p.Rand()
	r.Rand()
	c2.ScalarMultBase(&r)
	c1.PublicScalarMult(&publicKey, &r)
	c1.Add(&c1, &p)

	// Decrypt (c1,c2) back to p
	var blinding, p2 ristretto.Point
	blinding.ScalarMult(&c2, &secretKey)
	p2.Sub(&c1, &blinding)

	fmt.Printf("%v", bytes.Equal(p.Bytes(), p2.Bytes()))
	// Output:
	// true
}