{"id":20850594,"url":"https://github.com/multiformats/unsigned-varint","last_synced_at":"2025-12-24T13:41:59.618Z","repository":{"id":10120323,"uuid":"64546681","full_name":"multiformats/unsigned-varint","owner":"multiformats","description":"unsigned varint in use in multiformat specs","archived":false,"fork":false,"pushed_at":"2025-04-28T16:20:38.000Z","size":25,"stargazers_count":81,"open_issues_count":2,"forks_count":21,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-04-28T17:27:17.622Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/multiformats.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-07-30T13:58:08.000Z","updated_at":"2025-04-28T16:20:42.000Z","dependencies_parsed_at":"2023-12-17T23:48:27.228Z","dependency_job_id":null,"html_url":"https://github.com/multiformats/unsigned-varint","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/multiformats/unsigned-varint","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multiformats%2Funsigned-varint","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multiformats%2Funsigned-varint/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multiformats%2Funsigned-varint/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multiformats%2Funsigned-varint/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/multiformats","download_url":"https://codeload.github.com/multiformats/unsigned-varint/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multiformats%2Funsigned-varint/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28003431,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-24T02:00:07.193Z","response_time":83,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-18T03:10:30.085Z","updated_at":"2025-12-24T13:41:59.613Z","avatar_url":"https://github.com/multiformats.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# unsigned-varint\n\n[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](https://protocol.ai)\n[![](https://img.shields.io/badge/project-multiformats-blue.svg?style=flat-square)](https://github.com/multiformats/multiformats)\n[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](https://webchat.freenode.net/?channels=%23ipfs)\n[![](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)\n\n\u003e unsigned varint in use in multiformat specs\n\nThis unsigned varint (VARiable INTeger) format is for the use in all the multiformats.\n\n## Format\n\nOur unsigned varint is a variant of unsigned [LEB128](https://en.wikipedia.org/wiki/LEB128) with some additional restrictions.\n\nSpecifically, unlike LEB128:\n\n- Signed numbers are not supported.\n- Leading zeros must be trimmed when encoding and must be rejected when decoding. The only number that can end in a `0x00` is `0`.\n\n### Spec\n\nThe encoding is:\n- unsigned integers are serialized 7 bits at a time, starting with the least significant bits\n- the most significant bit (msb) in each output byte indicates if there is a continuation byte (msb = 1)\n- there are no signed integers\n- integers are minimally encoded\n\nExamples:\n\n```\n1 (0x01)        =\u003e 00000001 (0x01)\n127 (0x7f)      =\u003e 01111111 (0x7f)\n128 (0x80)      =\u003e 10000000 00000001 (0x8001)\n255 (0xff)      =\u003e 11111111 00000001 (0xff01)\n300 (0x012c)    =\u003e 10101100 00000010 (0xac02)\n16384 (0x4000)  =\u003e 10000000 10000000 00000001 (0x808001)\n```\n\n```\nbyte   # |              0 |            1   |          2     |\nbit    # |c 6 5 4 3 2 1 0 |c 5 4 3 2 1 0 7 |c 4 3 2 1 0 7 6 |\n         |----------------|----------------|----------------|\n16384 =\u003e |1 0 0 0 0 0 0 0 |1 0 0 0 0 0 0 0 |0 0 0 0 0 0 0 1 |\n```\n\nCode that generates this.\n\n```go\npackage main\n\n// test program. we can use the go one.\nimport (\n  \"encoding/binary\" // varint is here\n  \"fmt\"\n)\n\nfunc main() {\n  ints := []uint64{1, 127, 128, 255, 300, 16384}\n  for _, i := range ints {\n    buf := make([]byte, 10)\n    n := binary.PutUvarint(buf, uint64(i))\n\n    hexStr := fmt.Sprintf(\"%x\", i)\n    if len(hexStr)%2 == 1 {\n      hexStr = \"0\" + hexStr\n    }\n    fmt.Print(i, \" (0x\"+hexStr+\")\\t=\u003e \")\n    for c := 0; c \u003c n; c++ {\n      fmt.Printf(\"%08b \", int(buf[c]))\n    }\n    fmt.Printf(\"(0x%x)\\n\", buf[:n])\n  }\n}\n```\n\n\n\n### Practical maximum of 9 bytes (for security)\n\nFor security, to avoid memory attacks, we use a \"practical max\" of 9 bytes. Though there is no theoretical limit, and future specs can grow this number if it is truly necessary to have code or length values equal to or larger than `2^63`.\n\nFor the forseeable future:\n\n- Implementations MUST restrict the size of the varint to a max of 9 bytes (63 bits).\n- A multiformat spec MAY explicitly declare a smaller maximum when using varints.\n- A multiformat spec MAY NOT explicitly declare a larger maximum when using varints without first changing this spec.\n\n### Incompatibilities with Go varints\n\nThis MSB-based unsigned varint is based on the [varint of the Go standard library](https://golang.org/src/encoding/binary/varint.go), which itself was based on the protocol buffers one.\n\nHowever, we have two modifications:\n\n- Multiformats varint only supports unsigned integers, the Go varint supports signed (using zig-zag encoding).\n- Multiformats varints must be minimally encoded. That is, numbers must be encoded in the least number of bytes possible.\n\n\u003e What do we mean by minimally encoded?\n\nMultiformat varints must be encoded in as few bytes as possible. To illustrate\nthe issue, take `{0x81 0x00}`. This is a valid golang varint encoding of 0x1.\nHowever, the _minimal_ encoding of 0x1 is `{0x1}`.\n\n## Implementations\n\n* [Rust](https://github.com/paritytech/unsigned-varint)\n* [JS](https://github.com/chrisdickinson/varint)\n* [PHP](https://github.com/team-acaisia/php-varint)\n* [Go](https://github.com/multiformats/go-varint)\n\n## Contribute\n\nContributions welcome. Please check out [the issues](https://github.com/multiformats/unsigned-varint/issues).\n\nCheck out our [contributing document](https://github.com/multiformats/multiformats/blob/master/contributing.md) for more information on how we work, and about contributing in general. Please be aware that all interactions related to multiformats are subject to the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).\n\nSmall note: If editing the README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification.\n\n## License\n\nThis repository is only for documents. All of these are licensed under the [CC-BY-SA 3.0](https://ipfs.io/ipfs/QmVreNvKsQmQZ83T86cWSjPu2vR3yZHGPm5jnxFuunEB9u) license © 2016 Protocol Labs Inc. Any code is under a [MIT](LICENSE) © 2016 Protocol Labs Inc.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmultiformats%2Funsigned-varint","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmultiformats%2Funsigned-varint","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmultiformats%2Funsigned-varint/lists"}