symbolic bitwise

79 views
Skip to first unread message

Guillaume CONNAN

unread,
Apr 2, 2015, 2:53:29 PM4/2/15
to sage-s...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Hello,

is there a library to do some symbolic calculations over integer but
with bitwise operations ?
I need to work on symbolic polynomials mixing arithmetic + and * and
bitwise >> << & | ^ .

Thanks,

- --
Guillaume Connan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.14 (GNU/Linux)

iF4EAREIAAYFAlUdkCIACgkQ3evBxNNLf8hgngD/WLzznem2wkVeih1/toPuWPLU
Swg1JwYbgLk2HxGPNfwBAKhJYeYXHVp4m2p3+dTnC6uj2LOC6yEsG0SBKULYUoxd
=yvUg
-----END PGP SIGNATURE-----

Jori Mantysalo

unread,
Apr 3, 2015, 5:23:30 AM4/3/15
to sage-s...@googlegroups.com
On Thu, 2 Apr 2015, Guillaume CONNAN wrote:

> is there a library to do some symbolic calculations over integer but
> with bitwise operations ?
> I need to work on symbolic polynomials mixing arithmetic + and * and
> bitwise >> << & | ^ .

I don't know about a library. But in any case, isn't it quite easy to make
some functions? Do I understand correctly that you mean something like

P.<a>=ZZ[]
P1=10*a^2-7*a+3
P2=11*a^2-15*a+4
P3=(P1.coefficients()[0] | P2.coefficients()[0]) + (P1.coefficients()[1] | P2.coefficients()[1])*a + (P1.coefficients()[2] | P2.coefficients()[2])*a^2
P3

--
Jori Mäntysalo

Guillaume CONNAN

unread,
Apr 3, 2015, 12:39:28 PM4/3/15
to sage-s...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Thanks,

it's to do something like ( P1 * P2 | a ) & P3 + 2

I'll try to do some coercion to make it as easily as P3 * P1 + P2 + 5...

- --
Guillaume

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.14 (GNU/Linux)

iF4EAREIAAYFAlUewjkACgkQ3evBxNNLf8i+aQD7BQ42+HkoIm8ng3T3jqggMJ/6
2eNFP5v10A6i6Bg7fvYBAIKm952yyugxdn7oSWi5zD1rPYes3ZkvntJlXHZG7jnX
=RwFC
-----END PGP SIGNATURE-----

Jori Mantysalo

unread,
Apr 3, 2015, 1:19:13 PM4/3/15
to sage-s...@googlegroups.com
On Fri, 3 Apr 2015, Guillaume CONNAN wrote:

> it's to do something like ( P1 * P2 | a ) & P3 + 2

Can you give a concrete example of "P1 * P2 | a"?

If you want to manipulate all coefficients of a polynomial, you can do it
at least like this:

P.<a>=ZZ[]
P1=10*a^2-7*a+3
P2=sum([a^i*(P1[i] | 42) for i in range(0,3)])
P2

But I'm quite sure that there is some nicer way.

--
Jori Mäntysalo

Guillaume CONNAN

unread,
Apr 4, 2015, 6:24:37 AM4/4/15
to sage-s...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Le 03/04/2015 19:19, Jori Mantysalo a écrit :
> On Fri, 3 Apr 2015, Guillaume CONNAN wrote:
>
>> it's to do something like ( P1 * P2 | a ) & P3 + 2
>
> Can you give a concrete example of "P1 * P2 | a"?

something like :

p1 = lambda z: ((3*z^3 | 2*z^2) ^^ 5*z) + 2

This is a little bit more tricky algebraicaly speaking than + and *
since some laws are not distributive on others

It is to study code obfuscation (but this is a secret.).

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.14 (GNU/Linux)

iF4EAREIAAYFAlUfu94ACgkQ3evBxNNLf8gGJQEAiemUH3tixD0BdimfF+8l906C
mru+E13rsdlKsyNfCMUBAKiVHRfQwr77DuMe5z0xRWN+zfzJ5RUe9f8DjfXZarHM
=lD3P
-----END PGP SIGNATURE-----

Jori Mantysalo

unread,
Apr 8, 2015, 3:27:27 AM4/8/15
to sage-s...@googlegroups.com
On Sat, 4 Apr 2015, Guillaume CONNAN wrote:

> p1 = lambda z: ((3*z^3 | 2*z^2) ^^ 5*z) + 2

