From e0aae6d8ac1b4389044c5a6332add796bdf1d3a7 Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Fri, 13 Dec 2019 11:42:47 +0200 Subject: [PATCH] Moved base32, 58 and 64 to stew --- libp2p.nimble | 3 +- libp2p/base32.nim | 285 ------------------------- libp2p/base58.nim | 239 --------------------- libp2p/base64.nim | 238 --------------------- libp2p/cid.nim | 3 +- libp2p/daemon/daemonapi.nim | 4 +- libp2p/multiaddress.nim | 3 +- libp2p/multibase.nim | 2 +- libp2p/multihash.nim | 4 +- libp2p/peer.nim | 4 +- tests/testbase32.nim | 409 ------------------------------------ tests/testbase58.nim | 132 ------------ tests/testbase64.nim | 164 --------------- tests/testnative.nim | 2 +- tests/testpeer.nim | 4 +- 15 files changed, 16 insertions(+), 1480 deletions(-) delete mode 100644 libp2p/base32.nim delete mode 100644 libp2p/base58.nim delete mode 100644 libp2p/base64.nim delete mode 100644 tests/testbase32.nim delete mode 100644 tests/testbase58.nim delete mode 100644 tests/testbase64.nim diff --git a/libp2p.nimble b/libp2p.nimble index 5dce11a63..803e669bc 100644 --- a/libp2p.nimble +++ b/libp2p.nimble @@ -12,7 +12,8 @@ requires "nim > 0.19.4", "nimcrypto >= 0.4.1", "chronos >= 2.3.4", "bearssl >= 0.1.3", - "chronicles >= 0.7.0" + "chronicles >= 0.7.0", + "stew" proc runTest(filename: string) = exec "nim c -r tests/" & filename diff --git a/libp2p/base32.nim b/libp2p/base32.nim deleted file mode 100644 index fc36156fb..000000000 --- a/libp2p/base32.nim +++ /dev/null @@ -1,285 +0,0 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. - -## This module implements BASE32 encoding and decoding procedures. -## This module supports RFC4648's BASE32. - -type - Base32Status* {.pure.} = enum - Error, - Success, - Incorrect, - Overrun - - Base32Alphabet* = object - decode*: array[128, int8] - encode*: array[32, uint8] - - Base32Upper* = object - ## Type to use RFC4648 alphabet in uppercase without padding - Base32Lower* = object - ## Type to use RFC4648 alphabet in lowercase without padding - Base32UpperPad* = object - ## Type to use RFC4648 alphabet in uppercase with padding - Base32LowerPad* = object - ## Type to use RFC4648 alphabet in lowercase with padding - HexBase32Upper* = object - ## Type to use RFC4648-HEX alphabet in uppercase without padding - HexBase32Lower* = object - ## Type to use RFC4648-HEX alphabet in lowercase without padding - HexBase32UpperPad* = object - ## Type to use RFC4648-HEX alphabet in uppercase with padding - HexBase32LowerPad* = object - ## Type to use RFC4648-HEX alphabet in lowercase with padding - Base32* = Base32Upper - ## By default we are using RFC4648 alphabet in uppercase without padding - Base32PadTypes* = Base32UpperPad | Base32LowerPad | - HexBase32UpperPad | HexBase32LowerPad - ## All types with padding support - Base32NoPadTypes* = Base32Upper | Base32Lower | HexBase32Upper | - HexBase32Lower - ## All types without padding - Base32Types* = Base32NoPadTypes | Base32PadTypes - ## Supported types - - Base32Error* = object of CatchableError - ## Base32 specific exception type - -proc newAlphabet32*(s: string): Base32Alphabet = - doAssert(len(s) == 32) - for i in 0..= 1: - outbytes[0] = chr(inbytes[0] shr 3) - outbytes[1] = chr((inbytes[0] and 7'u8) shl 2) - result = 2 - if length >= 2: - outbytes[1] = chr(cast[byte](outbytes[1]) or cast[byte](inbytes[1] shr 6)) - outbytes[2] = chr((inbytes[1] shr 1) and 31'u8) - outbytes[3] = chr((inbytes[1] and 1'u8) shl 4) - result = 4 - if length >= 3: - outbytes[3] = chr(cast[byte](outbytes[3]) or (inbytes[2] shr 4)) - outbytes[4] = chr((inbytes[2] and 15'u8) shl 1) - result = 5 - if length >= 4: - outbytes[4] = chr(cast[byte](outbytes[4]) or (inbytes[3] shr 7)) - outbytes[5] = chr((inbytes[3] shr 2) and 31'u8) - outbytes[6] = chr((inbytes[3] and 3'u8) shl 3) - result = 7 - if length >= 5: - outbytes[6] = chr(cast[byte](outbytes[6]) or (inbytes[4] shr 5)) - outbytes[7] = chr(inbytes[4] and 31'u8) - result = 8 - -proc convert8to5(inbytes: openarray[byte], outbytes: var openarray[byte], - length: int): int {.inline.} = - if length >= 2: - outbytes[0] = inbytes[0] shl 3 - outbytes[0] = outbytes[0] or (inbytes[1] shr 2) - result = 1 - if length >= 4: - outbytes[1] = (inbytes[1] and 3'u8) shl 6 - outbytes[1] = outbytes[1] or (inbytes[2] shl 1) - outbytes[1] = outbytes[1] or (inbytes[3] shr 4) - result = 2 - if length >= 5: - outbytes[2] = (inbytes[3] and 15'u8) shl 4 - outbytes[2] = outbytes[2] or (inbytes[4] shr 1) - result = 3 - if length >= 7: - outbytes[3] = (inbytes[4] and 1'u8) shl 7 - outbytes[3] = outbytes[3] or (inbytes[5] shl 2) - outbytes[3] = outbytes[3] or (inbytes[6] shr 3) - result = 4 - if length >= 8: - outbytes[4] = (inbytes[6] and 7'u8) shl 5 - outbytes[4] = outbytes[4] or (inbytes[7] and 31'u8) - result = 5 - -proc encode*(btype: typedesc[Base32Types], inbytes: openarray[byte], - outstr: var openarray[char], outlen: var int): Base32Status = - ## Encode array of bytes ``inbytes`` using BASE32 encoding and store - ## result to ``outstr``. On success ``Base32Status.Success`` will be returned - ## and ``outlen`` will be set to number of characters stored inside of - ## ``outstr``. If length of ``outstr`` is not enough then - ## ``Base32Status.Overrun`` will be returned and ``outlen`` will be set to - ## number of characters required. - when (btype is Base32Upper) or (btype is Base32UpperPad): - const alphabet = RFCUpperCaseAlphabet - elif (btype is Base32Lower) or (btype is Base32LowerPad): - const alphabet = RFCLowerCaseAlphabet - elif (btype is HexBase32Upper) or (btype is HexBase32UpperPad): - const alphabet = HEXUpperCaseAlphabet - elif (btype is HexBase32Lower) or (btype is HexBase32LowerPad): - const alphabet = HEXLowerCaseAlphabet - - if len(inbytes) == 0: - outlen = 0 - return Base32Status.Success - - let length = btype.encodedLength(len(inbytes)) - if length > len(outstr): - outlen = length - return Base32Status.Overrun - - let reminder = len(inbytes) mod 5 - let limit = len(inbytes) - reminder - var i, k: int - while i < limit: - discard convert5to8(inbytes.toOpenArray(i, i + 4), - outstr.toOpenArray(k, k + 7), 5) - for j in 0..7: - outstr[k + j] = chr(alphabet.encode[ord(outstr[k + j])]) - k += 8 - i += 5 - - if reminder != 0: - let left = convert5to8(inbytes.toOpenArray(i, i + reminder - 1), - outstr.toOpenArray(k, length - 1), reminder) - for j in 0..(left - 1): - outstr[k] = chr(alphabet.encode[ord(outstr[k])]) - inc(k) - when (btype is Base32UpperPad) or (btype is Base32LowerPad) or - (btype is HexBase32UpperPad) or (btype is HexBase32LowerPad): - while k < len(outstr): - outstr[k] = '=' - inc(k) - outlen = k - result = Base32Status.Success - -proc encode*(btype: typedesc[Base32Types], - inbytes: openarray[byte]): string {.inline.} = - ## Encode array of bytes ``inbytes`` using BASE32 encoding and return - ## encoded string. - if len(inbytes) == 0: - result = "" - else: - var length = 0 - result = newString(btype.encodedLength(len(inbytes))) - if btype.encode(inbytes, result, length) == Base32Status.Success: - result.setLen(length) - else: - result = "" - -proc decode*[T: byte|char](btype: typedesc[Base32Types], instr: openarray[T], - outbytes: var openarray[byte], outlen: var int): Base32Status = - ## Decode BASE32 string and store array of bytes to ``outbytes``. On success - ## ``Base32Status.Success`` will be returned and ``outlen`` will be set - ## to number of bytes stored. - ## - ## ## If length of ``outbytes`` is not enough to store decoded bytes, then - ## ``Base32Status.Overrun`` will be returned and ``outlen`` will be set to - ## number of bytes required. - when (btype is Base32Upper) or (btype is Base32UpperPad): - const alphabet = RFCUpperCaseAlphabet - elif (btype is Base32Lower) or (btype is Base32LowerPad): - const alphabet = RFCLowerCaseAlphabet - elif (btype is HexBase32Upper) or (btype is HexBase32UpperPad): - const alphabet = HEXUpperCaseAlphabet - elif (btype is HexBase32Lower) or (btype is HexBase32LowerPad): - const alphabet = HEXLowerCaseAlphabet - - if len(instr) == 0: - outlen = 0 - return Base32Status.Success - - let length = btype.decodedLength(len(instr)) - if length > len(outbytes): - outlen = length - return Base32Status.Overrun - - var inlen = len(instr) - when (btype is Base32PadTypes): - for i in countdown(inlen - 1, 0): - if instr[i] != '=': - break - dec(inlen) - - let reminder = inlen mod 8 - let limit = inlen - reminder - var buffer: array[8, byte] - var i, k: int - while i < limit: - for j in 0..<8: - if (cast[byte](instr[i + j]) and 0x80'u8) != 0: - outlen = 0 - return Base32Status.Incorrect - let ch = alphabet.decode[cast[int8](instr[i + j])] - if ch == -1: - outlen = 0 - return Base32Status.Incorrect - buffer[j] = cast[byte](ch) - discard convert8to5(buffer, outbytes.toOpenArray(k, k + 4), 8) - k += 5 - i += 8 - - var left = 0 - if reminder != 0: - if reminder == 1 or reminder == 3 or reminder == 6: - outlen = 0 - return Base32Status.Incorrect - for j in 0.. hi) or (carry != 0'u32): - carry = carry + uint32(256'u32 * buffer[j]) - buffer[j] = cast[byte](carry mod 58) - carry = carry div 58 - dec(j) - hi = j - inc(i) - - j = 0 - while (j < size) and (buffer[j] == 0x00'u8): - inc(j) - - let needed = zcount + size - j - outlen = needed - if len(outstr) < needed: - result = Base58Status.Overrun - else: - for k in 0.. m: - result = Base58Status.Overrun - return - break - inc(m) - dec(outlen) - - if m < binsz: - moveMem(addr outbytes[zcount], addr outbytes[binsz - outlen], outlen) - outlen += zcount - result = Base58Status.Success - -proc decode*(btype: typedesc[Base58C], instr: string): seq[byte] = - ## Decode BASE58 string ``instr`` and return sequence of bytes as result. - if len(instr) > 0: - var size = len(instr) + 4 - result = newSeq[byte](size) - if btype.decode(instr, result, size) == Base58Status.Success: - result.setLen(size) - else: - raise newException(Base58Error, "Incorrect base58 string") diff --git a/libp2p/base64.nim b/libp2p/base64.nim deleted file mode 100644 index 5d6889e85..000000000 --- a/libp2p/base64.nim +++ /dev/null @@ -1,238 +0,0 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. - -## This module implements BASE64 encoding and decoding procedures. - -type - Base64Status* {.pure.} = enum - Error, - Success, - Incorrect, - Overrun - - Base64Alphabet* = object - decode*: array[128, int8] - encode*: array[64, uint8] - - Base64* = object - ## Type to use RFC4648 alphabet without padding - Base64Pad* = object - ## Type to use RFC4648 alphabet with padding - Base64Url* = object - ## Type to use RFC4648 URL alphabet without padding - Base64UrlPad* = object - ## Type to use RFC4648 URL alphabet with padding - - Base64PadTypes* = Base64Pad | Base64UrlPad - ## All types with padding support - Base64NoPadTypes* = Base64 | Base64Url - ## All types without padding support - Base64Types* = Base64 | Base64Pad | Base64Url | Base64UrlPad - ## All types - - Base64Error* = object of CatchableError - ## Base64 specific exception type - -proc newAlphabet64*(s: string): Base64Alphabet = - doAssert(len(s) == 64) - for i in 0.. len(outbytes): - outlen = length - return Base64Status.Overrun - - var inlen = len(instr) - when (btype is Base64PadTypes): - for i in countdown(inlen - 1, 0): - if instr[i] != '=': - break - dec(inlen) - - let reminder = inlen mod 4 - let limit = inlen - reminder - var buffer: array[4, byte] - var i, k: int - while i < limit: - for j in 0..<4: - if (cast[byte](instr[i + j]) and 0x80'u8) != 0: - outlen = 0 - zeroMem(addr outbytes[0], i + 3) - return Base64Status.Incorrect - let ch = alphabet.decode[cast[int8](instr[i + j])] - if ch == -1: - outlen = 0 - zeroMem(addr outbytes[0], i + 3) - return Base64Status.Incorrect - buffer[j] = cast[byte](ch) - outbytes[k] = cast[byte]((buffer[0] shl 2) or (buffer[1] shr 4)) - inc(k) - outbytes[k] = cast[byte]((buffer[1] shl 4) or (buffer[2] shr 2)) - inc(k) - outbytes[k] = cast[byte]((buffer[2] shl 6) or buffer[3]) - inc(k) - i += 4 - - if reminder > 0: - if reminder == 1: - outlen = 0 - return Base64Status.Incorrect - - for j in 0.. 1: - outbytes[k] = cast[byte]((buffer[0] shl 2) or (buffer[1] shr 4)) - inc(k) - if reminder > 2: - outbytes[k] = cast[byte]((buffer[1] shl 4) or (buffer[2] shr 2)) - inc(k) - - outlen = k - result = Base64Status.Success - -proc decode*[T: byte|char](btype: typedesc[Base64Types], - instr: openarray[T]): seq[byte] = - ## Decode BASE64 string ``instr`` and return sequence of bytes as result. - if len(instr) == 0: - result = newSeq[byte]() - else: - var length = 0 - result = newSeq[byte](btype.decodedLength(len(instr))) - if btype.decode(instr, result, length) == Base64Status.Success: - result.setLen(length) - else: - raise newException(Base64Error, "Incorrect base64 string") diff --git a/libp2p/cid.nim b/libp2p/cid.nim index 22968461e..2212bafea 100644 --- a/libp2p/cid.nim +++ b/libp2p/cid.nim @@ -9,7 +9,8 @@ ## This module implementes CID (Content IDentifier). import tables -import multibase, multicodec, multihash, vbuffer, varint, base58 +import multibase, multicodec, multihash, vbuffer, varint +import stew/base58 type CidStatus* {.pure.} = enum diff --git a/libp2p/daemon/daemonapi.nim b/libp2p/daemon/daemonapi.nim index 55eda1976..f299c6570 100644 --- a/libp2p/daemon/daemonapi.nim +++ b/libp2p/daemon/daemonapi.nim @@ -9,8 +9,8 @@ ## This module implementes API for `go-libp2p-daemon`. import os, osproc, strutils, tables, streams, strtabs -import chronos -import ../varint, ../multiaddress, ../multicodec, ../base58, ../cid, ../peer +import chronos, stew/base58 +import ../varint, ../multiaddress, ../multicodec, ../cid, ../peer import ../wire, ../multihash, ../protobuf/minprotobuf import ../crypto/crypto diff --git a/libp2p/multiaddress.nim b/libp2p/multiaddress.nim index 6f824b395..efada6787 100644 --- a/libp2p/multiaddress.nim +++ b/libp2p/multiaddress.nim @@ -11,7 +11,8 @@ import nativesockets import tables, strutils, net import chronos -import multicodec, multihash, multibase, transcoder, base58, base32, vbuffer +import multicodec, multihash, multibase, transcoder, vbuffer +import stew/[base58, base32] from peer import PeerID {.deadCodeElim:on.} diff --git a/libp2p/multibase.nim b/libp2p/multibase.nim index 1f76641a1..fb7117562 100644 --- a/libp2p/multibase.nim +++ b/libp2p/multibase.nim @@ -12,7 +12,7 @@ ## TODO: ## 1. base32z import tables -import base32, base58, base64 +import stew/[base32, base58, base64] type MultibaseStatus* {.pure.} = enum diff --git a/libp2p/multihash.nim b/libp2p/multihash.nim index c21b9ca06..df69167b3 100644 --- a/libp2p/multihash.nim +++ b/libp2p/multihash.nim @@ -22,8 +22,8 @@ ## 2. MURMUR import tables import nimcrypto/[sha, sha2, keccak, blake2, hash, utils] -import varint, vbuffer, base58, multicodec, multibase - +import varint, vbuffer, multicodec, multibase +import stew/base58 # This is workaround for Nim `import` bug. export sha, sha2, keccak, blake2, hash, utils diff --git a/libp2p/peer.nim b/libp2p/peer.nim index 0d70a1522..4f49983df 100644 --- a/libp2p/peer.nim +++ b/libp2p/peer.nim @@ -9,8 +9,8 @@ ## This module implementes API for libp2p peer. import hashes -import nimcrypto/utils -import crypto/crypto, multicodec, multihash, base58, vbuffer +import nimcrypto/utils, stew/base58 +import crypto/crypto, multicodec, multihash, vbuffer import protobuf/minprotobuf const diff --git a/tests/testbase32.nim b/tests/testbase32.nim deleted file mode 100644 index 4812833fc..000000000 --- a/tests/testbase32.nim +++ /dev/null @@ -1,409 +0,0 @@ -import unittest -import ../libp2p/base32 - -when defined(nimHasUsed): {.used.} - -const TVBaseUpperPadding = [ - ["f", "MY======"], - ["fo", "MZXQ===="], - ["foo", "MZXW6==="], - ["foob", "MZXW6YQ="], - ["fooba", "MZXW6YTB"], - ["foobar", "MZXW6YTBOI======"] -] - -const TVBaseUpperNoPadding = [ - ["f", "MY"], - ["fo", "MZXQ"], - ["foo", "MZXW6"], - ["foob", "MZXW6YQ"], - ["fooba", "MZXW6YTB"], - ["foobar", "MZXW6YTBOI"] -] - -const TVBaseLowerPadding = [ - ["f", "my======"], - ["fo", "mzxq===="], - ["foo", "mzxw6==="], - ["foob", "mzxw6yq="], - ["fooba", "mzxw6ytb"], - ["foobar", "mzxw6ytboi======"] -] - -const TVBaseLowerNoPadding = [ - ["f", "my"], - ["fo", "mzxq"], - ["foo", "mzxw6"], - ["foob", "mzxw6yq"], - ["fooba", "mzxw6ytb"], - ["foobar", "mzxw6ytboi"] -] - -const TVHexUpperPadding = [ - ["f", "CO======"], - ["fo", "CPNG===="], - ["foo", "CPNMU==="], - ["foob", "CPNMUOG="], - ["fooba", "CPNMUOJ1"], - ["foobar", "CPNMUOJ1E8======"] -] - -const TVHexUpperNoPadding = [ - ["f", "CO"], - ["fo", "CPNG"], - ["foo", "CPNMU"], - ["foob", "CPNMUOG"], - ["fooba", "CPNMUOJ1"], - ["foobar", "CPNMUOJ1E8"] -] - -const TVHexLowerPadding = [ - ["f", "co======"], - ["fo", "cpng===="], - ["foo", "cpnmu==="], - ["foob", "cpnmuog="], - ["fooba", "cpnmuoj1"], - ["foobar", "cpnmuoj1e8======"] -] - -const TVHexLowerNoPadding = [ - ["f", "co"], - ["fo", "cpng"], - ["foo", "cpnmu"], - ["foob", "cpnmuog"], - ["fooba", "cpnmuoj1"], - ["foobar", "cpnmuoj1e8"] -] - -suite "BASE32 encoding test suite": - test "Empty seq/string test": - var empty1 = newSeq[byte]() - var empty2 = "" - var encoded = newString(16) - var decoded = newSeq[byte](16) - - var o1, o2, o3, o4, o5, o6, o7, o8: int - var e1 = Base32Upper.encode(empty1) - var e2 = Base32Lower.encode(empty1) - var e3 = Base32UpperPad.encode(empty1) - var e4 = Base32LowerPad.encode(empty1) - var e5 = HexBase32Upper.encode(empty1) - var e6 = HexBase32Lower.encode(empty1) - var e7 = HexBase32UpperPad.encode(empty1) - var e8 = HexBase32LowerPad.encode(empty1) - check: - Base32Upper.encode(empty1, encoded, o1) == Base32Status.Success - Base32Lower.encode(empty1, encoded, o2) == Base32Status.Success - Base32UpperPad.encode(empty1, encoded, o3) == Base32Status.Success - Base32LowerPad.encode(empty1, encoded, o4) == Base32Status.Success - HexBase32Upper.encode(empty1, encoded, o5) == Base32Status.Success - HexBase32Lower.encode(empty1, encoded, o6) == Base32Status.Success - HexBase32UpperPad.encode(empty1, encoded, o7) == Base32Status.Success - HexBase32LowerPad.encode(empty1, encoded, o8) == Base32Status.Success - len(e1) == 0 - len(e2) == 0 - len(e3) == 0 - len(e4) == 0 - len(e5) == 0 - len(e6) == 0 - len(e7) == 0 - len(e8) == 0 - o1 == 0 - o2 == 0 - o3 == 0 - o4 == 0 - o5 == 0 - o6 == 0 - o7 == 0 - o8 == 0 - var d1 = Base32Upper.decode("") - var d2 = Base32Lower.decode("") - var d3 = Base32UpperPad.decode("") - var d4 = Base32LowerPad.decode("") - var d5 = HexBase32Upper.decode("") - var d6 = HexBase32Lower.decode("") - var d7 = HexBase32UpperPad.decode("") - var d8 = HexBase32LowerPad.decode("") - check: - Base32Upper.decode(empty2, decoded, o1) == Base32Status.Success - Base32Lower.decode(empty2, decoded, o2) == Base32Status.Success - Base32UpperPad.decode(empty2, decoded, o3) == Base32Status.Success - Base32LowerPad.decode(empty2, decoded, o4) == Base32Status.Success - HexBase32Upper.decode(empty2, decoded, o5) == Base32Status.Success - HexBase32Lower.decode(empty2, decoded, o6) == Base32Status.Success - HexBase32UpperPad.decode(empty2, decoded, o7) == Base32Status.Success - HexBase32LowerPad.decode(empty2, decoded, o8) == Base32Status.Success - len(d1) == 0 - len(d2) == 0 - len(d3) == 0 - len(d4) == 0 - len(d5) == 0 - len(d6) == 0 - len(d7) == 0 - len(d8) == 0 - o1 == 0 - o2 == 0 - o3 == 0 - o4 == 0 - o5 == 0 - o6 == 0 - o7 == 0 - o8 == 0 - - test "Zero test": - var s = newString(256) - for i in 0..255: - s[i] = 'A' - var buffer: array[256, byte] - for i in 0..255: - var a = Base32.encode(buffer.toOpenArray(0, i)) - var b = Base32.decode(a) - check b == buffer[0..i] - - test "Leading zero test": - var buffer: array[256, byte] - for i in 0..255: - buffer[255] = byte(i) - var a = Base32.encode(buffer) - var b = Base32.decode(a) - check: - equalMem(addr buffer[0], addr b[0], 256) == true - - test "BASE32 uppercase padding test vectors": - for item in TVBaseUpperPadding: - let plain = cast[seq[byte]](item[0]) - let expect = item[1] - var elen = 0 - var dlen = 0 - - var e1 = Base32UpperPad.encode(plain) - var e2 = newString(Base32UpperPad.encodedLength(len(plain))) - check: - Base32UpperPad.encode(plain, e2, elen) == Base32Status.Success - e2.setLen(elen) - check: - e1 == expect - e2 == expect - - var d1 = Base32UpperPad.decode(expect) - var d2 = newSeq[byte](Base32UpperPad.decodedLength(len(expect))) - check: - Base32UpperPad.decode(expect, d2, dlen) == Base32Status.Success - d2.setLen(dlen) - check: - d1 == plain - d2 == plain - - test "BASE32 lowercase padding test vectors": - for item in TVBaseLowerPadding: - let plain = cast[seq[byte]](item[0]) - let expect = item[1] - var elen = 0 - var dlen = 0 - - var e1 = Base32LowerPad.encode(plain) - var e2 = newString(Base32LowerPad.encodedLength(len(plain))) - check: - Base32LowerPad.encode(plain, e2, elen) == Base32Status.Success - e2.setLen(elen) - check: - e1 == expect - e2 == expect - - var d1 = Base32LowerPad.decode(expect) - var d2 = newSeq[byte](Base32LowerPad.decodedLength(len(expect))) - check: - Base32LowerPad.decode(expect, d2, dlen) == Base32Status.Success - d2.setLen(dlen) - check: - d1 == plain - d2 == plain - - test "BASE32 uppercase no-padding test vectors": - for item in TVBaseUpperNoPadding: - let plain = cast[seq[byte]](item[0]) - let expect = item[1] - var elen = 0 - var dlen = 0 - - var e1 = Base32Upper.encode(plain) - var e2 = newString(Base32Upper.encodedLength(len(plain))) - check: - Base32Upper.encode(plain, e2, elen) == Base32Status.Success - e2.setLen(elen) - check: - e1 == expect - e2 == expect - - var d1 = Base32Upper.decode(expect) - var d2 = newSeq[byte](Base32Upper.decodedLength(len(expect))) - check: - Base32Upper.decode(expect, d2, dlen) == Base32Status.Success - d2.setLen(dlen) - check: - d1 == plain - d2 == plain - - test "BASE32 lowercase no-padding test vectors": - for item in TVBaseLowerNoPadding: - let plain = cast[seq[byte]](item[0]) - let expect = item[1] - var elen = 0 - var dlen = 0 - - var e1 = Base32Lower.encode(plain) - var e2 = newString(Base32Lower.encodedLength(len(plain))) - check: - Base32Lower.encode(plain, e2, elen) == Base32Status.Success - e2.setLen(elen) - check: - e1 == expect - e2 == expect - - var d1 = Base32Lower.decode(expect) - var d2 = newSeq[byte](Base32Lower.decodedLength(len(expect))) - check: - Base32Lower.decode(expect, d2, dlen) == Base32Status.Success - d2.setLen(dlen) - check: - d1 == plain - d2 == plain - - test "HEX-BASE32 uppercase padding test vectors": - for item in TVHexUpperPadding: - let plain = cast[seq[byte]](item[0]) - let expect = item[1] - var elen = 0 - var dlen = 0 - - var e1 = HexBase32UpperPad.encode(plain) - var e2 = newString(HexBase32UpperPad.encodedLength(len(plain))) - check: - HexBase32UpperPad.encode(plain, e2, elen) == Base32Status.Success - e2.setLen(elen) - check: - e1 == expect - e2 == expect - - var d1 = HexBase32UpperPad.decode(expect) - var d2 = newSeq[byte](HexBase32UpperPad.decodedLength(len(expect))) - check: - HexBase32UpperPad.decode(expect, d2, dlen) == Base32Status.Success - d2.setLen(dlen) - check: - d1 == plain - d2 == plain - - test "HEX-BASE32 lowercase padding test vectors": - for item in TVHexLowerPadding: - let plain = cast[seq[byte]](item[0]) - let expect = item[1] - var elen = 0 - var dlen = 0 - - var e1 = HexBase32LowerPad.encode(plain) - var e2 = newString(HexBase32LowerPad.encodedLength(len(plain))) - check: - HexBase32LowerPad.encode(plain, e2, elen) == Base32Status.Success - e2.setLen(elen) - check: - e1 == expect - e2 == expect - - var d1 = HexBase32LowerPad.decode(expect) - var d2 = newSeq[byte](HexBase32LowerPad.decodedLength(len(expect))) - check: - HexBase32LowerPad.decode(expect, d2, dlen) == Base32Status.Success - d2.setLen(dlen) - check: - d1 == plain - d2 == plain - - test "HEX-BASE32 uppercase no-padding test vectors": - for item in TVHexUpperNoPadding: - let plain = cast[seq[byte]](item[0]) - let expect = item[1] - var elen = 0 - var dlen = 0 - - var e1 = HexBase32Upper.encode(plain) - var e2 = newString(HexBase32Upper.encodedLength(len(plain))) - check: - HexBase32Upper.encode(plain, e2, elen) == Base32Status.Success - e2.setLen(elen) - check: - e1 == expect - e2 == expect - - var d1 = HexBase32Upper.decode(expect) - var d2 = newSeq[byte](HexBase32Upper.decodedLength(len(expect))) - check: - HexBase32Upper.decode(expect, d2, dlen) == Base32Status.Success - d2.setLen(dlen) - check: - d1 == plain - d2 == plain - - test "HEX-BASE32 lowercase no-padding test vectors": - for item in TVHexLowerNoPadding: - let plain = cast[seq[byte]](item[0]) - let expect = item[1] - var elen = 0 - var dlen = 0 - - var e1 = HexBase32Lower.encode(plain) - var e2 = newString(HexBase32Lower.encodedLength(len(plain))) - check: - HexBase32Lower.encode(plain, e2, elen) == Base32Status.Success - e2.setLen(elen) - check: - e1 == expect - e2 == expect - - var d1 = HexBase32Lower.decode(expect) - var d2 = newSeq[byte](HexBase32Lower.decodedLength(len(expect))) - check: - HexBase32Lower.decode(expect, d2, dlen) == Base32Status.Success - d2.setLen(dlen) - check: - d1 == plain - d2 == plain - - test "Buffer Overrun test": - var encres = "" - var encsize = 0 - var decres: seq[byte] = @[] - var decsize = 0 - check: - Base32.encode([0'u8], encres, encsize) == Base32Status.Overrun - encsize == Base32.encodedLength(1) - Base32.decode("AA", decres, decsize) == Base32Status.Overrun - decsize == Base32.decodedLength(2) - - test "Incorrect test": - var decres = newSeq[byte](10) - var decsize = 0 - check: - Base32.decode("A", decres, decsize) == Base32Status.Incorrect - decsize == 0 - Base32.decode("AAA", decres, decsize) == Base32Status.Incorrect - decsize == 0 - Base32.decode("AAAAAA", decres, decsize) == Base32Status.Incorrect - decsize == 0 - Base32Upper.decode("aa", decres, decsize) == Base32Status.Incorrect - decsize == 0 - Base32Upper.decode("11", decres, decsize) == Base32Status.Incorrect - decsize == 0 - Base32Lower.decode("AA", decres, decsize) == Base32Status.Incorrect - decsize == 0 - Base32Lower.decode("11", decres, decsize) == Base32Status.Incorrect - decsize == 0 - HexBase32Upper.decode("aa", decres, decsize) == Base32Status.Incorrect - decsize == 0 - HexBase32Upper.decode("WW", decres, decsize) == Base32Status.Incorrect - decsize == 0 - HexBase32Lower.decode("AA", decres, decsize) == Base32Status.Incorrect - decsize == 0 - HexBase32Lower.decode("ww", decres, decsize) == Base32Status.Incorrect - decsize == 0 - diff --git a/tests/testbase58.nim b/tests/testbase58.nim deleted file mode 100644 index 32df60c5d..000000000 --- a/tests/testbase58.nim +++ /dev/null @@ -1,132 +0,0 @@ -import unittest -import ../libp2p/base58 - -when defined(nimHasUsed): {.used.} - -proc hexToBytes*(a: string, result: var openarray[byte]) = - doAssert(len(a) == 2 * len(result)) - var i = 0 - var k = 0 - var r = 0 - if len(a) > 0: - while i < len(a): - let c = a[i] - if i != 0 and i %% 2 == 0: - result[k] = r.byte - r = 0 - inc(k) - else: - r = r shl 4 - case c - of 'a'..'f': - r = r or (10 + ord(c) - ord('a')) - of 'A'..'F': - r = r or (10 + ord(c) - ord('A')) - of '0'..'9': - r = r or (ord(c) - ord('0')) - else: - doAssert(false) - inc(i) - result[k] = r.byte - -proc fromHex*(a: string): seq[byte] = - doAssert(len(a) %% 2 == 0) - if len(a) == 0: - result = newSeq[byte]() - else: - result = newSeq[byte](len(a) div 2) - hexToBytes(a, result) - -const TestVectors = [ - ["", ""], - ["61", "2g"], - ["626262", "a3gV"], - ["636363", "aPEr"], - ["73696d706c792061206c6f6e6720737472696e67", "2cFupjhnEsSn59qHXstmK2ffpLv2"], - ["00eb15231dfceb60925886b67d065299925915aeb172c06647", "1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L"], - ["516b6fcd0f", "ABnLTmg"], - ["bf4f89001e670274dd", "3SEo3LWLoPntC"], - ["572e4794", "3EFU7m"], - ["ecac89cad93923c02321", "EJDM8drfXA6uyA"], - ["10c8511e", "Rt5zm"], - ["00000000000000000000", "1111111111"], - ["000111d38e5fc9071ffcd20b4a763cc9ae4f252bb4e48fd66a835e252ada93ff480d6dd43dc62a641155a5", "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"], - ["000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", "1cWB5HCBdLjAuqGGReWE3R3CguuwSjw6RHn39s2yuDRTS5NsBgNiFpWgAnEx6VQi8csexkgYw3mdYrMHr8x9i7aEwP8kZ7vccXWqKDvGv3u1GxFKPuAkn8JCPPGDMf3vMMnbzm6Nh9zh1gcNsMvH3ZNLmP5fSG6DGbbi2tuwMWPthr4boWwCxf7ewSgNQeacyozhKDDQQ1qL5fQFUW52QKUZDZ5fw3KXNQJMcNTcaB723LchjeKun7MuGW5qyCBZYzA1KjofN1gYBV3NqyhQJ3Ns746GNuf9N2pQPmHz4xpnSrrfCvy6TVVz5d4PdrjeshsWQwpZsZGzvbdAdN8MKV5QsBDY"] -] - -suite "BASE58 encoding test suite": - test "Empty seq/string test": - var a = Base58.encode([]) - check len(a) == 0 - var b = Base58.decode("") - check len(b) == 0 - test "Zero test": - var s = newString(256) - for i in 0..255: - s[i] = '1' - var buffer: array[256, byte] - for i in 0..255: - var a = Base58.encode(buffer.toOpenArray(0, i)) - check a == s[0..i] - var b = Base58.decode(a) - check b == buffer[0..i] - test "Leading zero test": - var buffer: array[256, byte] - for i in 0..255: - buffer[255] = byte(i) - var a = Base58.encode(buffer) - var b = Base58.decode(a) - check: - equalMem(addr buffer[0], addr b[0], 256) == true - test "Small amount of bytes test": - var buffer1: array[1, byte] - var buffer2: array[2, byte] - for i in 0..255: - buffer1[0] = byte(i) - var enc = Base58.encode(buffer1) - var dec = Base58.decode(enc) - check: - len(dec) == 1 - dec[0] == buffer1[0] - - for i in 0..255: - for k in 0..255: - buffer2[0] = byte(i) - buffer2[1] = byte(k) - var enc = Base58.encode(buffer2) - var dec = Base58.decode(enc) - check: - len(dec) == 2 - dec[0] == buffer2[0] - dec[1] == buffer2[1] - test "Test Vectors test": - for item in TestVectors: - var a = fromHex(item[0]) - var enc = Base58.encode(a) - var dec = Base58.decode(item[1]) - check: - enc == item[1] - dec == a - test "Buffer Overrun test": - var encres = "" - var encsize = 0 - var decres: seq[byte] = @[] - var decsize = 0 - check: - Base58.encode([0'u8], encres, encsize) == Base58Status.Overrun - encsize == 1 - Base58.decode("1", decres, decsize) == Base58Status.Overrun - decsize == 5 - test "Incorrect test": - var decres = newSeq[byte](10) - var decsize = 0 - check: - Base58.decode("l", decres, decsize) == Base58Status.Incorrect - decsize == 0 - Base58.decode("2l", decres, decsize) == Base58Status.Incorrect - decsize == 0 - Base58.decode("O", decres, decsize) == Base58Status.Incorrect - decsize == 0 - Base58.decode("2O", decres, decsize) == Base58Status.Incorrect - decsize == 0 - diff --git a/tests/testbase64.nim b/tests/testbase64.nim deleted file mode 100644 index 22ea7d978..000000000 --- a/tests/testbase64.nim +++ /dev/null @@ -1,164 +0,0 @@ -import unittest -import ../libp2p/base64 - -when defined(nimHasUsed): {.used.} - -const TVBasePadding = [ - ["f", "Zg=="], - ["fo", "Zm8="], - ["foo", "Zm9v"], - ["foob", "Zm9vYg=="], - ["fooba", "Zm9vYmE="], - ["foobar", "Zm9vYmFy"] -] - -const TVBaseNoPadding = [ - ["f", "Zg"], - ["fo", "Zm8"], - ["foo", "Zm9v"], - ["foob", "Zm9vYg"], - ["fooba", "Zm9vYmE"], - ["foobar", "Zm9vYmFy"] -] - -suite "BASE64 encoding test suite": - test "Empty seq/string test": - var empty1 = newSeq[byte]() - var empty2 = "" - var encoded = newString(16) - var decoded = newSeq[byte](16) - - var o1, o2, o3, o4: int - var e1 = Base64.encode(empty1) - var e2 = Base64Url.encode(empty1) - var e3 = Base64Pad.encode(empty1) - var e4 = Base64UrlPad.encode(empty1) - check: - Base64.encode(empty1, encoded, o1) == Base64Status.Success - Base64Url.encode(empty1, encoded, o2) == Base64Status.Success - Base64Pad.encode(empty1, encoded, o3) == Base64Status.Success - Base64UrlPad.encode(empty1, encoded, o4) == Base64Status.Success - len(e1) == 0 - len(e2) == 0 - len(e3) == 0 - len(e4) == 0 - o1 == 0 - o2 == 0 - o3 == 0 - o4 == 0 - var d1 = Base64.decode("") - var d2 = Base64Url.decode("") - var d3 = Base64Pad.decode("") - var d4 = Base64UrlPad.decode("") - check: - Base64.decode(empty2, decoded, o1) == Base64Status.Success - Base64Url.decode(empty2, decoded, o2) == Base64Status.Success - Base64Pad.decode(empty2, decoded, o3) == Base64Status.Success - Base64UrlPad.decode(empty2, decoded, o4) == Base64Status.Success - len(d1) == 0 - len(d2) == 0 - len(d3) == 0 - len(d4) == 0 - o1 == 0 - o2 == 0 - o3 == 0 - o4 == 0 - - test "Zero test": - var s = newString(256) - for i in 0..255: - s[i] = 'A' - var buffer: array[256, byte] - for i in 0..255: - var a = Base64.encode(buffer.toOpenArray(0, i)) - var b = Base64.decode(a) - check b == buffer[0..i] - - test "Leading zero test": - var buffer: array[256, byte] - for i in 0..255: - buffer[255] = byte(i) - var a = Base64.encode(buffer) - var b = Base64.decode(a) - check: - equalMem(addr buffer[0], addr b[0], 256) == true - - test "BASE64 padding test vectors": - for item in TVBasePadding: - let plain = cast[seq[byte]](item[0]) - let expect = item[1] - var elen = 0 - var dlen = 0 - - var e1 = Base64Pad.encode(plain) - var e2 = newString(Base64Pad.encodedLength(len(plain))) - check: - Base64Pad.encode(plain, e2, elen) == Base64Status.Success - e2.setLen(elen) - check: - e1 == expect - e2 == expect - - var d1 = Base64Pad.decode(expect) - var d2 = newSeq[byte](Base64Pad.decodedLength(len(expect))) - check: - Base64Pad.decode(expect, d2, dlen) == Base64Status.Success - d2.setLen(dlen) - check: - d1 == plain - d2 == plain - - test "BASE64 no padding test vectors": - for item in TVBaseNoPadding: - let plain = cast[seq[byte]](item[0]) - let expect = item[1] - var elen = 0 - var dlen = 0 - - var e1 = Base64.encode(plain) - var e2 = newString(Base64.encodedLength(len(plain))) - check: - Base64.encode(plain, e2, elen) == Base64Status.Success - e2.setLen(elen) - check: - e1 == expect - e2 == expect - - var d1 = Base64.decode(expect) - var d2 = newSeq[byte](Base64.decodedLength(len(expect))) - check: - Base64.decode(expect, d2, dlen) == Base64Status.Success - d2.setLen(dlen) - check: - d1 == plain - d2 == plain - - test "Buffer Overrun test": - var encres = "" - var encsize = 0 - var decres: seq[byte] = @[] - var decsize = 0 - check: - Base64.encode([0'u8], encres, encsize) == Base64Status.Overrun - encsize == Base64.encodedLength(1) - Base64.decode("AA", decres, decsize) == Base64Status.Overrun - decsize == Base64.decodedLength(2) - - test "Incorrect test": - var decres = newSeq[byte](10) - var decsize = 0 - check: - Base64.decode("A", decres, decsize) == Base64Status.Incorrect - decsize == 0 - Base64.decode("AAAAA", decres, decsize) == Base64Status.Incorrect - decsize == 0 - Base64.decode("!", decres, decsize) == Base64Status.Incorrect - decsize == 0 - Base64.decode("!!", decres, decsize) == Base64Status.Incorrect - decsize == 0 - Base64.decode("AA==", decres, decsize) == Base64Status.Incorrect - decsize == 0 - Base64.decode("_-", decres, decsize) == Base64Status.Incorrect - decsize == 0 - Base64Url.decode("/+", decres, decsize) == Base64Status.Incorrect - decsize == 0 diff --git a/tests/testnative.nim b/tests/testnative.nim index 503774d23..d8790af6a 100644 --- a/tests/testnative.nim +++ b/tests/testnative.nim @@ -1,4 +1,4 @@ -import testvarint, testbase32, testbase58, testbase64 +import testvarint import testrsa, testecnist, tested25519, testsecp256k1, testcrypto import testmultibase, testmultihash, testmultiaddress, testcid, testpeer diff --git a/tests/testpeer.nim b/tests/testpeer.nim index fb5b88bff..1f4e68ac7 100644 --- a/tests/testpeer.nim +++ b/tests/testpeer.nim @@ -10,8 +10,8 @@ ## Test vectors was made using Go implementation ## https://github.com/libp2p/go-libp2p-peer import unittest -import nimcrypto/utils -import ../libp2p/crypto/crypto, ../libp2p/peer, ../libp2p/base58 +import nimcrypto/utils, stew/base58 +import ../libp2p/crypto/crypto, ../libp2p/peer when defined(nimHasUsed): {.used.}