@@ -10,6 +10,10 @@ module ascon
1010// constants for up to 16 rounds to accommodate potential functionality extensions in the future.
1111const max_nr_perm = 16
1212
13+ // The number how many round(s) for the Ascon permutation routine called.
14+ const ascon_prnd_8 = 8
15+ const ascon_prnd_12 = 12
16+
1317// The constants to derive round constants of the Ascon permutations
1418// See Table 5. of NIST SP 800-232 docs
1519//
@@ -26,70 +30,72 @@ const max_nr_perm = 16
2630const rnc = [u8 (0x3c ), 0x2d , 0x1e , 0x0f , 0xf0 , 0xe1 , 0xd2 , 0xc3 , 0xb4 , 0xa5 , 0x96 , 0x87 , 0x78 ,
2731 0x69 , 0x5a , 0x4b ]
2832
29- // ascon_pnr is ascon permutation routine with specified numbers of round nr, where 1 ≤ nr ≤ 16
33+ // ascon_pnr is the core of Ascon family permutation routine with specified numbers of round nr, where 1 ≤ nr ≤ 16
34+ // Its consist of iterations of the round function that is defined as the composition of three steps, ie:
35+ // 1. the constant-addition layer (see Sec. 3.2),
36+ // 2. the substitution layer (see Sec.3.3), and,
37+ // 3. the linear diffusion layer (Sec 3.4)
3038@[direct_array_access]
3139fn ascon_pnr (mut s State, nr int ) {
3240 // We dont allow nr == 0
3341 if nr < 1 || nr > 16 {
3442 panic ('Invalid round number' )
3543 }
44+ // Ascon permutation routine
3645 for i := max_nr_perm - nr; i < max_nr_perm; i++ {
37- ascon_perm (mut s, rnc[i])
38- }
39- }
46+ // 3.2 Constant-Addition Layer step
47+ //
48+ // The constant-addition layer adds a 64-bit round constant 𝑐𝑖
49+ // to 𝑆₂ in round 𝑖, for 𝑖 ≥ 0, ie, this is equivalent to applying
50+ // the constant to only the least significant eight bits of 𝑆₂
51+ s.e2 ^= rnc[i]
4052
41- // ascon_perm was the main permutations routine in Ascon-family crypto. Its consist of
42- // iterations of the round function that is defined as the composition of three steps, ie:
43- // 1. the constant-addition layer (see Sec. 3.2),
44- // 2. the substitution layer (see Sec.3.3), and,
45- // 3. the linear diffusion layer
46- fn ascon_perm (mut s State, c u8 ) {
47- // 3.2 Constant-Addition Layer step
48- //
49- // The constant-addition layer adds a 64-bit round constant 𝑐𝑖
50- // to 𝑆₂ in round 𝑖, for 𝑖 ≥ 0, ie, this is equivalent to applying
51- // the constant to only the least significant eight bits of 𝑆₂
52- s.e2 ^= c
53-
54- // 3.3. Substitution Layer
55- // The substitution layer updates the state S with 64 parallel applications of the 5-bit
56- // substitution box SBOX
57- s.e0 ^= s.e4
58- s.e4 ^= s.e3
59- s.e2 ^= s.e1
53+ // 3.3. Substitution Layer
54+ // The substitution layer updates the state S with 64 parallel applications of the 5-bit
55+ // substitution box SBOX
56+ s.e0 ^= s.e4
57+ s.e4 ^= s.e3
58+ s.e2 ^= s.e1
6059
61- t0 := s.e4 ^ (~ s.e0 & s.e1 )
62- t1 := s.e0 ^ (~ s.e1 & s.e2 )
63- t2 := s.e1 ^ (~ s.e2 & s.e3 )
64- t3 := s.e2 ^ (~ s.e3 & s.e4 )
65- t4 := s.e3 ^ (~ s.e4 & s.e0 )
60+ t0 := s.e4 ^ (~ s.e0 & s.e1 )
61+ t1 := s.e0 ^ (~ s.e1 & s.e2 )
62+ t2 := s.e1 ^ (~ s.e2 & s.e3 )
63+ t3 := s.e2 ^ (~ s.e3 & s.e4 )
64+ t4 := s.e3 ^ (~ s.e4 & s.e0 )
6665
67- s.e0 = t1
68- s.e1 = t2
69- s.e2 = t3
70- s.e3 = t4
71- s.e4 = t0
66+ s.e0 = t1
67+ s.e1 = t2
68+ s.e2 = t3
69+ s.e3 = t4
70+ s.e4 = t0
7271
73- s.e1 ^= s.e0
74- s.e0 ^= s.e4
75- s.e3 ^= s.e2
76- s.e2 = ~ (s.e2 )
72+ s.e1 ^= s.e0
73+ s.e0 ^= s.e4
74+ s.e3 ^= s.e2
75+ s.e2 = ~ (s.e2 )
7776
78- // 3.4. Linear Diffusion Layer
79- //
80- // The linear diffusion layer provides diffusion within each 64-bit word S,
81- // defined as :
82- // Σ0(𝑆0) = 𝑆0 ⊕ (𝑆0 ⋙ 19) ⊕ (𝑆0 ⋙ 28)
83- // Σ1(𝑆1) = 𝑆1 ⊕ (𝑆1 ⋙ 61) ⊕ (𝑆1 ⋙ 39)
84- // Σ2(𝑆2) = 𝑆2 ⊕ (𝑆2 ⋙ 1) ⊕ (𝑆2 ⋙ 6)
85- // Σ3(𝑆3) = 𝑆3 ⊕ (𝑆3 ⋙ 10) ⊕ (𝑆3 ⋙ 17)
86- // Σ4(𝑆4) = 𝑆4 ⊕ (𝑆4 ⋙ 7) ⊕ (𝑆4 ⋙ 41)
87-
88- s.e0 ^= ascon_rotate_right (s.e0 , 19 ) ^ ascon_rotate_right (s.e0 , 28 )
89- s.e1 ^= ascon_rotate_right (s.e1 , 61 ) ^ ascon_rotate_right (s.e1 , 39 )
90- s.e2 ^= ascon_rotate_right (s.e2 , 1 ) ^ ascon_rotate_right (s.e2 , 6 )
91- s.e3 ^= ascon_rotate_right (s.e3 , 10 ) ^ ascon_rotate_right (s.e3 , 17 )
92- s.e4 ^= ascon_rotate_right (s.e4 , 7 ) ^ ascon_rotate_right (s.e4 , 41 )
77+ // 3.4. Linear Diffusion Layer
78+ //
79+ // The linear diffusion layer provides diffusion within each 64-bit word S,
80+ // defined as :
81+ // Σ0(𝑆0) = 𝑆0 ⊕ (𝑆0 ⋙ 19) ⊕ (𝑆0 ⋙ 28)
82+ // Σ1(𝑆1) = 𝑆1 ⊕ (𝑆1 ⋙ 61) ⊕ (𝑆1 ⋙ 39)
83+ // Σ2(𝑆2) = 𝑆2 ⊕ (𝑆2 ⋙ 1) ⊕ (𝑆2 ⋙ 6)
84+ // Σ3(𝑆3) = 𝑆3 ⊕ (𝑆3 ⋙ 10) ⊕ (𝑆3 ⋙ 17)
85+ // Σ4(𝑆4) = 𝑆4 ⊕ (𝑆4 ⋙ 7) ⊕ (𝑆4 ⋙ 41)
86+ //
87+ // This diffusion layer, especially on the bits right rotation part is a most widely called
88+ // for Ascon permutation routine. So, even bits rotation almost efficient on most platform,
89+ // to reduce overhead on function call, we work on the raw bits right rotation here.
90+ // Bits right rotation, basically can be defined as:
91+ // ror = (x >> n) | x << (64 - n) for some u64 x
92+ //
93+ s.e0 ^= (s.e0 >> 19 | (s.e0 << (64 - 19 ))) ^ (s.e0 >> 28 | (s.e0 << (64 - 28 )))
94+ s.e1 ^= (s.e1 >> 61 | (s.e1 << (64 - 61 ))) ^ (s.e1 >> 39 | (s.e1 << (64 - 39 )))
95+ s.e2 ^= (s.e2 >> 1 | (s.e2 << (64 - 1 ))) ^ (s.e2 >> 6 | (s.e2 << (64 - 6 ))) //
96+ s.e3 ^= (s.e3 >> 10 | (s.e3 << (64 - 10 ))) ^ (s.e3 >> 17 | (s.e3 << (64 - 17 )))
97+ s.e4 ^= (s.e4 >> 7 | (s.e4 << (64 - 7 ))) ^ (s.e4 >> 41 | (s.e4 << (64 - 41 )))
98+ }
9399}
94100
95101// State is structure represents Ascon state. Its operates on the 320-bit opaque,
0 commit comments