What is the supposed result of

3*z^3 | 2*z^2

?

--
Jori Mäntysalo

Guillaume CONNAN

unread,
Apr 8, 2015, 4:44:19 PM4/8/15
to sage-s...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Le 08/04/2015 09:26, Jori Mantysalo a écrit :
> On Sat, 4 Apr 2015, Guillaume CONNAN wrote:
>
>> p1 = lambda z: ((3*z^3 | 2*z^2) ^^ 5*z) + 2
>
> What is the supposed result of
>
> 3*z^3 | 2*z^2

p1(2) is ((24 | 8) ^^ 10) + 2 = 20 for instance but p1(4) = 246
It's a mix of bitwise and arithmetic operations which makes
obfuscation efficient.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.14 (GNU/Linux)

iF4EAREIAAYFAlUlkxIACgkQ3evBxNNLf8gRrwD8DHC2zd4HZnJSeTUa8S37ms/d
dX1xcYb7tfQzQbRVb/UA/04dktMHooztvJNbxBQi+Or7n0F+B8cG4WMld8Q2rrFZ
=SxCX
-----END PGP SIGNATURE-----

Jori Mantysalo

unread,
Apr 10, 2015, 3:42:09 AM4/10/15
to sage-s...@googlegroups.com
On Wed, 8 Apr 2015, Guillaume CONNAN wrote:

>>> p1 = lambda z: ((3*z^3 | 2*z^2) ^^ 5*z) + 2
>>
>> What is the supposed result of
>>
>> 3*z^3 | 2*z^2
>
> p1(2) is ((24 | 8) ^^ 10) + 2 = 20

Understood. You can not define it like

p1(z)=((3*z^3 | 2*z^2) ^^ 5*z) + 2

but you can just make a function:

def p1(z):
return ((3*z^3 | 2*z^2) ^^ 5*z) + 2
p1(2)

outputs 20. You can have function taking function as parameter(s), like

def p2(f, z):
return f(z)^f(z)
p2(p1, 2)

outputs 104857600000000000000000000.

--
Jori Mäntysalo

Guillaume CONNAN

unread,
Apr 14, 2015, 8:52:47 AM4/14/15
to sage-s...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

OK and thank you.
Now I would like to deal with bitwise operations with symbolic
expressions :

sage: z = var('z')
sage: e = ((3*z^3 | 2*z^2) ^^ 5*z) + 2
-
---------------------------------------------------------------------------
TypeError Traceback (most recent call
last)
<ipython-input-2-d41626504269> in <module>()
- ----> 1 e = ((Integer(3)*z**Integer(3) | Integer(2)*z**Integer(2)) ^
Integer(5)*z) + Integer(2)

TypeError: unsupported operand type(s) for |:
'sage.symbolic.expression.Expression' and
'sage.symbolic.expression.Expression'

and that was my question actually : is there something already done to
do it ? Or is it something interesting to investigate ?

- --
G.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.14 (GNU/Linux)

iF4EAREIAAYFAlUtDZkACgkQ3evBxNNLf8isDwD/ZlqNGZaxwhIVS45LkvT7ZQSI
4giLftswOvQkPvMRPsYA+wU5tq0X31LDVTiMmOj5ecrF6y1qwleU8n7Rlj7dkQZm
=Fz0H
-----END PGP SIGNATURE-----

Nils Bruin

unread,
Apr 14, 2015, 11:45:57 AM4/14/15
to sage-s...@googlegroups.com, gco...@free.fr
On Tuesday, April 14, 2015 at 5:52:47 AM UTC-7, Guillaume wrote:
OK and thank you.
Now I would like to deal with bitwise operations with symbolic
expressions :

sage: z = var('z')
sage: e = ((3*z^3 | 2*z^2) ^^ 5*z) + 2

Assuming that "^^" means XOR you could spell this

sage: function("OR")
sage: function("XOR")
sage: e = XOR(OR(3*z^3,2*z^2),5*z)+2

If you read up on the NewSymbolicFunction functionality you could even make them so that you can evaluate the expression for numerical (integer) values of z.

What you obviously don't get from this approach is that sage can test for you symbolically that

NOT(AND(x, y)) == OR(NOT(x),NOT(y))

but then again, your stated goal is to work with operations that do not have nice mutual algebraic relations. It does raise the question what the benefit is that you'll be deriving from representing such expressions symbolically.
Reply all
Reply to author
Forward
0 new messages