Skip to content

Commit c762629

Browse files
committed
[sw,cryptolib] Change expected values for tests with result 0
This commit changes the expected values for the tests that have an expected result of 0. This makes sure we don't run into issues where the tests are not executing properly and we still get a correct result due to the expected value of 0. Furthermore, this commit moves the tests to the hjson framework. The input and output values don't change from before this change. This commit merely skips the unnecessary calculations. Signed-off-by: Hakim Filali <[email protected]>
1 parent 6d41f4b commit c762629

22 files changed

+202
-459
lines changed

sw/otbn/crypto/tests/BUILD

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ otbn_sim_test(
409409
srcs = [
410410
"p256_arithmetic_to_boolean_test.s",
411411
],
412-
exp = "p256_arithmetic_to_boolean_test.exp",
412+
testcase = "p256_arithmetic_to_boolean_test.hjson",
413413
deps = [
414414
"//sw/otbn/crypto:p256_base",
415415
"//sw/otbn/crypto:p256_isoncurve",
@@ -423,7 +423,7 @@ otbn_sim_test(
423423
srcs = [
424424
"p256_arithmetic_to_boolean_mod_test.s",
425425
],
426-
exp = "p256_arithmetic_to_boolean_mod_test.exp",
426+
testcase = "p256_arithmetic_to_boolean_mod_test.hjson",
427427
deps = [
428428
"//sw/otbn/crypto:p256_base",
429429
"//sw/otbn/crypto:p256_isoncurve",
@@ -465,7 +465,7 @@ otbn_sim_test(
465465
srcs = [
466466
"p384_boolean_to_arithmetic_test.s",
467467
],
468-
exp = "p384_boolean_to_arithmetic_test.exp",
468+
testcase = "p384_boolean_to_arithmetic_test.hjson",
469469
deps = [
470470
"//sw/otbn/crypto:p384_b2a",
471471
],
@@ -476,7 +476,7 @@ otbn_sim_test(
476476
srcs = [
477477
"p384_arithmetic_to_boolean_test.s",
478478
],
479-
exp = "p384_arithmetic_to_boolean_test.exp",
479+
testcase = "p384_arithmetic_to_boolean_test.hjson",
480480
deps = [
481481
"//sw/otbn/crypto:p384_a2b",
482482
"//sw/otbn/crypto:p384_base",
@@ -488,7 +488,7 @@ otbn_sim_test(
488488
srcs = [
489489
"p384_arithmetic_to_boolean_mod_test.s",
490490
],
491-
exp = "p384_arithmetic_to_boolean_mod_test.exp",
491+
testcase = "p384_arithmetic_to_boolean_mod_test.hjson",
492492
deps = [
493493
"//sw/otbn/crypto:p384_a2b",
494494
"//sw/otbn/crypto:p384_base",
@@ -601,7 +601,7 @@ otbn_sim_test(
601601
srcs = [
602602
"p384_keygen_test.s",
603603
],
604-
exp = "p384_keygen_test.exp",
604+
testcase = "p384_keygen_test.hjson",
605605
deps = [
606606
"//sw/otbn/crypto:p384_base",
607607
"//sw/otbn/crypto:p384_keygen",
@@ -614,7 +614,7 @@ otbn_sim_test(
614614
srcs = [
615615
"p384_keygen_from_seed_test.s",
616616
],
617-
exp = "p384_keygen_from_seed_test.exp",
617+
testcase = "p384_keygen_from_seed_test.hjson",
618618
deps = [
619619
"//sw/otbn/crypto:p384_b2a",
620620
"//sw/otbn/crypto:p384_base",

sw/otbn/crypto/tests/p256_arithmetic_to_boolean_mod_test.exp

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright lowRISC contributors (OpenTitan project).
2+
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
// SPDX-License-Identifier: Apache-2.0
4+
{
5+
"entrypoint": "p256_arithmetic_to_boolean_mod_test",
6+
"input": {
7+
"regs": {
8+
# w31 is zero
9+
"w31": "0x0000000000000000000000000000000000000000000000000000000000000000"
10+
# First arithmetic share of x.
11+
"w11": "0x2328ce20226e4542f38f1ca699ce6272592fbe18c2d1a4f0eefd3e816b35c5e6",
12+
# Second arithmetic share of x.
13+
"w19": "0x53038ee6d4a25811969abf6f892fa008dc0f09fc3c6c760991349036bf81b6ba"
14+
}
15+
}
16+
"output": {
17+
"regs": {
18+
# Unmasked result x.
19+
"w20": "0x762c5d06f7109d548a29dc1622fe027b353ec814ff3e1afa8031ceb82ab77ca0",
20+
}
21+
}
22+
}

sw/otbn/crypto/tests/p256_arithmetic_to_boolean_mod_test.s

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212

1313
.section .text.start
1414

15-
p256_arithmetic_to_boolean_test:
16-
17-
/* init all-zero register */
18-
bn.xor w31, w31, w31
15+
p256_arithmetic_to_boolean_mod_test:
1916

2017
/* Load domain parameter.
2118
w29 = dmem[p256_p] */
@@ -26,58 +23,18 @@ p256_arithmetic_to_boolean_test:
2623
/* Set MOD to p */
2724
bn.wsrw MOD, w29
2825

29-
/* Load values into WDRs */
30-
31-
/* w11 <= dmem[x] mod p */
32-
li x3, 11
33-
la x4, x
34-
bn.lid x3, 0(x4)
35-
bn.addm w11, w11, w31
36-
37-
/* w19 <= URND mod p */
38-
bn.wsrr w19, URND
39-
bn.addm w19, w19, w31
40-
41-
/* Arithmetic masking */
42-
43-
/* w11 = A <= w11 - w19 = x - r */
44-
bn.subm w11, w11, w19
45-
4626
/* Arithmetic to boolean conversion */
4727
jal x1, arithmetic_to_boolean_mod
4828

49-
/* Unmask and compare values
50-
after conversion */
29+
/* Unmask after conversion */
5130

5231
/* w20 <= w20 ^ w19 = x' ^ r = x */
5332
bn.xor w20, w20, w19
5433

55-
/* w10 <= dmem[x] mod p */
56-
li x3, 10
57-
la x4, x
58-
bn.lid x3, 0(x4)
59-
bn.addm w10, w10, w31
60-
61-
/* w0 <= w10 - w20 */
62-
bn.sub w0, w20, w10
63-
6434
ecall
6535

66-
6736
.data
6837

69-
.globl x
70-
.balign 32
71-
x:
72-
.word 0x2ab77ca0
73-
.word 0x8031ceb8
74-
.word 0xff3e1afa
75-
.word 0x353ec814
76-
.word 0x22fe027b
77-
.word 0x8a29dc16
78-
.word 0xf7109d54
79-
.word 0x762c5d06
80-
8138
/* Public key z-coordinate. */
8239
.globl z
8340
.balign 32

sw/otbn/crypto/tests/p256_arithmetic_to_boolean_test.exp

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright lowRISC contributors (OpenTitan project).
2+
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
// SPDX-License-Identifier: Apache-2.0
4+
{
5+
"entrypoint": "p256_arithmetic_to_boolean_test",
6+
"input": {
7+
"regs": {
8+
# w31 is zero
9+
"w31": "0x0000000000000000000000000000000000000000000000000000000000000000",
10+
# First arithmetic share of x.
11+
"w11": "0x58b8977117dfeb954edfca6e9a7a6dc50957e6bd913a34a2d986834026276da7",
12+
"w12": "0x0000000000000000000000000000000000000000000000000000000000000000",
13+
# Second arithmetic share of x.
14+
"w18": "0x1d73c595df30b1bf3b4a11a7888394b62be6e1576e03e657a6ab4b7804900ef9",
15+
"w19": "0x0000000000000000000000000000000000000000000000000000000000000001"
16+
}
17+
}
18+
"output": {
19+
"regs": {
20+
# Unmasked result x.
21+
"w20": "0x762c5d06f7109d548a29dc1622fe027b353ec814ff3e1afa8031ceb82ab77ca0",
22+
"w21": "0x0000000000000000000000000000000000000000000000000000000000000001",
23+
}
24+
}
25+
}

sw/otbn/crypto/tests/p256_arithmetic_to_boolean_test.s

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414

1515
p256_arithmetic_to_boolean_test:
1616

17-
/* init all-zero register */
18-
bn.xor w31, w31, w31
19-
2017
/* Load domain parameter.
2118
w29 = dmem[p256_p] */
2219
li x2, 29
@@ -26,87 +23,20 @@ p256_arithmetic_to_boolean_test:
2623
/* Set MOD to p */
2724
bn.wsrw MOD, w29
2825

29-
/* Load values into WDRs */
30-
31-
/* w11 <= dmem[x_l] */
32-
li x3, 11
33-
la x4, x_l
34-
bn.lid x3, 0(x4)
35-
36-
/* w12 <= dmem[x_u] */
37-
li x3, 12
38-
la x4, x_u
39-
bn.lid x3, 0(x4)
40-
41-
/* w18 <= URND
42-
w19 <= URND (1 bit) */
43-
bn.wsrr w18, URND
44-
bn.wsrr w19, URND
45-
bn.rshi w19, w31, w19 >> 255
46-
47-
/* Arithmetic masking */
48-
49-
/* [w12,w11] = A <= [w12,w11] - [w19,w18] mod 2^257 = x - r mod 2^257
50-
This may result in bits above 2^257, but these will be stripped off. */
51-
bn.sub w11, w11, w18
52-
bn.subb w12, w12, w19
53-
bn.rshi w12, w12, w31 >> 1
54-
bn.rshi w12, w31, w12 >> 255
55-
5626
/* Arithmetic to boolean conversion */
5727
jal x1, arithmetic_to_boolean
5828

59-
/* Unmask and compare values
60-
after conversion */
29+
/* Unmask after conversion */
6130

6231
/* w20 <= w20 ^ w18 = x' ^ r
6332
w21 <= w21 ^ w19 = x' ^ r */
6433
bn.xor w20, w20, w18
6534
bn.xor w21, w21, w19
6635

67-
/* w11 <= dmem[x_l] */
68-
li x3, 11
69-
la x4, x_l
70-
bn.lid x3, 0(x4)
71-
72-
/* w12 <= dmem[x_u] */
73-
li x3, 12
74-
la x4, x_u
75-
bn.lid x3, 0(x4)
76-
77-
/* [w1,w0] <= [w12,w11] - [w21,w20] */
78-
bn.sub w0, w11, w20
79-
bn.subb w1, w12, w21
80-
8136
ecall
8237

83-
8438
.data
8539

86-
.globl x_u
87-
.balign 32
88-
x_u:
89-
.word 0x00000001
90-
.word 0x00000000
91-
.word 0x00000000
92-
.word 0x00000000
93-
.word 0x00000000
94-
.word 0x00000000
95-
.word 0x00000000
96-
.word 0x00000000
97-
98-
.globl x_l
99-
.balign 32
100-
x_l:
101-
.word 0x2ab77ca0
102-
.word 0x8031ceb8
103-
.word 0xff3e1afa
104-
.word 0x353ec814
105-
.word 0x22fe027b
106-
.word 0x8a29dc16
107-
.word 0xf7109d54
108-
.word 0x762c5d06
109-
11040
/* Public key z-coordinate. */
11141
.globl z
11242
.balign 32

sw/otbn/crypto/tests/p384_arithmetic_to_boolean_mod_test.exp

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright lowRISC contributors (OpenTitan project).
2+
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
// SPDX-License-Identifier: Apache-2.0
4+
{
5+
"entrypoint": "p384_arithmetic_to_boolean_mod_test",
6+
"input": {
7+
"regs": {
8+
# w31 is zero
9+
"w31": "0x0000000000000000000000000000000000000000000000000000000000000000"
10+
# First arithmetic share of x.
11+
"w11": "0x034084329b46f1f0d277b31ea21c79370defc8a41b42246d3f4aae8c76f51f07",
12+
"w12": "0x000000000000000000000000000000007679a2adc67c5fc8dd94a7f9c7ebf0b3",
13+
# Second arithmetic share of x.
14+
"w18": "0xe6ed0dfd7175e4ce3b976dff369604fb339e0775ef413315f03ca38e57185a0b",
15+
"w19": "0x00000000000000000000000000000000bec5256738c1bb31a29d26be62cb8bed"
16+
}
17+
}
18+
"output": {
19+
"regs": {
20+
# Unmasked result x.
21+
"w20": "0xea2d92300cbcd6bf0e0f211dd8b27e33418dd01b0a8357832f875219ce0d7913",
22+
"w21": "0x00000000000000000000000000000000353ec814ff3e1afa8031ceb82ab77ca0"
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)