Debian Codegraph @

ristretto255_test.go

raw symbols SCIP
1 package ristretto255
2
3 import (
4 "bytes"
5 "crypto/sha512"
6 "encoding/hex"
7 "encoding/json"
8 "math/big"
9 "testing"
10
11 "filippo.io/edwards25519/field"
12 )
13
14 // The encoding of the canonical generator.
15 var compressedRistrettoBasepoint, _ = hex.DecodeString("e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76")
16
17 func TestRistrettoBasepointRoundTrip(t *testing.T) {
18 decodedBasepoint := &Element{}
19 err := decodedBasepoint.Decode(compressedRistrettoBasepoint)
20 if err != nil {
21 t.Fatal(err)
22 }
23
24 ristrettoBasepoint := (&Element{}).Base()
25 if decodedBasepoint.Equal(ristrettoBasepoint) != 1 {
26 t.Error("decode succeeded, but got wrong point")
27 }
28
29 roundtripBasepoint := decodedBasepoint.Encode(nil)
30 if !bytes.Equal(compressedRistrettoBasepoint, roundtripBasepoint) {
31 t.Error("decode<>encode roundtrip produced different results")
32 }
33
34 encodedBasepoint := ristrettoBasepoint.Encode(nil)
35 if !bytes.Equal(compressedRistrettoBasepoint, encodedBasepoint) {
36 t.Error("point encode produced different results")
37 }
38 }
39
40 func TestRistrettoSmallMultiplesTestVectors(t *testing.T) {
41 // From RFC 9496, Appendix A.1.
42 var testVectors = [16]string{
43 // This is the identity point
44 "0000000000000000000000000000000000000000000000000000000000000000",
45 // This is the basepoint
46 "e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76",
47 // These are small multiples of the basepoint
48 "6a493210f7499cd17fecb510ae0cea23a110e8d5b901f8acadd3095c73a3b919",
49 "94741f5d5d52755ece4f23f044ee27d5d1ea1e2bd196b462166b16152a9d0259",
50 "da80862773358b466ffadfe0b3293ab3d9fd53c5ea6c955358f568322daf6a57",
51 "e882b131016b52c1d3337080187cf768423efccbb517bb495ab812c4160ff44e",
52 "f64746d3c92b13050ed8d80236a7f0007c3b3f962f5ba793d19a601ebb1df403",
53 "44f53520926ec81fbd5a387845beb7df85a96a24ece18738bdcfa6a7822a176d",
54 "903293d8f2287ebe10e2374dc1a53e0bc887e592699f02d077d5263cdd55601c",
55 "02622ace8f7303a31cafc63f8fc48fdc16e1c8c8d234b2f0d6685282a9076031",
56 "20706fd788b2720a1ed2a5dad4952b01f413bcf0e7564de8cdc816689e2db95f",
57 "bce83f8ba5dd2fa572864c24ba1810f9522bc6004afe95877ac73241cafdab42",
58 "e4549ee16b9aa03099ca208c67adafcafa4c3f3e4e5303de6026e3ca8ff84460",
59 "aa52e000df2e16f55fb1032fc33bc42742dad6bd5a8fc0be0167436c5948501f",
60 "46376b80f409b29dc2b5f6f0c52591990896e5716f41477cd30085ab7f10301e",
61 "e0c418f7c8d9c4cdd7395b93ea124f3ad99021bb681dfc3302a9d99a2e53e64e",
62 }
63
64 basepointMultiple := (&Element{}).Zero()
65 ristrettoBasepoint := (&Element{}).Base()
66
67 for i := range testVectors {
68 // Grab the bytes of the encoding
69 encoding, err := hex.DecodeString(testVectors[i])
70 if err != nil {
71 t.Fatalf("#%d: bad hex encoding in test vector: %v", i, err)
72 }
73
74 // Decode the test vector to a ristretto255 element
75 decodedPoint := Element{}
76 err = decodedPoint.Decode(encoding)
77 if err != nil {
78 t.Fatalf("#%d: could not decode test vector: %v", i, err)
79 }
80 // Re-encode and check round trips
81 roundtripEncoding := decodedPoint.Encode(nil)
82 if !bytes.Equal(encoding, roundtripEncoding) {
83 t.Errorf("#%d: decode<>encode roundtrip failed", i)
84 }
85
86 // Check that the test vector encodes i * B
87 if basepointMultiple.Equal(&decodedPoint) != 1 {
88 t.Errorf("decoded small multiple %d * B is not %d * B", i, i)
89 }
90 computedEncoding := basepointMultiple.Encode(nil)
91 if !bytes.Equal(encoding, computedEncoding) {
92 t.Errorf("#%d: encoding computed value did not match", i)
93 }
94
95 // Ensure basepointMultiple = i * B in the next iteration
96 basepointMultiple.Add(basepointMultiple, ristrettoBasepoint)
97 }
98 }
99
100 func TestRistrettoBadEncodingsTestVectors(t *testing.T) {
101 // From RFC 9496, Appendix A.2.
102 var testVectors = []string{
103 // These are all bad because they're non-canonical field encodings.
104 "00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
105 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f",
106 "f3ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f",
107 "edffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f",
108 // These are all bad because they're negative field elements.
109 "0100000000000000000000000000000000000000000000000000000000000000",
110 "01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f",
111 "ed57ffd8c914fb201471d1c3d245ce3c746fcbe63a3679d51b6a516ebebe0e20",
112 "c34c4e1826e5d403b78e246e88aa051c36ccf0aafebffe137d148a2bf9104562",
113 "c940e5a4404157cfb1628b108db051a8d439e1a421394ec4ebccb9ec92a8ac78",
114 "47cfc5497c53dc8e61c91d17fd626ffb1c49e2bca94eed052281b510b1117a24",
115 "f1c6165d33367351b0da8f6e4511010c68174a03b6581212c71c0e1d026c3c72",
116 "87260f7a2f12495118360f02c26a470f450dadf34a413d21042b43b9d93e1309",
117 // These are all bad because they give a nonsquare x^2.
118 "26948d35ca62e643e26a83177332e6b6afeb9d08e4268b650f1f5bbd8d81d371",
119 "4eac077a713c57b4f4397629a4145982c661f48044dd3f96427d40b147d9742f",
120 "de6a7b00deadc788eb6b6c8d20c0ae96c2f2019078fa604fee5b87d6e989ad7b",
121 "bcab477be20861e01e4a0e295284146a510150d9817763caf1a6f4b422d67042",
122 "2a292df7e32cababbd9de088d1d1abec9fc0440f637ed2fba145094dc14bea08",
123 "f4a9e534fc0d216c44b218fa0c42d99635a0127ee2e53c712f70609649fdff22",
124 "8268436f8c4126196cf64b3c7ddbda90746a378625f9813dd9b8457077256731",
125 "2810e5cbc2cc4d4eece54f61c6f69758e289aa7ab440b3cbeaa21995c2f4232b",
126 // These are all bad because they give a negative xy value.
127 "3eb858e78f5a7254d8c9731174a94f76755fd3941c0ac93735c07ba14579630e",
128 "a45fdc55c76448c049a1ab33f17023edfb2be3581e9c7aade8a6125215e04220",
129 "d483fe813c6ba647ebbfd3ec41adca1c6130c2beeee9d9bf065c8d151c5f396e",
130 "8a2e1d30050198c65a54483123960ccc38aef6848e1ec8f5f780e8523769ba32",
131 "32888462f8b486c68ad7dd9610be5192bbeaf3b443951ac1a8118419d9fa097b",
132 "227142501b9d4355ccba290404bde41575b037693cef1f438c47f8fbf35d1165",
133 "5c37cc491da847cfeb9281d407efc41e15144c876e0170b499a96a22ed31e01e",
134 "445425117cb8c90edcbc7c1cc0e74f747f2c1efa5630a967c64f287792a48a4b",
135 // This is s = -1, which causes y = 0.
136 "ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f",
137 }
138
139 basepointMultiple := Element{}
140 basepointMultiple.Zero()
141
142 for i := range testVectors {
143 // Grab the bytes of the encoding
144 encoding, err := hex.DecodeString(testVectors[i])
145 if err != nil {
146 t.Fatalf("#%d: bad hex encoding in test vector: %v", i, err)
147 }
148
149 // Attempt decoding
150 decodedPoint := Element{}
151 err = decodedPoint.Decode(encoding)
152 if err == nil {
153 t.Fatalf("#%d: did not fail on bad encoding", i)
154 }
155 }
156 }
157
158 func TestRistrettoFromUniformBytesTestVectors(t *testing.T) {
159 // From RFC 9496, Appendix A.3, except the RFC published the SHA-512 images.
160 inputs := []string{
161 "Ristretto is traditionally a short shot of espresso coffee",
162 "made with the normal amount of ground coffee but extracted with",
163 "about half the amount of water in the same amount of time",
164 "by using a finer grind.",
165 "This produces a concentrated shot of coffee per volume.",
166 "Just pulling a normal shot short will produce a weaker shot",
167 "and is not a Ristretto as some believe.",
168 }
169 elements := []string{
170 "3066f82a1a747d45120d1740f14358531a8f04bbffe6a819f86dfe50f44a0a46",
171 "f26e5b6f7d362d2d2a94c5d0e7602cb4773c95a2e5c31a64f133189fa76ed61b",
172 "006ccd2a9e6867e6a2c5cea83d3302cc9de128dd2a9a57dd8ee7b9d7ffe02826",
173 "f8f0c87cf237953c5890aec3998169005dae3eca1fbb04548c635953c817f92a",
174 "ae81e7dedf20a497e10c304a765c1767a42d6e06029758d2d7e8ef7cc4c41179",
175 "e2705652ff9f5e44d3e841bf1c251cf7dddb77d140870d1ab2ed64f1a9ce8628",
176 "80bd07262511cdde4863f8a7434cef696750681cb9510eea557088f76d9e5065",
177 }
178
179 var element Element
180 for i, input := range inputs {
181 hash := sha512.Sum512([]byte(input))
182 element.FromUniformBytes(hash[:])
183 if encoding := hex.EncodeToString(element.Encode(nil)); encoding != elements[i] {
184 t.Errorf("#%d: expected %q, got %q", i, elements[i], encoding)
185 }
186 }
187 }
188
189 func TestEquivalentFromUniformBytes(t *testing.T) {
190 // From RFC 9496, Appendix A.3.
191 inputs := []string{
192 "edffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
193 "1200000000000000000000000000000000000000000000000000000000000000",
194 "edffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f" +
195 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
196 "0000000000000000000000000000000000000000000000000000000000000080" +
197 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f",
198 "0000000000000000000000000000000000000000000000000000000000000000" +
199 "1200000000000000000000000000000000000000000000000000000000000080",
200 }
201 expected := "304282791023b73128d277bdcb5c7746ef2eac08dde9f2983379cb8e5ef0517f"
202
203 var element Element
204 for i, input := range inputs {
205 h, err := hex.DecodeString(input)
206 if err != nil {
207 t.Fatalf("#%d: bad hex encoding in test vector: %v", i, err)
208 }
209
210 element.FromUniformBytes(h[:])
211 if encoding := hex.EncodeToString(element.Encode(nil)); encoding != expected {
212 t.Errorf("#%d: expected %q, got %q", i, expected, encoding)
213 }
214 }
215 }
216
217 func TestMarshalScalar(t *testing.T) {
218 x := new(Scalar)
219 // generate an arbitrary scalar
220 xbytes := sha512.Sum512([]byte("Hello World"))
221 x.SetUniformBytes(xbytes[:])
222 text, err := json.Marshal(x)
223 if err != nil {
224 t.Fatalf("Could not marshal json: %v", err)
225 }
226 t.Logf("json: %s", text)
227 y := new(Scalar)
228 err = json.Unmarshal(text, y)
229 if err != nil || y.Equal(x) == 0 {
230 t.Fatalf("Error unmarshaling scalar from json: %s %v", text, err)
231 }
232 }
233
234 func TestMarshalElement(t *testing.T) {
235 x := new(Element)
236 // generate an arbitrary element
237 xbytes := sha512.Sum512([]byte("Hello World"))
238 x.FromUniformBytes(xbytes[:])
239 text, err := json.Marshal(x)
240 if err != nil {
241 t.Fatalf("Could not marshal json: %v", err)
242 }
243 t.Logf("json: %s", text)
244 y := new(Element)
245 err = json.Unmarshal(text, y)
246 if err != nil || y.Equal(x) == 0 {
247 t.Fatalf("Error unmarshaling element from json: %s %v", text, err)
248 }
249 }
250
251 func TestElementSet(t *testing.T) {
252 // Test this, because the internal point type being hard-copyable isn't part of the spec.
253
254 el1 := NewIdentityElement()
255 el2 := NewGeneratorElement()
256
257 if el1.Equal(el2) == 1 {
258 t.Error("shouldn't be the same")
259 }
260
261 // Check new value
262 el1.Set(el2)
263
264 if el1.Equal(el2) == 0 {
265 t.Error("failed to set the value")
266 }
267
268 // Mutate source var
269 el2.Add(el2, el2)
270
271 if el1.Equal(el2) == 1 {
272 t.Error("shouldn't have changed")
273 }
274 }
275
276 func TestScalarSet(t *testing.T) {
277 // Test this, because the internal scalar representation being hard-copyable isn't part of the spec.
278
279 // 32-byte little endian value of "1"
280 scOne := make([]byte, 32)
281 scOne[0] = 0x01
282
283 sc1, sc2 := NewScalar(), NewScalar()
284
285 // sc1 <- 1
286 sc1.SetCanonicalBytes(scOne)
287
288 // 1 != 0
289 if sc1.Equal(sc2) == 1 {
290 t.Error("shouldn't be the same")
291 }
292
293 // sc2 <- sc1
294 sc2.Set(sc1)
295
296 // 1 == 1
297 if sc1.Equal(sc2) == 0 {
298 t.Error("failed to set the value")
299 }
300
301 // sc1 <- 1 + 1
302 sc1.Add(sc1, sc1)
303
304 // 2 != 1
305 if sc1.Equal(sc2) == 1 {
306 t.Error("shouldn't have changed")
307 }
308 }
309
310 func TestConstants(t *testing.T) {
311 // From RFC 9496, Section 4.1.
312 t.Run("d", func(t *testing.T) {
313 testConstant(t, d,
314 "37095705934669439343138083508754565189542113879843219016388785533085940283555")
315 })
316 t.Run("sqrtM1", func(t *testing.T) {
317 testConstant(t, sqrtM1,
318 "19681161376707505956807079304988542015446066515923890162744021073123829784752")
319 })
320 t.Run("sqrtADMinusOne", func(t *testing.T) {
321 testConstant(t, sqrtADMinusOne,
322 "25063068953384623474111414158702152701244531502492656460079210482610430750235")
323 })
324 t.Run("invSqrtAMinusD", func(t *testing.T) {
325 testConstant(t, invSqrtAMinusD,
326 "54469307008909316920995813868745141605393597292927456921205312896311721017578")
327 })
328 t.Run("oneMinusDSQ", func(t *testing.T) {
329 testConstant(t, oneMinusDSQ,
330 "1159843021668779879193775521855586647937357759715417654439879720876111806838")
331 })
332 t.Run("dMinusOneSQ", func(t *testing.T) {
333 testConstant(t, dMinusOneSQ,
334 "40440834346308536858101042469323190826248399146238708352240133220865137265952")
335 })
336 }
337
338 func testConstant(t *testing.T, f *field.Element, decimal string) {
339 b, ok := new(big.Int).SetString(decimal, 10)
340 if !ok {
341 t.Fatal("invalid decimal")
342 }
343 buf := b.FillBytes(make([]byte, 32))
344 for i := 0; i < len(buf)/2; i++ {
345 buf[i], buf[len(buf)-i-1] = buf[len(buf)-i-1], buf[i]
346 }
347 if !bytes.Equal(f.Bytes(), buf) {
348 t.Errorf("expected %x", buf)
349 }
350 }