unsigned

浅谈密码学中数论基础

三世轮回 提交于 2019-11-30 06:40:38
1.模运算(mod) 模运算也可以称为取余运算,例如 23≡11(mod12),因此如果a=kn+b,也可以表示为a ≡ b(mod n),运算规则: (a+b) mod n = ((a mod n) + (b mod n))mod n (a*b) mod n = ((a mod n) * (b mod n)) mod n 完全剩余集合 1~n-1构成了自然数n的完全剩余集合,对于任意一个整数m%n都存在于1~n的集合中 。 构造加法链 在加密算法中,运用到了大量的取模运算,对于一个k位数模n,所有的运算例如加减乘除中间结构将不会超过2k位,因此例如a x mod n将会大大简化计算时的复杂度。 例如a 8 mod n 在计算时可以这样计算 ((a 2 mod n) 2 mod n) 2 mod n 当指数x不是2的倍数时则需要构造加法链,例如25,25 = 16 + 8 +1 = 2 4 + 2 3 + 2 0 因此a 25 mod n = (a * a 8 * a 16 ) mod n = ((((a 2 * a) 2 ) 2 ) 2 *a)mod n C语言表示 unsigned long ss(unsigned long x , unsigned long y , unsigned long n) { unsigned long s,t,u; int i ; s = 1;

Is subtracting larger unsigned value from smaller in C++ undefined behaviour?

放肆的年华 提交于 2019-11-30 06:01:13
问题 In a program below after "integral promotions" and "usual arithmetic conversions" both operands of operator - remain unsigned long long . Afterwards, C++11 standard says: 5.7.3 The result of the binary - operator is the difference resulting from the subtraction of the second operand from the first. Does the standard define anywhere in more detail how exactly the subtraction is performed (or refers to some other document that defines it)? Does subtracting a larger unsigned integer from smaller

Linux相关函数

£可爱£侵袭症+ 提交于 2019-11-30 04:27:54
目录 内核、用户数据拷贝 module_platform_driver() 驱动获得进程的信息 DEVICE_ATTR EXPORT_SYMBOL() container_of insmod给驱动传参数 其他函数 内核、用户数据拷贝 常见的有四个函数: copy_from_user、copy_to_user、get_user(或__get_user)、put_user(或__put_user) copy_from_user: 原型:static inline long copy_from_user(void *to, const void __user * from, unsigned long n) 第一个参数to: 内核空间的数据目标地址指针 第二个参数from: 用户空间的数据源地址指针 (void __user *也可) 第三个参数n: 数据的长度,以字节为单位。 如果数据拷贝成功,则返回零;否则,返回没有拷贝成功的数据字节数。 例: static struct file_operations ker_rw_ops = { .owner = THIS_MODULE, .unlocked_ioctl = ker_rw_ioctl, }; static long ker_rw_ioctl(struct file *file, unsigned int cmd, unsigned

I/O模型之阻塞(在中断基础上------jz2440)

江枫思渺然 提交于 2019-11-30 04:25:20
1 阻塞: 在应用层调用read函数的时候,如果硬件中的数据没有准备好,此时进程会进入休眠状态,当硬件的数据准备好的时候会给驱动发送中断。驱动收到中断之后,唤醒休眠的进程。这个被唤醒的进程在driver_read读取硬件的数据,并把数据 返回到用户空间。(模型中断) 可以使用队列,把整个进程进入到队列中,使进程进入阻塞的状态,当有数据产生的时候,产生中断,唤醒在队列中的进程,从而读取数据. 队列: wait_queue_head_t wq //定义等待队列头 init_waitqueue_head(&wq) //初始化等待队列头 wait_event(wq, condition) //不可中断的等待态 wait_event_interruptible (wq, condition)//可中断的等待态(当产生中断信号就可以唤醒) 参数: @wq :等待对列头 @condition :如果条件为假表示可休眠,如果为真表示不休眠 返回值:成功返回0,失败返回错误码 wake_up(&wq) wake_up_interruptible(&wq) 唤醒休眠 condition = 1; 分析:wake_up_interruptible()唤醒后,wait_event_interruptible(wq, condition)宏,自身再检查“condition”这个条件以决定是返回还是继续休眠

Bran的内核开发教程(bkerndev)-04 创建main函数和链接C文件

故事扮演 提交于 2019-11-30 03:19:45
创建main函数和链接C文件   一般C语言使用main()函数作为程序的入口点, 为了符合我们平时的编程习惯, 这里我们也使用main()函数作为C代码的入口点, 并在"start.asm"文件中添加中断服务程序来调用C函数。   在这一节教程,我们将尝试创建一个"main.c"文件和一个包含常用函数原型的头文件"system.h"。"main.c"中包含mian()函数, 它将作为你C代码的入口。在内核开发中, 我们一般不从main()函数返回。多数操作系统在main中初始化内核和子程序、加载shell, 然后main函数会进入空循环中。在多任务系统中, 当没有其他需要运行的任务时, 将一直执行这个空循环。下面是"main.c"文件的示例,其中包含了最基本的main()函数和一些我们以后会用到的函数体。 main.c #include <system.h> /* 你将要自己完成这些代码 */ unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count) { /* 在此处添加代码, 将'src'中count字节的数据复制到'dest'中, * 最后返回'dest' */ } unsigned char *memset(unsigned char *dest, unsigned

Why is (18446744073709551615 == -1) true?

懵懂的女人 提交于 2019-11-30 01:40:27
问题 When I was working on string::npos I noticed something and I couldn't find any explanation for it on the web. (string::npos == ULONG_MAX) and (string::npos == -1) are true. So I tried this: (18446744073709551615 == -1) which is also true. How can it be possible? Is it because of binary conversation? 回答1: 18,446,744,073,709,551,615 This number mentioned, 18,446,744,073,709,551,615 , is actually 2^64 − 1 . The important thing here is that 2^64-1 is essentially 0-based 2^64 . The first digit of

In C, why is “signed int” faster than “unsigned int”?

给你一囗甜甜゛ 提交于 2019-11-29 23:55:26
In C, why is signed int faster than unsigned int ? True, I know that this has been asked and answered multiple times on this website (links below). However, most people said that there is no difference. I have written code and accidentally found a significant performance difference. Why would the "unsigned" version of my code be slower than the "signed" version (even when testing the same number)? (I have a x86-64 Intel processor). Similar links Faster comparing signed than unsigned ints performance of unsigned vs signed integers Compile Command: gcc -Wall -Wextra -pedantic -O3 -Wl,-O3 -g0

Calculating bits required to store decimal number

浪尽此生 提交于 2019-11-29 21:48:24
This is a homework question that I am stuck with. Consider unsigned integer representation. How many bits will be required to store a decimal number containing: i) 3 digits ii) 4 digits iii) 6 digits iv) n digits I know that the range of the unsigned integer will be 0 to 2^n but I don't get how the number of bits required to represent a number depends upon it. Please help me out. Thanks in advance. Well, you just have to calculate the range for each case and find the lowest power of 2 that is higher than that range. For instance, in i), 3 decimal digits -> 10^3 = 1000 possible numbers so you

