Giter Site home page Giter Site logo

ntru's Introduction

ntru

ntru is a simple implementation of NTRUEncrypt cryptosystem, written in Python 3.6. Polynomial operations are implemented using SymPy library. It was made as a homework project for "Error-Correcting Codes and Cryptography" workshops on Faculty of Mathematics and Information Science of Warsaw University of Technology.

This package was made as an exercise. It shouldn't be used anywhere to secure data.

How to run?

Install Python 3.x

You should have Python 3.x installed on your system. To do this on Fedora OS you need to execute

sudo dnf install python3

Install dependencies

You should have SymPy, NumPy and docopt package installed.

pip3 install --user sympy
pip3 install --user numpy
pip3 install --user docopt

How to use it?

To print help screen execute ntru.py script with -h argument.

$ ./ntru.py -h
NTRU v0.1

Usage:
  ntru.py [options] enc PUB_KEY_FILE [FILE]
  ntru.py [options] dec PRIV_KEY_FILE [FILE]
  ntru.py [options] gen N P Q PRIV_KEY_FILE PUB_KEY_FILE
  ntru.py (-h | --help)
  ntru.py --version

Options:
  -b, --block        Interpret input/output as
                       block stream.
  -i, --poly-input   Interpret input as polynomial
                       represented by integer array.
  -o, --poly-output  Interpret output as polynomial
                       represented by integer array.
  -h, --help         Show this screen.
  --version          Show version.
  -d, --debug        Debug mode.
  -v, --verbose      Verbose mode.

As you can see there are few commands:

  • enc - encrypt a file using public key
  • dec - decrypt a file using private key
  • gen - generate a key pair with given parameters

Generating a key pair

To generate random public and private key pair execute:

$ ./ntru.py -v gen 167 3 128 myKey.priv myKey.pub

The myKey.priv.npz and myKey.pub.npz files should appear in current working directory. These are zip compressed NumPy files which contains:

  • both keys:
    • N
    • p
    • q
  • public key:
    • f
    • f_p
  • private key:
    • h

One can see these parameters by running the program in verbose mode:

