def blackbox_pyteal_example1(): # Example 1: Using blackbox_pyteal for a simple test of both an app and logic sig: from graviton.blackbox import DryRunEncoder from pyteal import Int, Mode, Subroutine, TealType from tests.blackbox import Blackbox @Blackbox(input_types=[TealType.uint64]) @Subroutine(TealType.uint64) def square(x): return x**Int(2) # provide args for evaluation (will compute x^2) x = 9 args = [x] # evaluate the programs app_result = PyTealDryRunExecutor(square, Mode.Application).dryrun(args) lsig_result = PyTealDryRunExecutor(square, Mode.Signature).dryrun(args) # check to see that x^2 is at the top of the stack as expected assert app_result.stack_top() == x**2, app_result.report( args, "stack_top() gave unexpected results for app") assert lsig_result.stack_top() == x**2, lsig_result.report( args, "stack_top() gave unexpected results for lsig") # check to see that itob of x^2 has been logged (only for the app case) assert app_result.last_log() == DryRunEncoder.hex(x**2), app_result.report( args, "last_log() gave unexpected results from app")
def test_decompress(): @Blackbox(input_types=[]) @Subroutine(TealType.uint64) def decompress(): return EcdsaDecompress( EcdsaCurve.Secp256k1, Bytes( "base16", "03bd83d54f6a799d05b496653b64bc933e17a898cda4793fe662d50645ecc977d1", ), ).outputReducer(lambda x, y: And( x == Bytes( "base16", "bd83d54f6a799d05b496653b64bc933e17a898cda4793fe662d50645ecc977d1", ), y == Bytes( "base16", "d4f3063a1ffca4139ea921b5696a6597640289175afece3bc38217a29d6270f9", ), )) args = [] app_result = PyTealDryRunExecutor(decompress, Mode.Application).dryrun( args, compiler_version=5) assert app_result.stack_top() == 1, app_result.report( args, "stack_top() is not equal to 1, indicating ecdsa verification failed.")
def test_recover(): @Blackbox(input_types=[]) @Subroutine(TealType.uint64) def recover(): return EcdsaRecover( EcdsaCurve.Secp256k1, Sha512_256(Bytes("testdata")), Int(1), Bytes( "base16", "cabed943e1403fb93b388174c59a52c759b321855f2d7c4fcc23c99a8a6dce79", ), Bytes( "base16", "56192820dde344c32f81450db05e51c6a6f45a2a2db229f657d2c040baf31537", ), ).outputReducer(lambda x, y: And( x == Bytes( "base16", "71539e0c7a6902a3f5413d6e28a455b2a14316fcf0f6b21193343b3b9d455053", ), y == Bytes( "base16", "fa49ccd95795c7c9a447fdeee83a2193472507a4e41a47e0d50eeeb547b74c51", ), )) args = [] app_result = PyTealDryRunExecutor(recover, Mode.Application).dryrun( args, compiler_version=5) assert app_result.stack_top() == 1, app_result.report( args, "stack_top() is not equal to 1, indicating ecdsa verification failed.")
def blackbox_pyteal_while_continue_test(): from tests.blackbox import Blackbox from pyteal import ( Continue, Int, Mode, Return, ScratchVar, Seq, Subroutine, TealType, While, ) @Blackbox(input_types=[TealType.uint64]) @Subroutine(TealType.uint64) def while_continue_accumulation(n): i = ScratchVar(TealType.uint64) return Seq( i.store(Int(0)), While(i.load() < n).Do( Seq( i.store(i.load() + Int(1)), Continue(), )), Return(i.load()), ) for x in range(30): args = [x] lsig_result = PyTealDryRunExecutor(while_continue_accumulation, Mode.Signature).dryrun(args) if x == 0: assert not lsig_result.passed() else: assert lsig_result.passed() assert lsig_result.stack_top() == x, lsig_result.report( args, "stack_top() gave unexpected results for lsig")
def test_verify(): @Blackbox(input_types=[]) @Subroutine(TealType.uint64) def verify(): return EcdsaVerify( EcdsaCurve.Secp256k1, Sha512_256(Bytes("testdata")), Bytes( "base16", "33602297203d2753372cea7794ffe1756a278cbc4907b15a0dd132c9fb82555e", ), Bytes( "base16", "20f112126cf3e2eac6e8d4f97a403d21bab07b8dbb77154511bb7b07c0173195", ), ( Bytes( "base16", "d6143a58c90c06b594e4414cb788659c2805e0056b1dfceea32c03f59efec517", ), Bytes( "base16", "00bd2400c479efe5ea556f37e1dc11ccb20f1e642dbfe00ca346fffeae508298", ), ), ) args = [] app_result = PyTealDryRunExecutor(verify, Mode.Application).dryrun( args, compiler_version=5) assert app_result.stack_top() == 1, app_result.report( args, "stack_top() is not equal to 1, indicating ecdsa verification failed.") @Blackbox(input_types=[]) @Subroutine(TealType.uint64) def verify_fail(): return EcdsaVerify( EcdsaCurve.Secp256k1, Sha512_256(Bytes("testdata")), Bytes( "base16", "13602297203d2753372cea7794ffe1756a278cbc4907b15a0dd132c9fb82555e", ), Bytes( "base16", "20f112126cf3e2eac6e8d4f97a403d21bab07b8dbb77154511bb7b07c0173195", ), ( Bytes( "base16", "d6143a58c90c06b594e4414cb788659c2805e0056b1dfceea32c03f59efec517", ), Bytes( "base16", "00bd2400c479efe5ea556f37e1dc11ccb20f1e642dbfe00ca346fffeae508298", ), ), ) args = [] app_result = PyTealDryRunExecutor(verify_fail, Mode.Application).dryrun( args, compiler_version=5) assert app_result.stack_top() == 0, app_result.report( args, "stack_top() is not equal to 0, indicating ecdsa verification succeeded when a failure was expected.", )