Class: Tem::Benchmarks

Inherits:
Object
  • Object
show all
Defined in:
lib/tem/benchmarks/vm_perf.rb,
lib/tem/benchmarks/blank_sec.rb,
lib/tem/benchmarks/benchmarks.rb,
lib/tem/benchmarks/post_buffer.rb,
lib/tem/benchmarks/simple_apdu.rb,
lib/tem/benchmarks/vm_perf_bound.rb,
lib/tem/benchmarks/devchip_decrypt.rb,
lib/tem/benchmarks/blank_bound_secpack.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#timingObject (readonly)

Returns the value of attribute timing.



19
20
21
# File 'lib/tem/benchmarks/benchmarks.rb', line 19

def timing
  @timing
end

Class Method Details

.all_benchmarksObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tem/benchmarks/benchmarks.rb', line 63

def self.all_benchmarks
  benchmarks = {}
  t = Tem::Benchmarks.new
  t.setup
  t.methods.select { |m| m =~ /time_/ }.sort.each do |m|
    print "Timing: #{m[5..-1]}...\n"
    t.send m.to_sym
    benchmarks[m] = t.timing
  end
  t.teardown
  benchmarks
end

.display_all_benchmarksObject



76
77
78
79
80
81
# File 'lib/tem/benchmarks/benchmarks.rb', line 76

def self.display_all_benchmarks
  benchmarks = Tem::Benchmarks.all_benchmarks
  benchmarks.map { |k, v| [k.to_s, v] }.sort.each do |benchmark|
    print "#{benchmark.first}: #{'%.5f' % benchmark.last}s\n"
  end    
end

Instance Method Details

#blank_seclosureObject

The SEClosure used in the blank benchmark.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tem/benchmarks/blank_sec.rb', line 17

def blank_seclosure
  @tem.assemble { |s|
    s.ldbc 0
    s.outnew
    s.halt
    s.label :secret
    s.zeros :tem_ubyte, 50
    s.label :plain
    s.zeros :tem_ubyte, 220
    s.stack 1
  }
end

#blank_seclosure_opcountObject

Number of opcodes executed by the blank SEClosure.



31
32
33
# File 'lib/tem/benchmarks/blank_sec.rb', line 31

def blank_seclosure_opcount
  3
end

#blank_seclosure_outcountObject

Number of bytes output by the blank SEClosure.



36
37
38
# File 'lib/tem/benchmarks/blank_sec.rb', line 36

def blank_seclosure_outcount
  0
end

#do_timingObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tem/benchmarks/benchmarks.rb', line 34

def do_timing
  @tem.flush_buffers
  
  n = 10
  loop do
    timings = (0...3).map do |i|
      t_start = Time.now
      n.times do
        yield
      end
      t_delta = Time.now - t_start
    end
    avg_time = timings.inject { |a,v| a + v } / timings.length
    max_diff = timings.map { |t| (t - avg_time).abs }.max
    uncertainty = max_diff / avg_time
    print "%8d: %3.8fs per run, %3.8fs uncertainty (%2.5f%%)\n" %
        [n, avg_time / n, max_diff / n, 100 * uncertainty]
    
    if max_diff / avg_time >= 0.01
      n *= 2
      next
    end
    
    @timing = avg_time / n
    break
  end
  @timing
end

#setupObject



21
22
23
24
25
26
27
# File 'lib/tem/benchmarks/benchmarks.rb', line 21

def setup
  @tem = Tem.auto_tem
  
  @tem.kill
  @tem.activate
  @tem.emit
end

#teardownObject



29
30
31
32
# File 'lib/tem/benchmarks/benchmarks.rb', line 29

def teardown
  @tem.kill
  @tem.disconnect if @tem
end

#time_blank_bound_secpack_3desObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tem/benchmarks/blank_bound_secpack.rb', line 21

def time_blank_bound_secpack_3des
  key = Tem::Keys::Symmetric.generate
  authz = [1] * 20
  key_id = @tem.tk_post_key key, authz
  
  secpack = blank_seclosure
  secpack.bind key, :secret, :plain
  print "3DES-bound SECpack has #{secpack.body.length} bytes, " +
        "executes #{blank_seclosure_opcount} instructions and produces " +
        "#{blank_seclosure_outcount} bytes\n"
  do_timing { @tem.execute secpack, key_id }

  @tem.release_key key_id
end

#time_blank_bound_secpack_rsaObject



12
13
14
15
16
17
18
19
# File 'lib/tem/benchmarks/blank_bound_secpack.rb', line 12

def time_blank_bound_secpack_rsa
  secpack = blank_seclosure
  secpack.bind @tem.pubek, :secret, :plain
  print "RSA-bound SECpack has #{secpack.body.length} bytes, " +
        "executes #{blank_seclosure_opcount} instructions and produces " +
        "#{blank_seclosure_outcount} bytes\n"
  do_timing { @tem.execute secpack }
