doing math in gdscript

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sxkod

Hi all

I am struggling to make some simple math work in gdscript. I am sure, problem lies between the chair and the keyboard. Any help greatly appreciated.

I am trying to calculate keys using Diffie-Hellman-Markle. Below is the python code and it’s results.

#! /usr/bin/env python

g=2

p=0xffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aacaa68ffffffffffffffffL


a=59
b=71

A=(g**a) % p
B=(g**b) % p

ka=(B ** a) % p
kb=(A ** b) % p

print('a is ',a)
print('b is ',b)
print("A is ",A)
print("B is ",B)
print("ka is ",ka)
print("kb is ",kb)


print("eq ",ka==kb)

This results in

('a is ', 59)
('b is ', 71)
('A is ', 576460752303423488L)
('B is ', 2361183241434822606848L)
('ka is ', 16442213848486767445132359556492741324196303870626733711342453589301991576186875468021996289205535481956706062406580444049894685799041662148315908991512434555079133502568108234424576835136035002426900230264860071927088078634783411337136555440444798986932564700842086066779890968157190606729087123006256280837616483050403424961295873167794537071586976006633439101515724844252739121135638828643335552430697667706791009879097575069247751655870623159351499789203448857863463148408381295979647484345355034960837367804305299206929151203289277281330788670743337399727353498727798513629921453186748124980622290180040496979805L)
('kb is ', 16442213848486767445132359556492741324196303870626733711342453589301991576186875468021996289205535481956706062406580444049894685799041662148315908991512434555079133502568108234424576835136035002426900230264860071927088078634783411337136555440444798986932564700842086066779890968157190606729087123006256280837616483050403424961295873167794537071586976006633439101515724844252739121135638828643335552430697667706791009879097575069247751655870623159351499789203448857863463148408381295979647484345355034960837367804305299206929151203289277281330788670743337399727353498727798513629921453186748124980622290180040496979805L)
('eq ', True)

However in gdscript
print(pow(2,42) % 23)
raises an error - invalid operands for operator.

What is the way round? I tried to convert all of them to floats without success.

Thanks

The % operation (in godot 3.0.6 at least) is only available for both arguments to be ints, but pow gives a float resutl. So float % int is not permmited and that’s the error you have.

You could do this print(int(pow(2,42)) % 23) but the problem here is that pow(2,42) exceeds the maximun value of a 16bit integer, and so the result will be negative (due to complements implementation).

However print(int(pow(2,42)) % 23) works fine in godot 3.1 alpha 5, and i assume also works in beta. If this is necesary for you, perhaps you should use 3.1 beta or wait until end of month till the stable release comes.

EDIT Never mind, @kidscancode answer is better.

p7f | 2019-01-08 16:51

Thanks p7f. I will try the fmod.

sxkod | 2019-01-08 17:18

It should work! i already tryed in my pc. If it works, please select @kidscancode answer so others see its solved.

p7f | 2019-01-08 17:38

:bust_in_silhouette: Reply From: kidscancode

pow() returns a float. Unlike Python, the% operator in GDScript only works with integers. If you need to do a modulus operation on a floating point number, the function is fmod(). So your code snippet would become

print(fmod(pow(2, 42), 23))

Ref: @GDScript.fmod()

this answer is better! i did not know that function

p7f | 2019-01-08 16:54

Hi there, thanks.

That solved one problem and now I think I may be hitting against the number resolution.

var dhmP=0xffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a63a3620ffffffffffffffff
var dhmG=2
print("P ",dhmP)
print("G ",dhmG)
print(fmod(pow(dhmG,32),dhmP))

This shows

P -1
G 2
0

I am using the prime numbers and generators from IETF.
RFC 2409 - The Internet Key Exchange (IKE)

dhmP as a long is

var dhmP=1552518092300708935130918131258481755631334049434514313202351194902966239949102107258669453876591642442910007680288864229150803718918046342632727613031282983744380820890196288509170691316593175367469551763119843371637221007210577919

I am following the algorithm from various tutorials
eg: Simple Diffie–Hellman Key Exchange Example With Python – Sublime Robots

Not sure what I am doing wrong.

Also
print(“raw”,fmod(pow(494,21),1509)) in gdscript gives 305.
In python 494**21 % 1509 gives 1457L

I am lost!!

Thanks

sxkod | 2019-01-08 20:57