Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The flow only support with "list length = 32" #8

Open
jason19970210 opened this issue Apr 19, 2024 · 1 comment
Open

The flow only support with "list length = 32" #8

jason19970210 opened this issue Apr 19, 2024 · 1 comment

Comments

@jason19970210
Copy link

I have tried to implement generate_kyber_keys, encrypt, decrypt from cpake.py, but I found that the flow will only support "list length = 32"

here is my test script:

import numpy as np
from cpake import generate_kyber_keys, encrypt, decrypt

def main():

    '''
    The range of `generate_key_in_paramk` can only be 1 to 4
    '''
    generate_key_in_paramk = 2

    sk, pk = generate_kyber_keys(params_k=.generate_key_in_paramk)
    print(f"{len(sk)=}\n{len(pk)=}")

    min = 0
    max = 20

    for i in range(1, 51):
        print(f"\n{i=}")
        m1 = np.random.randint(min, max, i).tolist()
        coins = np.random.randint(min, max, i).tolist()

        try:
            cipher = encrypt(m=m1, pubkey=pk, coins=coins, params_k=generate_key_in_paramk)
            m2 = decrypt(packed_ciphertext=cipher, private_key=sk, params_k=generate_key_in_paramk)
            if m1 == m2:
                print("status: success")
            else:
                print("status: failed")
                print(f"{len(m1)=}, {m1=}\n{coins=}\n{len(m2)=}, {m2=}")
        except Exception as e:
            print(f"{i=} error: {e}")
            continue

    
if __name__ == '__main__':
    main()

And here is the output:

len(sk)=1152
len(pk)=1184

i=1
i=1 error: list index out of range

i=2
i=2 error: list index out of range

i=3
i=3 error: list index out of range

i=4
i=4 error: list index out of range

i=5
i=5 error: list index out of range

i=6
i=6 error: list index out of range

i=7
i=7 error: list index out of range

i=8
i=8 error: list index out of range

i=9
i=9 error: list index out of range

i=10
i=10 error: list index out of range

i=11
i=11 error: list index out of range

i=12
i=12 error: list index out of range

i=13
i=13 error: list index out of range

i=14
i=14 error: list index out of range

i=15
i=15 error: list index out of range

i=16
i=16 error: list index out of range

i=17
i=17 error: list index out of range

i=18
i=18 error: list index out of range

i=19
i=19 error: list index out of range

i=20
i=20 error: list index out of range

i=21
i=21 error: list index out of range

i=22
i=22 error: list index out of range

i=23
i=23 error: list index out of range

i=24
i=24 error: list index out of range

i=25
i=25 error: list index out of range

i=26
i=26 error: list index out of range

i=27
i=27 error: list index out of range

i=28
i=28 error: list index out of range

i=29
i=29 error: list index out of range

i=30
i=30 error: list index out of range

i=31
i=31 error: list index out of range

i=32
status: success

i=33
status: failed
len(m1)=33, m1=[19, 9, 14, 2, 3, 12, 19, 4, 0, 10, 10, 13, 19, 10, 11, 17, 11, 3, 19, 3, 6, 13, 5, 2, 1, 16, 19, 9, 18, 15, 4, 3, 17]
coins=[14, 11, 19, 6, 7, 5, 5, 11, 12, 17, 14, 14, 3, 15, 5, 13, 18, 12, 18, 3, 16, 15, 12, 6, 5, 6, 2, 9, 15, 6, 2, 18, 2]
len(m2)=32, m2=[19, 9, 14, 2, 3, 12, 19, 4, 0, 10, 10, 13, 19, 10, 11, 17, 11, 3, 19, 3, 6, 13, 5, 2, 1, 16, 19, 9, 18, 15, 4, 3]

i=34
status: failed
len(m1)=34, m1=[16, 11, 9, 18, 15, 11, 9, 18, 1, 2, 4, 11, 9, 14, 10, 19, 19, 12, 2, 5, 10, 17, 15, 2, 8, 5, 19, 17, 11, 8, 7, 7, 10, 17]
coins=[4, 6, 6, 13, 19, 5, 19, 6, 8, 12, 12, 3, 12, 6, 2, 10, 8, 10, 14, 3, 6, 7, 4, 19, 6, 17, 7, 9, 15, 14, 17, 19, 0, 0]
len(m2)=32, m2=[16, 11, 9, 18, 15, 11, 9, 18, 1, 2, 4, 11, 9, 14, 10, 19, 19, 12, 2, 5, 10, 17, 15, 2, 8, 5, 19, 17, 11, 8, 7, 7]