end

#time_blank_secObject



40
41
42
43
44
45
46
# File 'lib/tem/benchmarks/blank_sec.rb', line 40

def time_blank_sec
  secpack = blank_seclosure
  print "SECpack has #{secpack.body.length} bytes, " +
        "executes #{blank_seclosure_opcount} instructions and produces " +
        "#{blank_seclosure_outcount} bytes\n"
  do_timing { @tem.execute secpack }
end

#time_devchip_decrypt_3desObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/tem/benchmarks/devchip_decrypt.rb', line 22

def time_devchip_decrypt_3des
  key = Tem::Keys::Symmetric.generate
  authz = [1] * 20
  key_id = @tem.tk_post_key key, authz
  data = (1...23).map { |i| (i * i * 217 + i * 661 + 393) % 256 }
  encrypted_data = key.encrypt data
  print "3DES-encrypted blob has #{encrypted_data.length} bytes\n"
  do_timing { @tem.devchip_decrypt encrypted_data, key_id }
  @tem.release_key key_id
end

#time_devchip_decrypt_3des_longObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/tem/benchmarks/devchip_decrypt.rb', line 33

def time_devchip_decrypt_3des_long
  key = Tem::Keys::Symmetric.generate
  authz = [1] * 20
  key_id = @tem.tk_post_key key, authz
  data = (1...120).map { |i| (i * i * 217 + i * 661 + 393) % 256 }
  encrypted_data = key.encrypt data
  print "3DES-encrypted blob has #{encrypted_data.length} bytes\n"
  do_timing { @tem.devchip_decrypt encrypted_data, key_id }
  @tem.release_key key_id
end

#time_devchip_decrypt_rsa_longObject



14
15
16
17
18
19
20
# File 'lib/tem/benchmarks/devchip_decrypt.rb', line 14

def time_devchip_decrypt_rsa_long
  pubek = @tem.pubek
  data = (1...120).map { |i| (i * i * 217 + i * 661 + 393) % 256 }
  encrypted_data = pubek.encrypt data
  print "RSA-encrypted blob has #{encrypted_data.length} bytes\n"
  do_timing { @tem.devchip_decrypt encrypted_data, 0 }
end

#time_post_bufferObject



10
11
12
13
14
15
16
17
# File 'lib/tem/benchmarks/post_buffer.rb', line 10

def time_post_buffer
  data = (0...490).map { |i| (39 * i * i + 91 * i + 17) % 256 }
  p @tem.stat_buffers
  do_timing do
    buffer_id = @tem.post_buffer(data)
    @tem.release_buffer buffer_id
  end
end

#time_simple_apduObject



13
14
15
# File 'lib/tem/benchmarks/simple_apdu.rb', line 13

def time_simple_apdu
  do_timing { @tem.fw_version }
end

#time_vm_perfObject



161
162
163
164
165
166
167
# File 'lib/tem/benchmarks/vm_perf.rb', line 161

def time_vm_perf
  secpack = vm_perf_seclosure
  print "SECpack has #{secpack.body.length} bytes, " +
        "executes #{vm_perf_seclosure_opcount} instructions and produces " +
        "#{vm_perf_seclosure_outcount} bytes\n"
  do_timing { @tem.execute secpack }
end

#time_vm_perf_bound_3desObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tem/benchmarks/vm_perf_bound.rb', line 23

def time_vm_perf_bound_3des
  key = Tem::Keys::Symmetric.generate
  authz = [1] * 20
  key_id = @tem.tk_post_key key, authz
  
  secpack = vm_perf_seclosure
  secpack.bind key, :done, :stack
  print "3DES-bound SECpack has #{secpack.body.length} bytes, " +
        "executes #{vm_perf_seclosure_opcount} instructions and produces " +
        "#{vm_perf_seclosure_outcount} bytes\n"
  do_timing { @tem.execute secpack, key_id }

  @tem.release_key key_id    
end

#time_vm_perf_bound_rsaObject



14
15
16
17
18
19
20
21
# File 'lib/tem/benchmarks/vm_perf_bound.rb', line 14

def time_vm_perf_bound_rsa
  secpack = vm_perf_seclosure
  secpack.bind @tem.pubek, :done, :stack
  print "RSA-bound SECpack has #{secpack.body.length} bytes, " +
        "executes #{vm_perf_seclosure_opcount} instructions and produces " +
        "#{vm_perf_seclosure_outcount} bytes\n"
  do_timing { @tem.execute secpack }
end

#vm_perf_seclosureObject

The SEClosure used in the vm_perf benchmark.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/tem/benchmarks/vm_perf.rb', line 14