$ ./ntru.py -v gen 167 3 128 myKey.priv myKey.pub
NTRU(N=167,p=3,q=128) initiated
g: Poly(x**166 - x**157 + x**128 + x**127 - x**122 - x**121 + x**110 + x**101 + x**93 + x**78 - x**64 - x**63 - x**62 + x**61 - x**56 - x**50 + x**46 - x**44 - x**39 + x**36 + x**32 - x**5, x, domain='ZZ')
g coeffs: Counter({1: 11, -1: 11})
f: Poly(x**165 - x**164 - x**163 + x**158 + x**152 - x**151 - x**150 + x**149 - x**148 + x**147 + x**146 - x**145 + x**142 + x**141 - x**140 + x**139 + x**138 - x**136 - x**134 - x**132 - x**131 + x**130 + x**127 - x**126 - x**125 - x**124 - x**123 - x**121 + x**117 - x**116 + x**115 - x**114 - x**113 + x**112 - x**111 + x**110 - x**109 - x**107 + x**105 - x**104 + x**101 - x**100 - x**99 + x**97 + x**96 + x**94 - x**92 + x**90 + x**85 - x**84 + x**83 - x**82 + x**81 - x**79 - x**77 + x**76 + x**75 + x**74 - x**73 + x**72 + x**71 + x**69 + x**68 + x**67 - x**65 + x**64 + x**63 - x**62 + x**61 + x**60 + x**59 + x**58 - x**54 - x**53 + x**52 - x**51 + x**49 - x**47 - x**45 + x**44 + x**43 - x**41 + x**40 - x**36 + x**35 - x**34 + x**33 + x**31 + x**30 - x**29 + x**28 - x**26 - x**24 + x**22 - x**21 - x**20 - x**18 - x**17 - x**16 - x**15 + x**14 + x**11 - x**10 + x**8 + x**6 - x**5 - x**4 - x**2 + 1, x, domain='ZZ')
f coeffs: Counter({1: 55, -1: 54})
f_p: Poly(-x**166 + x**165 + x**164 - x**162 - x**161 + x**160 - x**159 + x**158 - x**156 - x**155 - x**153 + x**152 + x**151 - x**150 - x**149 + x**148 - x**146 - x**145 + x**144 + x**142 + x**141 + x**140 - x**139 + x**138 + x**137 - x**135 + x**134 - x**132 + x**130 - x**129 + x**128 - x**126 - x**124 - x**123 + x**121 + x**118 + x**116 - x**115 - x**113 - x**112 - x**111 - x**110 + x**109 + x**108 - x**107 + x**106 - x**105 + x**104 + x**103 + x**102 - x**101 + x**100 + x**99 - x**96 + x**94 + x**91 + x**90 + x**88 + x**87 - x**84 + x**83 + x**82 - x**81 - x**79 + x**78 - x**77 + x**71 + x**69 + x**66 - x**65 - x**64 + x**61 + x**59 - x**56 - x**55 - x**54 - x**53 + x**51 + x**49 - x**48 + x**45 - x**44 + x**43 + x**42 + x**40 - x**39 - x**38 - x**37 - x**36 - x**35 - x**34 + x**33 - x**32 + x**31 - x**30 + x**29 + x**27 + x**26 + x**25 + x**22 + x**21 + x**20 - x**19 - x**17 - x**14 + x**12 - x**9 + x**8 - x**6 + x**4 + x**3 - x**2 + 1, x, modulus=3)
f_q: Poly(-45*x**166 + 49*x**165 + 43*x**164 - 5*x**163 - 31*x**162 + 12*x**161 + 46*x**160 - 25*x**159 - 12*x**158 + 50*x**157 - 60*x**156 + 42*x**155 + 3*x**154 + 24*x**153 + 63*x**152 + 41*x**151 + x**150 - 50*x**149 + 16*x**148 + 48*x**147 + 62*x**146 + 59*x**145 - 36*x**144 + 54*x**143 - 16*x**142 + 36*x**141 - 34*x**140 - 43*x**139 + 16*x**138 + 64*x**137 + 36*x**136 - 33*x**135 - 40*x**134 + 11*x**133 - 16*x**132 - 49*x**131 + 51*x**130 + 40*x**129 + 10*x**128 - 53*x**127 - 33*x**126 + 16*x**125 + 64*x**124 - 50*x**123 + 35*x**122 - 59*x**121 - 59*x**120 + 35*x**119 - 58*x**118 + 43*x**117 + 28*x**116 + 5*x**115 - 45*x**114 - 33*x**113 - 14*x**112 - 56*x**111 - 24*x**110 + 31*x**109 + 53*x**108 + 30*x**107 + 59*x**106 + 23*x**105 - 30*x**104 + 50*x**103 - 41*x**102 - 23*x**101 - 51*x**100 - 56*x**99 + 40*x**98 + 25*x**97 + 50*x**96 - 12*x**95 + 11*x**94 - 6*x**93 + 45*x**92 - 2*x**91 - 43*x**90 - 7*x**89 - 51*x**88 + 16*x**87 + 39*x**86 - 57*x**85 + 31*x**83 + 41*x**82 - 40*x**81 + 38*x**80 + 25*x**79 + 49*x**78 - 30*x**77 - 35*x**76 + 22*x**75 + 23*x**74 + 57*x**73 - 45*x**72 - 9*x**71 + 26*x**70 + 41*x**69 + 59*x**68 - 19*x**67 + 17*x**66 - 41*x**65 - 12*x**64 - 60*x**63 + 25*x**62 + 14*x**61 - 26*x**60 + 30*x**59 - 44*x**58 - 19*x**57 - 39*x**56 + 59*x**55 - 42*x**54 + 17*x**53 + 58*x**52 - 40*x**51 + 42*x**50 + 51*x**49 - 30*x**48 + 19*x**47 - 44*x**46 - 45*x**45 + 15*x**44 - 15*x**43 - 48*x**42 - 9*x**41 + 62*x**40 + 25*x**39 - 25*x**38 - x**37 + 61*x**36 - 57*x**35 + 45*x**34 - 24*x**33 - 61*x**32 - 4*x**31 + 59*x**30 + 27*x**29 + 10*x**28 - 22*x**27 - 5*x**26 + 5*x**25 - 10*x**24 - 26*x**23 + 49*x**22 + 63*x**20 - 48*x**19 + 42*x**18 - 49*x**17 - 34*x**16 + 26*x**15 + 2*x**14 + 46*x**13 - 23*x**12 + 11*x**11 + 21*x**10 + 27*x**9 - 9*x**8 - 11*x**7 - 35*x**6 + 23*x**5 - 7*x**4 + 2*x**3 - 52*x**2 - 53*x + 51, x, domain='ZZ')
h: Poly(62*x**166 - 20*x**165 + 48*x**164 + 32*x**163 - 15*x**162 - 22*x**161 + 16*x**160 - 54*x**159 - 17*x**158 - 27*x**157 - 33*x**156 - 40*x**155 - 59*x**154 + 54*x**153 + 30*x**152 - 18*x**151 - 24*x**150 + 48*x**149 - 6*x**148 - 40*x**147 + 10*x**146 + 47*x**145 + 33*x**144 + 51*x**143 + 12*x**142 + 57*x**141 + 52*x**140 + 44*x**139 + 55*x**138 + 53*x**137 + 60*x**136 + 8*x**135 - 63*x**134 + 32*x**133 + 10*x**132 + 36*x**131 - 45*x**130 + 38*x**129 - 59*x**128 + 62*x**127 + 29*x**126 + 11*x**125 - 40*x**124 + 11*x**123 + 48*x**122 + 31*x**121 + 38*x**120 + 54*x**119 + 47*x**118 - 57*x**117 - 27*x**116 + x**115 + 6*x**114 - 24*x**113 - 15*x**112 - 59*x**111 + 45*x**110 - 35*x**109 - 58*x**108 - 43*x**107 + 15*x**106 - 25*x**105 + 4*x**104 - 4*x**103 - x**102 + 58*x**101 - 25*x**100 - 20*x**99 + 42*x**98 + 51*x**97 + 10*x**96 + 29*x**95 + x**94 + 23*x**93 - 40*x**92 - 5*x**91 - 56*x**90 + 21*x**89 - 55*x**88 + x**87 + x**86 + 29*x**85 - 9*x**84 - 47*x**83 + 19*x**82 + 39*x**81 + 47*x**80 + 46*x**79 + 26*x**78 - 19*x**77 + 37*x**76 - 22*x**75 + 25*x**74 - 19*x**73 - 43*x**72 + 22*x**71 + 34*x**70 + 42*x**69 - 36*x**68 + 61*x**67 - 37*x**66 + x**65 - 61*x**64 - 19*x**63 + 55*x**62 + 25*x**61 + 58*x**60 + 5*x**59 - 11*x**58 - 33*x**57 - 44*x**56 + 23*x**55 - 63*x**54 - 20*x**53 - 53*x**52 + 50*x**51 + 57*x**50 + 12*x**49 - 9*x**48 + 50*x**47 - 45*x**46 + 6*x**45 + 17*x**44 - 38*x**43 + 50*x**42 - 52*x**41 - 42*x**40 - 56*x**39 - 29*x**38 + 60*x**37 + 21*x**36 + 35*x**35 + 47*x**34 - 3*x**33 + 55*x**32 - 7*x**31 + 40*x**30 - 19*x**29 + 49*x**28 + 22*x**27 + 2*x**26 - 19*x**25 - 61*x**24 + 19*x**23 + 53*x**22 - 19*x**21 + 23*x**20 + 50*x**19 + 26*x**18 + 28*x**17 + 36*x**16 - 28*x**15 + 54*x**14 + 37*x**13 - 15*x**12 - 36*x**11 - 24*x**10 + 40*x**9 - 36*x**8 + 26*x**7 - 63*x**6 + 55*x**5 - 57*x**4 + 5*x**3 + 39*x**2 - 61*x - 31, x, domain='ZZ')
Private key saved to myKey.priv file
Private key saved to myKey.pub file