i=35
status: failed
len(m1)=35, m1=[12, 10, 1, 15, 7, 4, 18, 6, 17, 3, 4, 5, 7, 3, 19, 15, 9, 18, 2, 1, 17, 2, 3, 8, 4, 4, 2, 15, 2, 8, 3, 1, 15, 16, 8]
coins=[3, 16, 5, 0, 17, 12, 13, 19, 18, 10, 14, 13, 6, 11, 19, 0, 9, 15, 10, 2, 7, 15, 6, 0, 18, 19, 4, 16, 19, 6, 15, 13, 19, 18, 15]
len(m2)=32, m2=[12, 10, 1, 15, 7, 4, 18, 6, 17, 3, 4, 5, 7, 3, 19, 15, 9, 18, 2, 1, 17, 2, 3, 8, 4, 4, 2, 15, 2, 8, 3, 1]

i=36
status: failed
len(m1)=36, m1=[12, 15, 7, 19, 10, 17, 13, 0, 5, 1, 10, 18, 2, 6, 10, 1, 11, 16, 11, 1, 7, 17, 4, 9, 16, 5, 12, 15, 15, 19, 16, 3, 5, 0, 11, 1]
coins=[11, 12, 16, 7, 12, 12, 2, 10, 10, 7, 6, 3, 16, 10, 17, 14, 5, 15, 12, 3, 1, 17, 8, 9, 13, 7, 18, 6, 7, 14, 19, 1, 5, 4, 0, 7]
len(m2)=32, m2=[12, 15, 7, 19, 10, 17, 13, 0, 5, 1, 10, 18, 2, 6, 10, 1, 11, 16, 11, 1, 7, 17, 4, 9, 16, 5, 12, 15, 15, 19, 16, 3]

i=37
status: failed
len(m1)=37, m1=[8, 11, 4, 13, 15, 2, 18, 19, 8, 4, 2, 3, 11, 5, 6, 9, 2, 11, 3, 5, 2, 8, 15, 19, 19, 12, 6, 15, 12, 4, 9, 11, 4, 3, 4, 17, 7]
coins=[15, 8, 6, 1, 10, 10, 1, 15, 9, 5, 17, 9, 12, 10, 17, 8, 19, 19, 16, 13, 1, 5, 15, 15, 12, 7, 2, 12, 15, 15, 19, 12, 14, 9, 12, 6, 2]
len(m2)=32, m2=[8, 11, 4, 13, 15, 2, 18, 19, 8, 4, 2, 3, 11, 5, 6, 9, 2, 11, 3, 5, 2, 8, 15, 19, 19, 12, 6, 15, 12, 4, 9, 11]

i=38
status: failed
len(m1)=38, m1=[13, 17, 0, 13, 13, 2, 8, 15, 18, 5, 11, 0, 8, 9, 11, 4, 16, 0, 12, 7, 19, 16, 3, 18, 19, 18, 5, 10, 14, 18, 9, 7, 17, 17, 0, 7, 3, 11]
coins=[0, 8, 6, 12, 5, 10, 0, 2, 7, 0, 7, 1, 3, 0, 5, 10, 1, 12, 6, 10, 14, 1, 13, 13, 3, 3, 9, 14, 12, 1, 12, 6, 14, 4, 2, 0, 0, 7]
len(m2)=32, m2=[13, 17, 0, 13, 13, 2, 8, 15, 18, 5, 11, 0, 8, 9, 11, 4, 16, 0, 12, 7, 19, 16, 3, 18, 19, 18, 5, 10, 14, 18, 9, 7]

