tests/testthat/test-utils.R

context("Check that the functions for signing a transaction are working as expected.")

test_that("The .bit function produces the same outputs as the Python SDK's functions.", {

  python_expected_bit_sequence = c(1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1,
                                   0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1,
                                   1, 0)
  test_hash = stellaRbase:::.hash("SO12C4")

  for (i in 0:(length(python_expected_bit_sequence) - 1)) {
    expect_equal(stellaRbase:::.bit(test_hash, i),
                 python_expected_bit_sequence[i + 1])
  }
})


test_that(
  "The hashing functions are consistent with those produced by hashlib.digest() in Python 2.7.",
  {
    py_hashes = c(
      "?\x1e`e\xde\xd2+{\x83\x96&\x8c^\x0b\x17\xca\x18\xbei\x13=%\xbb\xcd\xf2|#\xdd\xe4\xbdF1\xf3\xbe0\xc0\xa1\xcb\xe2\xd3\xa8t\xbb}a\xf5}\xf5\xf1MB\xa4\xd4\x8b4\x9a\x8a\xfd\xdb^\xfa\x0e\xa7 ",
      "\x88d2\x80\x9d<\xd6P]8\xcar\x94\xe4\x89#~1I)\xeaF},\xa2\xfd\xfb@\xfd\xe1\xd9\xccVZ;jP\xed_\x8f\x1d\x1c\xc2\x1cILp\x05\x86v\x88\xf1\x0f\xa6\xd6Rf7\xc9\xeb\xb8[\xa4\x98",
      "\xcf\xdc\x88{W\xd8Q\xb4\x9c\xcc!D\xbb\x10'FOF\xba0\x11p\x90\x10\xc2\xaa\x94\xb6\xd0\xb2\xbc\x1dku\xe4{v\x9a0\xb7\xfc\xf7\xb9l\x13\xcfv\xab\xfcl\xe5\x19XW\xc6`\x92\x08=\xe8^\xb9\x1cj"
    )

    sks = c("Oui", "weeeeeeeeeeeeeeeee", "WEEEEEEEeEEEEeEEEE")

    for (hash in 1:3) {
      py_hash_raw = charToRaw(py_hashes[hash])
      r_hash_raw = stellaRbase:::.hash(sks[hash])

      expect_true(all(py_hash_raw == r_hash_raw))
      expect_length(r_hash_raw, 64)
      expect_equal(length(r_hash_raw), length(py_hash_raw))
    }

  }
)

test_that("The .hint function works as expected.", {
  python_expected_hints = c(
    "1149576597304348905430829758847045164739019033864025442941878553162293532990451185819152297343395787636361014526862184370545617082586934477722038696416566",
    "10964187330039060450303790117751371053898523928713975255596099016379749236593565932551113363914510535663502366716891775783884962461105390333038942397423533",
    "10168841401341733583236732730437046933163901820479663723665062955135008041778324090826950037345480019051595789447674890883182337679092417708296010754484919"
  )

  r_hints = c("Hello", "Is it me you're looking for?", "Lionel")

  for (hint in 1:3) {
    expect_equal(stellaRbase:::.hint(r_hints[hint]),
                 as.bigz(python_expected_hints[hint]))
  }

})

