ristretto255: add Encode test

This commit is contained in:
George Tankersley 2019-03-31 00:37:13 +00:00 committed by Filippo Valsorda
parent b5155ea127
commit 1a1e45849a
2 changed files with 28 additions and 13 deletions

View File

@ -133,7 +133,7 @@ func mapToPoint(out *edwards25519.ExtendedGroupElement, t *radix51.FieldElement)
} }
// Encode encodes a Ristretto group element to its canonical bytestring. // Encode encodes a Ristretto group element to its canonical bytestring.
func (e *Element) Encode(ee *Element) []byte { func (ee *Element) Encode() []byte {
tmp := &radix51.FieldElement{} tmp := &radix51.FieldElement{}
// u1 = (z0 + y0) * (z0 - y0) // u1 = (z0 + y0) * (z0 - y0)

View File

@ -1,6 +1,7 @@
package ristretto255 package ristretto255
import ( import (
"bytes"
"encoding/hex" "encoding/hex"
"testing" "testing"
@ -64,20 +65,20 @@ func TestSqrtRatioM1(t *testing.T) {
} }
} }
func TestRistrettoBasepointDecode(t *testing.T) { var (
var ( // The encoding of Ristretto element that can be represented internally by the Curve25519 base point.
// The encoding of Ristretto element that can be represented internally by the Curve25519 base point. compressedRistrettoBasepoint, _ = hex.DecodeString("e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76")
compressedRistrettoBasepoint, _ = hex.DecodeString("e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76")
// The representative Ristretto basepoint in extended coordinates. // The representative Ristretto basepoint in extended coordinates.
ristrettoBasepoint = Element{r: edwards25519.ExtendedGroupElement{ ristrettoBasepoint = Element{r: edwards25519.ExtendedGroupElement{
X: radix51.FieldElement([5]uint64{426475514619346, 2063872706840040, 14628272888959, 107677749330612, 288339085807592}), X: radix51.FieldElement([5]uint64{426475514619346, 2063872706840040, 14628272888959, 107677749330612, 288339085807592}),
Y: radix51.FieldElement([5]uint64{1934594822876571, 2049809580636559, 1991994783322914, 1758681962032007, 380046701118659}), Y: radix51.FieldElement([5]uint64{1934594822876571, 2049809580636559, 1991994783322914, 1758681962032007, 380046701118659}),
Z: radix51.FieldElement([5]uint64{1, 0, 0, 0, 0}), Z: radix51.FieldElement([5]uint64{1, 0, 0, 0, 0}),
T: radix51.FieldElement([5]uint64{410445769351754, 2235400917701188, 1495825632738689, 1351628537510093, 430502003771208}), T: radix51.FieldElement([5]uint64{410445769351754, 2235400917701188, 1495825632738689, 1351628537510093, 430502003771208}),
}} }}
) )
func TestRistrettoEncoding(t *testing.T) {
decodedBasepoint := &Element{} decodedBasepoint := &Element{}
err := decodedBasepoint.Decode(compressedRistrettoBasepoint) err := decodedBasepoint.Decode(compressedRistrettoBasepoint)
if err != nil { if err != nil {
@ -87,4 +88,18 @@ func TestRistrettoBasepointDecode(t *testing.T) {
if decodedBasepoint.Equal(&ristrettoBasepoint) != 1 { if decodedBasepoint.Equal(&ristrettoBasepoint) != 1 {
t.Error("decode succeeded, but got wrong point") t.Error("decode succeeded, but got wrong point")
} }
roundtripBasepoint := decodedBasepoint.Encode()
if !bytes.Equal(compressedRistrettoBasepoint, roundtripBasepoint) {
t.Error("decode<>encode roundtrip produced different results")
}
encodedBasepoint := ristrettoBasepoint.Encode()
if !bytes.Equal(compressedRistrettoBasepoint, encodedBasepoint) {
t.Error("point encode produced different results")
}
}
func TestRistrettoRoundtrip(t *testing.T) {
// TODO quickcheck
} }