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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_benchmarksObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tem/benchmarks/benchmarks.rb', line 48

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

.display_all_benchmarksObject



60
61
62
63
64
65
# File 'lib/tem/benchmarks/benchmarks.rb', line 60

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

#do_timingObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tem/benchmarks/benchmarks.rb', line 25

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 = 100 * 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]
    
    return avg_time / n unless max_diff / avg_time >= 0.01
    n *= 2
  end
end

#setupObject



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

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

#teardownObject



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

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

#time_blank_bound_secpackObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/tem/benchmarks/blank_bound_secpack.rb', line 2

def time_blank_bound_secpack
  secpack = @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
  }
  secpack.bind @tem.pubek, :secret, :plain

  print "SECpack has #{secpack.body.length} bytes, runs 3 instructions and produces 0 bytes\n"
  do_timing { @tem.execute secpack }
end

#time_blank_secObject



2
3
4
5
6
7
8
9
10
11
12
13
# File 'lib/tem/benchmarks/blank_sec.rb', line 2

def time_blank_sec
  secpack = @tem.assemble { |s|
    s.ldbc 0
    s.outnew
    s.halt
    s.zeros :tem_ubyte, 70
    s.stack 1
  }

  print "SECpack has #{secpack.body.length} bytes, runs 3 instructions and produces 0 bytes\n"
  do_timing { @tem.execute secpack }
end

#time_devchip_decryptObject



2
3
4
5
6
7
8
# File 'lib/tem/benchmarks/devchip_decrypt.rb', line 2

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

#time_post_bufferObject



2
3
4
5
6
7
8
9
# File 'lib/tem/benchmarks/post_buffer.rb', line 2

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



2
3
4
# File 'lib/tem/benchmarks/simple_apdu.rb', line 2

def time_simple_apdu
  do_timing { @tem.get_tag_length }
end

#time_vm_perfObject



2
3
4
5
6
7
8
9
10
11
12
13
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
# File 'lib/tem/benchmarks/vm_perf.rb', line 2

def time_vm_perf
  secpack = @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
  }
  print "SECpack has #{secpack.body.length} bytes, runs 1020 instructions and produces 470 bytes\n"
  do_timing { @tem.execute secpack }
end

#time_vm_perf_boundObject



2
3
4
5
6
7
8
9
10
11
12
13
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
# File 'lib/tem/benchmarks/vm_perf_bound.rb', line 2

def time_vm_perf_bound
  secpack = @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
  }
  secpack.bind @tem.pubek, :done, :stack
  print "SECpack has #{secpack.body.length} bytes, runs 1020 instructions and produces 470 bytes\n"
  do_timing { @tem.execute secpack }
end