test_that("The statistical/math functions handle large numbers as expected.",
          {
            expect_true(is.bigz(stellaRbase:::.inv(50)))
            expect_equal(stellaRbase:::.inv(1), 1)
            expect_equal(
              stellaRbase:::.inv(2),
              as.bigz(
                "28948022309329048855892746252171976963317496166410141009864396001978282409975"
              )
            )
            expect_equal(stellaRbase:::.inv(stellaRbase:::.inv(2)), 2)
            expect_equal(
              stellaRbase:::.inv(-1),
              as.bigz(
                "57896044618658097711785492504343953926634992332820282019728792003956564819948"
              )
            )

            expect_true(is.bigz(stellaRbase:::.square(12)))
            expect_equal(stellaRbase:::.square(3), 9)
            expect_equal(stellaRbase:::.square(3), as.bigz(9))
            expect_equal(
              stellaRbase:::.square(stellaRbase:::.inv(2)),
              as.bigz(
                "43422033463993573283839119378257965444976244249615211514796594002967423614962"
              )
            )
            expect_equal(stellaRbase:::.square(stellaRbase:::.inv(-1)), 1)

            expect_true(is.bigz(stellaRbase:::.mult(12, 90)))
            expect_equal(stellaRbase:::.mult(1, 9), 9)
            expect_equal(stellaRbase:::.mult(3, 3), as.bigz(9))
            expect_equal(
              stellaRbase:::.mult(stellaRbase:::.inv(15), 3),
              as.bigz(
                "11579208923731619542357098500868790785326998466564056403945758400791312963990"
              )
            )
            expect_equal(
              stellaRbase:::.mult(stellaRbase:::.inv(12),-1),
              as.bigz(
                "4824670384888174809315457708695329493886249361068356834977399333663047068329"
              )
            )

            expect_true(is.bigz(stellaRbase:::.add(123, 456)))
            expect_equal(stellaRbase:::.add(5, 6), 11)
            expect_equal(stellaRbase:::.add(3, 9), as.bigz(12))
            expect_equal(
              stellaRbase:::.add(stellaRbase:::.inv(2), stellaRbase:::.inv(3)),
              as.bigz(
                "9649340769776349618630915417390658987772498722136713669954798667326094136659"
              )
            )
            expect_equal(
              stellaRbase:::.add(stellaRbase:::.inv(12),-1),
              as.bigz(
                "53071374233769922902470034795648624432748742971751925184751392670293517751619"
              )
            )

          })

test_that("The EDWARDS DOUBLE function produces consistent results.", {
  x = c(0, 2, 1)
  y = c(1, 5, 0)
  z = c(1, 3, 7)

  python_edwards_double_expected_x = c("0",
                                       "60",
                                       "0")
  python_edwards_double_expected_y = c(
    "57896044618658097711785492504343953926634992332820282019728792003956564819948",
    "57896044618658097711785492504343953926634992332820282019728792003956564819340",
    "1"
  )
  python_edwards_double_expected_z = c(
    "57896044618658097711785492504343953926634992332820282019728792003956564819948",
    "63",
    "99"
  )

  for (i in 1:3) {
    result = stellaRbase:::.edwards_double(x[i], y[i], z[i])
    expect_equal(as.bigz(python_edwards_double_expected_x[i]), result[1])
    expect_equal(as.bigz(python_edwards_double_expected_y[i]), result[2])
    expect_equal(as.bigz(python_edwards_double_expected_z[i]), result[3])
  }

  python_edwards_double_expected_x2 = c(
    "0",
    "57896044618658097711785492504343953926634992332820282019728792003930304033509",
    "0"
  )
  python_edwards_double_expected_y2 = c(
    "57896044618658097711785492504343953926634992332820282019728792003956564819948",
    "57896044618658097711785492504343953926634992332820282019728792003819025063788",
    "57896044618658097711785492504343953926634992332820282019728792003956564819948"
  )
  python_edwards_double_expected_z2 = c(
    "57896044618658097711785492504343953926634992332820282019728792003956564819948",
    "131979856383",
    "57896044618658097711785492504343953926634992332820282019728792003956564800348"
  )

  for (i in 1:3) {
    result = stellaRbase:::.edwards_double(
      as.bigz(python_edwards_double_expected_x[i]),
      as.bigz(python_edwards_double_expected_y[i]),
      as.bigz(python_edwards_double_expected_z[i])
    )

    expect_equal(as.bigz(python_edwards_double_expected_x2[i]), result[1])
    expect_equal(as.bigz(python_edwards_double_expected_y2[i]), result[2])
    expect_equal(as.bigz(python_edwards_double_expected_z2[i]), result[3])
  }

})

