Skip to content

Commit 84cf8d3

Browse files
committed
Improve efficiency of cx_bn_gf2_n_mul() for Nano S
1 parent 56ad188 commit 84cf8d3

File tree

3 files changed

+36
-35
lines changed

3 files changed

+36
-35
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [1.8.0] - 2024-10-22
8+
## [1.8.0] - 2024-10-28
99

1010
### Added
1111

@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222

2323
- Some plausible yet wrong mnemonic were deemed valid on NBGL devices
2424
- Merge Nano code
25+
- Improve efficiency of `cx_bn_gf2_n_mul()` for Nano S
2526

2627
## [1.7.4] - 2024-06-20
2728

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
### Todo
44

55
- [ ] Improve the efficiency of the method used to perform an inverse operation in GF(256)
6-
- [ ] Improve the efficiency of the custom cx_bn_gf2_n_mul() function used for Nano S devices
76
- [ ] Update automated function tests to test on nanox and nanosp
87
- [ ] There is just enough memory available on Nano S to hold the phrases for 10 shares. Maybe just store SSKR Bytewords as shorter two letter minimal Bytewords rather than a 4 letter Byteword plus space for each share. Convert minimal ByteWords back to four letter Bytewords just prior to display.
98

109
### In Progress
1110

1211
- [ ] Add BIP85 menus to Stax and Flex
1312
- [ ] Merge Nano code
13+
- [ ] Improve the efficiency of the custom cx_bn_gf2_n_mul() function used for Nano S devices
1414

1515
### Done ✓
1616