Encrypting / decrypting

There are two ways of providing input to the program and two ways of interpreting this input.

Supported input:

  • file
$ ./ntru.py enc myKey.pub.npz test.txt
!֘Tމ���c|���}3i1<pV�9�p��[E�VS��X1�4��ц4�6�A�ƚ�;����˔O)'�G8��5Mt[RXcj0]���y���hq�^1�ē���b944
                                                                                           (�n�'u���y4��W��	���6����5W�*��
  • standard input
$ echo "hello!" | ./ntru.py enc myKey.pub.npz
|�w�T�a�V�"I�&�C�(�i��C���y��~���U��|8� Qg
                                          �f�(�B��?�c{r����giܝ���7LxQwH*�G*�
                                                                            ��Y'��7����f@�F�ɂ�9�TU�ʪ�Oܠ�l���(@�
                                                                                                               )e���$�(B���)�

Supported input/output types:

  • binary encryption input (decryption output) is translated into (from a) polynomial with coefficients of ones and zeros (we can see the array of this polynomial using debug mode)
$ echo "hello!" | ./ntru.py -d enc myKey.pub.npz

...

---INPUT---
b'hello!\n'
-----------
POLYNOMIAL DEGREE: 54
BINARY: [0 1 1 0 1 0 0 0 0 1 1 0 0 1 0 1 0 1 1 0 1 1 0 0 0 1 1 0 1 1 0 0 0 1 1 0 1
 1 1 1 0 0 1 0 0 0 0 1 0 0 0 0 1 0 1]
  • binary encryption output (decryption input) is translated from a polynomial with coefficients in Z_q field into binary stream where each coefficient is encoded into log(q) bits
  • polynomial as integer array (e.g. [1,0,1])
$ echo "hello!" | ./ntru.py -o enc myKey.pub.npz
[-13, 39, 0, 14, 7, 19, 40, 61, 45, -24, -32, 41, 12, -58, 42, 2, -48, 63, 14, -26, 22, 41, 35, 22, 16, 50, 62, -17, 30, -60, -20, 50, 17, 16, -58, 54, -45, 4, 20, -11, 33, 59, 43, -32, 60, -18, 60, 20, 57, 3, 47, 55, -54, 9, 37, -30, 43, 30, 55, -25, 17, -45, 37, -54, -56, -52, -49, 21, 24, 64, 9, 34, -59, -8, 64, -10, -49, 17, 2, 31, -48, 39, -20, 64, -42, -22, 7, 0, -34, -40, -20, 27, -9, 60, -25, -57, 45, -4, 62, -50, -22, -13, 59, 9, 15, -49, -60, 31, 2, 38, -13, -17, -55, -40, 36, -22, -42, 46, -63, -56, 28, -32, -51, 13, -27, -28, -57, -62, -17, -3, -9, 35, -6, 16, 56, 31, -14, 48, -20, 54, -30, 59, 61, -53, -44, 44, 49, -58, -20, -19, -9, -7, -55, 52, 10, 7, 18, 24, -49, -57, 61, -38, 48, 55, 11, -54, 43]
$ echo "[0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1,0,1,1,0,1,1,0,0,0,1,1,0,1,1,0,0,0,1,1,0,1,1,1,1,0,0,1,0,0,0,0,1,0,0,0,0,1,0,1]" | ./ntru.py -i enc myKey.pub.npz | ./ntru.py dec myKey.priv.npz
hello!
$ echo "[0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1,0,1,1,0,1,1,0,0,0,1,1,0,1,1,0,0,0,1,1,0,1,1,1,1,0,0,1,0,0,0,0,1,0,0,0,0,1,0,1]" | ./ntru.py -i enc myKey.pub.npz | ./ntru.py dec myKey.priv.npz | xxd -b
00000000: 01101000 01100101 01101100 01101100 01101111 00100001  hello!
00000006: 00001010    

The program always returns its output to standard output. You can save the output to file by redirecting standard output to file. Remember not to use verbose or debug mode in this case.

$ ./ntru.py enc myKey.pub.npz test.txt > encrypted_test.txt
$ ./ntru.py dec myKey.priv.npz encrypted_test.txt
hello!
$ cat test.txt
hello!
$ cat encrypted_test.txt
�o�K_�gi���f�H�qlnh�|{��w���r/��)HL)F���9}�8
                                            Ė�bX��pw@�.*�-۬����D!Au��&�@9��/S�J��q]�~�o�O�D��Yp����M�Ly%_��Ѩ*\;�����P!�iAs*��

Input size

If the input you provided is too large, the program will return this error.

$ ./ntru.py enc myKey.pub.npz test.txt
Traceback (most recent call last):
  File "./ntru.py", line 149, in <module>
    output = encrypt(args['PUB_KEY_FILE'], input_arr, bin_output=not poly_output, block=block)
  File "./ntru.py", line 58, in encrypt
    raise Exception("Input is too large for current N")
Exception: Input is too large for current N

In this case, you can use the block mode, which splits the input into blocks of the requested size. Make sure you are using this mode in both encrypting and decrypting process.

$ ./ntru.py -b enc myKey.pub.npz test.txt | ./ntru.py -b dec myKey.priv.npz
hello!

Verbose mode

When running each process in verbose mode, the program prints to standard output all important variables. Meaning of these could be found in papers about NTRUEncrypt (or even Wikipedia).

Most of variables are polynomials in form of:

g: Poly(-x**10 - x**8 + x**5 + x, x, domain='ZZ')

Some polynomials have particular coefficients counted:

g coeffs: Counter({-1: 2, 1: 2})

Issues

  • Using small N and q values don't work well and leads to the random success of encryption
  • Encrypting even quite small files (~100kB) in block stream mode can take a lot of time

ntru's People

Contributors

handyaputra avatar jkrauze avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ntru's Issues