i=39
status: failed
len(m1)=39, m1=[7, 17, 2, 1, 16, 17, 4, 3, 10, 11, 17, 0, 12, 16, 0, 14, 17, 8, 4, 5, 15, 15, 19, 4, 15, 18, 3, 19, 1, 10, 4, 3, 15, 1, 14, 8, 5, 12, 13]
coins=[9, 13, 16, 9, 18, 7, 14, 19, 19, 2, 8, 5, 17, 16, 8, 0, 17, 13, 4, 9, 7, 19, 9, 19, 17, 0, 1, 12, 9, 18, 4, 5, 16, 9, 13, 6, 5, 14, 16]
len(m2)=32, m2=[7, 17, 2, 1, 16, 17, 4, 3, 10, 11, 17, 0, 12, 16, 0, 14, 17, 8, 4, 5, 15, 15, 19, 4, 15, 18, 3, 19, 1, 10, 4, 3]

i=40
status: failed
len(m1)=40, m1=[11, 14, 7, 8, 2, 2, 1, 0, 16, 17, 12, 6, 5, 4, 2, 11, 6, 17, 13, 0, 1, 15, 6, 18, 15, 1, 7, 10, 1, 17, 18, 13, 16, 11, 6, 9, 14, 17, 18, 1]
coins=[15, 5, 2, 6, 0, 4, 7, 6, 14, 11, 11, 3, 8, 6, 16, 9, 0, 4, 1, 6, 15, 17, 15, 13, 4, 17, 14, 16, 4, 17, 4, 3, 16, 19, 14, 1, 1, 15, 19, 7]
len(m2)=32, m2=[11, 14, 7, 8, 2, 2, 1, 0, 16, 17, 12, 6, 5, 4, 2, 11, 6, 17, 13, 0, 1, 15, 6, 18, 15, 1, 7, 10, 1, 17, 18, 13]

i=41
status: failed
len(m1)=41, m1=[6, 0, 11, 11, 18, 17, 1, 5, 0, 6, 1, 10, 8, 8, 9, 0, 17, 10, 12, 1, 16, 16, 8, 5, 4, 14, 4, 5, 9, 10, 14, 14, 8, 11, 13, 13, 3, 2, 0, 2, 15]
coins=[0, 10, 14, 17, 19, 15, 3, 19, 16, 17, 17, 3, 0, 18, 0, 0, 9, 14, 10, 9, 6, 13, 9, 1, 7, 8, 18, 11, 3, 8, 14, 16, 12, 0, 14, 0, 7, 10, 9, 11, 13]
len(m2)=32, m2=[6, 0, 11, 11, 18, 17, 1, 5, 0, 6, 1, 10, 8, 8, 9, 0, 17, 10, 12, 1, 16, 16, 8, 5, 4, 14, 4, 5, 9, 10, 14, 14]

i=42
status: failed
len(m1)=42, m1=[7, 19, 16, 14, 12, 9, 4, 16, 14, 18, 15, 4, 14, 6, 18, 13, 13, 10, 19, 16, 18, 5, 11, 19, 8, 1, 15, 17, 4, 5, 18, 5, 16, 15, 17, 12, 12, 6, 15, 19, 12, 4]
coins=[16, 7, 11, 3, 17, 6, 15, 18, 14, 13, 12, 12, 18, 12, 14, 1, 17, 12, 7, 3, 17, 16, 5, 18, 0, 15, 19, 0, 15, 17, 16, 15, 2, 15, 8, 3, 0, 14, 16, 17, 10, 4]
len(m2)=32, m2=[7, 19, 16, 14, 12, 9, 4, 16, 14, 18, 15, 4, 14, 6, 18, 13, 13, 10, 19, 16, 18, 5, 11, 19, 8, 1, 15, 17, 4, 5, 18, 5]

i=43
status: failed
len(m1)=43, m1=[11, 2, 15, 13, 11, 8, 13, 17, 15, 13, 2, 13, 6, 18, 10, 10, 8, 18, 18, 17, 1, 12, 11, 1, 6, 12, 11, 16, 3, 18, 5, 13, 15, 16, 9, 12, 0, 0, 2, 7, 5, 5, 2]
coins=[5, 8, 13, 18, 9, 17, 17, 8, 0, 15, 5, 18, 1, 5, 5, 15, 16, 1, 8, 18, 1, 2, 14, 7, 16, 11, 6, 6, 18, 0, 0, 1, 11, 18, 8, 0, 16, 0, 2, 9, 9, 4, 14]
len(m2)=32, m2=[11, 2, 15, 13, 11, 8, 13, 17, 15, 13, 2, 13, 6, 18, 10, 10, 8, 18, 18, 17, 1, 12, 11, 1, 6, 12, 11, 16, 3, 18, 5, 13]