test_that("The EDWARDS ADD B function produces consistent results.", {
  x = c(6, 1, 9)
  y = c(4, 3, 9)
  z = c(2, 5, 19)

  python_edwards_add_b_expected_x = c(
    "8587363414700046631075038461983202388398796509648204944093017152558846010252",
    "6976320831692791835967166004935920001302844156187496664686650277120988377474",
    "51256063240319703810541875860135730837824371505891899429778482843756215123755"
  )
  python_edwards_add_b_expected_y = c(
    "42527070347955995548142907057918196881316743613731396346935582533871154594550",
    "44341564563393573957277922026339196458738130180275591675426772856941466679425",
    "10619938994668469354403305273488868914208814761166274867245935870464253173834"
  )
  python_edwards_add_b_expected_z = c(
    "33162392189179885969134062993126414866651928313978947068288352992416369927742",
    "24038430504285787913680576064157331514986902015114160618456827267113860238850",
    "39410362789593880113921214458820588581683884506497920896931892531121538171748"
  )

  for (i in 1:3) {
    result = stellaRbase:::.edwards_add_b(x[i], y[i], z[i])
    expect_equal(as.bigz(python_edwards_add_b_expected_x[i]), result[1])
    expect_equal(as.bigz(python_edwards_add_b_expected_y[i]), result[2])
    expect_equal(as.bigz(python_edwards_add_b_expected_z[i]), result[3])
  }

  python_edwards_add_b_expected_x2 = c(
    "56848010651172740898747064653959056962555624439364531916931638098246264621334",
    "49495822689067511696663407279144330335491256854127685356005837585419032182780",
    "11295163045440149129074911188942648867901238955410304580572375602430524340954"
  )
  python_edwards_add_b_expected_y2 = c(
    "12608845036404446302482021286307036545364524599434092029934833131901886373676",
    "17258650546169903960399724902955770454691286385603468047828547699975628221475",
    "26820102810984664620700833487863162045604825195509681623808158783142253751821"
  )
  python_edwards_add_b_expected_z2 = c(
    "16465709467169865234060768112347314011160759778043292402707362500124412376976",
    "31584901077884869386045117716847542132773546058944200928351112836989142413447",
    "28381857181050850456357323399883414782969382088539709422853683273001439706535"
  )

  for (i in 1:3) {
    result = stellaRbase:::.edwards_add_b(
      as.bigz(python_edwards_add_b_expected_x[i]),
      as.bigz(python_edwards_add_b_expected_y[i]),
      as.bigz(python_edwards_add_b_expected_z[i])
    )

    expect_equal(as.bigz(python_edwards_add_b_expected_x2[i]), result[1])
    expect_equal(as.bigz(python_edwards_add_b_expected_y2[i]), result[2])
    expect_equal(as.bigz(python_edwards_add_b_expected_z2[i]), result[3])
  }

})

test_that("The scalar multiple and encoding functions return the correct results.",
          {
            python_scalar_multiply_expected = c(
              as.bigz(
                "10593080468914978578954316149578855170502344604886137564370015851276669104055"
              ),
              as.bigz(
                "55796284422491918605008127743680813251749702717113703981918900034647788902482"
              )
            )

            python_final_expected = c(
              'RX\xb8}w?\x14aX\x1c\xecggS\xd8\xaa\x12"\xe1\xab\'\xa1\xe2f\xf3Ct\xa7\xed\x93[\xfb',
              ")DM\xe9\x94[]\xd7\xf7\xc1\xd6\xfb\xed}\x84\x8d(v\xce\xdbO\xdb8\x8b\xaf\\\xcd\x90~\x85\xb8E"
            )

            scalar_multiply_result = stellaRbase:::.scalar_multiply(79)

            expect_equal(python_scalar_multiply_expected[1], scalar_multiply_result[1])
            expect_equal(python_scalar_multiply_expected[2], scalar_multiply_result[2])

            encode_point_result = stellaRbase:::.encode_point(scalar_multiply_result[1],
                                                              scalar_multiply_result[2])

            expect_true(all(
              charToRaw(python_final_expected[1]) == charToRaw(encode_point_result)
            ))
            expect_true(all(
              charToRaw(python_final_expected[2]) == charToRaw(stellaRbase:::.multiply_and_encode(90210))
            ))

          })

test_that("The full end to end process of signing a hash using a secret key runs.",
          {
            python_signature_expected = '\x14\xbc\xac\n\x9ac\xbd\xd6&\xc0%\x1c"C\xc2pR\xcdd\x0e\xa7\xca\x96%\xcc\x80\x0c\xdf\x92/^\x82\xbf\x8a\xcd\xb7\x1c\x8a\xe0\xe4<v\x862\xe9]\tQB\xceD\x8d;q?\x02\x8f\x19Kdr\xaf\x17\t'

            signing_hash = stellaRbase:::.sign(sk = "SuPeRsEcReTbEePbOoP",
                                               m = "MyTrAnSaCt1oNhA5H")

            expect_true(all(
              charToRaw(python_signature_expected) == charToRaw(signing_hash)
            ))

          })
froocpu/stellaR documentation built on May 17, 2019, 7:05 p.m.