Skip to content
This repository was archived by the owner on Sep 13, 2025. It is now read-only.
This repository was archived by the owner on Sep 13, 2025. It is now read-only.

trytes conversion doesn't work for longer trytes #11

@akashgoswami

Description

@akashgoswami

While converting larger trytes in IOTA, the conversion doesn't work reliably and produces different output than the conventional method.

Here is a program which demonstrates the problem

const Trytes = require('trytes');


var RADIX = 3;
var RADIX_BYTES = 256;
var MAX_TRIT_VALUE = 1;
var MIN_TRIT_VALUE = -1;
var BYTE_HASH_LENGTH = 48;

// All possible tryte values
var trytesAlphabet = "9ABCDEFGHIJKLMNOPQRSTUVWXYZ"

// map of all trits representations
var trytesTrits = [
    [ 0,  0,  0],
    [ 1,  0,  0],
    [-1,  1,  0],
    [ 0,  1,  0],
    [ 1,  1,  0],
    [-1, -1,  1],
    [ 0, -1,  1],
    [ 1, -1,  1],
    [-1,  0,  1],
    [ 0,  0,  1],
    [ 1,  0,  1],
    [-1,  1,  1],
    [ 0,  1,  1],
    [ 1,  1,  1],
    [-1, -1, -1],
    [ 0, -1, -1],
    [ 1, -1, -1],
    [-1,  0, -1],
    [ 0,  0, -1],
    [ 1,  0, -1],
    [-1,  1, -1],
    [ 0,  1, -1],
    [ 1,  1, -1],
    [-1, -1,  0],
    [ 0, -1,  0],
    [ 1, -1,  0],
    [-1,  0,  0]
];

function Trits( input, state ) {

    var trits = state || [];

    if (Number.isInteger(input)) {

        var absoluteValue = input < 0 ? -input : input;

        while (absoluteValue > 0) {

            var remainder = absoluteValue % 3;
            absoluteValue = Math.floor(absoluteValue / 3);

            if (remainder > 1) {
                remainder = -1;
                absoluteValue++;
            }

            trits[trits.length] = remainder;
        }
        if (input < 0) {

            for (var i = 0; i < trits.length; i++) {

                trits[i] = -trits[i];
            }
        }
    } else {

        for (var i = 0; i < input.length; i++) {

            var index = trytesAlphabet.indexOf(input.charAt(i));
            trits[i * 3] = trytesTrits[index][0];
            trits[i * 3 + 1] = trytesTrits[index][1];
            trits[i * 3 + 2] = trytesTrits[index][2];
        }
    }

    return trits;
}

const NUMBER_OF_TRITS_IN_A_BYTE = 5;
const NUMBER_OF_TRITS_IN_A_TRYTE = 3;

function Trits2Bytes(trits) {

    var expectedLength = Math.floor((trits.length + NUMBER_OF_TRITS_IN_A_BYTE - 1) / NUMBER_OF_TRITS_IN_A_BYTE);
    var output = new Uint8Array(expectedLength);
    
    for (var i = 0; i < expectedLength; i++) {
        var value = 0;
        for (var j = (trits.length - i * NUMBER_OF_TRITS_IN_A_BYTE) < 5 ? (trits.length - i * NUMBER_OF_TRITS_IN_A_BYTE) : NUMBER_OF_TRITS_IN_A_BYTE; j-- > 0; ) {
            value = value * 3 + trits[ i * NUMBER_OF_TRITS_IN_A_BYTE + j];
        }
        
        output[i] = value & 0xFF;
    }
    
    return output;
    
}



var raw = "999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999THIRRJFYZLEFMIWYVWLGUQST9A9A9CSJMD9M9MXQYYGKTEGJBYCVLPEJYNBARFUVZPTVFBOFWTVNPRTCX9SOVSA999999999999999999999999999999999999999999999999IBV9XZD99C99999999C99999999TRMRK9PYWHCSPCHLNAKHPEHI9BJUOILLILO9KCAHGNN9CUG9OXBQGRBMNDUTL9IQIAFYKWOWHKL9QIAOXZKGDXZNBSAKIZXSDUCHKVAATCSNV9RKOCE99IYHYSVVTECXFP9EQVZ9JFNZMSNOKCBVVEJQKXPOWYO999ZKGDXZNBSAKIZXSDUCHKVAATCSNV9RKOCE99IYHYSVVTECXFP9EQVZ9JNZMSNOKCBVVEJQKXPOWYO999999999999999999999999999999KMDGQWQLE999999999MMMMMMMMM9F9VUKQUCZWOOGZZSSYHQDCXNSK";



var trits = Trits(raw);
var bytes = Trits2Bytes(trits);
var bytes2 = Trytes.encodeTryteStringAsBytes(raw);    

console.log("Length ", bytes.length, bytes2.length);

for ( var i = 0; i < bytes.length; i++){
    if (bytes[i] !== bytes2[i]){
        console.log("Index ", i, " unequal. ", bytes[i], bytes2[i]);
    }
}
                

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions