以下代码摘自 Marc Briceno、Ian Goldberg 和 David Wagner 的开创性著作“GSM A5/1 和 A5/2“语音隐私”加密算法的教学实现”:
typedef unsigned char byte;
typedef unsigned long word;
typedef word bit;
...
word R1, R2, R3;
#ifdef A5_2
word R4;
#endif /* A5_2 */
...
/* Generate an output bit from the current state.
* You grab a bit from each register via the output generation taps;
* then you XOR the resulting three bits. For A5/2, in addition to
* the top bit of each of R1,R2,R3, also XOR in a majority function
* of three particular bits of the register (one of them complemented)
* to make it non-linear. Also, for A5/2, delay the output by one
* clock cycle for some reason. */
bit getbit() {
bit topbits = (((R1 >> 18) ^ (R2 >> 21) ^ (R3 >> 22)) & 0x01);
#ifndef A5_2
return topbits;
#else /* A5_2 */
static bit delaybit = 0;
bit nowbit = delaybit;
delaybit = (
topbits
^ majority(R1&0x8000, (~R1)&0x4000, R1&0x1000)
^ majority((~R2)&0x10000, R2&0x2000, R2&0x200)
^ majority(R3&0x40000, R3&0x10000, (~R3)&0x2000)
);
return nowbit;
#endif /* A5_2 */
}
我已经研究这个实现几天了,我了解数据的去向和来源(以及它在算法中的用途)方面发生了什么。
我唯一不明白的是/*此外,对于A5/2,由于某种原因将输出延迟一个时钟周期。 */
.
我不明白怎么办
static bit delaybit = 0;
bit nowbit = delaybit;
delaybit = (...);
return nowbit;
给出“延迟”输出。
即。 delaybit
到底是怎么延迟的?如果我们返回 nowbit
,为什么我们还要生成 delaybit
在代码的其他地方,延迟的概念作为寄存器(R1
, R2
和 R3
)通过专用时钟功能进行时钟控制。
我不认为 nowbit
和 delayedbit
在此函数中作为两个独立实体的用途。
我们不是只需要一个变量吗?通过将其混合在一个时钟周期内,我们将执行延迟。
我的理解有问题吗?或者也许用代码?这是一个广泛传播的文件,因此我希望找到代码中此类问题的公共(public)记录(如果有的话)。
最佳答案
“技巧”在这里:
static bit delaybit = 0;
静态变量在调用之间保留其值。第一次设置为 0,然后返回该值。然后计算下一个值,并由将来的调用返回。
在我看来,有点太可爱了。
关于c - 在 Briceno 等人的 A5/2 实现中,他们延迟了 LSFR 周期而不运行时钟周期函数。有人可以帮我理解吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70225405/