v / vlib / crypto / ed25519 / internal / edwards25519 / scalar_test.v
210 lines · 170 sloc · 5.78 KB
Raw
1module edwards25519
2
3import os
4import rand
5import encoding.hex
6import math.big
7
8const github_job = os.getenv('GITHUB_JOB')
9
10fn testsuite_begin() {
11 if github_job != '' {
12 // ensure that the CI does not run flaky tests:
13 rand.seed([u32(0xffff24), 0xabcd])
14 }
15}
16
17fn test_scalar_equal() {
18 assert sc_one.equal(sc_minus_one) != 1
19
20 assert sc_minus_one.equal(sc_minus_one) != 0
21}
22
23@[direct_array_access]
24fn test_scalar_non_adjacent_form() {
25 mut s := Scalar{
26 s: [u8(0x1a), 0x0e, 0x97, 0x8a, 0x90, 0xf6, 0x62, 0x2d, 0x37, 0x47, 0x02, 0x3f, 0x8a, 0xd8,
27 0x26, 0x4d, 0xa7, 0x58, 0xaa, 0x1b, 0x88, 0xe0, 0x40, 0xd1, 0x58, 0x9e, 0x7b, 0x7f,
28 0x23, 0x76, 0xef, 0x09]!
29 }
30 expected_naf := [i8(0), 13, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, -9, 0, 0, 0, 0, -11,
31 0, 0, 0, 0, 3, 0, 0, 0, 0, 1, 0, 0, 0, 0, 9, 0, 0, 0, 0, -5, 0, 0, 0, 0, 0, 0, 3, 0, 0,
32 0, 0, 11, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, -9, 0, 0, 0, 0, 0, -3, 0, 0, 0, 0, 9, 0, 0, 0,
33 0, 0, 1, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, -15, 0, 0, 0, 0, -7, 0, 0,
34 0, 0, -9, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, -3, 0, 0, 0, 0, -11, 0, 0, 0,
35 0, -7, 0, 0, 0, 0, -13, 0, 0, 0, 0, 11, 0, 0, 0, 0, -9, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
36 -15, 0, 0, 0, 0, 1, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 13, 0, 0,
37 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, -9, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0,
38 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, -15, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 15, 0, 0, 0, 0, 15, 0,
39 0, 0, 0, 0, 1, 0, 0, 0, 0]!
40
41 snaf := s.non_adjacent_form(5)
42 for i := 0; i < 256; i++ {
43 assert expected_naf[i] == snaf[i]
44 }
45}
46
47fn addlike_subneg(x Scalar, y Scalar) bool {
48 // Compute t1 = x - y
49 mut t1 := Scalar{}
50 t1.subtract(x, y)
51
52 // Compute t2 = -y + x
53 mut t2 := Scalar{}
54 t2.negate(y)
55 t2.add(t2, x)
56
57 return t1 == t2 && is_reduced(t1)
58}
59
60fn test_scalar_add_like_subneg() {
61 for i in 0 .. 15 {
62 x := generate_scalar(1000) or { panic(err) }
63 y := generate_scalar(1000) or { panic(err) }
64 assert addlike_subneg(x, y) == true
65 }
66}
67
68fn fg(sc Scalar) bool {
69 return is_reduced(sc)
70}
71
72fn test_scalar_generate() {
73 for i in 0 .. 15 {
74 sc := generate_scalar(1000) or { panic(err) }
75
76 assert fg(sc) == true
77 }
78}
79
80//
81fn test_scalar_set_canonical_bytes() {
82 for i in 0 .. 10 {
83 mut buf := rand.bytes(32) or { panic(err) }
84 mut sc := generate_scalar(1000) or { panic(err) }
85 buf[buf.len - 1] &= (1 << 4) - 1
86 sc = sc.set_canonical_bytes(buf) or { panic(err) }
87
88 assert buf[..] == sc.bytes()
89 assert is_reduced(sc)
90 }
91}
92
93fn test_scalar_set_canonical_bytes_round_trip() {
94 for i in 0 .. 10 {
95 mut sc1 := generate_scalar(2)!
96 mut sc2 := generate_scalar(6)!
97 sc2.set_canonical_bytes(sc1.bytes()) or { panic(err) }
98
99 assert sc1 == sc2
100 }
101}
102
103const sc_error = Scalar{
104 s: [32]u8{init: (u8(-1))}
105}
106
107fn test_scalar_set_canonical_bytes_on_noncanonical_value() {
108 mut b := sc_minus_one.s
109 b[31] += 1
110
111 mut s := sc_one
112 out :=
113 s.set_canonical_bytes(b[..]) or { sc_error } // set_canonical_bytes shouldn't worked on a non-canonical value"
114 assert out == sc_error
115 assert s == sc_one
116}
117
118fn test_scalar_set_uniform_bytes() {
119 // mod, _ := new(big.Integer).SetString("27742317777372353535851937790883648493", 10)
120 mut mod := big.integer_from_string('27742317777372353535851937790883648493')!
121 // mod.Add(mod, new(big.Integer).Lsh(big.NewInt(1), 252))
122 mod = mod + big.integer_from_i64(1).left_shift(252)
123
124 mut sc := generate_scalar(100)!
125 inp := rand.bytes(64)!
126
127 sc.set_uniform_bytes(inp[..])!
128 assert is_reduced(sc) == true
129
130 scbig := bigint_from_le_bytes(sc.s[..])
131 inbig := bigint_from_le_bytes(inp)
132 // return inbig.Mod(inbig, mod).Cmp(scbig) == 0
133 _, m := inbig.div_mod(mod)
134 assert m.abs_cmp(scbig) == 0 // NEED FIX
135}
136
137fn bigint_from_le_bytes(b []u8) big.Integer {
138 mut bc := b.clone()
139 buf := swap_endianness(mut bc) // WITHOUT THIS, some test would fail
140 bg := big.integer_from_bytes(buf)
141 return bg
142}
143
144fn test_scalar_set_bytes_with_clamping() {
145 // Generated with libsodium.js 1.0.18 crypto_scalarmult_ed25519_base.
146 /*
147 random := "633d368491364dc9cd4c1bf891b1d59460face1644813240a313e61f2c88216e"
148 s, _ := new(Scalar).SetBytesWithClamping(decodeHex(random))
149 p := new(Point).ScalarBaseMult(s)
150 want := "1d87a9026fd0126a5736fe1628c95dd419172b5b618457e041c9c861b2494a94"
151 if got := hex.EncodeToString(p.Bytes()); got != want {
152 t.Errorf("random: got %q, want %q", got, want)
153 }*/
154 random := '633d368491364dc9cd4c1bf891b1d59460face1644813240a313e61f2c88216e'
155 random_bytes := hex.decode(random) or { []u8{} }
156
157 mut s0 := Scalar{}
158 s0.set_bytes_with_clamping(random_bytes) or { Scalar{} }
159
160 mut p0 := Point{}
161 p0.scalar_base_mult(mut s0)
162
163 want0 := '1d87a9026fd0126a5736fe1628c95dd419172b5b618457e041c9c861b2494a94'
164 got0 := hex.encode(p0.bytes())
165
166 assert got0 == want0
167
168 zero := '0000000000000000000000000000000000000000000000000000000000000000'
169 mut s1 := Scalar{}
170 zero_bytes := hex.decode(zero) or { []u8{} }
171 s1.set_bytes_with_clamping(zero_bytes) or { Scalar{} }
172 mut p1 := Point{}
173 p1.scalar_base_mult(mut s1)
174
175 want1 := '693e47972caf527c7883ad1b39822f026f47db2ab0e1919955b8993aa04411d1'
176 got1 := hex.encode(p1.bytes())
177 assert want1 == got1
178
179 one := 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
180 mut s2 := Scalar{}
181 mut one_bytes := hex.decode(one) or { []u8{} }
182 s2.set_bytes_with_clamping(one_bytes) or { Scalar{} }
183 mut p2 := Point{}
184 p2.scalar_base_mult(mut s2)
185
186 want2 := '12e9a68b73fd5aacdbcaf3e88c46fea6ebedb1aa84eed1842f07f8edab65e3a7'
187 got2 := hex.encode(p2.bytes())
188
189 assert want2 == got2
190}
191
192fn test_scalar_multiply_distributes_over_add() {
193 x := generate_scalar(100)!
194 y := generate_scalar(100)!
195 z := generate_scalar(100)!
196
197 // Compute t1 = (x+y)*z
198 mut t1 := Scalar{}
199 t1.add(x, y)
200 t1.multiply(t1, z)
201
202 // Compute t2 = x*z + y*z
203 mut t2 := Scalar{}
204 mut t3 := Scalar{}
205 t2.multiply(x, z)
206 t3.multiply(y, z)
207 t2.add(t2, t3)
208
209 assert t1 == t2 && is_reduced(t1) && is_reduced(t3)
210}
211