src/common/sskr/sss/interpolate.c

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* The modulus must be an irreducible polynomial over GF(2)
4040
* of degree n.
4141
*
42-
* @param[in] bn_h BN index of the second montgomery constant.
42+
* @param[in] bn_h BN index of the second Montgomery constant.
4343
*
4444
* @return Error code:
4545
* - CX_OK on success
@@ -52,7 +52,7 @@ cx_err_t cx_bn_gf2_n_mul(cx_bn_t bn_r,
5252
const cx_bn_t bn_b,
5353
const cx_bn_t bn_n,
5454
const cx_bn_t bn_h __attribute__((unused))) {
55-
cx_err_t error = CX_OK;
55+
cx_err_t error = CX_OK; // By default, until some error occurs
5656
uint32_t degree, nbits_a, nbits_b;
5757

5858
// Ensure bn_r is distinct from bn_a and bn_b
@@ -68,50 +68,50 @@ cx_err_t cx_bn_gf2_n_mul(cx_bn_t bn_r,
6868
CX_CHECK(cx_bn_cnt_bits(bn_a, &nbits_a));
6969
CX_CHECK(cx_bn_cnt_bits(bn_b, &nbits_b));
7070

71-
// Ensure both operands are in field
72-
if (degree < 1 || nbits_a > degree || nbits_b > degree) {
71+
// Ensure degree is valid and both operands are in field
72+
if (degree < nbits_a || degree < nbits_b || degree < 1) {
7373
error = CX_INVALID_PARAMETER;
7474
goto end;
7575
}
7676

77-
// Preliminaries
78-
cx_bn_t bn_tempa, bn_copy;
79-
uint32_t bit_indexb = 0;
80-
size_t nbytes;
81-
bool bit_set = 0;
82-
83-
CX_CHECK(cx_bn_nbytes(bn_n, &nbytes));
84-
CX_CHECK(cx_bn_alloc(&bn_tempa, nbytes));
85-
CX_CHECK(cx_bn_alloc(&bn_copy, nbytes));
86-
87-
CX_CHECK(cx_bn_copy(bn_tempa, bn_a));
8877
CX_CHECK(cx_bn_set_u32(bn_r, (uint32_t) 0));
8978

90-
// Main loop for multiplication
91-
if (nbits_a) {
92-
while (nbits_b > bit_indexb) {
93-
CX_CHECK(cx_bn_tst_bit(bn_b, bit_indexb, &bit_set));
79+
// If either operand is zero then result is zero
80+
if (nbits_a && nbits_b) {
81+
// Preliminaries
82+
cx_bn_t bn_temp, bn_copy;
83+
uint32_t bit_index = 0;
84+
size_t nbytes;
85+
bool bit_set;
86+
87+
CX_CHECK(cx_bn_nbytes(bn_n, &nbytes));
88+
CX_CHECK(cx_bn_alloc(&bn_temp, nbytes));
89+
CX_CHECK(cx_bn_alloc(&bn_copy, nbytes));
90+
CX_CHECK(cx_bn_copy(bn_temp, bn_a));
91+
92+
// Main loop for multiplication
93+
do {
94+
CX_CHECK(cx_bn_tst_bit(bn_b, bit_index++, &bit_set));
9495
if (bit_set) {
9596
CX_CHECK(cx_bn_copy(bn_copy, bn_r));
96-
CX_CHECK(cx_bn_xor(bn_r, bn_tempa, bn_copy));
97+
CX_CHECK(cx_bn_xor(bn_r, bn_temp, bn_copy));
9798
}
9899

99-
CX_CHECK(cx_bn_shl(bn_tempa, 1));
100-
CX_CHECK(cx_bn_tst_bit(bn_tempa, degree, &bit_set));
100+
if (!--nbits_b) break;
101101

102-
if (bit_set) {
103-
CX_CHECK(cx_bn_copy(bn_copy, bn_tempa));
104-
CX_CHECK(cx_bn_xor(bn_tempa, bn_n, bn_copy));
102+
CX_CHECK(cx_bn_shl(bn_temp, 1));
103+
if (nbits_a++ == degree) {
104+
CX_CHECK(cx_bn_copy(bn_copy, bn_temp));
105+
CX_CHECK(cx_bn_xor(bn_temp, bn_n, bn_copy));
106+
CX_CHECK(cx_bn_cnt_bits(bn_temp, &nbits_a));
105107
}
108+
} while (nbits_a);
106109

107-
bit_indexb++;
108-
}
110+
// Clean up
111+
CX_CHECK(cx_bn_destroy(&bn_temp));
112+
CX_CHECK(cx_bn_destroy(&bn_copy));
109113
}
110114

111-
// Clean up
112-
CX_CHECK(cx_bn_destroy(&bn_tempa));
113-
CX_CHECK(cx_bn_destroy(&bn_copy));
114-
115115
end:
116116
return error;
117117
}
@@ -151,7 +151,7 @@ cx_err_t interpolate(uint8_t n,
151151
CX_CHECK(cx_bn_set_u32(bn_xc_i, (uint32_t) xi[i]));
152152
CX_CHECK(cx_bn_set_u32(bn_lagrange, (uint32_t) 1));
153153

154-
// calculate the lagrange basis coefficient for the lagrange polynomial
154+
// calculate the Lagrange basis coefficient for the Lagrange polynomial
155155
// defined by the x coordinates xi at the value x.
156156
//
157157
// After loop runs, bn_lagrange should hold data satisfying
@@ -194,7 +194,7 @@ cx_err_t interpolate(uint8_t n,
194194
// bn_tempb = denominator^254
195195
CX_CHECK(cx_bn_gf2_n_mul(bn_tempb, bn_result, bn_tempc, bn_n, bn_r2));
196196

197-
// Calculate the lagrange basis coefficient
197+
// Calculate the Lagrange basis coefficient
198198
CX_CHECK(cx_bn_gf2_n_mul(bn_tempa, bn_numerator, bn_lagrange, bn_n, bn_r2));
199199
CX_CHECK(cx_bn_gf2_n_mul(bn_lagrange, bn_tempa, bn_tempb, bn_n, bn_r2));
200200
}

0 commit comments

Comments
 (0)