Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f00fa241 authored by Nicolai Stange's avatar Nicolai Stange Committed by Herbert Xu
Browse files

lib/mpi: mpi_read_buffer(): optimize skipping of leading zero limbs



Currently, if the number of leading zeros is greater than fits into a
complete limb, mpi_read_buffer() skips them by iterating over them
limb-wise.

Instead of skipping the high order zero limbs within the loop as shown
above, adjust the copying loop's bounds.

Signed-off-by: default avatarNicolai Stange <nicstange@gmail.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent d7552906
Loading
Loading
Loading
Loading
+8 −10
Original line number Original line Diff line number Diff line
@@ -184,7 +184,9 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
	p = buf;
	p = buf;
	*nbytes = n - lzeros;
	*nbytes = n - lzeros;


	for (i = a->nlimbs - 1; i >= 0; i--) {
	for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB,
			lzeros %= BYTES_PER_MPI_LIMB;
		i >= 0; i--) {
		alimb = a->d[i];
		alimb = a->d[i];
#if BYTES_PER_MPI_LIMB == 4
#if BYTES_PER_MPI_LIMB == 4
		*p++ = alimb >> 24;
		*p++ = alimb >> 24;
@@ -205,15 +207,11 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
#endif
#endif


		if (lzeros > 0) {
		if (lzeros > 0) {
			if (lzeros >= sizeof(alimb)) {
				p -= sizeof(alimb);
			} else {
			mpi_limb_t *limb1 = (void *)p - sizeof(alimb);
			mpi_limb_t *limb1 = (void *)p - sizeof(alimb);
			mpi_limb_t *limb2 = (void *)p - sizeof(alimb)
			mpi_limb_t *limb2 = (void *)p - sizeof(alimb)
				+ lzeros;
				+ lzeros;
			*limb1 = *limb2;
			*limb1 = *limb2;
			p -= lzeros;
			p -= lzeros;
			}
			lzeros -= sizeof(alimb);
			lzeros -= sizeof(alimb);
		}
		}
	}
	}