内核定时器与延时

£可爱£侵袭症+ 提交于 2019-11-29 21:42:00
内核需要定时器来实现一定的延时。 数据结构定义: struct timer_list { struct list_head entry; /* entry in linked list of timers */ unsigned long expires; /* expiration value, in jiffies */ void (*function)(unsigned long); /* the timer handler function */ unsigned long data; /* lone argument to the handler */ struct tvec_t_base_s *base; /* internal timer field, do not touch */ }; 操作: void init_timer(struct timer_list *timer); TIMER_INITIALIZER(_functioin, _expires, _data) 宏用于赋值定时器结构体的 function 、 expires 、 data 和 base 成员。 DEFINE_TIMER(_name, _function, _expires, _data) setup_timer() 也可用于初始化定时器并赋值其成员。 增加定时器 void add

What is the difference between “int” and “uint” / “long” and “ulong”?

巧了我就是萌 提交于 2019-11-29 19:41:02
I know about int and long (32-bit and 64-bit numbers), but what are uint and ulong ? Isak Savo The primitive data types prefixed with "u" are unsigned versions with the same bit sizes. Effectively, this means they cannot store negative numbers, but on the other hand they can store positive numbers twice as large as their signed counterparts. The signed counterparts do not have "u" prefixed. The limits for int (32 bit) are: int: –2147483648 to 2147483647 uint: 0 to 4294967295 And for long (64 bit): long: -9223372036854775808 to 9223372036854775807 ulong: 0 to 18446744073709551615 Mark Byers