| 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 |
package elligator2 |
| 32 |
|
| 33 |
import ( |
| 34 |
"encoding/hex" |
| 35 |
"fmt" |
| 36 |
"strings" |
| 37 |
"testing" |
| 38 |
|
| 39 |
"filippo.io/edwards25519/field" |
| 40 |
) |
| 41 |
|
| 42 |
const ( |
| 43 |
montgomeryUniformSize = 32 |
| 44 |
montgomeryPointSize = 32 |
| 45 |
) |
| 46 |
|
| 47 |
func montgomeryFromUniformBytes(in []byte) (*field.Element, *field.Element, error) { |
| 48 |
if len(in) != montgomeryUniformSize { |
| 49 |
return nil, nil, fmt.Errorf("curve/montgomery: unexpected representative size") |
| 50 |
} |
| 51 |
|
| 52 |
var r field.Element |
| 53 |
if _, err := r.SetBytes(in[:]); err != nil { |
| 54 |
return nil, nil, fmt.Errorf("curve/montgomery: failed to deserailize r: %w", err) |
| 55 |
} |
| 56 |
|
| 57 |
u, v := MontgomeryFlavor(&r) |
| 58 |
return u, v, nil |
| 59 |
} |
| 60 |
|
| 61 |
func TestElligator2(t *testing.T) { |
| 62 |
t.Run("Montgomery", testElligator2Montgomery) |
| 63 |
} |
| 64 |
|
| 65 |
func testElligator2Montgomery(t *testing.T) { |
| 66 |
|
| 67 |
testVectors := []struct { |
| 68 |
repr []byte |
| 69 |
expected *field.Element |
| 70 |
}{ |
| 71 |
{ |
| 72 |
mustUnhex(t, "0000000000000000000000000000000000000000000000000000000000000000"), |
| 73 |
mustUnhexElement(t, "0000000000000000000000000000000000000000000000000000000000000000"), |
| 74 |
}, |
| 75 |
{ |
| 76 |
mustUnhex(t, "0000000000000000000000000000000000000000000000000000000000000040"), |
| 77 |
mustUnhexElement(t, "0000000000000000000000000000000000000000000000000000000000000000"), |
| 78 |
}, |
| 79 |
{ |
| 80 |
mustUnhex(t, "0000000000000000000000000000000000000000000000000000000000000080"), |
| 81 |
mustUnhexElement(t, "0000000000000000000000000000000000000000000000000000000000000000"), |
| 82 |
}, |
| 83 |
{ |
| 84 |
mustUnhex(t, "00000000000000000000000000000000000000000000000000000000000000c0"), |
| 85 |
mustUnhexElement(t, "0000000000000000000000000000000000000000000000000000000000000000"), |
| 86 |
}, |
| 87 |
{ |
| 88 |
mustUnhex(t, "673a505e107189ee54ca93310ac42e4545e9e59050aaac6f8b5f64295c8ec02f"), |
| 89 |
mustUnhexElement(t, "242ae39ef158ed60f20b89396d7d7eef5374aba15dc312a6aea6d1e57cacf85e"), |
| 90 |
}, |
| 91 |
{ |
| 92 |
mustUnhex(t, "922688fa428d42bc1fa8806998fbc5959ae801817e85a42a45e8ec25a0d7545a"), |
| 93 |
mustUnhexElement(t, "696f341266c64bcfa7afa834f8c34b2730be11c932e08474d1a22f26ed82410b"), |
| 94 |
}, |
| 95 |
{ |
| 96 |
mustUnhex(t, "0d3b0eb88b74ed13d5f6a130e03c4ad607817057dc227152827c0506a538bbba"), |
| 97 |
mustUnhexElement(t, "0b00df174d9fb0b6ee584d2cf05613130bad18875268c38b377e86dfefef177f"), |
| 98 |
}, |
| 99 |
{ |
| 100 |
mustUnhex(t, "01a3ea5658f4e00622eeacf724e0bd82068992fae66ed2b04a8599be16662ef5"), |
| 101 |
mustUnhexElement(t, "7ae4c58bc647b5646c9f5ae4c2554ccbf7c6e428e7b242a574a5a9c293c21f7e"), |
| 102 |
}, |
| 103 |
{ |
| 104 |
mustUnhex(t, "69599ab5a829c3e9515128d368da7354a8b69fcee4e34d0a668b783b6cae550f"), |
| 105 |
mustUnhexElement(t, "09024abaaef243e3b69366397e8dfc1fdc14a0ecc7cf497cbe4f328839acce69"), |
| 106 |
}, |
| 107 |
{ |
| 108 |
mustUnhex(t, "9172922f96d2fa41ea0daf961857056f1656ab8406db80eaeae76af58f8c9f50"), |
| 109 |
mustUnhexElement(t, "beab745a2a4b4e7f1a7335c3ffcdbd85139f3a72b667a01ee3e3ae0e530b3372"), |
| 110 |
}, |
| 111 |
{ |
| 112 |
mustUnhex(t, "6850a20ac5b6d2fa7af7042ad5be234d3311b9fb303753dd2b610bd566983281"), |
| 113 |
mustUnhexElement(t, "1287388eb2beeff706edb9cf4fcfdd35757f22541b61528570b86e8915be1530"), |
| 114 |
}, |
| 115 |
{ |
| 116 |
mustUnhex(t, "84417826c0e80af7cb25a73af1ba87594ff7048a26248b5757e52f2824e068f1"), |
| 117 |
mustUnhexElement(t, "51acd2e8910e7d28b4993db7e97e2b995005f26736f60dcdde94bdf8cb542251"), |
| 118 |
}, |
| 119 |
{ |
| 120 |
mustUnhex(t, "b0fbe152849f49034d2fa00ccc7b960fad7b30b6c4f9f2713eb01c147146ad31"), |
| 121 |
mustUnhexElement(t, "98508bb3590886af3be523b61c3d0ce6490bb8b27029878caec57e4c750f993d"), |
| 122 |
}, |
| 123 |
{ |
| 124 |
mustUnhex(t, "a0ca9ff75afae65598630b3b93560834c7f4dd29a557aa29c7becd49aeef3753"), |
| 125 |
mustUnhexElement(t, "3c5fad0516bb8ec53da1c16e910c23f792b971c7e2a0ee57d57c32e3655a646b"), |
| 126 |
}, |
| 127 |
} |
| 128 |
|
| 129 |
for i, v := range testVectors { |
| 130 |
|
| 131 |
|
| 132 |
var clamped [montgomeryPointSize]byte |
| 133 |
copy(clamped[:], v.repr) |
| 134 |
clamped[31] &= 63 |
| 135 |
|
| 136 |
u, _, err := montgomeryFromUniformBytes(clamped[:]) |
| 137 |
if err != nil { |
| 138 |
t.Fatalf("p.SetUniformBytes(v[%d].repr): %v", i, err) |
| 139 |
} |
| 140 |
if u.Equal(v.expected) != 1 { |
| 141 |
t.Fatalf("p[%d] != vector[%d] (Got: %v)", i, i, u) |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func mustUnhexElement(t *testing.T, x string) *field.Element { |
| 147 |
b := mustUnhex(t, x) |
| 148 |
|
| 149 |
fe, err := new(field.Element).SetBytes(b) |
| 150 |
if err != nil { |
| 151 |
t.Fatalf("failed to parse fe hex: %v", err) |
| 152 |
} |
| 153 |
|
| 154 |
return fe |
| 155 |
} |
| 156 |
|
| 157 |
func mustUnhex(t *testing.T, x string) []byte { |
| 158 |
b, err := hex.DecodeString(strings.ReplaceAll(x, " ", "")) |
| 159 |
if err != nil { |
| 160 |
t.Fatalf("failed to parse hex: %v", err) |
| 161 |
} |
| 162 |
|
| 163 |
return b |
| 164 |
} |