def vm_perf_seclosure
  @tem.assemble { |s|
    s.ldwc 48 * 10
    s.outnew

    s.ldwc 10 # number of times to loop (4 instructions in loop)
    s.label :main_loop      

    # arithmetic (18 instructions, 10 bytes out)
    s.ldwc 0x1234
    s.ldwc 0x5678
    s.dupn :n => 2
    s.add
    s.outw
    s.sub
    s.outw
    s.ldwc 0x0155
    s.ldwc 0x02AA
    s.mul
    s.outw
    s.ldwc 0x390C
    s.ldwc 0x00AA
    s.dupn :n => 2
    s.div
    s.outw
    s.mod
    s.outw
    
    # memory (28 instructions, 16 bytes out)
    s.ldwc 0x55AA
    s.stw :clobber
    s.ldb :clobber
    s.outw
    s.ldw :clobber
    s.outw
    s.ldbc 0xA5 - (1 << 8)
    s.stb :clobber
    s.ldw :clobber
    s.outw
    s.ldwc :clobber2
    s.dupn :n => 1
    s.dupn :n => 2
    s.ldwc 0x9966 - (1 << 16)
    s.stwv
    s.ldbv
    s.outw
    s.ldbc 0x98 - (1 << 8)
    s.stbv
    s.ldwv
    s.outw
    s.ldwc 0x1122
    s.ldwc 0x3344
    s.ldwc 0x5566
    s.flipn :n => 3
    s.outw
    s.outw
    s.outw
    
    # memory comparisons (22 instructions, 16 bytes out)
    s.ldwc :const => 6
    s.ldwc :cmp_med
    s.ldwc :cmp_lo
    s.mcmpvb
    s.outw
    s.mcmpfxb :size => 6, :op1 => :cmp_med, :op2 => :cmp_hi
    s.outw
    s.ldwc :const => 4
    s.ldwc :cmp_lo
    s.ldwc :cmp_med
    s.mcmpvb
    s.outw            
    s.mcfxb :size => 6, :from => :cmp_hi, :to => :copy_buf
    s.pop
    s.outfxb :size => 6, :from => :copy_buf      
    s.ldwc :const => 4
    s.ldwc :cmp_hi
    s.ldwc :copy_buf2
    s.mcvb
    s.pop
    s.outfxb :size => 4, :from => :copy_buf2            
    
    # jumps (30 instructions, 6 bytes) from 6 * (5 instructions, 1 byte)
    failed = 0xFA - (1 << 8)
    [
      [:ja,  [1, 1, failed]],
      [:jae, [1, 4, failed]], 
      [:jb,  [1, failed, 7]],
      [:jbe, [1, failed, 10]],
      [:jz,  [1, failed, 13]],
      [:jne, [1, 16, failed]], 
    ].each do |op_line|
      op = op_line.shift
      op_line.each_index do |i|
        then_label = "#{op}_l#{i}_t".to_sym
        out_label  = "#{op}_l#{i}_o".to_sym
        
        s.ldbc op_line[i][0]        
        s.send op, :to => then_label
        s.ldbc op_line[i][2]
        s.jmp :to => out_label
        s.label then_label
        s.ldbc op_line[i][1]
        s.label out_label
        s.outb          
      end
    end

    # loop back
    s.ldbc 1
    s.sub
    s.dupn :n => 1
    s.ja :to => :main_loop
    
    s.label :done
    s.halt
    
    s.label :cmp_lo
    s.data :tem_ubyte, [0xA3, 0x2C, 0x51, 0x63, 0x2C, 0x12]
    s.label :cmp_med
    s.data :tem_ubyte, [0xA3, 0x2C, 0x51, 0x63, 0x2D, 0x11]
    s.label :cmp_hi
    s.data :tem_ubyte, [0xA3, 0x2C, 0x51, 0x63, 0x2E, 0x10]
    s.label :cmp_hi2
    s.data :tem_ubyte, [0xA3, 0x2C, 0x51, 0x63, 0x2E, 0x10]
    s.label :copy_buf
    s.zeros :tem_ubyte, 6
    s.label :copy_buf2
    s.zeros :tem_ubyte, 4      
    s.label :clobber
    s.zeros :tem_ubyte, 2
    s.label :clobber2
    s.zeros :tem_ubyte, 2
    s.label :stack
    s.stack 12
  }
end

#vm_perf_seclosure_opcountObject

Number of opcodes executed by the vm_perf SEClosure.



152
153
154
# File 'lib/tem/benchmarks/vm_perf.rb', line 152

def vm_perf_seclosure_opcount
  1020
end

#vm_perf_seclosure_outcountObject

Number of bytes output by the vm_perf SEClosure.



157
158
159
# File 'lib/tem/benchmarks/vm_perf.rb', line 157

def vm_perf_seclosure_outcount
  470
end