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

not sure what exactly you want to do #1

Open
ckrmml opened this issue Apr 13, 2019 · 2 comments
Open

not sure what exactly you want to do #1

ckrmml opened this issue Apr 13, 2019 · 2 comments

Comments

@ckrmml
Copy link

ckrmml commented Apr 13, 2019

but all you do is translate from the alphabet list (tern) to numeric value (data list) and vice versa but that ain't no trytes or tryte conversion as far as i can see, if that is what you want to do. perhaps search on github for python projects which already do this? otherwise going after this:
https://steemit.com/iota/@wiredcrypto/understanding-iota-trits-and-trytes

First, each letter must be assigned its ASCII decimal value.
For H, the ASCII value is 72, for O 79, for D 68 and L 76.

ascii_val = ord('H')

Then the respective ASCII decimal value is modulated by 27, the number of characters of the IOTA alphabet.
The resulting remainder results in the first part of the tryte.
The remainder defines the position of the letter in the IOTA alphabet.

# we can hardcode it like this
iota_alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '9']

# or generate it somewhat like this
import string
iota_alphabet = list(string.ascii_uppercase)
iota_alphabet.append(9)

# we know it's 27 characters long, A-Z and a 9, so we could just hardcode 27
remainder =  ascii_val % 27

# or get it's length
remainder = ascii_val % len(iota_alphabet)

Then the remainder value is subtracted from the ASCII value and divided by the number of characters of the IOTA
alphabet to obtain the second value of the tryte or the position of the second letter in the IOTA alphabet.

tryte_val_two = (ascii_val - remainder) / 27

# or again getting the length
tryte_val_two = (ascii_val - remainder) / len(iota_alphabet)

# this gives us a float val, we need an int
# so we make it one
tryte_val_two = int((ascii_val - remainder) / len(iota_alphabet))

# could also be done when getting the char from the list like this
iota_alphabet[int(tryte_val_two)]

# now we have the two characters for the trye, or at least their index in the alphabet
# let's get this done
tryte = iota_alphabet[remainder] + iota_alphabet[tryte_val_two]

looks great, but doesn't help that much in this cluttered fashion, so let's put this together in a function

def char_to_tryte(char):
	ascii_val = ord(char)
	# tc1 should be an int, but for good measure we make sure it will be one
	tc1 = int(ascii_val % len(iota_alphabet))
	tc2 = int((ascii_val - tc1) / len(iota_alphabet))
	tryte = iota_alphabet[tc1] + iota_alphabet[tc2]

	return tryte

and run this

print(char_to_tryte('H'))
SC

# ascii_val -> 72
# tc1 -> 18
# tc2 -> 2

this is working and gives us the right numbers but the return value looks kinda wrong. we wanted RB and got SC. luckily we know python lists start at 0, not 1. so what we need to do is subtract 1 from our values somewhere. either directly while getting them or while using them as list indices

# as list indices:
def char_to_tryte(char):
	ascii_val = ord(char)
	# tc1 should be an int, but for good measure we make sure it will be one
	tc1 = int(ascii_val % len(iota_alphabet))
	tc2 = int((ascii_val - tc1) / len(iota_alphabet))
	tryte = iota_alphabet[tc1 - 1] + iota_alphabet[tc2 - 1]

	return tryte

# while getting the numbers
def char_to_tryte(char):
	ascii_val = ord(char)
	# tc1 should be an int, but for good measure we make sure it will be one
	tc1 = int((ascii_val % len(iota_alphabet)) - 1)
	tc2 = int(((ascii_val - tc1) / len(iota_alphabet)) - 1)
	tryte = iota_alphabet[tc1] + iota_alphabet[tc2]

	return tryte

print(char_to_tryte('H'))
# now gives us
RB

# when subtracting from list indices
# ascii_val -> 72
# tc1 -> 18
# tc2 -> 2

# when subtracting while getting our numbers
# ascii_val -> 72
# tc1 -> 17
# tc2 -> 1

so while this works, it's def not a good function and i have tried and failed with reversing this from tryte to char have a look at how its done here:
https://github.com/iotaledger/iota.lib.py/blob/master/iota/codecs.py

@logiota
Copy link
Owner

logiota commented May 22, 2019

I am really sorry for the late reply, I didn't know I needed to subscribe to my own Repo :P
My latest rewrite is easier to understand I think.
The purpose for this Repo is to have a way to compress/ decompress a CSV file on the Tangle for logging w/o hashing.
Please run the code example and you will surely understand how this is useful for logging tons of data in one TX and also being able to read the data directly in a tangle explorer.

@logiota
Copy link
Owner

logiota commented May 22, 2019

The main issue now is the absence of a vetted encryption for ternary :/

@logiota logiota closed this as completed May 22, 2019
@logiota logiota reopened this May 22, 2019
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

2 participants