Question about generating polynomial

Hey, it's not really an issue but I have a question for you.

I'm using your library but I wanted to use my own random number generator to create the polynomials. I see that in "ntru/ntru/ntrucipher.py" you are using two times the function " random_poly" from mathutils. I am wondering why one time you're using it with

  • "random_poly(self.N, int(math.sqrt(self.q)))"
    and another time with
  • "f_poly = random_poly(self.N, self.N // 3, neg_ones_diff=-1)"

It doesn't look like your polynomials are that random, the only randomness here is how your shuffling coefficient in your array of coefficient. Am I true ? Thanks for your help.

question

"Using small N and q values don't work well and leads to the random success of encryption" why?

Error while generating keys

$ ./ntru.py -v gen 706 289 129 mykey.priv mykey.pu
b
NTRU(N=706,p=289,q=129) initiated                 
g: Poly(x**692 - x**691 - x**649 - x**630 - x**612
 - x**584 - x**565 - x**554 + x**548 - x**502 + x*
*495 + x**485 + x**468 + x**453 + x**392 + x**307 
- x**298 - x**265 + x**227 - x**209 + x**198 + x**
111, x, domain='ZZ')                              
g coeffs: Counter({1: 11, -1: 11})                
f: Poly(-x**705 - x**704 + x**703 + x**702 + x**70
0 + x**697 - x**696 - x**695 + x**694 - x**692 + x
**690 - x**689 + x**688 + x**687 - x**686 + x**685
 + x**683 + x**680 - x**678 + x**677 - x**676 + x*
*671 - x**670 + x**669 - x**667 - x**664 + x**663 
+ x**662 + x**661 - x**660 + x**659 - x**658 + x**
657 - x**656 + x**655 + x**654 - x**652 + x**651 -
 x**650 - x**649 - x**646 + x**645 - x**644 + x**6
43 + x**642 - x**639 + x**637 - x**634 + x**633 + 
x**632 - x**631 + x**629 - x**628 + x**627 - x**62
6 - x**625 - x**624 + x**623 - x**622 - x**621 + x
**620 - x**618 - x**617 + x**614 + x**612 - x**609
 - x**608 - x**607 + x**606 + x**605 - x**604 - x*
*603 + x**601 - x**600 + x**599 + x**598 - x**593 
- x**591 - x**590 - x**589 - x**588 - x**587 - x**
586 + x**585 + x**581 + x**580 + x**579 + x**578 -
 x**576 - x**575 + x**574 - x**573 - x**572 - x**5
71 - x**569 + x**567 + x**563 + x**562 + x**561 + 
x**559 + x**555 + x**554 - x**553 + x**552 + x**55
1 + x**548 - x**547 + x**546 + x**544 - x**543 + x
**541 - x**540 + x**538 - x**537 - x**535 + x**534
 + x**532 - x**531 + x**529 + x**528 - x**527 - x*
*526 + x**525 - x**524 - x**523 - x**522 - x**521 
- x**520 + x**518 + x**517 - x**515 + x**514 - x**
513 - x**511 + x**510 - x**509 - x**507 - x**506 +
 x**504 + x**500 + x**498 + x**497 + x**495 - x**4
94 + x**491 - x**490 - x**489 + x**488 - x**486 - 
x**485 + x**484 - x**483 + x**482 - x**481 + x**48
0 + x**478 - x**474 + x**473 - x**472 + x**471 + x
**470 + x**469 - x**468 + x**467 + x**466 + x**465
 + x**464 + x**463 - x**462 + x**460 - x**459 - x*
*458 + x**457 - x**456 - x**453 + x**452 + x**451 
+ x**450 + x**448 + x**447 - x**446 + x**445 + x**
444 + x**443 - x**442 + x**440 - x**439 - x**438 +
 x**433 + x**432 - x**431 - x**430 + x**427 - x**4
26 - x**424 + x**421 - x**420 + x**419 + x**417 - 
x**415 - x**414 + x**413 - x**412 + x**410 + x**40
9 - x**408 - x**407 - x**406 + x**404 - x**402 + x
**401 - x**398 + x**397 - x**396 + x**395 - x**393
 + x**392 + x**390 + x**389 + x**387 + x**386 - x*
*385 + x**384 - x**383 + x**381 - x**380 + x**378 
+ x**377 - x**375 + x**374 - x**370 + x**369 + x**
367 + x**366 - x**364 - x**362 - x**361 + x**360 -
 x**359 + x**357 + x**356 + x**355 - x**353 + x**3
50 + x**349 + x**348 - x**347 + x**344 - x**342 + 
x**340 + x**339 + x**338 + x**337 - x**336 + x**33
5 + x**334 + x**333 + x**332 - x**331 + x**328 - x
**327 - x**324 - x**323 - x**322 - x**321 + x**320
 - x**319 + x**315 + x**313 + x**312 - x**311 + x*
*310 - x**309 + x**305 + x**304 + x**302 - x**301 
- x**300 + x**297 + x**296 + x**295 + x**294 - x**
292 + x**290 - x**289 - x**287 + x**286 - x**285 +
 x**282 + x**281 - x**280 + x**279 - x**278 + x**2
76 + x**275 - x**273 + x**271 + x**270 + x**269 + 
x**267 - x**266 - x**264 - x**263 + x**261 - x**25
6 - x**255 - x**254 + x**253 + x**252 + x**251 - x
**250 - x**247 - x**245 + x**243 + x**242 - x**240
 + x**239 - x**238 - x**234 + x**233 - x**232 + x*
*230 - x**229 - x**225 - x**224 - x**223 - x**222 
+ x**221 + x**219 + x**217 - x**215 - x**214 - x**
213 - x**212 + x**209 - x**208 + x**206 + x**203 +
 x**201 - x**200 - x**199 + x**198 - x**197 + x**1
96 - x**195 - x**194 + x**193 - x**192 - x**191 + 
x**190 - x**188 + x**187 - x**186 - x**185 + x**18
4 - x**182 - x**180 - x**179 - x**178 - x**177 - x
**174 - x**173 - x**172 - x**169 - x**168 + x**166
 + x**165 + x**163 + x**160 - x**155 - x**154 + x*
*153 - x**152 - x**150 + x**149 - x**147 - x**146 
- x**145 + x**144 - x**143 + x**141 + x**140 + x**
139 - x**138 + x**137 - x**135 + x**134 - x**133 -
 x**132 - x**131 + x**129 + x**128 - x**126 - x**1
25 + x**122 + x**118 + x**117 + x**116 - x**115 - 
x**114 + x**113 - x**112 + x**110 - x**108 + x**10
7 + x**105 + x**102 - x**101 - x**100 - x**99 - x*
*98 - x**97 - x**92 + x**90 + x**89 - x**88 - x**8
6 + x**85 + x**82 - x**81 + x**80 - x**79 + x**77 
- x**76 - x**75 - x**73 + x**69 - x**65 + x**64 - 
x**62 - x**61 + x**60 - x**59 - x**55 + x**54 - x*
*53 + x**51 - x**50 - x**49 + x**47 - x**46 + x**4
4 - x**43 + x**42 + x**41 - x**40 - x**39 - x**37 
+ x**34 - x**33 - x**31 - x**30 - x**27 - x**25 - 
x**23 + x**22 + x**20 + x**19 - x**17 + x**14 - x*
*13 + x**12 - x**11 - x**10 + x**9 + x**6 + x**3 -
 x**2 + 1, x, domain='ZZ')
