| 1 | package ristretto_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/hex" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/bwesterb/go-ristretto" |
| 9 | ) |
| 10 | |
| 11 | func TestPointDerive(t *testing.T) { |
| 12 | testVectors := []struct{ in, out string }{ |
| 13 | {"test", "b01d60504aa5f4c5bd9a7541c457661f9a789d18cb4e136e91d3c953488bd208"}, |
| 14 | {"pep", "3286c8d171dec02e70549c280d62524430408a781efc07e4428d1735671d195b"}, |
| 15 | {"ristretto", "c2f6bb4c4dab8feab66eab09e77e79b36095c86b3cd1145b9a2703205858d712"}, |
| 16 | {"elligator", "784c727b1e8099eb94e5a8edbd260363567fdbd35106a7a29c8b809cd108b322"}, |
| 17 | } |
| 18 | for _, v := range testVectors { |
| 19 | var p ristretto.Point |
| 20 | p.Derive([]byte(v.in)) |
| 21 | out2 := hex.EncodeToString(p.Bytes()) |
| 22 | if out2 != v.out { |
| 23 | t.Fatalf("Derive(%v) = %v != %v", v.in, v.out, out2) |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | // Test vectors from https://ristretto.group/test_vectors/ristretto255.html |
| 29 | func TestRistretto255TestVectors(t *testing.T) { |
| 30 | smallMultiples := []string{ |
| 31 | "0000000000000000000000000000000000000000000000000000000000000000", |
| 32 | "e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76", |
| 33 | "6a493210f7499cd17fecb510ae0cea23a110e8d5b901f8acadd3095c73a3b919", |
| 34 | "94741f5d5d52755ece4f23f044ee27d5d1ea1e2bd196b462166b16152a9d0259", |
| 35 | "da80862773358b466ffadfe0b3293ab3d9fd53c5ea6c955358f568322daf6a57", |
| 36 | "e882b131016b52c1d3337080187cf768423efccbb517bb495ab812c4160ff44e", |
| 37 | "f64746d3c92b13050ed8d80236a7f0007c3b3f962f5ba793d19a601ebb1df403", |
| 38 | "44f53520926ec81fbd5a387845beb7df85a96a24ece18738bdcfa6a7822a176d", |
| 39 | "903293d8f2287ebe10e2374dc1a53e0bc887e592699f02d077d5263cdd55601c", |
| 40 | "02622ace8f7303a31cafc63f8fc48fdc16e1c8c8d234b2f0d6685282a9076031", |
| 41 | "20706fd788b2720a1ed2a5dad4952b01f413bcf0e7564de8cdc816689e2db95f", |
| 42 | "bce83f8ba5dd2fa572864c24ba1810f9522bc6004afe95877ac73241cafdab42", |
| 43 | "e4549ee16b9aa03099ca208c67adafcafa4c3f3e4e5303de6026e3ca8ff84460", |
| 44 | "aa52e000df2e16f55fb1032fc33bc42742dad6bd5a8fc0be0167436c5948501f", |
| 45 | "46376b80f409b29dc2b5f6f0c52591990896e5716f41477cd30085ab7f10301e", |
| 46 | "e0c418f7c8d9c4cdd7395b93ea124f3ad99021bb681dfc3302a9d99a2e53e64e", |
| 47 | } |
| 48 | var B, pt ristretto.Point |
| 49 | B.SetBase() |
| 50 | pt.SetZero() |
| 51 | for i, pt2 := range smallMultiples { |
| 52 | if hex.EncodeToString(pt.Bytes()) != pt2 { |
| 53 | t.Fatalf("%d * B = %s != %v", i, pt2, hex.EncodeToString(pt.Bytes())) |
| 54 | } |
| 55 | pt.Add(&B, &pt) |
| 56 | } |
| 57 | |
| 58 | badEncodings := []string{ |
| 59 | // Non-canonical field encodings. |
| 60 | "00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", |
| 61 | "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f", |
| 62 | "f3ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f", |
| 63 | "edffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f", |
| 64 | |
| 65 | // Negative field elements. |
| 66 | "0100000000000000000000000000000000000000000000000000000000000000", |
| 67 | "01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f", |
| 68 | "ed57ffd8c914fb201471d1c3d245ce3c746fcbe63a3679d51b6a516ebebe0e20", |
| 69 | "c34c4e1826e5d403b78e246e88aa051c36ccf0aafebffe137d148a2bf9104562", |
| 70 | "c940e5a4404157cfb1628b108db051a8d439e1a421394ec4ebccb9ec92a8ac78", |
| 71 | "47cfc5497c53dc8e61c91d17fd626ffb1c49e2bca94eed052281b510b1117a24", |
| 72 | "f1c6165d33367351b0da8f6e4511010c68174a03b6581212c71c0e1d026c3c72", |
| 73 | "87260f7a2f12495118360f02c26a470f450dadf34a413d21042b43b9d93e1309", |
| 74 | |
| 75 | // Non-square x^2. |
| 76 | "26948d35ca62e643e26a83177332e6b6afeb9d08e4268b650f1f5bbd8d81d371", |
| 77 | "4eac077a713c57b4f4397629a4145982c661f48044dd3f96427d40b147d9742f", |
| 78 | "de6a7b00deadc788eb6b6c8d20c0ae96c2f2019078fa604fee5b87d6e989ad7b", |
| 79 | "bcab477be20861e01e4a0e295284146a510150d9817763caf1a6f4b422d67042", |
| 80 | "2a292df7e32cababbd9de088d1d1abec9fc0440f637ed2fba145094dc14bea08", |
| 81 | "f4a9e534fc0d216c44b218fa0c42d99635a0127ee2e53c712f70609649fdff22", |
| 82 | "8268436f8c4126196cf64b3c7ddbda90746a378625f9813dd9b8457077256731", |
| 83 | "2810e5cbc2cc4d4eece54f61c6f69758e289aa7ab440b3cbeaa21995c2f4232b", |
| 84 | |
| 85 | // Negative xy value. |
| 86 | "3eb858e78f5a7254d8c9731174a94f76755fd3941c0ac93735c07ba14579630e", |
| 87 | "a45fdc55c76448c049a1ab33f17023edfb2be3581e9c7aade8a6125215e04220", |
| 88 | "d483fe813c6ba647ebbfd3ec41adca1c6130c2beeee9d9bf065c8d151c5f396e", |
| 89 | "8a2e1d30050198c65a54483123960ccc38aef6848e1ec8f5f780e8523769ba32", |
| 90 | "32888462f8b486c68ad7dd9610be5192bbeaf3b443951ac1a8118419d9fa097b", |
| 91 | "227142501b9d4355ccba290404bde41575b037693cef1f438c47f8fbf35d1165", |
| 92 | "5c37cc491da847cfeb9281d407efc41e15144c876e0170b499a96a22ed31e01e", |
| 93 | "445425117cb8c90edcbc7c1cc0e74f747f2c1efa5630a967c64f287792a48a4b", |
| 94 | |
| 95 | // s = -1, which causes y = 0. |
| 96 | "ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f", |
| 97 | } |
| 98 | |
| 99 | for _, ptHex := range badEncodings { |
| 100 | var pt ristretto.Point |
| 101 | var buf [32]byte |
| 102 | tmp, _ := hex.DecodeString(ptHex) |
| 103 | copy(buf[:], tmp) |
| 104 | if pt.SetBytes(&buf) { |
| 105 | t.Fatalf("%v should not decode", ptHex) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | encodedHashToPoints := []struct{ label, encoding string }{ |
| 110 | {"Ristretto is traditionally a short shot of espresso coffee", |
| 111 | "3066f82a1a747d45120d1740f14358531a8f04bbffe6a819f86dfe50f44a0a46"}, |
| 112 | {"made with the normal amount of ground coffee but extracted with", |
| 113 | "f26e5b6f7d362d2d2a94c5d0e7602cb4773c95a2e5c31a64f133189fa76ed61b"}, |
| 114 | {"about half the amount of water in the same amount of time", |
| 115 | "006ccd2a9e6867e6a2c5cea83d3302cc9de128dd2a9a57dd8ee7b9d7ffe02826"}, |
| 116 | {"by using a finer grind.", |
| 117 | "f8f0c87cf237953c5890aec3998169005dae3eca1fbb04548c635953c817f92a"}, |
| 118 | {"This produces a concentrated shot of coffee per volume.", |
| 119 | "ae81e7dedf20a497e10c304a765c1767a42d6e06029758d2d7e8ef7cc4c41179"}, |
| 120 | {"Just pulling a normal shot short will produce a weaker shot", |
| 121 | "e2705652ff9f5e44d3e841bf1c251cf7dddb77d140870d1ab2ed64f1a9ce8628"}, |
| 122 | {"and is not a Ristretto as some believe.", |
| 123 | "80bd07262511cdde4863f8a7434cef696750681cb9510eea557088f76d9e5065"}, |
| 124 | } |
| 125 | |
| 126 | for _, tp := range encodedHashToPoints { |
| 127 | var p ristretto.Point |
| 128 | p.DeriveDalek([]byte(tp.label)) |
| 129 | |
| 130 | res := hex.EncodeToString(p.Bytes()) |
| 131 | if res != tp.encoding { |
| 132 | t.Fatalf("Test string %v produced %s instead of %s", |
| 133 | tp.label, res, tp.encoding) |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // Test base point multiplication |
| 139 | func TestBasePointMultiples(t *testing.T) { |
| 140 | var s ristretto.Scalar |
| 141 | var p1, p2, B ristretto.Point |
| 142 | B.SetBase() |
| 143 | for i := 0; i < 1000; i++ { |
| 144 | s.Rand() |
| 145 | p1.ScalarMultBase(&s) |
| 146 | p2.ScalarMult(&B, &s) |
| 147 | if !p1.Equals(&p2) { |
| 148 | t.Fatalf("[%v]B = %v != %v", s, p2, p1) |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | func TestConditionalSet(t *testing.T) { |
| 154 | var p1, p2 ristretto.Point |
| 155 | for i := 0; i < 1000; i++ { |
| 156 | p1.Rand() |
| 157 | p2.Rand() |
| 158 | p1.ConditionalSet(&p2, 0) |
| 159 | if p1.Equals(&p2) { |
| 160 | t.Fatal() |
| 161 | } |
| 162 | p1.ConditionalSet(&p2, 1) |
| 163 | if !p1.Equals(&p2) { |
| 164 | t.Fatal() |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | func testLizardVector(t *testing.T, in, out string) { |
| 170 | var p ristretto.Point |
| 171 | var inBuf [16]byte |
| 172 | var outBuf [32]byte |
| 173 | hex.Decode(inBuf[:], []byte(in)) |
| 174 | hex.Decode(outBuf[:], []byte(out)) |
| 175 | p.SetLizard(&inBuf) |
| 176 | if !bytes.Equal(p.Bytes(), outBuf[:]) { |
| 177 | t.Fatalf("Lizard(%s) is wrong", in) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | func TestLizardVectors(t *testing.T) { |
| 182 | testLizardVector(t, "00000000000000000000000000000000", |
| 183 | "f0b7e34484f74cf00f15024b738539738646bbbe1e9bc7509a676815227e774f") |
| 184 | testLizardVector(t, "01010101010101010101010101010101", |
| 185 | "cc92e81f585afc5caac88660d8d17e9025a44489a363042123f6af0702156e65") |
| 186 | } |
| 187 | |
| 188 | func TestLizardInjective(t *testing.T) { |
| 189 | var buf1, buf2 [16]byte |
| 190 | var buf [32]byte |
| 191 | var p ristretto.Point |
| 192 | for i := 0; i < 1000; i++ { |
| 193 | rnd.Read(buf1[:]) |
| 194 | p.SetLizard(&buf1) |
| 195 | p.BytesInto(&buf) |
| 196 | if !p.SetBytes(&buf) { |
| 197 | t.Fatal() |
| 198 | } |
| 199 | err := p.LizardInto(&buf2) |
| 200 | if err != nil { |
| 201 | t.Fatalf("LizardInto: %v", err) |
| 202 | } |
| 203 | if !bytes.Equal(buf1[:], buf2[:]) { |
| 204 | t.Fatalf("Lizard^-1 o Lizard != id: %v != %v", buf1, buf2) |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | func BenchmarkLizardDecode(b *testing.B) { |
| 210 | buf := [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16} |
| 211 | var p ristretto.Point |
| 212 | p.SetLizard(&buf) |
| 213 | b.ResetTimer() |
| 214 | for n := 0; n < b.N; n++ { |
| 215 | p.Lizard() |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | func BenchmarkLizardEncode(b *testing.B) { |
| 220 | var buf [16]byte |
| 221 | var p ristretto.Point |
| 222 | for n := 0; n < b.N; n++ { |
| 223 | rnd.Read(buf[:]) |
| 224 | p.SetLizard(&buf) |
| 225 | } |
| 226 | } |