i=44
status: failed
len(m1)=44, m1=[3, 9, 18, 1, 13, 15, 3, 17, 19, 4, 14, 12, 3, 6, 14, 5, 2, 16, 0, 1, 15, 15, 3, 13, 7, 17, 4, 17, 11, 13, 9, 11, 8, 16, 2, 18, 5, 0, 11, 15, 2, 14, 9, 18]
coins=[19, 6, 9, 19, 13, 11, 14, 8, 15, 12, 17, 2, 14, 6, 15, 1, 11, 6, 17, 1, 0, 18, 13, 13, 1, 14, 15, 13, 6, 9, 14, 8, 17, 4, 6, 16, 4, 3, 14, 17, 0, 4, 18, 10]
len(m2)=32, m2=[3, 9, 18, 1, 13, 15, 3, 17, 19, 4, 14, 12, 3, 6, 14, 5, 2, 16, 0, 1, 15, 15, 3, 13, 7, 17, 4, 17, 11, 13, 9, 11]

i=45
status: failed
len(m1)=45, m1=[6, 10, 15, 10, 9, 1, 0, 18, 5, 18, 15, 11, 10, 4, 3, 0, 12, 18, 0, 13, 1, 8, 16, 10, 11, 14, 15, 9, 6, 0, 11, 0, 3, 16, 4, 13, 8, 4, 12, 13, 19, 16, 11, 13, 17]
coins=[13, 10, 2, 18, 12, 11, 19, 2, 1, 12, 12, 0, 1, 16, 18, 7, 0, 15, 2, 2, 9, 16, 17, 4, 9, 18, 3, 14, 2, 0, 13, 2, 14, 6, 9, 10, 3, 14, 0, 0, 11, 12, 10, 4, 4]
len(m2)=32, m2=[6, 10, 15, 10, 9, 1, 0, 18, 5, 18, 15, 11, 10, 4, 3, 0, 12, 18, 0, 13, 1, 8, 16, 10, 11, 14, 15, 9, 6, 0, 11, 0]

i=46
status: failed
len(m1)=46, m1=[1, 11, 1, 5, 5, 19, 2, 8, 7, 2, 1, 15, 14, 8, 10, 1, 18, 12, 19, 13, 17, 2, 1, 5, 11, 10, 9, 6, 7, 9, 9, 19, 11, 11, 13, 10, 17, 1, 2, 7, 5, 5, 13, 2, 9, 14]
coins=[19, 16, 3, 5, 5, 16, 9, 6, 14, 10, 3, 5, 11, 13, 12, 7, 14, 11, 4, 19, 3, 2, 8, 3, 15, 18, 12, 19, 10, 15, 17, 7, 3, 8, 13, 7, 11, 8, 9, 17, 1, 5, 9, 13, 9, 19]
len(m2)=32, m2=[1, 11, 1, 5, 5, 19, 2, 8, 7, 2, 1, 15, 14, 8, 10, 1, 18, 12, 19, 13, 17, 2, 1, 5, 11, 10, 9, 6, 7, 9, 9, 19]

i=47
status: failed
len(m1)=47, m1=[1, 14, 5, 7, 14, 19, 16, 17, 6, 5, 12, 10, 0, 0, 3, 3, 10, 9, 19, 4, 6, 6, 18, 2, 6, 1, 14, 18, 9, 12, 7, 10, 10, 2, 15, 15, 1, 19, 2, 9, 10, 4, 0, 12, 1, 16, 19]
coins=[15, 8, 13, 1, 19, 19, 1, 6, 3, 2, 8, 5, 11, 14, 19, 9, 8, 6, 17, 11, 16, 2, 10, 17, 12, 3, 11, 6, 14, 15, 11, 3, 8, 2, 1, 15, 0, 17, 5, 8, 4, 19, 14, 10, 17, 3, 13]
len(m2)=32, m2=[1, 14, 5, 7, 14, 19, 16, 17, 6, 5, 12, 10, 0, 0, 3, 3, 10, 9, 19, 4, 6, 6, 18, 2, 6, 1, 14, 18, 9, 12, 7, 10]

