charm icon indicating copy to clipboard operation
charm copied to clipboard

encode/decode unintuitive behavior

Open purchawek opened this issue 7 years ago • 1 comments

While playing around with charm crypto, I tried this:

>>> group = IntegerGroup()
>>> group.paramgen(10)
>>> g = group.randomGen()
>>> g
469 mod 863
>>> group.decode(g)
b'\x89'
>>> g
393 mod 863

Why does decoding change the value of g? Is it a desired behavior? On https://jhuisi.github.io/charm/cryptographers.html it is stated that:

Since the scheme defines messages as a group element, it is important to use the encode/decode methods to convert the message string into a member of the group, G.

And by that I thought it only reads the value. However, the implementation of decode_message in core/math/integer/integermodule.c explicitly does the substraction in lines 1606 and 1612. Since the file is very old, without any recent modification, I don't know if it is a bug or a feature.

Simlarly, encoding also works weirdly:

>>> group.encode(b'a')
354 mod 863
>>> group.encode(b'aa')
156002 mod 863

Why is the result bigger than p?

purchawek avatar Mar 09 '19 16:03 purchawek

I have the same problem. Sometimes decode, apart from changing the value, returns empty array of bytes like here:

>>> g = group.randomGen()
>>> g
824 mod 1019
>>> group.decode(g)
b''
>>> g
194 mod 1019

KonradSwierczynski avatar Mar 11 '19 08:03 KonradSwierczynski