kpqc 0.1.0
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.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +174 -0
- data/Rakefile +57 -0
- data/THIRD_PARTY_NOTICES.md +53 -0
- data/ext/kpqc/aimer128f/extconf.rb +4 -0
- data/ext/kpqc/aimer128s/extconf.rb +4 -0
- data/ext/kpqc/aimer192f/extconf.rb +4 -0
- data/ext/kpqc/aimer192s/extconf.rb +4 -0
- data/ext/kpqc/aimer256f/extconf.rb +4 -0
- data/ext/kpqc/aimer256s/extconf.rb +4 -0
- data/ext/kpqc/extconf_helper.rb +93 -0
- data/ext/kpqc/haetae2/extconf.rb +4 -0
- data/ext/kpqc/haetae3/extconf.rb +4 -0
- data/ext/kpqc/haetae5/extconf.rb +4 -0
- data/ext/kpqc/module.c +195 -0
- data/ext/kpqc/ntruplus1152/extconf.rb +4 -0
- data/ext/kpqc/ntruplus768/extconf.rb +4 -0
- data/ext/kpqc/ntruplus864/extconf.rb +4 -0
- data/ext/kpqc/randombytes.c +107 -0
- data/ext/kpqc/smaugt128/extconf.rb +4 -0
- data/ext/kpqc/smaugt192/extconf.rb +4 -0
- data/ext/kpqc/smaugt256/extconf.rb +4 -0
- data/ext/kpqc/timer/extconf.rb +4 -0
- data/kpqc.gemspec +41 -0
- data/lib/kpqc/aimer.rb +38 -0
- data/lib/kpqc/api.rb +129 -0
- data/lib/kpqc/haetae.rb +25 -0
- data/lib/kpqc/ntruplus.rb +25 -0
- data/lib/kpqc/smaugt.rb +29 -0
- data/lib/kpqc/types.rb +103 -0
- data/lib/kpqc/version.rb +6 -0
- data/lib/kpqc.rb +33 -0
- data/test/algorithm_test.rb +111 -0
- data/test/api_test.rb +52 -0
- data/test/interop.mjs +34 -0
- data/test/interop_test.rb +82 -0
- data/test/kat/aimer128f.rsp +10 -0
- data/test/kat/aimer128s.rsp +10 -0
- data/test/kat/aimer192f.rsp +10 -0
- data/test/kat/aimer192s.rsp +10 -0
- data/test/kat/aimer256f.rsp +10 -0
- data/test/kat/aimer256s.rsp +10 -0
- data/test/kat/haetae2.rsp +10 -0
- data/test/kat/haetae3.rsp +10 -0
- data/test/kat/haetae5.rsp +10 -0
- data/test/kat/ntruplus1152.rsp +8 -0
- data/test/kat/ntruplus768.rsp +8 -0
- data/test/kat/ntruplus864.rsp +8 -0
- data/test/kat/smaugt128.rsp +8 -0
- data/test/kat/smaugt192.rsp +8 -0
- data/test/kat/smaugt256.rsp +8 -0
- data/test/kat/timer.rsp +8 -0
- data/test/kat_drbg.rb +155 -0
- data/test/kat_test.rb +99 -0
- data/test/test_helper.rb +21 -0
- data/vendor/AIMer/LICENSE +21 -0
- data/vendor/AIMer/aim2.c +163 -0
- data/vendor/AIMer/aim2.h +36 -0
- data/vendor/AIMer/aim2_constant.h +74 -0
- data/vendor/AIMer/api.h +42 -0
- data/vendor/AIMer/common/crypto_declassify.h +7 -0
- data/vendor/AIMer/common/fips202.c +878 -0
- data/vendor/AIMer/common/fips202.h +166 -0
- data/vendor/AIMer/common/rng.h +56 -0
- data/vendor/AIMer/field.h +43 -0
- data/vendor/AIMer/field128.c +454 -0
- data/vendor/AIMer/field192.c +485 -0
- data/vendor/AIMer/field256.c +564 -0
- data/vendor/AIMer/hash.c +50 -0
- data/vendor/AIMer/hash.h +37 -0
- data/vendor/AIMer/params/params-aimer-128f.h +34 -0
- data/vendor/AIMer/params/params-aimer-128s.h +34 -0
- data/vendor/AIMer/params/params-aimer-192f.h +34 -0
- data/vendor/AIMer/params/params-aimer-192s.h +34 -0
- data/vendor/AIMer/params/params-aimer-256f.h +34 -0
- data/vendor/AIMer/params/params-aimer-256s.h +34 -0
- data/vendor/AIMer/params.h +5 -0
- data/vendor/AIMer/sign.c +631 -0
- data/vendor/AIMer/sign.h +40 -0
- data/vendor/AIMer/tree.c +75 -0
- data/vendor/AIMer/tree.h +26 -0
- data/vendor/HAETAE/LICENSE +22 -0
- data/vendor/HAETAE/include/api.h +54 -0
- data/vendor/HAETAE/include/common.h +12 -0
- data/vendor/HAETAE/include/config.h +42 -0
- data/vendor/HAETAE/include/decompose.h +19 -0
- data/vendor/HAETAE/include/encoding.h +20 -0
- data/vendor/HAETAE/include/fft.h +26 -0
- data/vendor/HAETAE/include/fips202.h +40 -0
- data/vendor/HAETAE/include/fixpoint.h +125 -0
- data/vendor/HAETAE/include/ntt.h +16 -0
- data/vendor/HAETAE/include/packing.h +58 -0
- data/vendor/HAETAE/include/params.h +117 -0
- data/vendor/HAETAE/include/poly.h +65 -0
- data/vendor/HAETAE/include/polyfix.h +65 -0
- data/vendor/HAETAE/include/polymat.h +32 -0
- data/vendor/HAETAE/include/polyvec.h +131 -0
- data/vendor/HAETAE/include/randombytes.h +39 -0
- data/vendor/HAETAE/include/rans_byte.h +335 -0
- data/vendor/HAETAE/include/reduce.h +31 -0
- data/vendor/HAETAE/include/sampler.h +23 -0
- data/vendor/HAETAE/include/symmetric.h +52 -0
- data/vendor/HAETAE/src/decompose.c +73 -0
- data/vendor/HAETAE/src/encoding.c +700 -0
- data/vendor/HAETAE/src/fft.c +235 -0
- data/vendor/HAETAE/src/fips202.c +745 -0
- data/vendor/HAETAE/src/fixpoint.c +143 -0
- data/vendor/HAETAE/src/ntt.c +98 -0
- data/vendor/HAETAE/src/packing.c +595 -0
- data/vendor/HAETAE/src/poly.c +371 -0
- data/vendor/HAETAE/src/polyfix.c +298 -0
- data/vendor/HAETAE/src/polymat.c +82 -0
- data/vendor/HAETAE/src/polyvec.c +534 -0
- data/vendor/HAETAE/src/reduce.c +96 -0
- data/vendor/HAETAE/src/sampler.c +310 -0
- data/vendor/HAETAE/src/sign.c +558 -0
- data/vendor/HAETAE/src/symmetric-shake.c +50 -0
- data/vendor/NTRUplus/LICENSE +21 -0
- data/vendor/NTRUplus/NTRU+1152/api.h +24 -0
- data/vendor/NTRUplus/NTRU+1152/fips202/fips202.c +878 -0
- data/vendor/NTRUplus/NTRU+1152/fips202/fips202.h +166 -0
- data/vendor/NTRUplus/NTRU+1152/kem.c +286 -0
- data/vendor/NTRUplus/NTRU+1152/ntt.c +394 -0
- data/vendor/NTRUplus/NTRU+1152/ntt.h +16 -0
- data/vendor/NTRUplus/NTRU+1152/params.h +18 -0
- data/vendor/NTRUplus/NTRU+1152/poly.c +318 -0
- data/vendor/NTRUplus/NTRU+1152/poly.h +31 -0
- data/vendor/NTRUplus/NTRU+1152/randombytes.h +9 -0
- data/vendor/NTRUplus/NTRU+1152/symmetric.c +39 -0
- data/vendor/NTRUplus/NTRU+1152/symmetric.h +12 -0
- data/vendor/NTRUplus/NTRU+768/api.h +24 -0
- data/vendor/NTRUplus/NTRU+768/fips202/fips202.c +878 -0
- data/vendor/NTRUplus/NTRU+768/fips202/fips202.h +166 -0
- data/vendor/NTRUplus/NTRU+768/kem.c +286 -0
- data/vendor/NTRUplus/NTRU+768/ntt.c +379 -0
- data/vendor/NTRUplus/NTRU+768/ntt.h +16 -0
- data/vendor/NTRUplus/NTRU+768/params.h +18 -0
- data/vendor/NTRUplus/NTRU+768/poly.c +320 -0
- data/vendor/NTRUplus/NTRU+768/poly.h +31 -0
- data/vendor/NTRUplus/NTRU+768/randombytes.h +9 -0
- data/vendor/NTRUplus/NTRU+768/symmetric.c +39 -0
- data/vendor/NTRUplus/NTRU+768/symmetric.h +12 -0
- data/vendor/NTRUplus/NTRU+864/api.h +24 -0
- data/vendor/NTRUplus/NTRU+864/fips202/fips202.c +878 -0
- data/vendor/NTRUplus/NTRU+864/fips202/fips202.h +166 -0
- data/vendor/NTRUplus/NTRU+864/kem.c +286 -0
- data/vendor/NTRUplus/NTRU+864/ntt.c +383 -0
- data/vendor/NTRUplus/NTRU+864/ntt.h +16 -0
- data/vendor/NTRUplus/NTRU+864/params.h +18 -0
- data/vendor/NTRUplus/NTRU+864/poly.c +314 -0
- data/vendor/NTRUplus/NTRU+864/poly.h +31 -0
- data/vendor/NTRUplus/NTRU+864/randombytes.h +9 -0
- data/vendor/NTRUplus/NTRU+864/symmetric.c +39 -0
- data/vendor/NTRUplus/NTRU+864/symmetric.h +12 -0
- data/vendor/SMAUG-T/LICENSE +22 -0
- data/vendor/SMAUG-T/include/api.h +37 -0
- data/vendor/SMAUG-T/include/cbd.h +15 -0
- data/vendor/SMAUG-T/include/ciphertext.h +45 -0
- data/vendor/SMAUG-T/include/common.h +12 -0
- data/vendor/SMAUG-T/include/config.h +48 -0
- data/vendor/SMAUG-T/include/dg.h +28 -0
- data/vendor/SMAUG-T/include/fips202.h +41 -0
- data/vendor/SMAUG-T/include/hash.h +21 -0
- data/vendor/SMAUG-T/include/hwt.h +28 -0
- data/vendor/SMAUG-T/include/indcpa.h +33 -0
- data/vendor/SMAUG-T/include/key.h +34 -0
- data/vendor/SMAUG-T/include/pack.h +78 -0
- data/vendor/SMAUG-T/include/packring.h +50 -0
- data/vendor/SMAUG-T/include/params.h +84 -0
- data/vendor/SMAUG-T/include/poly.h +41 -0
- data/vendor/SMAUG-T/include/randombytes.h +11 -0
- data/vendor/SMAUG-T/include/toomcook.h +14 -0
- data/vendor/SMAUG-T/include/verify.h +16 -0
- data/vendor/SMAUG-T/src/cbd.c +159 -0
- data/vendor/SMAUG-T/src/ciphertext.c +95 -0
- data/vendor/SMAUG-T/src/dg.c +112 -0
- data/vendor/SMAUG-T/src/fips202.c +745 -0
- data/vendor/SMAUG-T/src/hash.c +15 -0
- data/vendor/SMAUG-T/src/hwt.c +121 -0
- data/vendor/SMAUG-T/src/indcpa.c +187 -0
- data/vendor/SMAUG-T/src/kem.c +194 -0
- data/vendor/SMAUG-T/src/key.c +102 -0
- data/vendor/SMAUG-T/src/pack.c +349 -0
- data/vendor/SMAUG-T/src/packring.c +503 -0
- data/vendor/SMAUG-T/src/poly.c +200 -0
- data/vendor/SMAUG-T/src/toomcook.c +256 -0
- data/vendor/SMAUG-T/src/verify.c +48 -0
- data/vendor/SOURCES.json +130 -0
- metadata +280 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
|
|
3
|
+
#ifndef HAETAE_REDUCE_H
|
|
4
|
+
#define HAETAE_REDUCE_H
|
|
5
|
+
|
|
6
|
+
#include "params.h"
|
|
7
|
+
|
|
8
|
+
#include <stdint.h>
|
|
9
|
+
|
|
10
|
+
#define MONT 14321 // 2^32 % HAETAE_Q
|
|
11
|
+
#define MONTSQ 4214 // 2^64 % HAETAE_Q
|
|
12
|
+
#define QINV 940508161 // q^(-1) mod 2^32
|
|
13
|
+
#define QREC 66575 // 2^32 / HAETAE_Q for Barrett
|
|
14
|
+
#define DQREC 33287 // 2^32 / DQ for Barrett
|
|
15
|
+
|
|
16
|
+
#define montgomery_reduce HAETAE_NAMESPACE(montgomery_reduce)
|
|
17
|
+
int32_t montgomery_reduce(int64_t a);
|
|
18
|
+
|
|
19
|
+
#define caddq HAETAE_NAMESPACE(caddq)
|
|
20
|
+
int32_t caddq(int32_t a);
|
|
21
|
+
|
|
22
|
+
#define freeze HAETAE_NAMESPACE(freeze)
|
|
23
|
+
int32_t freeze(int32_t a);
|
|
24
|
+
|
|
25
|
+
#define reduce32_2q HAETAE_NAMESPACE(reduce32_2q)
|
|
26
|
+
int32_t reduce32_2q(int32_t a);
|
|
27
|
+
|
|
28
|
+
#define freeze2q HAETAE_NAMESPACE(freeze2q)
|
|
29
|
+
int32_t freeze2q(int32_t a);
|
|
30
|
+
|
|
31
|
+
#endif /* !HAETAE_REDUCE_H */
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
|
|
3
|
+
#ifndef HAETAE_SAMPLER_H
|
|
4
|
+
#define HAETAE_SAMPLER_H
|
|
5
|
+
|
|
6
|
+
#include "fixpoint.h"
|
|
7
|
+
#include "params.h"
|
|
8
|
+
|
|
9
|
+
#include <stdint.h>
|
|
10
|
+
|
|
11
|
+
#define rej_uniform HAETAE_NAMESPACE(rej_uniform)
|
|
12
|
+
unsigned int rej_uniform(int32_t *a, unsigned int len, const uint8_t *buf,
|
|
13
|
+
unsigned int buflen);
|
|
14
|
+
#define rej_eta HAETAE_NAMESPACE(rej_eta)
|
|
15
|
+
unsigned int rej_eta(int32_t *a, unsigned int len, const uint8_t *buf,
|
|
16
|
+
unsigned int buflen);
|
|
17
|
+
|
|
18
|
+
#define sample_gauss_N HAETAE_NAMESPACE(sample_gauss_N)
|
|
19
|
+
void sample_gauss_N(uint64_t *r, uint8_t *signs, fp96_76 *sqsum,
|
|
20
|
+
const uint8_t seed[HAETAE_CRHBYTES], uint16_t nonce,
|
|
21
|
+
size_t len);
|
|
22
|
+
|
|
23
|
+
#endif /* !HAETAE_SAMPLER_H */
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
|
|
3
|
+
#ifndef HAETAE_SYMMETRIC_H
|
|
4
|
+
#define HAETAE_SYMMETRIC_H
|
|
5
|
+
|
|
6
|
+
#include "fips202.h"
|
|
7
|
+
#include "params.h"
|
|
8
|
+
|
|
9
|
+
#include <stdint.h>
|
|
10
|
+
|
|
11
|
+
typedef keccak_state xof256_state;
|
|
12
|
+
|
|
13
|
+
void shake256_absorb_twice(keccak_state *state, const uint8_t *in1,
|
|
14
|
+
size_t in1len, const uint8_t *in2, size_t in2len);
|
|
15
|
+
void shake256_absorb_thrice(keccak_state *state, const uint8_t *in1,
|
|
16
|
+
size_t in1len, const uint8_t *in2, size_t in2len,
|
|
17
|
+
const uint8_t *in3, size_t in3len);
|
|
18
|
+
|
|
19
|
+
#define XOF256_BLOCKBYTES SHAKE256_RATE
|
|
20
|
+
|
|
21
|
+
#define xof256_absorb_once(STATE, IN, IN_LEN) \
|
|
22
|
+
shake256_absorb_once(STATE, IN, IN_LEN)
|
|
23
|
+
#define xof256_absorb_twice(STATE, IN, IN_LEN, IN2, IN2_LEN) \
|
|
24
|
+
shake256_absorb_twice(STATE, IN, IN_LEN, IN2, IN2_LEN)
|
|
25
|
+
#define xof256_absorb_thrice(STATE, IN, IN_LEN, IN2, IN2_LEN, IN3, IN3_LEN) \
|
|
26
|
+
shake256_absorb_thrice(STATE, IN, IN_LEN, IN2, IN2_LEN, IN3, IN3_LEN)
|
|
27
|
+
#define xof256_squeeze(OUT, OUT_LEN, STATE) \
|
|
28
|
+
shake256_squeeze(OUT, OUT_LEN, STATE)
|
|
29
|
+
#define xof256_squeezeblocks(OUT, OUTBLOCKS, STATE) \
|
|
30
|
+
shake256_squeezeblocks(OUT, OUTBLOCKS, STATE)
|
|
31
|
+
|
|
32
|
+
typedef keccak_state stream128_state;
|
|
33
|
+
typedef keccak_state stream256_state;
|
|
34
|
+
|
|
35
|
+
void shake128_stream_init(keccak_state *state,
|
|
36
|
+
const uint8_t seed[HAETAE_SEEDBYTES], uint16_t nonce);
|
|
37
|
+
void shake256_stream_init(keccak_state *state,
|
|
38
|
+
const uint8_t seed[HAETAE_CRHBYTES], uint16_t nonce);
|
|
39
|
+
|
|
40
|
+
#define STREAM128_BLOCKBYTES SHAKE128_RATE // 168
|
|
41
|
+
#define STREAM256_BLOCKBYTES SHAKE256_RATE // 136
|
|
42
|
+
|
|
43
|
+
#define stream128_init(STATE, SEED, NONCE) \
|
|
44
|
+
shake128_stream_init(STATE, SEED, NONCE)
|
|
45
|
+
#define stream128_squeezeblocks(OUT, OUTBLOCKS, STATE) \
|
|
46
|
+
shake128_squeezeblocks(OUT, OUTBLOCKS, STATE)
|
|
47
|
+
#define stream256_init(STATE, SEED, NONCE) \
|
|
48
|
+
shake256_stream_init(STATE, SEED, NONCE)
|
|
49
|
+
#define stream256_squeezeblocks(OUT, OUTBLOCKS, STATE) \
|
|
50
|
+
shake256_squeezeblocks(OUT, OUTBLOCKS, STATE)
|
|
51
|
+
|
|
52
|
+
#endif /* !HAETAE_SYMMETRIC_H */
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
|
|
3
|
+
#include "decompose.h"
|
|
4
|
+
#include "params.h"
|
|
5
|
+
|
|
6
|
+
#include <stdint.h>
|
|
7
|
+
|
|
8
|
+
/*************************************************
|
|
9
|
+
* Name: decompose_z1
|
|
10
|
+
*
|
|
11
|
+
* Description: For finite field element r, compute high and lowbits
|
|
12
|
+
* hb, lb such that r = hb * b + lb with -b/4 < lb <= b/4.
|
|
13
|
+
*
|
|
14
|
+
* Arguments: - int32_t r: input element
|
|
15
|
+
* - int32_t *lowbits: pointer to output element lb
|
|
16
|
+
* - int32_t *highbits: pointer to output element hb
|
|
17
|
+
**************************************************/
|
|
18
|
+
void decompose_z1(int32_t *highbits, int32_t *lowbits, const int32_t r) {
|
|
19
|
+
const int alpha = 256; // Algorithm parameter.
|
|
20
|
+
const int log_alpha = 8;
|
|
21
|
+
|
|
22
|
+
int32_t lb, center;
|
|
23
|
+
uint32_t alpha_mask = alpha - 1;
|
|
24
|
+
|
|
25
|
+
lb = r & alpha_mask;
|
|
26
|
+
center = ((alpha >> 1) - (lb + 1)) >> 31; // if lb >= HALF_ALPHA
|
|
27
|
+
lb -= alpha & center;
|
|
28
|
+
*lowbits = lb;
|
|
29
|
+
*highbits = (r + (alpha >> 1)) >> log_alpha;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/*************************************************
|
|
33
|
+
* Name: decompose_hint
|
|
34
|
+
*
|
|
35
|
+
* Description: For finite field element r, compute highbits
|
|
36
|
+
* hb, lb such that r = hb * b + lb with -b/4 < lb <= b/4.
|
|
37
|
+
*
|
|
38
|
+
* Arguments: - int32_t r: input element
|
|
39
|
+
* - int32_t *highbits: pointer to output element hb
|
|
40
|
+
*
|
|
41
|
+
* Specification: Implements Algorithm 20, DecomposeHint.
|
|
42
|
+
**************************************************/
|
|
43
|
+
|
|
44
|
+
void decompose_hint(int32_t *highbits, const int32_t r) {
|
|
45
|
+
int32_t hb, edgecase;
|
|
46
|
+
|
|
47
|
+
hb = (r + HAETAE_HALF_ALPHA_HINT) >> HAETAE_LOG_ALPHA_HINT;
|
|
48
|
+
edgecase = ((HAETAE_DQ - 2) / HAETAE_ALPHA_HINT - (hb + 1)) >>
|
|
49
|
+
31; // if hb == (HAETAE_DQ-2)/ALPHA
|
|
50
|
+
hb -= (HAETAE_DQ - 2) / HAETAE_ALPHA_HINT & edgecase; // hb = 0
|
|
51
|
+
|
|
52
|
+
*highbits = hb;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/*************************************************
|
|
56
|
+
* Name: decompose_vk
|
|
57
|
+
*
|
|
58
|
+
* Description: For finite field element a, compute a0, a1 such that
|
|
59
|
+
* a mod^+ HAETAE_Q = a1*2^D + a0 with -2^{D-1} <= a0 < 2^{D-1}.
|
|
60
|
+
* Assumes a to be standard representative.
|
|
61
|
+
*
|
|
62
|
+
* Arguments: - int32_t a: input element
|
|
63
|
+
* - int32_t *a0: pointer to output element a0
|
|
64
|
+
*
|
|
65
|
+
* Returns: - a1
|
|
66
|
+
*
|
|
67
|
+
* Specification: Implements Algorithm 17, DecomposeVK.
|
|
68
|
+
**************************************************/
|
|
69
|
+
int32_t decompose_vk(int32_t *a0, const int32_t a) {
|
|
70
|
+
*a0 = a & 1;
|
|
71
|
+
*a0 -= ((a >> 1) & *a0) << 1;
|
|
72
|
+
return (a - *a0) >> 1;
|
|
73
|
+
}
|