@pulse-compute/crypto 1.0.0-beta.4 → 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 +89 -8
- package/as/digest-text.as.ts +34 -0
- package/as/pulse-hmac-as.ts +7 -1
- package/conformance/digest-text.json +38 -0
- package/conformance/hs256.json +85 -0
- 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/digest.d.ts +14 -0
- package/dist/digest.d.ts.map +1 -0
- package/dist/digest.js +17 -0
- package/dist/digest.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -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 +22 -4
- package/pulse.package.json +64 -0
- package/pulsewasm.compiler.cjs +89 -0
- package/pulsewasm.manifest.cjs +15 -0
- package/pulsewasm.native.cjs +79 -7
- package/src/provider.cjs +141 -2
- package/src/provider.d.cts +6 -0
|
@@ -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_add(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 & (uint32_t)0x7FFFFFFF, aw);
|
|
44
|
+
}
|
|
45
|
+
return cc;
|
|
46
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
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_bit_length(uint32_t *x, size_t xlen)
|
|
30
|
+
{
|
|
31
|
+
uint32_t tw, twk;
|
|
32
|
+
|
|
33
|
+
tw = 0;
|
|
34
|
+
twk = 0;
|
|
35
|
+
while (xlen -- > 0) {
|
|
36
|
+
uint32_t w, c;
|
|
37
|
+
|
|
38
|
+
c = EQ(tw, 0);
|
|
39
|
+
w = x[xlen];
|
|
40
|
+
tw = MUX(c, w, tw);
|
|
41
|
+
twk = MUX(c, (uint32_t)xlen, twk);
|
|
42
|
+
}
|
|
43
|
+
return (twk << 5) + BIT_LENGTH(tw);
|
|
44
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
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_decode_mod(uint32_t *x, const void *src, size_t len, const uint32_t *m)
|
|
30
|
+
{
|
|
31
|
+
/*
|
|
32
|
+
* Two-pass algorithm: in the first pass, we determine whether the
|
|
33
|
+
* value fits; in the second pass, we do the actual write.
|
|
34
|
+
*
|
|
35
|
+
* During the first pass, 'r' contains the comparison result so
|
|
36
|
+
* far:
|
|
37
|
+
* 0x00000000 value is equal to the modulus
|
|
38
|
+
* 0x00000001 value is greater than the modulus
|
|
39
|
+
* 0xFFFFFFFF value is lower than the modulus
|
|
40
|
+
*
|
|
41
|
+
* Since we iterate starting with the least significant bytes (at
|
|
42
|
+
* the end of src[]), each new comparison overrides the previous
|
|
43
|
+
* except when the comparison yields 0 (equal).
|
|
44
|
+
*
|
|
45
|
+
* During the second pass, 'r' is either 0xFFFFFFFF (value fits)
|
|
46
|
+
* or 0x00000000 (value does not fit).
|
|
47
|
+
*
|
|
48
|
+
* We must iterate over all bytes of the source, _and_ possibly
|
|
49
|
+
* some extra virtual bytes (with value 0) so as to cover the
|
|
50
|
+
* complete modulus as well. We also add 4 such extra bytes beyond
|
|
51
|
+
* the modulus length because it then guarantees that no accumulated
|
|
52
|
+
* partial word remains to be processed.
|
|
53
|
+
*/
|
|
54
|
+
const unsigned char *buf;
|
|
55
|
+
size_t mlen, tlen;
|
|
56
|
+
int pass;
|
|
57
|
+
uint32_t r;
|
|
58
|
+
|
|
59
|
+
buf = src;
|
|
60
|
+
mlen = (m[0] + 31) >> 5;
|
|
61
|
+
tlen = (mlen << 2);
|
|
62
|
+
if (tlen < len) {
|
|
63
|
+
tlen = len;
|
|
64
|
+
}
|
|
65
|
+
tlen += 4;
|
|
66
|
+
r = 0;
|
|
67
|
+
for (pass = 0; pass < 2; pass ++) {
|
|
68
|
+
size_t u, v;
|
|
69
|
+
uint32_t acc;
|
|
70
|
+
int acc_len;
|
|
71
|
+
|
|
72
|
+
v = 1;
|
|
73
|
+
acc = 0;
|
|
74
|
+
acc_len = 0;
|
|
75
|
+
for (u = 0; u < tlen; u ++) {
|
|
76
|
+
uint32_t b;
|
|
77
|
+
|
|
78
|
+
if (u < len) {
|
|
79
|
+
b = buf[len - 1 - u];
|
|
80
|
+
} else {
|
|
81
|
+
b = 0;
|
|
82
|
+
}
|
|
83
|
+
acc |= (b << acc_len);
|
|
84
|
+
acc_len += 8;
|
|
85
|
+
if (acc_len >= 31) {
|
|
86
|
+
uint32_t xw;
|
|
87
|
+
|
|
88
|
+
xw = acc & (uint32_t)0x7FFFFFFF;
|
|
89
|
+
acc_len -= 31;
|
|
90
|
+
acc = b >> (8 - acc_len);
|
|
91
|
+
if (v <= mlen) {
|
|
92
|
+
if (pass) {
|
|
93
|
+
x[v] = r & xw;
|
|
94
|
+
} else {
|
|
95
|
+
uint32_t cc;
|
|
96
|
+
|
|
97
|
+
cc = (uint32_t)CMP(xw, m[v]);
|
|
98
|
+
r = MUX(EQ(cc, 0), r, cc);
|
|
99
|
+
}
|
|
100
|
+
} else {
|
|
101
|
+
if (!pass) {
|
|
102
|
+
r = MUX(EQ(xw, 0), r, 1);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
v ++;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/*
|
|
110
|
+
* When we reach this point at the end of the first pass:
|
|
111
|
+
* r is either 0, 1 or -1; we want to set r to 0 if it
|
|
112
|
+
* is equal to 0 or 1, and leave it to -1 otherwise.
|
|
113
|
+
*
|
|
114
|
+
* When we reach this point at the end of the second pass:
|
|
115
|
+
* r is either 0 or -1; we want to leave that value
|
|
116
|
+
* untouched. This is a subcase of the previous.
|
|
117
|
+
*/
|
|
118
|
+
r >>= 1;
|
|
119
|
+
r |= (r << 1);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
x[0] = m[0];
|
|
123
|
+
return r & (uint32_t)1;
|
|
124
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
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_decode(uint32_t *x, const void *src, size_t len)
|
|
30
|
+
{
|
|
31
|
+
const unsigned char *buf;
|
|
32
|
+
size_t u, v;
|
|
33
|
+
uint32_t acc;
|
|
34
|
+
int acc_len;
|
|
35
|
+
|
|
36
|
+
buf = src;
|
|
37
|
+
u = len;
|
|
38
|
+
v = 1;
|
|
39
|
+
acc = 0;
|
|
40
|
+
acc_len = 0;
|
|
41
|
+
while (u -- > 0) {
|
|
42
|
+
uint32_t b;
|
|
43
|
+
|
|
44
|
+
b = buf[u];
|
|
45
|
+
acc |= (b << acc_len);
|
|
46
|
+
acc_len += 8;
|
|
47
|
+
if (acc_len >= 31) {
|
|
48
|
+
x[v ++] = acc & (uint32_t)0x7FFFFFFF;
|
|
49
|
+
acc_len -= 31;
|
|
50
|
+
acc = b >> (8 - acc_len);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (acc_len != 0) {
|
|
54
|
+
x[v ++] = acc;
|
|
55
|
+
}
|
|
56
|
+
x[0] = br_i31_bit_length(x + 1, v - 1);
|
|
57
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
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_decode_reduce(uint32_t *x,
|
|
30
|
+
const void *src, size_t len, const uint32_t *m)
|
|
31
|
+
{
|
|
32
|
+
uint32_t m_ebitlen, m_rbitlen;
|
|
33
|
+
size_t mblen, k;
|
|
34
|
+
const unsigned char *buf;
|
|
35
|
+
uint32_t acc;
|
|
36
|
+
int acc_len;
|
|
37
|
+
|
|
38
|
+
/*
|
|
39
|
+
* Get the encoded bit length.
|
|
40
|
+
*/
|
|
41
|
+
m_ebitlen = m[0];
|
|
42
|
+
|
|
43
|
+
/*
|
|
44
|
+
* Special case for an invalid (null) modulus.
|
|
45
|
+
*/
|
|
46
|
+
if (m_ebitlen == 0) {
|
|
47
|
+
x[0] = 0;
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/*
|
|
52
|
+
* Clear the destination.
|
|
53
|
+
*/
|
|
54
|
+
br_i31_zero(x, m_ebitlen);
|
|
55
|
+
|
|
56
|
+
/*
|
|
57
|
+
* First decode directly as many bytes as possible. This requires
|
|
58
|
+
* computing the actual bit length.
|
|
59
|
+
*/
|
|
60
|
+
m_rbitlen = m_ebitlen >> 5;
|
|
61
|
+
m_rbitlen = (m_ebitlen & 31) + (m_rbitlen << 5) - m_rbitlen;
|
|
62
|
+
mblen = (m_rbitlen + 7) >> 3;
|
|
63
|
+
k = mblen - 1;
|
|
64
|
+
if (k >= len) {
|
|
65
|
+
br_i31_decode(x, src, len);
|
|
66
|
+
x[0] = m_ebitlen;
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
buf = src;
|
|
70
|
+
br_i31_decode(x, buf, k);
|
|
71
|
+
x[0] = m_ebitlen;
|
|
72
|
+
|
|
73
|
+
/*
|
|
74
|
+
* Input remaining bytes, using 31-bit words.
|
|
75
|
+
*/
|
|
76
|
+
acc = 0;
|
|
77
|
+
acc_len = 0;
|
|
78
|
+
while (k < len) {
|
|
79
|
+
uint32_t v;
|
|
80
|
+
|
|
81
|
+
v = buf[k ++];
|
|
82
|
+
if (acc_len >= 23) {
|
|
83
|
+
acc_len -= 23;
|
|
84
|
+
acc <<= (8 - acc_len);
|
|
85
|
+
acc |= v >> acc_len;
|
|
86
|
+
br_i31_muladd_small(x, acc, m);
|
|
87
|
+
acc = v & (0xFF >> (8 - acc_len));
|
|
88
|
+
} else {
|
|
89
|
+
acc = (acc << 8) | v;
|
|
90
|
+
acc_len += 8;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/*
|
|
95
|
+
* We may have some bits accumulated. We then perform a shift to
|
|
96
|
+
* be able to inject these bits as a full 31-bit word.
|
|
97
|
+
*/
|
|
98
|
+
if (acc_len != 0) {
|
|
99
|
+
acc = (acc | (x[1] << acc_len)) & 0x7FFFFFFF;
|
|
100
|
+
br_i31_rshift(x, 31 - acc_len);
|
|
101
|
+
br_i31_muladd_small(x, acc, m);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
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_encode(void *dst, size_t len, const uint32_t *x)
|
|
30
|
+
{
|
|
31
|
+
unsigned char *buf;
|
|
32
|
+
size_t k, xlen;
|
|
33
|
+
uint32_t acc;
|
|
34
|
+
int acc_len;
|
|
35
|
+
|
|
36
|
+
xlen = (x[0] + 31) >> 5;
|
|
37
|
+
if (xlen == 0) {
|
|
38
|
+
memset(dst, 0, len);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
buf = (unsigned char *)dst + len;
|
|
42
|
+
k = 1;
|
|
43
|
+
acc = 0;
|
|
44
|
+
acc_len = 0;
|
|
45
|
+
while (len != 0) {
|
|
46
|
+
uint32_t w;
|
|
47
|
+
|
|
48
|
+
w = (k <= xlen) ? x[k] : 0;
|
|
49
|
+
k ++;
|
|
50
|
+
if (acc_len == 0) {
|
|
51
|
+
acc = w;
|
|
52
|
+
acc_len = 31;
|
|
53
|
+
} else {
|
|
54
|
+
uint32_t z;
|
|
55
|
+
|
|
56
|
+
z = acc | (w << acc_len);
|
|
57
|
+
acc_len --;
|
|
58
|
+
acc = w >> (31 - acc_len);
|
|
59
|
+
if (len >= 4) {
|
|
60
|
+
buf -= 4;
|
|
61
|
+
len -= 4;
|
|
62
|
+
br_enc32be(buf, z);
|
|
63
|
+
} else {
|
|
64
|
+
switch (len) {
|
|
65
|
+
case 3:
|
|
66
|
+
buf[-3] = (unsigned char)(z >> 16);
|
|
67
|
+
/* fall through */
|
|
68
|
+
case 2:
|
|
69
|
+
buf[-2] = (unsigned char)(z >> 8);
|
|
70
|
+
/* fall through */
|
|
71
|
+
case 1:
|
|
72
|
+
buf[-1] = (unsigned char)z;
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
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_from_monty(uint32_t *x, const uint32_t *m, uint32_t m0i)
|
|
30
|
+
{
|
|
31
|
+
size_t len, u, v;
|
|
32
|
+
|
|
33
|
+
len = (m[0] + 31) >> 5;
|
|
34
|
+
for (u = 0; u < len; u ++) {
|
|
35
|
+
uint32_t f;
|
|
36
|
+
uint64_t cc;
|
|
37
|
+
|
|
38
|
+
f = MUL31_lo(x[1], m0i);
|
|
39
|
+
cc = 0;
|
|
40
|
+
for (v = 0; v < len; v ++) {
|
|
41
|
+
uint64_t z;
|
|
42
|
+
|
|
43
|
+
z = (uint64_t)x[v + 1] + MUL31(f, m[v + 1]) + cc;
|
|
44
|
+
cc = z >> 31;
|
|
45
|
+
if (v != 0) {
|
|
46
|
+
x[v] = (uint32_t)z & 0x7FFFFFFF;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
x[len] = (uint32_t)cc;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/*
|
|
53
|
+
* We may have to do an extra subtraction, but only if the
|
|
54
|
+
* value in x[] is indeed greater than or equal to that of m[],
|
|
55
|
+
* which is why we must do two calls (first call computes the
|
|
56
|
+
* carry, second call performs the subtraction only if the carry
|
|
57
|
+
* is 0).
|
|
58
|
+
*/
|
|
59
|
+
br_i31_sub(x, m, NOT(br_i31_sub(x, m, 0)));
|
|
60
|
+
}
|
|
@@ -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_iszero(const uint32_t *x)
|
|
30
|
+
{
|
|
31
|
+
uint32_t z;
|
|
32
|
+
size_t u;
|
|
33
|
+
|
|
34
|
+
z = 0;
|
|
35
|
+
for (u = (x[0] + 31) >> 5; u > 0; u --) {
|
|
36
|
+
z |= x[u];
|
|
37
|
+
}
|
|
38
|
+
return ~(z | -z) >> 31;
|
|
39
|
+
}
|