f coeffs: Counter({1: 235, -1: 234})
Traceback (most recent call last):
  File "./ntru.py", line 147, in <module>
    generate(int(args['N']), int(args['P']), int(a
rgs['Q']), args['PRIV_KEY_FILE'], args['PUB_KEY_FI
LE'])
  File "./ntru.py", line 43, in generate
    ntru.generate_random_keys()
  File "/data/data/com.termux/files/home/projects/
python/ntru/ntru/ntrucipher.py", line 42, in gener
ate_random_keys
    self.generate_public_key(f_poly, g_poly)
  File "/data/data/com.termux/files/home/projects/
python/ntru/ntru/ntrucipher.py", line 54, in gener
ate_public_key
    self.f_p_poly = invert_poly(self.f_poly, self.
R_poly, self.p)
  File "/data/data/com.termux/files/home/projects/
python/ntru/ntru/mathutils.py", line 41, in invert
_poly
    raise Exception("Cannot invert polynomial in Z
_{}".format(p))
Exception: Cannot invert polynomial in Z_289

Error while decryption

Generated keys with following parameters
N = 251, p = 3, q = 128

input file 'pt.txt' contains text "hello"

encryption operation works without errors to generate output which is stored in output file with the following command:
python .\ntru.py enc -b .\myKey.pub.npz .\pt.txt > ct.txt

The code gives the following error while decrypting with the following command:
python .\ntru.py dec -b .\myKey.priv.npz .\ct.txt

Error:
Traceback (most recent call last):
File ".\ntru.py", line 151, in
output = decrypt(args['PRIV_KEY_FILE'], input_arr, bin_input=not poly_input, block=block)
File ".\ntru.py", line 99, in decrypt
input_arr = input_arr.reshape((-1, ntru.N))
ValueError: cannot reshape array of size 1016 into shape (251)

ValueError: Object arrays cannot be loaded when allow_pickle=False

Hello,

I just cloned the repository but when I tried the examples in the doc, for example:

python ./ntru.py enc ./myKey.pub.npz ./test.txt

Here is the output:

Traceback (most recent call last):
  File "./ntru.py", line 149, in <module>
    output = encrypt(args['PUB_KEY_FILE'], input_arr, bin_output=not poly_output, block=block)
  File "./ntru.py", line 55, in encrypt
    ntru.h_poly = Poly(pub_key['h'].astype(np.int)[::-1], x).set_domain(ZZ)
  File "/nix/store/vvlzfcwnsdjacv0fq7vg7rchmskj5d96-python3-3.7.5-env/lib/python3.7/site-packages/numpy/lib/npyio.py", line 262, in __getitem__
    pickle_kwargs=self.pickle_kwargs)
  File "/nix/store/vvlzfcwnsdjacv0fq7vg7rchmskj5d96-python3-3.7.5-env/lib/python3.7/site-packages/numpy/lib/format.py", line 722, in read_array
    raise ValueError("Object arrays cannot be loaded when "
ValueError: Object arrays cannot be loaded when allow_pickle=False

I'm using python 3.7.5 on nixos (linux 5.4.8).

Thank you!

Example

I think it would be a good idea adding an example code. I'm having problems with the code exe and an example done by you could explain some parts of your code. Thanks.

Terminal auto closing after execution and numpy has no attrubute 'int'

Hello,

I am using this (great) script for research purposes, but i encountered some issues.

-When executed, after installation and setup, the script opens a terminal, that shut downs automatically, where as in the example, it shows the result right below the command. How could I reproduce that please?

-I successfully generated my key pair, but when I try to use them for data encryption, I get the following error : "AttributeError: module 'numpy' has no attribute 'int'. Did you mean 'inf'? in numpy_init_.py, line 284, in getattr .

Thank you for your help.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.