@pulse-compute/crypto 1.0.0-beta.5 → 1.0.0-beta.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -4
- package/dist/contracts.d.ts +34 -12
- package/dist/contracts.d.ts.map +1 -1
- package/dist/contracts.js +9 -3
- package/dist/contracts.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/rsa.d.ts +58 -0
- package/dist/internal/rsa.d.ts.map +1 -0
- package/dist/internal/rsa.js +185 -0
- package/dist/internal/rsa.js.map +1 -0
- package/dist/internal/verification.d.ts +3 -3
- package/dist/internal/verification.d.ts.map +1 -1
- package/dist/internal/verification.js +2 -2
- package/dist/internal/verification.js.map +1 -1
- package/guests/es256-rustcrypto/README.md +43 -0
- package/guests/es256-rustcrypto/build.cjs +297 -0
- package/guests/es256-rustcrypto/prebuilt/es256-verifier.unoptimized.wasm +0 -0
- package/guests/es256-rustcrypto/prebuilt/es256-verifier.wasm +0 -0
- package/guests/es256-rustcrypto/probe/.cargo/config.toml +18 -0
- package/guests/es256-rustcrypto/probe/Cargo.lock +238 -0
- package/guests/es256-rustcrypto/probe/Cargo.toml +26 -0
- package/guests/es256-rustcrypto/probe/README.md +35 -0
- package/guests/es256-rustcrypto/probe/rust-toolchain.toml +5 -0
- package/guests/es256-rustcrypto/probe/src/lib.rs +108 -0
- package/guests/es256-rustcrypto/pulse.guest-unit.json +119 -0
- package/guests/es256-rustcrypto/source/.cargo/config.toml +18 -0
- package/guests/es256-rustcrypto/source/Cargo.lock +265 -0
- package/guests/es256-rustcrypto/source/Cargo.toml +23 -0
- package/guests/es256-rustcrypto/source/README.md +26 -0
- package/guests/es256-rustcrypto/source/bearssl/LICENSE.txt +21 -0
- package/guests/es256-rustcrypto/source/bearssl/UPSTREAM.json +49 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl.h +170 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_aead.h +1059 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_block.h +2618 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_ec.h +883 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_hash.h +1346 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_hmac.h +241 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_kdf.h +185 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_pem.h +294 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_prf.h +150 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_rand.h +397 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_rsa.h +1366 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_ssl.h +4296 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_x509.h +1397 -0
- package/guests/es256-rustcrypto/source/bearssl/src/codec/ccopy.c +44 -0
- package/guests/es256-rustcrypto/source/bearssl/src/codec/dec32be.c +38 -0
- package/guests/es256-rustcrypto/source/bearssl/src/codec/enc32be.c +38 -0
- package/guests/es256-rustcrypto/source/bearssl/src/config.h +229 -0
- package/guests/es256-rustcrypto/source/bearssl/src/inner.h +2532 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_add.c +46 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_bitlen.c +44 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_decmod.c +124 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_decode.c +57 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_decred.c +103 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_encode.c +79 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_fmont.c +60 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_iszero.c +39 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_moddiv.c +488 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_modpow.c +65 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_modpow2.c +160 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_montmul.c +93 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_mulacc.c +61 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_muladd.c +157 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_ninv31.c +39 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_reduce.c +66 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_rshift.c +47 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_sub.c +46 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_tmont.c +36 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i32_div32.c +56 -0
- package/guests/es256-rustcrypto/source/bearssl/src/rsa/rsa_i31_priv.c +203 -0
- package/guests/es256-rustcrypto/source/bearssl/src/rsa/rsa_i31_pub.c +106 -0
- package/guests/es256-rustcrypto/source/bearssl/src/rsa/rsa_pkcs1_sig_pad.c +100 -0
- package/guests/es256-rustcrypto/source/build.rs +38 -0
- package/guests/es256-rustcrypto/source/freestanding/string.h +8 -0
- package/guests/es256-rustcrypto/source/rs256.c +103 -0
- package/guests/es256-rustcrypto/source/rust-toolchain.toml +5 -0
- package/guests/es256-rustcrypto/source/src/lib.rs +308 -0
- package/package.json +6 -4
- package/pulsewasm.native.cjs +66 -0
- package/src/provider.cjs +71 -1
- package/src/provider.d.cts +6 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
5
|
+
* a copy of this software and associated documentation files (the
|
|
6
|
+
* "Software"), to deal in the Software without restriction, including
|
|
7
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
|
8
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
9
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
|
10
|
+
* the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be
|
|
13
|
+
* included in all copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
19
|
+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
20
|
+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
21
|
+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
#include "inner.h"
|
|
26
|
+
|
|
27
|
+
/* see inner.h */
|
|
28
|
+
void
|
|
29
|
+
br_i31_montymul(uint32_t *d, const uint32_t *x, const uint32_t *y,
|
|
30
|
+
const uint32_t *m, uint32_t m0i)
|
|
31
|
+
{
|
|
32
|
+
size_t len, len4, u, v;
|
|
33
|
+
uint64_t dh;
|
|
34
|
+
|
|
35
|
+
len = (m[0] + 31) >> 5;
|
|
36
|
+
len4 = len & ~(size_t)3;
|
|
37
|
+
br_i31_zero(d, m[0]);
|
|
38
|
+
dh = 0;
|
|
39
|
+
for (u = 0; u < len; u ++) {
|
|
40
|
+
uint32_t f, xu;
|
|
41
|
+
uint64_t r, zh;
|
|
42
|
+
|
|
43
|
+
xu = x[u + 1];
|
|
44
|
+
f = MUL31_lo((d[1] + MUL31_lo(x[u + 1], y[1])), m0i);
|
|
45
|
+
|
|
46
|
+
r = 0;
|
|
47
|
+
for (v = 0; v < len4; v += 4) {
|
|
48
|
+
uint64_t z;
|
|
49
|
+
|
|
50
|
+
z = (uint64_t)d[v + 1] + MUL31(xu, y[v + 1])
|
|
51
|
+
+ MUL31(f, m[v + 1]) + r;
|
|
52
|
+
r = z >> 31;
|
|
53
|
+
d[v + 0] = (uint32_t)z & 0x7FFFFFFF;
|
|
54
|
+
z = (uint64_t)d[v + 2] + MUL31(xu, y[v + 2])
|
|
55
|
+
+ MUL31(f, m[v + 2]) + r;
|
|
56
|
+
r = z >> 31;
|
|
57
|
+
d[v + 1] = (uint32_t)z & 0x7FFFFFFF;
|
|
58
|
+
z = (uint64_t)d[v + 3] + MUL31(xu, y[v + 3])
|
|
59
|
+
+ MUL31(f, m[v + 3]) + r;
|
|
60
|
+
r = z >> 31;
|
|
61
|
+
d[v + 2] = (uint32_t)z & 0x7FFFFFFF;
|
|
62
|
+
z = (uint64_t)d[v + 4] + MUL31(xu, y[v + 4])
|
|
63
|
+
+ MUL31(f, m[v + 4]) + r;
|
|
64
|
+
r = z >> 31;
|
|
65
|
+
d[v + 3] = (uint32_t)z & 0x7FFFFFFF;
|
|
66
|
+
}
|
|
67
|
+
for (; v < len; v ++) {
|
|
68
|
+
uint64_t z;
|
|
69
|
+
|
|
70
|
+
z = (uint64_t)d[v + 1] + MUL31(xu, y[v + 1])
|
|
71
|
+
+ MUL31(f, m[v + 1]) + r;
|
|
72
|
+
r = z >> 31;
|
|
73
|
+
d[v] = (uint32_t)z & 0x7FFFFFFF;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
zh = dh + r;
|
|
77
|
+
d[len] = (uint32_t)zh & 0x7FFFFFFF;
|
|
78
|
+
dh = zh >> 31;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/*
|
|
82
|
+
* We must write back the bit length because it was overwritten in
|
|
83
|
+
* the loop (not overwriting it would require a test in the loop,
|
|
84
|
+
* which would yield bigger and slower code).
|
|
85
|
+
*/
|
|
86
|
+
d[0] = m[0];
|
|
87
|
+
|
|
88
|
+
/*
|
|
89
|
+
* d[] may still be greater than m[] at that point; notably, the
|
|
90
|
+
* 'dh' word may be non-zero.
|
|
91
|
+
*/
|
|
92
|
+
br_i31_sub(d, m, NEQ(dh, 0) | NOT(br_i31_sub(d, m, 0)));
|
|
93
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
5
|
+
* a copy of this software and associated documentation files (the
|
|
6
|
+
* "Software"), to deal in the Software without restriction, including
|
|
7
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
|
8
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
9
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
|
10
|
+
* the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be
|
|
13
|
+
* included in all copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
19
|
+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
20
|
+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
21
|
+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
#include "inner.h"
|
|
26
|
+
|
|
27
|
+
/* see inner.h */
|
|
28
|
+
void
|
|
29
|
+
br_i31_mulacc(uint32_t *d, const uint32_t *a, const uint32_t *b)
|
|
30
|
+
{
|
|
31
|
+
size_t alen, blen, u;
|
|
32
|
+
uint32_t dl, dh;
|
|
33
|
+
|
|
34
|
+
alen = (a[0] + 31) >> 5;
|
|
35
|
+
blen = (b[0] + 31) >> 5;
|
|
36
|
+
|
|
37
|
+
/*
|
|
38
|
+
* We want to add the two bit lengths, but these are encoded,
|
|
39
|
+
* which requires some extra care.
|
|
40
|
+
*/
|
|
41
|
+
dl = (a[0] & 31) + (b[0] & 31);
|
|
42
|
+
dh = (a[0] >> 5) + (b[0] >> 5);
|
|
43
|
+
d[0] = (dh << 5) + dl + (~(uint32_t)(dl - 31) >> 31);
|
|
44
|
+
|
|
45
|
+
for (u = 0; u < blen; u ++) {
|
|
46
|
+
uint32_t f;
|
|
47
|
+
size_t v;
|
|
48
|
+
uint64_t cc;
|
|
49
|
+
|
|
50
|
+
f = b[1 + u];
|
|
51
|
+
cc = 0;
|
|
52
|
+
for (v = 0; v < alen; v ++) {
|
|
53
|
+
uint64_t z;
|
|
54
|
+
|
|
55
|
+
z = (uint64_t)d[1 + u + v] + MUL31(f, a[1 + v]) + cc;
|
|
56
|
+
cc = z >> 31;
|
|
57
|
+
d[1 + u + v] = (uint32_t)z & 0x7FFFFFFF;
|
|
58
|
+
}
|
|
59
|
+
d[1 + u + alen] = (uint32_t)cc;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
5
|
+
* a copy of this software and associated documentation files (the
|
|
6
|
+
* "Software"), to deal in the Software without restriction, including
|
|
7
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
|
8
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
9
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
|
10
|
+
* the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be
|
|
13
|
+
* included in all copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
19
|
+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
20
|
+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
21
|
+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
#include "inner.h"
|
|
26
|
+
|
|
27
|
+
/* see inner.h */
|
|
28
|
+
void
|
|
29
|
+
br_i31_muladd_small(uint32_t *x, uint32_t z, const uint32_t *m)
|
|
30
|
+
{
|
|
31
|
+
uint32_t m_bitlen;
|
|
32
|
+
unsigned mblr;
|
|
33
|
+
size_t u, mlen;
|
|
34
|
+
uint32_t a0, a1, b0, hi, g, q, tb;
|
|
35
|
+
uint32_t under, over;
|
|
36
|
+
uint32_t cc;
|
|
37
|
+
|
|
38
|
+
/*
|
|
39
|
+
* We can test on the modulus bit length since we accept to
|
|
40
|
+
* leak that length.
|
|
41
|
+
*/
|
|
42
|
+
m_bitlen = m[0];
|
|
43
|
+
if (m_bitlen == 0) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (m_bitlen <= 31) {
|
|
47
|
+
uint32_t lo;
|
|
48
|
+
|
|
49
|
+
hi = x[1] >> 1;
|
|
50
|
+
lo = (x[1] << 31) | z;
|
|
51
|
+
x[1] = br_rem(hi, lo, m[1]);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
mlen = (m_bitlen + 31) >> 5;
|
|
55
|
+
mblr = (unsigned)m_bitlen & 31;
|
|
56
|
+
|
|
57
|
+
/*
|
|
58
|
+
* Principle: we estimate the quotient (x*2^31+z)/m by
|
|
59
|
+
* doing a 64/32 division with the high words.
|
|
60
|
+
*
|
|
61
|
+
* Let:
|
|
62
|
+
* w = 2^31
|
|
63
|
+
* a = (w*a0 + a1) * w^N + a2
|
|
64
|
+
* b = b0 * w^N + b2
|
|
65
|
+
* such that:
|
|
66
|
+
* 0 <= a0 < w
|
|
67
|
+
* 0 <= a1 < w
|
|
68
|
+
* 0 <= a2 < w^N
|
|
69
|
+
* w/2 <= b0 < w
|
|
70
|
+
* 0 <= b2 < w^N
|
|
71
|
+
* a < w*b
|
|
72
|
+
* I.e. the two top words of a are a0:a1, the top word of b is
|
|
73
|
+
* b0, we ensured that b0 is "full" (high bit set), and a is
|
|
74
|
+
* such that the quotient q = a/b fits on one word (0 <= q < w).
|
|
75
|
+
*
|
|
76
|
+
* If a = b*q + r (with 0 <= r < q), we can estimate q by
|
|
77
|
+
* doing an Euclidean division on the top words:
|
|
78
|
+
* a0*w+a1 = b0*u + v (with 0 <= v < b0)
|
|
79
|
+
* Then the following holds:
|
|
80
|
+
* 0 <= u <= w
|
|
81
|
+
* u-2 <= q <= u
|
|
82
|
+
*/
|
|
83
|
+
hi = x[mlen];
|
|
84
|
+
if (mblr == 0) {
|
|
85
|
+
a0 = x[mlen];
|
|
86
|
+
memmove(x + 2, x + 1, (mlen - 1) * sizeof *x);
|
|
87
|
+
x[1] = z;
|
|
88
|
+
a1 = x[mlen];
|
|
89
|
+
b0 = m[mlen];
|
|
90
|
+
} else {
|
|
91
|
+
a0 = ((x[mlen] << (31 - mblr)) | (x[mlen - 1] >> mblr))
|
|
92
|
+
& 0x7FFFFFFF;
|
|
93
|
+
memmove(x + 2, x + 1, (mlen - 1) * sizeof *x);
|
|
94
|
+
x[1] = z;
|
|
95
|
+
a1 = ((x[mlen] << (31 - mblr)) | (x[mlen - 1] >> mblr))
|
|
96
|
+
& 0x7FFFFFFF;
|
|
97
|
+
b0 = ((m[mlen] << (31 - mblr)) | (m[mlen - 1] >> mblr))
|
|
98
|
+
& 0x7FFFFFFF;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/*
|
|
102
|
+
* We estimate a divisor q. If the quotient returned by br_div()
|
|
103
|
+
* is g:
|
|
104
|
+
* -- If a0 == b0 then g == 0; we want q = 0x7FFFFFFF.
|
|
105
|
+
* -- Otherwise:
|
|
106
|
+
* -- if g == 0 then we set q = 0;
|
|
107
|
+
* -- otherwise, we set q = g - 1.
|
|
108
|
+
* The properties described above then ensure that the true
|
|
109
|
+
* quotient is q-1, q or q+1.
|
|
110
|
+
*
|
|
111
|
+
* Take care that a0, a1 and b0 are 31-bit words, not 32-bit. We
|
|
112
|
+
* must adjust the parameters to br_div() accordingly.
|
|
113
|
+
*/
|
|
114
|
+
g = br_div(a0 >> 1, a1 | (a0 << 31), b0);
|
|
115
|
+
q = MUX(EQ(a0, b0), 0x7FFFFFFF, MUX(EQ(g, 0), 0, g - 1));
|
|
116
|
+
|
|
117
|
+
/*
|
|
118
|
+
* We subtract q*m from x (with the extra high word of value 'hi').
|
|
119
|
+
* Since q may be off by 1 (in either direction), we may have to
|
|
120
|
+
* add or subtract m afterwards.
|
|
121
|
+
*
|
|
122
|
+
* The 'tb' flag will be true (1) at the end of the loop if the
|
|
123
|
+
* result is greater than or equal to the modulus (not counting
|
|
124
|
+
* 'hi' or the carry).
|
|
125
|
+
*/
|
|
126
|
+
cc = 0;
|
|
127
|
+
tb = 1;
|
|
128
|
+
for (u = 1; u <= mlen; u ++) {
|
|
129
|
+
uint32_t mw, zw, xw, nxw;
|
|
130
|
+
uint64_t zl;
|
|
131
|
+
|
|
132
|
+
mw = m[u];
|
|
133
|
+
zl = MUL31(mw, q) + cc;
|
|
134
|
+
cc = (uint32_t)(zl >> 31);
|
|
135
|
+
zw = (uint32_t)zl & (uint32_t)0x7FFFFFFF;
|
|
136
|
+
xw = x[u];
|
|
137
|
+
nxw = xw - zw;
|
|
138
|
+
cc += nxw >> 31;
|
|
139
|
+
nxw &= 0x7FFFFFFF;
|
|
140
|
+
x[u] = nxw;
|
|
141
|
+
tb = MUX(EQ(nxw, mw), tb, GT(nxw, mw));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/*
|
|
145
|
+
* If we underestimated q, then either cc < hi (one extra bit
|
|
146
|
+
* beyond the top array word), or cc == hi and tb is true (no
|
|
147
|
+
* extra bit, but the result is not lower than the modulus). In
|
|
148
|
+
* these cases we must subtract m once.
|
|
149
|
+
*
|
|
150
|
+
* Otherwise, we may have overestimated, which will show as
|
|
151
|
+
* cc > hi (thus a negative result). Correction is adding m once.
|
|
152
|
+
*/
|
|
153
|
+
over = GT(cc, hi);
|
|
154
|
+
under = ~over & (tb | LT(cc, hi));
|
|
155
|
+
br_i31_add(x, m, over);
|
|
156
|
+
br_i31_sub(x, m, under);
|
|
157
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
5
|
+
* a copy of this software and associated documentation files (the
|
|
6
|
+
* "Software"), to deal in the Software without restriction, including
|
|
7
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
|
8
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
9
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
|
10
|
+
* the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be
|
|
13
|
+
* included in all copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
19
|
+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
20
|
+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
21
|
+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
#include "inner.h"
|
|
26
|
+
|
|
27
|
+
/* see inner.h */
|
|
28
|
+
uint32_t
|
|
29
|
+
br_i31_ninv31(uint32_t x)
|
|
30
|
+
{
|
|
31
|
+
uint32_t y;
|
|
32
|
+
|
|
33
|
+
y = 2 - x;
|
|
34
|
+
y *= 2 - y * x;
|
|
35
|
+
y *= 2 - y * x;
|
|
36
|
+
y *= 2 - y * x;
|
|
37
|
+
y *= 2 - y * x;
|
|
38
|
+
return MUX(x & 1, -y, 0) & 0x7FFFFFFF;
|
|
39
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
5
|
+
* a copy of this software and associated documentation files (the
|
|
6
|
+
* "Software"), to deal in the Software without restriction, including
|
|
7
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
|
8
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
9
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
|
10
|
+
* the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be
|
|
13
|
+
* included in all copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
19
|
+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
20
|
+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
21
|
+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
#include "inner.h"
|
|
26
|
+
|
|
27
|
+
/* see inner.h */
|
|
28
|
+
void
|
|
29
|
+
br_i31_reduce(uint32_t *x, const uint32_t *a, const uint32_t *m)
|
|
30
|
+
{
|
|
31
|
+
uint32_t m_bitlen, a_bitlen;
|
|
32
|
+
size_t mlen, alen, u;
|
|
33
|
+
|
|
34
|
+
m_bitlen = m[0];
|
|
35
|
+
mlen = (m_bitlen + 31) >> 5;
|
|
36
|
+
|
|
37
|
+
x[0] = m_bitlen;
|
|
38
|
+
if (m_bitlen == 0) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/*
|
|
43
|
+
* If the source is shorter, then simply copy all words from a[]
|
|
44
|
+
* and zero out the upper words.
|
|
45
|
+
*/
|
|
46
|
+
a_bitlen = a[0];
|
|
47
|
+
alen = (a_bitlen + 31) >> 5;
|
|
48
|
+
if (a_bitlen < m_bitlen) {
|
|
49
|
+
memcpy(x + 1, a + 1, alen * sizeof *a);
|
|
50
|
+
for (u = alen; u < mlen; u ++) {
|
|
51
|
+
x[u + 1] = 0;
|
|
52
|
+
}
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/*
|
|
57
|
+
* The source length is at least equal to that of the modulus.
|
|
58
|
+
* We must thus copy N-1 words, and input the remaining words
|
|
59
|
+
* one by one.
|
|
60
|
+
*/
|
|
61
|
+
memcpy(x + 1, a + 2 + (alen - mlen), (mlen - 1) * sizeof *a);
|
|
62
|
+
x[mlen] = 0;
|
|
63
|
+
for (u = 1 + alen - mlen; u > 0; u --) {
|
|
64
|
+
br_i31_muladd_small(x, a[u], m);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
5
|
+
* a copy of this software and associated documentation files (the
|
|
6
|
+
* "Software"), to deal in the Software without restriction, including
|
|
7
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
|
8
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
9
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
|
10
|
+
* the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be
|
|
13
|
+
* included in all copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
19
|
+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
20
|
+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
21
|
+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
#include "inner.h"
|
|
26
|
+
|
|
27
|
+
/* see inner.h */
|
|
28
|
+
void
|
|
29
|
+
br_i31_rshift(uint32_t *x, int count)
|
|
30
|
+
{
|
|
31
|
+
size_t u, len;
|
|
32
|
+
uint32_t r;
|
|
33
|
+
|
|
34
|
+
len = (x[0] + 31) >> 5;
|
|
35
|
+
if (len == 0) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
r = x[1] >> count;
|
|
39
|
+
for (u = 2; u <= len; u ++) {
|
|
40
|
+
uint32_t w;
|
|
41
|
+
|
|
42
|
+
w = x[u];
|
|
43
|
+
x[u - 1] = ((w << (31 - count)) | r) & 0x7FFFFFFF;
|
|
44
|
+
r = w >> count;
|
|
45
|
+
}
|
|
46
|
+
x[len] = r;
|
|
47
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
5
|
+
* a copy of this software and associated documentation files (the
|
|
6
|
+
* "Software"), to deal in the Software without restriction, including
|
|
7
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
|
8
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
9
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
|
10
|
+
* the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be
|
|
13
|
+
* included in all copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
19
|
+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
20
|
+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
21
|
+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
#include "inner.h"
|
|
26
|
+
|
|
27
|
+
/* see inner.h */
|
|
28
|
+
uint32_t
|
|
29
|
+
br_i31_sub(uint32_t *a, const uint32_t *b, uint32_t ctl)
|
|
30
|
+
{
|
|
31
|
+
uint32_t cc;
|
|
32
|
+
size_t u, m;
|
|
33
|
+
|
|
34
|
+
cc = 0;
|
|
35
|
+
m = (a[0] + 63) >> 5;
|
|
36
|
+
for (u = 1; u < m; u ++) {
|
|
37
|
+
uint32_t aw, bw, naw;
|
|
38
|
+
|
|
39
|
+
aw = a[u];
|
|
40
|
+
bw = b[u];
|
|
41
|
+
naw = aw - bw - cc;
|
|
42
|
+
cc = naw >> 31;
|
|
43
|
+
a[u] = MUX(ctl, naw & 0x7FFFFFFF, aw);
|
|
44
|
+
}
|
|
45
|
+
return cc;
|
|
46
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
5
|
+
* a copy of this software and associated documentation files (the
|
|
6
|
+
* "Software"), to deal in the Software without restriction, including
|
|
7
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
|
8
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
9
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
|
10
|
+
* the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be
|
|
13
|
+
* included in all copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
19
|
+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
20
|
+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
21
|
+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
#include "inner.h"
|
|
26
|
+
|
|
27
|
+
/* see inner.h */
|
|
28
|
+
void
|
|
29
|
+
br_i31_to_monty(uint32_t *x, const uint32_t *m)
|
|
30
|
+
{
|
|
31
|
+
uint32_t k;
|
|
32
|
+
|
|
33
|
+
for (k = (m[0] + 31) >> 5; k > 0; k --) {
|
|
34
|
+
br_i31_muladd_small(x, 0, m);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
5
|
+
* a copy of this software and associated documentation files (the
|
|
6
|
+
* "Software"), to deal in the Software without restriction, including
|
|
7
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
|
8
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
9
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
|
10
|
+
* the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be
|
|
13
|
+
* included in all copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
19
|
+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
20
|
+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
21
|
+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
#include "inner.h"
|
|
26
|
+
|
|
27
|
+
/* see inner.h */
|
|
28
|
+
uint32_t
|
|
29
|
+
br_divrem(uint32_t hi, uint32_t lo, uint32_t d, uint32_t *r)
|
|
30
|
+
{
|
|
31
|
+
/* TODO: optimize this */
|
|
32
|
+
uint32_t q;
|
|
33
|
+
uint32_t ch, cf;
|
|
34
|
+
int k;
|
|
35
|
+
|
|
36
|
+
q = 0;
|
|
37
|
+
ch = EQ(hi, d);
|
|
38
|
+
hi = MUX(ch, 0, hi);
|
|
39
|
+
for (k = 31; k > 0; k --) {
|
|
40
|
+
int j;
|
|
41
|
+
uint32_t w, ctl, hi2, lo2;
|
|
42
|
+
|
|
43
|
+
j = 32 - k;
|
|
44
|
+
w = (hi << j) | (lo >> k);
|
|
45
|
+
ctl = GE(w, d) | (hi >> k);
|
|
46
|
+
hi2 = (w - d) >> j;
|
|
47
|
+
lo2 = lo - (d << k);
|
|
48
|
+
hi = MUX(ctl, hi2, hi);
|
|
49
|
+
lo = MUX(ctl, lo2, lo);
|
|
50
|
+
q |= ctl << k;
|
|
51
|
+
}
|
|
52
|
+
cf = GE(lo, d) | hi;
|
|
53
|
+
q |= cf;
|
|
54
|
+
*r = MUX(cf, lo - d, lo);
|
|
55
|
+
return q;
|
|
56
|
+
}
|