i=48
status: failed
len(m1)=48, m1=[3, 7, 14, 17, 9, 7, 15, 17, 6, 17, 16, 11, 0, 18, 12, 8, 8, 4, 3, 5, 7, 14, 12, 10, 17, 10, 1, 19, 2, 15, 15, 10, 16, 0, 7, 3, 13, 10, 5, 5, 10, 5, 5, 7, 9, 8, 6, 16]
coins=[7, 3, 8, 10, 14, 13, 14, 10, 0, 2, 15, 11, 9, 10, 0, 2, 14, 11, 13, 9, 3, 13, 0, 9, 16, 16, 0, 19, 7, 15, 18, 11, 13, 19, 12, 13, 0, 7, 9, 3, 14, 12, 14, 16, 14, 11, 16, 15]
len(m2)=32, m2=[3, 7, 14, 17, 9, 7, 15, 17, 6, 17, 16, 11, 0, 18, 12, 8, 8, 4, 3, 5, 7, 14, 12, 10, 17, 10, 1, 19, 2, 15, 15, 10]

i=49
status: failed
len(m1)=49, m1=[15, 16, 16, 10, 1, 12, 10, 14, 12, 14, 16, 18, 15, 11, 0, 4, 9, 19, 9, 4, 4, 8, 4, 7, 4, 17, 4, 8, 17, 6, 17, 13, 1, 13, 9, 16, 4, 2, 5, 5, 11, 17, 2, 4, 12, 8, 13, 10, 3]
coins=[13, 9, 19, 6, 6, 18, 9, 17, 13, 2, 19, 17, 7, 2, 15, 12, 0, 3, 14, 18, 15, 12, 11, 6, 6, 16, 7, 10, 16, 6, 0, 18, 2, 0, 11, 15, 9, 19, 7, 11, 7, 5, 16, 14, 11, 13, 15, 13, 18]
len(m2)=32, m2=[15, 16, 16, 10, 1, 12, 10, 14, 12, 14, 16, 18, 15, 11, 0, 4, 9, 19, 9, 4, 4, 8, 4, 7, 4, 17, 4, 8, 17, 6, 17, 13]

i=50
status: failed
len(m1)=50, m1=[14, 13, 13, 6, 4, 19, 10, 19, 4, 13, 9, 12, 15, 7, 2, 17, 18, 15, 16, 6, 11, 0, 7, 16, 19, 3, 4, 3, 7, 5, 0, 14, 16, 15, 0, 9, 19, 18, 12, 18, 0, 6, 9, 0, 9, 4, 18, 1, 0, 17]
coins=[12, 19, 10, 15, 19, 17, 3, 4, 2, 2, 16, 7, 16, 17, 14, 4, 14, 4, 12, 5, 8, 19, 6, 16, 5, 15, 5, 18, 19, 10, 5, 7, 0, 10, 6, 14, 17, 9, 1, 3, 15, 12, 16, 2, 7, 15, 17, 16, 12, 1]
len(m2)=32, m2=[14, 13, 13, 6, 4, 19, 10, 19, 4, 13, 9, 12, 15, 7, 2, 17, 18, 15, 16, 6, 11, 0, 7, 16, 19, 3, 4, 3, 7, 5, 0, 14]

As the output, there is only 32 bytes be decrypted.

Any ideas ?

@jason19970210
Copy link
Author

The poly_from_bytes function in poly.py only support 32-byte message

def poly_from_data(msg):
    """
    convert a 32-byte message to a polynomial
    :param msg: byte array
    :return: short array
    """
    r = [ 0 for x in range(0, KYBER_N)]
    mask = 0
    for i in range(0, KYBER_N // 8):
        for j in range(0,8):
            mask = cast_to_short (-1 * cast_to_short (((msg[i] & 0xFF) >> j) & 1))
            r[8 * i + j] = cast_to_short (mask & cast_to_short ((KYBER_Q + 1) // 2))
    return r

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant