├── python ├── pins.txt ├── test.txt ├── c25519.py ├── ed25519.py ├── sec256k1.py ├── nist256.py ├── ed448.py ├── nist521.py ├── nist384.py ├── readme.md ├── bn254.py └── bn254cx.py ├── c ├── refman.pdf ├── .gitignore ├── randapi.c ├── config_big.h ├── config_ff.h ├── randapi.h ├── rom_field_C1665.c ├── config_field.h ├── rom_field_C1174.c ├── rom_curve_C25519.c ├── rom_field_NIST256.c ├── rom_field_F256PMW.c ├── rom_field_SM2.c ├── rom_field_MDC.c ├── rom_field_ANSSI.c ├── rom_field_JUBJUB.c ├── rom_field_HIFIVE.c ├── rom_field_BRAINPOOL.c ├── rom_field_F384PM.c ├── rom_field_C41417.c └── rom_field_NIST384.c ├── javascript ├── index.js ├── package.json ├── examples │ └── node │ │ ├── TestNHS.js │ │ └── TestKYBER.js └── src │ └── uint64.js ├── MIRACL_Core.pdf ├── go ├── test.txt ├── .gitignore ├── ARCH64.go ├── ARCH32.go ├── CONFIG_BIG.go ├── CONFIG_FF.go ├── ROM_C1665_64.go ├── ROM_SECP160R1_64.go ├── ROM_C25519_64.go ├── CONFIG_FIELD.go ├── ROM_SECP160R1_32.go ├── ROM_C1665_32.go ├── ROM_NUMS256E_64.go ├── ROM_NUMS256W_64.go ├── ROM_C25519_32.go ├── ROM_C13318_64.go ├── ROM_ED25519_64.go ├── ROM_NIST256_64.go ├── ROM_HIFIVE_64.go ├── ROM_TWEEDLEDEE_64.go ├── ROM_TWEEDLEDUM_64.go ├── ROM_C1174_64.go ├── ROM_ANSSI_64.go ├── ROM_MDC_64.go ├── ROM_SM2_64.go ├── ROM_NUMS256E_32.go ├── ROM_C13318_32.go ├── ROM_ED25519_32.go ├── ROM_NUMS256W_32.go ├── ROM_X448_64.go ├── ROM_JUBJUB_64.go ├── ROM_C41417_64.go └── TestNHS.go ├── NOTICE.txt ├── .editorconfig ├── java ├── .gitignore ├── pom.xml ├── CONFIG_BIG.java ├── CONFIG_FF.java ├── public_key.java ├── private_key.java ├── CONFIG_FIELD.java ├── ROM_C1665_64.java ├── ROM_SECP160R1_64.java ├── ROM_C25519_64.java ├── ROM_C1665_32.java ├── ROM_SECP160R1_32.java ├── ROM_C25519_32.java └── ROM_NUMS256E_64.java ├── cpp ├── .gitignore ├── config_big.h ├── config_ff.h ├── randapi.cpp ├── config_field.h ├── randapi.h ├── rom_field_C1665.cpp ├── rom_curve_C25519.cpp ├── rom_field_NIST256.cpp ├── rom_field_C1174.cpp ├── rom_field_F256PMW.cpp ├── rom_field_SM2.cpp ├── rom_field_ANSSI.cpp ├── rom_field_JUBJUB.cpp ├── rom_field_MDC.cpp ├── rom_field_BRAINPOOL.cpp ├── rom_field_HIFIVE.cpp ├── rom_curve_C1665.cpp ├── rom_field_F384PM.cpp ├── rom_field_NIST384.cpp ├── rom_field_C41417.cpp ├── newhope.h └── rom_field_SECP160R1.cpp ├── rust ├── .gitignore ├── modrsa.rs ├── arch32.rs ├── arch64.rs ├── modecc.rs ├── modpf.rs ├── modpf192.rs ├── modpf256.rs ├── lib.rs ├── rom_c1665_64.rs ├── rom_secp160r1_64.rs ├── rom_c1665_32.rs ├── rom_secp160r1_32.rs ├── rom_c25519_64.rs ├── rom_c13318_64.rs ├── rom_mdc_64.rs ├── rom_tweedledee_64.rs ├── rom_tweedledum_64.rs ├── rom_c1174_64.rs ├── rom_c13318_32.rs ├── rom_c25519_32.rs ├── rom_tweedledum_32.rs └── rom_tweedledee_32.rs ├── tools └── readme.md ├── CONTRIBUTORS.txt ├── .gitignore ├── .travis.yml └── swift ├── config_ff.swift ├── config_big.swift ├── config_field.swift └── config_curve.swift /python/pins.txt: -------------------------------------------------------------------------------- 1 | 1234 2 | 1234 3 | 4 | -------------------------------------------------------------------------------- /python/test.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 8 3 | 0 4 | 5 | -------------------------------------------------------------------------------- /c/refman.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/miracl/core/HEAD/c/refman.pdf -------------------------------------------------------------------------------- /javascript/index.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports.CTX = require('./src/ctx'); 3 | -------------------------------------------------------------------------------- /MIRACL_Core.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/miracl/core/HEAD/MIRACL_Core.pdf -------------------------------------------------------------------------------- /go/test.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 3 3 | 7 4 | 28 5 | 30 6 | 37 7 | 38 8 | 41 9 | 0 10 | 11 | -------------------------------------------------------------------------------- /NOTICE.txt: -------------------------------------------------------------------------------- 1 | MIRACL Core Crypto SDK 2 | Copyright (c) 2020 MIRACL.com 3 | 4 | https://www.miracl.com 5 | -------------------------------------------------------------------------------- /go/.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | # Dependency directories (remove the comment below to include it) 15 | # vendor/ 16 | 17 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: https://EditorConfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | charset = utf-8 7 | end_of_line = lf 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.{py,js,json,sh,xml,rs,c,cpp,swift,java,ino}] 12 | indent_style = space 13 | indent_size = 4 14 | 15 | [*.go] 16 | indent_style = tab 17 | indent_size = 4 18 | -------------------------------------------------------------------------------- /java/.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | 25 | -------------------------------------------------------------------------------- /cpp/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | -------------------------------------------------------------------------------- /javascript/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "miracl-js", 3 | "version": "3.0.0", 4 | "description": "MIRACL Core Library", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "Michael Scott, Alessandro Budroni, Kealan McCusker, Samuele Andreoli", 11 | "license": "Apache License 2.0", 12 | "dependencies": { 13 | "prompt": "^1.0.0" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /rust/.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 7 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 8 | Cargo.lock 9 | 10 | # These are backup files generated by rustfmt 11 | **/*.rs.bk 12 | 13 | # MSVC Windows builds of rustc generate these, which store debugging information 14 | *.pdb 15 | 16 | -------------------------------------------------------------------------------- /tools/readme.md: -------------------------------------------------------------------------------- 1 | # Description 2 | 3 | Several helper programs are provided to assist with the addition of 4 | new elliptic curves. Note that these programs will not be needed if using 5 | one of the supported curves. These programs must be built using the MIRACL 6 | library. See source code for compilation instructions 7 | 8 | bigtobig.cpp -- converts to BIG number format 9 | 10 | check.cpp -- checks for optimal choice of number base 11 | 12 | bestpair.cpp -- finds best BN, BLS12, BLS24 and BLS48 pairing-friendly curves 13 | 14 | romgen.cpp -- rough-and-ready program used to help generate ROM files for all of the different languages. 15 | 16 | -------------------------------------------------------------------------------- /c/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | 54 | -------------------------------------------------------------------------------- /java/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | org.miracl 5 | core 6 | 4.0-SNAPSHOT 7 | 8 | 9 | junit 10 | junit 11 | 4.13.1 12 | test 13 | 14 | 15 | 16 | 1.6 17 | 1.6 18 | 19 | 20 | -------------------------------------------------------------------------------- /javascript/examples/node/TestNHS.js: -------------------------------------------------------------------------------- 1 | const CTX = require('../../index').CTX; 2 | 3 | var ctx = new CTX(); 4 | 5 | var RAW=[]; 6 | var srng=new ctx.RAND(); srng.clean(); 7 | var crng=new ctx.RAND(); crng.clean(); 8 | for (i=0;i<100;i++) RAW[i]=i+1; 9 | srng.seed(100,RAW); 10 | 11 | for (i=0;i<100;i++) RAW[i]=i+2; 12 | crng.seed(100,RAW); 13 | var S=[]; 14 | var SB=[]; 15 | var UC=[]; 16 | var KEYA=[]; 17 | var KEYB=[]; 18 | // NewHope Simple key exchange - see https://eprint.iacr.org/2016/1157.pdf Protocol 1 19 | ctx.NHS.SERVER_1(srng,SB,S); 20 | 21 | ctx.NHS.CLIENT(crng,SB,UC,KEYB); 22 | 23 | ctx.NHS.SERVER_2(S,UC,KEYA); 24 | 25 | console.log("Alice Key= 0x"+ctx.NHS.bytestostring(KEYA)); 26 | console.log("Bob's Key= 0x"+ctx.NHS.bytestostring(KEYB)); 27 | -------------------------------------------------------------------------------- /rust/modrsa.rs: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2020 MIRACL UK Ltd. 3 | * 4 | * This file is part of MIRACL Core 5 | * (see https://github.com/miracl/core). 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | pub mod big; 20 | pub mod dbig; 21 | pub mod ff; 22 | pub mod rsa; 23 | -------------------------------------------------------------------------------- /CONTRIBUTORS.txt: -------------------------------------------------------------------------------- 1 | # Contributions to the Miracl Core project 2 | 3 | ## Maintainer 4 | 5 | * Mike Scott 6 | 7 | ## Contributors 8 | 9 | In alphabetical order: 10 | 11 | * Alessandro Budroni 12 | * Felix Hanley 13 | * Jason 14 | * Kittywhiskers Van Gogh 15 | * Kealan McCusker 16 | * Lovesh Harchandani 17 | * Max 18 | * Michael Burkhart 19 | * Pascal Schäfer 20 | * Samuele Andreoli 21 | * Vigneswaran R 22 | * Will Abramson 23 | * [Your name or handle] <[email or website]> 24 | -------------------------------------------------------------------------------- /javascript/examples/node/TestKYBER.js: -------------------------------------------------------------------------------- 1 | const CTX = require('../../index').CTX; 2 | 3 | var ctx = new CTX(); 4 | var LOOPS = 100; 5 | 6 | var RAW=[]; 7 | var rng=new ctx.RAND(); rng.clean(); 8 | 9 | for (i=0;i<100;i++) RAW[i]=i+1; 10 | rng.seed(100,RAW); 11 | 12 | var R64 = []; 13 | var R32 = []; 14 | var SK = []; 15 | var PK = []; 16 | var SS = []; 17 | var CT = []; 18 | 19 | console.log("\nTesting KYBER Key Exchange\n"); 20 | 21 | for (var j=0;jlen, RAW->val); 28 | } 29 | 30 | void KILL_CSPRNG(csprng *RNG) 31 | { 32 | RAND_clean(RNG); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /cpp/config_ff.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2020 MIRACL UK Ltd. 3 | * 4 | * This file is part of MIRACL Core 5 | * (see https://github.com/miracl/core). 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | #ifndef CONFIG_RSA_WWW_H 21 | #define CONFIG_RSA_WWW_H 22 | 23 | #include "core.h" 24 | #include "config_big_XXX.h" 25 | 26 | // FF stuff 27 | 28 | #define FFLEN_WWW @ML@ /**< 2^n multiplier of BIGBITS to specify supported Finite Field size, e.g 2048=256*2^3 where BIGBITS=256 */ 29 | 30 | namespace WWW_BIG = XXX; 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /cpp/randapi.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2020 MIRACL UK Ltd. 3 | * 4 | * This file is part of MIRACL Core 5 | * (see https://github.com/miracl/core). 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | #include "randapi.h" 21 | 22 | using namespace core; 23 | 24 | /* Initialise a Cryptographically Strong Random Number Generator from 25 | an octet of raw random data */ 26 | 27 | void core::CREATE_CSPRNG(csprng *RNG, octet *RAW) 28 | { 29 | RAND_seed(RNG, RAW->len, RAW->val); 30 | } 31 | 32 | void core::KILL_CSPRNG(csprng *RNG) 33 | { 34 | RAND_clean(RNG); 35 | } 36 | 37 | -------------------------------------------------------------------------------- /c/config_big.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2020 MIRACL UK Ltd. 3 | * 4 | * This file is part of MIRACL Core 5 | * (see https://github.com/miracl/core). 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @file config_big.h 22 | * @author Mike Scott 23 | * @brief Config BIG Header File 24 | * 25 | */ 26 | 27 | #ifndef CONFIG_BIG_XXX_H 28 | #define CONFIG_BIG_XXX_H 29 | 30 | #include "core.h" 31 | 32 | // BIG stuff 33 | 34 | #define MODBYTES_XXX @NB@ /**< Number of bytes in Modulus */ 35 | #define BASEBITS_XXX @BASE@ /**< Numbers represented to base 2*BASEBITS */ 36 | 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /python/c25519.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # 4 | # Copyright (c) 2012-2020 MIRACL UK Ltd. 5 | # 6 | # This file is part of MIRACL Core 7 | # (see https://github.com/miracl/core). 8 | # 9 | # Licensed under the Apache License, Version 2.0 (the "License"); 10 | # you may not use this file except in compliance with the License. 11 | # You may obtain a copy of the License at 12 | # 13 | # http://www.apache.org/licenses/LICENSE-2.0 14 | # 15 | # Unless required by applicable law or agreed to in writing, software 16 | # distributed under the License is distributed on an "AS IS" BASIS, 17 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | # See the License for the specific language governing permissions and 19 | # limitations under the License. 20 | # 21 | 22 | from constants import * 23 | 24 | SHA = 'sha256' # hash type to use with this curve 25 | EFS = 32 # elliptic curve field size in bytes 26 | CurveType = MONTGOMERY 27 | CurveCof = 8 28 | 29 | # field modulus 30 | p = 2**255 - 19 31 | r = 2**252 + 27742317777372353535851937790883648493 32 | 33 | # elliptic curve 34 | A = 486662 35 | B = 0 36 | 37 | # generator point 38 | Gx = 9 39 | Gy = 0 40 | -------------------------------------------------------------------------------- /c/config_ff.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2020 MIRACL UK Ltd. 3 | * 4 | * This file is part of MIRACL Core 5 | * (see https://github.com/miracl/core). 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @file config_ff.h 22 | * @author Mike Scott 23 | * @brief Config FF Header File 24 | * 25 | */ 26 | 27 | #ifndef CONFIG_RSA_WWW_H 28 | #define CONFIG_RSA_WWW_H 29 | 30 | #include "core.h" 31 | #include "config_big_XXX.h" 32 | 33 | // FF stuff 34 | 35 | #define FFLEN_WWW @ML@ /**< 2^n multiplier of BIGBITS to specify supported Finite Field size, e.g 2048=256*2^3 where BIGBITS=256 */ 36 | 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /cpp/config_field.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2020 MIRACL UK Ltd. 3 | * 4 | * This file is part of MIRACL Core 5 | * (see https://github.com/miracl/core). 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | #ifndef CONFIG_FIELD_YYY_H 21 | #define CONFIG_FIELD_YYY_H 22 | 23 | #include"core.h" 24 | #include "config_big_XXX.h" 25 | 26 | // FP stuff 27 | 28 | #define MBITS_YYY @NBT@ 29 | #define PM1D2_YYY @M8@ 30 | #define MODTYPE_YYY @MT@ 31 | #define MAXXES_YYY @SH@ 32 | #define QNRI_YYY @QI@ 33 | #define RIADZ_YYY @RZ@ 34 | #define RIADZG2A_YYY @RZ2A@ 35 | #define RIADZG2B_YYY @RZ2B@ 36 | #define TOWER_YYY @TW@ 37 | 38 | //#define BIG_ENDIAN_SIGN_YYY 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /go/CONFIG_BIG.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2020 MIRACL UK Ltd. 3 | * 4 | * This file is part of MIRACL Core 5 | * (see https://github.com/miracl/core). 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | package XXX 21 | 22 | // BIG length in bytes and number base 23 | const MODBYTES uint = @NB@ 24 | const BASEBITS uint = @BASE@ 25 | 26 | // BIG lengths and Masks 27 | const NLEN int = int((1 + ((8*MODBYTES - 1) / BASEBITS))) 28 | const DNLEN int = 2 * NLEN 29 | const BMASK Chunk = ((Chunk(1) << BASEBITS) - 1) 30 | const HBITS uint = (BASEBITS / 2) 31 | const HMASK Chunk = ((Chunk(1) << HBITS) - 1) 32 | const NEXCESS int = (1 << (uint(CHUNK) - BASEBITS - 1)) 33 | 34 | const BIGBITS int = int(MODBYTES * 8) 35 | 36 | -------------------------------------------------------------------------------- /go/CONFIG_FF.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2020 MIRACL UK Ltd. 3 | * 4 | * This file is part of MIRACL Core 5 | * (see https://github.com/miracl/core). 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | package XXX 21 | 22 | // Modulus as number of BIGs 23 | const FFLEN int = @ML@ 24 | 25 | // Modulus length in bits 26 | const FF_BITS int = (BIGBITS * FFLEN) /* Finite Field Size in bits - must be 256.2^n */ 27 | const HFLEN int = (FFLEN / 2) /* Useful for half-size RSA private key operations */ 28 | 29 | const P_MBITS uint = MODBYTES * 8 30 | const P_OMASK Chunk = (Chunk(-1) << (P_MBITS % BASEBITS)) 31 | const P_FEXCESS Chunk = (Chunk(1) << (BASEBITS*uint(NLEN) - P_MBITS - 1)) 32 | const P_TBITS uint = (P_MBITS % BASEBITS) 33 | 34 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | 3 | # temporary files which can be created if a process still has a handle open of a deleted file 4 | .fuse_hidden* 5 | 6 | # KDE directory preferences 7 | .directory 8 | 9 | # Linux trash folder which might appear on any partition or disk 10 | .Trash-* 11 | 12 | # .nfs files are created when an open file is removed but is still being accessed 13 | .nfs* 14 | 15 | # Windows thumbnail cache files 16 | Thumbs.db 17 | Thumbs.db:encryptable 18 | ehthumbs.db 19 | ehthumbs_vista.db 20 | 21 | # Dump file 22 | *.stackdump 23 | 24 | # Folder config file 25 | [Dd]esktop.ini 26 | 27 | # Recycle Bin used on file shares 28 | $RECYCLE.BIN/ 29 | 30 | # Windows Installer files 31 | *.cab 32 | *.msi 33 | *.msix 34 | *.msm 35 | *.msp 36 | 37 | # Windows shortcuts 38 | *.lnk 39 | 40 | # General 41 | .DS_Store 42 | .AppleDouble 43 | .LSOverride 44 | 45 | # Icon must end with two \r 46 | Icon 47 | 48 | 49 | # Thumbnails 50 | ._* 51 | 52 | # Files that might appear in the root of a volume 53 | .DocumentRevisions-V100 54 | .fseventsd 55 | .Spotlight-V100 56 | .TemporaryItems 57 | .Trashes 58 | .VolumeIcon.icns 59 | .com.apple.timemachine.donotpresent 60 | 61 | # Directories potentially created on remote AFP share 62 | .AppleDB 63 | .AppleDesktop 64 | Network Trash Folder 65 | Temporary Items 66 | .apdisk 67 | 68 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | ## 2 | ## Copyright (c) 2012-2020 MIRACL UK Ltd. 3 | ## 4 | ## This file is part of MIRACL Core 5 | ## (see https://github.com/miracl/core). 6 | ## 7 | ## Licensed under the Apache License, Version 2.0 (the "License"); 8 | ## you may not use this file except in compliance with the License. 9 | ## You may obtain a copy of the License at 10 | ## 11 | ## http://www.apache.org/licenses/LICENSE-2.0 12 | ## 13 | ## Unless required by applicable law or agreed to in writing, software 14 | ## distributed under the License is distributed on an "AS IS" BASIS, 15 | ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | ## See the License for the specific language governing permissions and 17 | ## limitations under the License. 18 | ## 19 | dist: xenial 20 | language: generic 21 | 22 | matrix: 23 | include: 24 | - language: c 25 | env: DIR=c SCRIPT=config64.py 26 | - language: cpp 27 | env: DIR=cpp SCRIPT=config64.py 28 | - language: java 29 | env: DIR=java SCRIPT=config64.py 30 | - language: python 31 | python: 3.6 32 | env: DIR=python SCRIPT=config.py 33 | - language: rust 34 | env: DIR=rust SCRIPT=config64.py 35 | - language: swift 36 | env: DIR=swift SCRIPT=config64.py 37 | 38 | script: 39 | - cd $DIR 40 | - python3 $SCRIPT test 41 | -------------------------------------------------------------------------------- /rust/lib.rs: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2020 MIRACL UK Ltd. 3 | * 4 | * This file is part of MIRACL Core 5 | * (see https://github.com/miracl/core). 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | // comment out if debugging with print macros !!! 21 | #![cfg_attr(not(feature = "std"), no_std)] 22 | #![allow(clippy::many_single_char_names)] 23 | #![allow(clippy::needless_range_loop)] 24 | #![allow(clippy::manual_memcpy)] 25 | #![allow(clippy::new_without_default)] 26 | #![allow(dangerous_implicit_autorefs)] 27 | pub mod aes; 28 | pub mod arch; 29 | pub mod dilithium; 30 | pub mod gcm; 31 | pub mod hash256; 32 | pub mod hash384; 33 | pub mod hash512; 34 | pub mod hmac; 35 | pub mod kyber; 36 | pub mod nhs; 37 | pub mod rand; 38 | pub mod sha3; 39 | pub mod share; 40 | pub mod x509; 41 | -------------------------------------------------------------------------------- /python/ed25519.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # 4 | # Copyright (c) 2012-2020 MIRACL UK Ltd. 5 | # 6 | # This file is part of MIRACL Core 7 | # (see https://github.com/miracl/core). 8 | # 9 | # Licensed under the Apache License, Version 2.0 (the "License"); 10 | # you may not use this file except in compliance with the License. 11 | # You may obtain a copy of the License at 12 | # 13 | # http://www.apache.org/licenses/LICENSE-2.0 14 | # 15 | # Unless required by applicable law or agreed to in writing, software 16 | # distributed under the License is distributed on an "AS IS" BASIS, 17 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | # See the License for the specific language governing permissions and 19 | # limitations under the License. 20 | # 21 | 22 | from constants import * 23 | 24 | SHA = 'sha256' # hash type to use with this curve 25 | EFS = 32 # elliptic curve field size in bytes 26 | CurveType = EDWARDS 27 | CurveCof = 8 28 | 29 | # field modulus 30 | p = 2**255 - 19 31 | r = 2**252 + 27742317777372353535851937790883648493 32 | 33 | # elliptic curve 34 | A = -1 35 | B = 0x52036CEE2B6FFE738CC740797779E89800700A4D4141D8AB75EB4DCA135978A3 36 | 37 | # generator point 38 | Gx = 0x216936D3CD6E53FEC0A4E231FDD6DC5C692CC7609525A7B2C9562D608F25D51A 39 | Gy = 0x6666666666666666666666666666666666666666666666666666666666666658 40 | -------------------------------------------------------------------------------- /cpp/randapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2020 MIRACL UK Ltd. 3 | * 4 | * This file is part of MIRACL Core 5 | * (see https://github.com/miracl/core). 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | #ifndef RANDOM_H 21 | #define RANDOM_H 22 | 23 | #include "core.h" 24 | 25 | namespace core { 26 | 27 | /** @brief Initialise a random number generator 28 | * 29 | @param R is a pointer to a cryptographically secure random number generator 30 | @param S is an input truly random seed value 31 | */ 32 | extern void CREATE_CSPRNG(csprng *R, octet *S); 33 | /** @brief Kill a random number generator 34 | * 35 | Deletes all internal state 36 | @param R is a pointer to a cryptographically secure random number generator 37 | */ 38 | extern void KILL_CSPRNG(csprng *R); 39 | 40 | } 41 | 42 | #endif 43 | 44 | -------------------------------------------------------------------------------- /swift/config_ff.swift: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2020 MIRACL UK Ltd. 3 | * 4 | * This file is part of MIRACL Core 5 | * (see https://github.com/miracl/core). 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | public struct CONFIG_FF{ 21 | static public let FFLEN:UInt=@ML@ 22 | 23 | static public let FF_BITS:UInt=(CONFIG_BIG.BIGBITS*CONFIG_FF.FFLEN) /* Finite Field Size in bits - must be BIGBITS.2^n */ 24 | static public let HFLEN=(CONFIG_FF.FFLEN/2); /* Useful for half-size RSA private key operations */ 25 | 26 | static let P_MBITS:UInt=CONFIG_BIG.MODBYTES*8 27 | static let P_OMASK:Chunk=Chunk(-1)<= 6.0 29 | #endif 30 | 31 | 32 | public struct CONFIG_BIG{ 33 | static let MODBYTES:UInt = @NB@ 34 | #if D32 35 | static public let CHUNK:Int=32 36 | static let BASEBITS:UInt = @BASE32@ 37 | #endif 38 | #if D64 39 | static public let CHUNK:Int=64 40 | static let BASEBITS:UInt = @BASE64@ 41 | #endif 42 | 43 | static let NLEN:Int=Int(1+((8*CONFIG_BIG.MODBYTES-1)/CONFIG_BIG.BASEBITS)) 44 | static let DNLEN:Int=2*CONFIG_BIG.NLEN 45 | static let BMASK:Chunk=((1<>> 0) + (y.bot >>> 0), 33 | low = t >>> 0, 34 | high = (this.top >>> 0) + (y.top >>> 0); 35 | 36 | this.bot = low; 37 | 38 | if (low != t) { 39 | this.top = (high + 1) >>> 0; 40 | } else { 41 | this.top = high; 42 | } 43 | 44 | return this; 45 | }, 46 | 47 | copy: function() { 48 | var r = new UInt64(this.top, this.bot); 49 | return r; 50 | }, 51 | 52 | shlb: function() { 53 | var t = this.bot >>> 24; 54 | this.top = t + (this.top << 8); 55 | this.bot <<= 8; 56 | return this; 57 | } 58 | }; 59 | 60 | return UInt64; 61 | }; 62 | 63 | if (typeof module !== "undefined" && typeof module.exports !== "undefined") { 64 | module.exports = { 65 | UInt64: UInt64 66 | }; 67 | } 68 | 69 | -------------------------------------------------------------------------------- /rust/rom_tweedledum_32.rs: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2020 MIRACL UK Ltd. 3 | * 4 | * This file is part of MIRACL Core 5 | * (see https://github.com/miracl/core). 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | use crate::tweedledum::big::NLEN; 21 | use crate::arch::Chunk; 22 | 23 | // Base bits= 29 24 | pub const MODULUS:[Chunk;NLEN]=[0x1,0xA032710,0xFD66E68,0x15424ED8,0x38,0x0,0x0,0x0,0x400000]; 25 | pub const R2MODP:[Chunk;NLEN]=[0x3FF4,0x1B622B40,0x1166FB28,0x5BEEDDF,0x16646668,0x321298C,0xDD7B28E,0x3E66507,0x22B644]; 26 | pub const ROI:[Chunk;NLEN]=[0x188837CE,0x1EF4F98D,0x1AA67594,0xDD1E7E7,0x2D57FF4,0x197557C6,0xBF3D893,0x2F121DC,0x2AE451]; 27 | pub const SQRTM3:[Chunk;NLEN]=[0x17DE6C70,0x10786BD5,0x1DC95B25,0xD55C069,0x184CB128,0x16EB9107,0x1B5B20A1,0x95285A0,0x15EF7D]; 28 | pub const MCONST:Chunk=0x1FFFFFFF; 29 | 30 | //*** rom curve parameters ***** 31 | pub const CURVE_COF_I:isize = 1; 32 | pub const CURVE_COF:[Chunk;NLEN]=[0x1,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0]; 33 | pub const CURVE_B_I:isize = 5; 34 | pub const CURVE_B:[Chunk;NLEN]=[0x5,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0]; 35 | pub const CURVE_ORDER:[Chunk;NLEN]=[0x1,0x1657EA0,0x18A1B261,0x15424ED2,0x38,0x0,0x0,0x0,0x400000]; 36 | pub const CURVE_GX:[Chunk;NLEN]=[0x0,0xA032710,0xFD66E68,0x15424ED8,0x38,0x0,0x0,0x0,0x400000]; 37 | pub const CURVE_GY:[Chunk;NLEN]=[0x2,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0]; 38 | pub const CURVE_HTPC:[Chunk;NLEN]=[0x1381795,0x16B3AA9A,0xF3EF0D9,0x15DD7A0,0xBAA53E1,0xC45B655,0xFD3BC53,0x1E66E041,0x3AB46C]; -------------------------------------------------------------------------------- /go/ROM_JUBJUB_64.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2020 MIRACL UK Ltd. 3 | * 4 | * This file is part of MIRACL Core 5 | * (see https://github.com/miracl/core). 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /* Fixed Data in ROM - Field and Curve parameters */ 21 | 22 | package JUBJUB 23 | 24 | //*** rom field parameters***** 25 | // Base Bits= 56 26 | var Modulus = [...]Chunk{0xFFFFFF00000001, 0xA402FFFE5BFEFF, 0x809A1D80553BD, 0x299D7D483339D8, 0x73EDA753} 27 | var R2modp = [...]Chunk{0x3B3440EC31BBA9, 0x8929657E045FB0, 0x2D645CF57C6E1A, 0xEA6A1C5012ECF5, 0x3C7B9D12} 28 | var ROI = [...]Chunk{0x788F500B912F1F, 0x4FF270B3E0941B, 0xC8D168D6C0C402, 0x5B416B6F0FD56D, 0x212D79E} 29 | 30 | const MConst Chunk = 0xFFFFFEFFFFFFFF 31 | 32 | //*** rom curve parameters ***** 33 | // Base Bits= 56 34 | 35 | const CURVE_Cof_I int = 8 36 | 37 | var CURVE_Cof = [...]Chunk{0x8, 0x0, 0x0, 0x0, 0x0} 38 | 39 | const CURVE_B_I int = 0 40 | 41 | var CURVE_B = [...]Chunk{0x65FD6D6343EB1, 0x7F6D37579D2601, 0x7E6BD7FD4292D, 0x4BFA2B48F5FD92, 0x2A9318E7} 42 | var CURVE_Order = [...]Chunk{0x970E5ED6F72CB7, 0x2093CCC81082D0, 0x101343B00A668, 0x6533AFA906673B, 0xE7DB4EA} 43 | var CURVE_Gx = [...]Chunk{0x4ECF1A74F976C4, 0x546BF2F39EDE7F, 0xDF00384882000C, 0xF8EFF38CA624B4, 0x5183972A} 44 | var CURVE_Gy = [...]Chunk{0x6BAD709349702E, 0x8707FFA6833B14, 0x5ABD9DC308096C, 0x2CA2FC2C9E8FCC, 0x3B43F847} 45 | var CURVE_HTPC = [...]Chunk{0x7AEF9E47BBBB6, 0xFF515175320A99, 0xCB473CD546E25D, 0x2B37B1D403E3EE, 0x62FD68B4} 46 | -------------------------------------------------------------------------------- /rust/rom_tweedledee_32.rs: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2020 MIRACL UK Ltd. 3 | * 4 | * This file is part of MIRACL Core 5 | * (see https://github.com/miracl/core). 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | use crate::tweedledee::big::NLEN; 21 | use crate::arch::Chunk; 22 | 23 | // Base bits= 29 24 | pub const MODULUS:[Chunk;NLEN]=[0x1,0x1657EA0,0x18A1B261,0x15424ED2,0x38,0x0,0x0,0x0,0x400000]; 25 | pub const R2MODP:[Chunk;NLEN]=[0x3FF4,0xE8E1080,0xA9C2909,0xC128269,0x89F8301,0x19607541,0x1F4C0E6F,0x3443009,0x22B644]; 26 | pub const ROI:[Chunk;NLEN]=[0x69D57EC,0x5E991A4,0x213EFC7,0x10FBA370,0x72930A2,0x5B22D4C,0x102FC76F,0xA21B807,0x113EFC]; 27 | pub const SQRTM3:[Chunk;NLEN]=[0x91F49FA,0x17E6AD31,0x19F5AE40,0x8F54DB0,0x3FFCD01,0xB5FF973,0x12C43D6C,0xE878126,0x2D8CDA]; 28 | pub const MCONST:Chunk=0x1FFFFFFF; 29 | 30 | //*** rom curve parameters ***** 31 | pub const CURVE_COF_I:isize = 1; 32 | pub const CURVE_COF:[Chunk;NLEN]=[0x1,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0]; 33 | pub const CURVE_B_I:isize = 5; 34 | pub const CURVE_B:[Chunk;NLEN]=[0x5,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0]; 35 | pub const CURVE_ORDER:[Chunk;NLEN]=[0x1,0xA032710,0xFD66E68,0x15424ED8,0x38,0x0,0x0,0x0,0x400000]; 36 | pub const CURVE_GX:[Chunk;NLEN]=[0x0,0x1657EA0,0x18A1B261,0x15424ED2,0x38,0x0,0x0,0x0,0x400000]; 37 | pub const CURVE_GY:[Chunk;NLEN]=[0x2,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0]; 38 | pub const CURVE_HTPC:[Chunk;NLEN]=[0xD69C848,0x1C30E5ED,0xBBDA1CC,0x180EA8CE,0xE9704A,0x1172DE0D,0x1C820B1A,0x1BC54C92,0x3AC188]; 39 | -------------------------------------------------------------------------------- /go/ROM_C41417_64.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2020 MIRACL UK Ltd. 3 | * 4 | * This file is part of MIRACL Core 5 | * (see https://github.com/miracl/core). 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /* Fixed Data in ROM - Field and Curve parameters */ 21 | 22 | package C41417 23 | 24 | // Base Bits= 60 25 | var Modulus = [...]Chunk{0xFFFFFFFFFFFFFEF, 0xFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFF} 26 | var ROI = [...]Chunk{0xFFFFFFFFFFFFFEE, 0xFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFF} 27 | var R2modp = [...]Chunk{0x121000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} 28 | 29 | const MConst Chunk = 0x11 30 | 31 | const CURVE_Cof_I int = 8 32 | 33 | var CURVE_Cof = [...]Chunk{0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} 34 | 35 | const CURVE_B_I int = 3617 36 | 37 | var CURVE_B = [...]Chunk{0xE21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} 38 | var CURVE_Order = [...]Chunk{0xB0E71A5E106AF79, 0x1C0338AD63CF181, 0x414CF706022B36F, 0xFFFFFFFFEB3CC92, 0xFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFF} 39 | var CURVE_Gx = [...]Chunk{0x4FD3812F3CBC595, 0x1A73FAA8537C64C, 0x4AB4D6D6BA11130, 0x3EC7F57FF35498A, 0xE5FCD46369F44C0, 0x300218C0631C326, 0x1A334905141443} 40 | var CURVE_Gy = [...]Chunk{0x22, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} 41 | var CURVE_HTPC = [...]Chunk{0xFFFFFFFFFFFFFEE, 0xFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFF} 42 | -------------------------------------------------------------------------------- /go/TestNHS.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2020 MIRACL UK Ltd. 3 | * 4 | * This file is part of MIRACL Core 5 | * (see https://github.com/miracl/core). 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /* test driver and function exerciser for NewHope Simple Functions */ 21 | // See https://eprint.iacr.org/2016/1157 (Alkim, Ducas, Popplemann and Schwabe) 22 | 23 | package main 24 | 25 | import "fmt" 26 | 27 | import "miracl/core" 28 | 29 | //import "core" 30 | 31 | func main() { 32 | 33 | fmt.Printf("\nTesting New Hope Key Exchange\n") 34 | 35 | srng := core.NewRAND() 36 | var sraw [100]byte 37 | for i := 0; i < 100; i++ { 38 | sraw[i] = byte(i + 1) 39 | } 40 | srng.Seed(100, sraw[:]) 41 | 42 | crng := core.NewRAND() 43 | var craw [100]byte 44 | for i := 0; i < 100; i++ { 45 | craw[i] = byte(i + 2) 46 | } 47 | crng.Seed(100, craw[:]) 48 | 49 | var S [1792]byte 50 | 51 | // NewHope Simple key exchange - - see https://eprint.iacr.org/2016/1157.pdf Protocol 1 52 | var SB [1824]byte 53 | core.NHS_SERVER_1(srng, SB[:], S[:]) 54 | var UC [2176]byte 55 | var KEYB [32]byte 56 | core.NHS_CLIENT(crng, SB[:], UC[:], KEYB[:]) 57 | 58 | fmt.Printf("Bob's Key= ") 59 | for i := 0; i < 32; i++ { 60 | fmt.Printf("%02x", KEYB[i]) 61 | } 62 | fmt.Printf("\n") 63 | var KEYA [32]byte 64 | core.NHS_SERVER_2(S[:], UC[:], KEYA[:]) 65 | 66 | fmt.Printf("Alice Key= ") 67 | for i := 0; i < 32; i++ { 68 | fmt.Printf("%02x", KEYA[i]) 69 | } 70 | fmt.Printf("\n") 71 | 72 | } 73 | -------------------------------------------------------------------------------- /cpp/newhope.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2020 MIRACL UK Ltd. 3 | * 4 | * This file is part of MIRACL Core 5 | * (see https://github.com/miracl/core). 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /* NewHope Simple API */ 21 | 22 | #ifndef NHS_H 23 | #define NHS_H 24 | 25 | #include "core.h" 26 | 27 | /* NewHope parameters */ 28 | 29 | //q= 12289 30 | 31 | #define RLWE_PRIME 0x3001 // q in Hex 32 | #define RLWE_LGN 10 // Degree n=2^LGN 33 | #define RLWE_ND 0xF7002FFF // 1/(R-q) mod R 34 | #define RLWE_ONE 0x2AC8 // R mod q 35 | #define RLWE_R2MODP 0x1620 // R^2 mod q 36 | 37 | namespace core { 38 | /** @brief NHS server first pass 39 | * 40 | @param RNG Random Number Generator handle 41 | @param SB seed and polynomial B concatenated - output 42 | @param S server secret - output 43 | 44 | */ 45 | extern void NHS_SERVER_1(csprng *RNG, octet *SB, octet *S); 46 | /** @brief NHS client pass 47 | * 48 | @param RNG Random Number Generator handle 49 | @param SB seed and polynomial B concatenated - input 50 | @param UC polynomial U and compressed polynomial c - output 51 | @param KEY client key 52 | */ 53 | extern void NHS_CLIENT(csprng *RNG, octet *SB, octet *UC, octet *KEY); 54 | /** @brief NHS server second pass 55 | * 56 | @param S server secret - input 57 | @param UC polynomial U and compressed polynomial c - input 58 | @param KEY server key 59 | */ 60 | extern void NHS_SERVER_2(octet *S, octet *UC, octet *KEY); 61 | 62 | } 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /cpp/rom_field_SECP160R1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012-2020 MIRACL UK Ltd. 3 | * 4 | * This file is part of MIRACL Core 5 | * (see https://github.com/miracl/core). 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | #include "arch.h" 20 | #include "fp_SECP160R1.h" 21 | 22 | namespace SECP160R1 { 23 | 24 | /* Curve SECP160R1 */ 25 | 26 | #if CHUNK==16 27 | using namespace B160_13; 28 | // Base Bits= 13 29 | const BIG Modulus= {0x1FFF,0x1FFF,0x1FDF,0x1FFF,0x1FFF,0x1FFF,0x1FFF,0x1FFF,0x1FFF,0x1FFF,0x1FFF,0x1FFF,0xF}; 30 | const BIG R2modp= {0x0,0x20,0x0,0x800,0x0,0x0,0x4,0x0,0x0,0x0,0x0,0x0,0x0}; 31 | const BIG ROI= {0x1FFE,0x1FFF,0x1FDF,0x1FFF,0x1FFF,0x1FFF,0x1FFF,0x1FFF,0x1FFF,0x1FFF,0x1FFF,0x1FFF,0xF}; 32 | const chunk MConst= 0x1; 33 | 34 | #endif 35 | 36 | #if CHUNK==32 37 | 38 | using namespace B160_29; 39 | 40 | // Base Bits= 29 41 | 42 | 43 | const BIG Modulus= {0x1FFFFFFF,0x1FFFFFFB,0x1FFFFFFF,0x1FFFFFFF,0x1FFFFFFF,0x7FFF}; 44 | const BIG R2modp= {0x10000000,0x0,0x4,0x8,0x0,0x0}; 45 | const BIG ROI= {0x1FFFFFFE,0x1FFFFFFB,0x1FFFFFFF,0x1FFFFFFF,0x1FFFFFFF,0x7FFF}; 46 | const chunk MConst= 0x1; 47 | 48 | 49 | #endif 50 | 51 | #if CHUNK==64 52 | 53 | using namespace B160_56; 54 | 55 | // Base Bits= 56 56 | const BIG Modulus= {0xFFFFFF7FFFFFFFL,0xFFFFFFFFFFFFFFL,0xFFFFFFFFFFFFL}; 57 | const BIG R2modp= {0x1000000010000L,0x400000L,0x0L}; 58 | const BIG ROI= {0xFFFFFF7FFFFFFEL,0xFFFFFFFFFFFFFFL,0xFFFFFFFFFFFFL}; 59 | const chunk MConst= 0xFFFFFF80000001L; 60 | 61 | 62 | #endif 63 | 64 | } 65 | --------------------------------------------------------------------------------