Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
5525818
feature: add manually input bytes for generate numbers; connect drive…
estoniec Mar 15, 2026
c9b2278
Add Galois Field library by Denis Potapov
estoniec Mar 15, 2026
9ef641d
feature: add bin files with manually bytes input for tests
estoniec Mar 15, 2026
8269c8e
feature-n-refactor: SMALL CHANGES DENIS POTAPOV LIBRARY (WITH PERMISS…
estoniec Mar 17, 2026
004df0c
refactor: SMALL CHANGES DENIS POTAPOV LIBRARY (WITH PERMISSION, TEMPO…
estoniec Mar 17, 2026
2fe743e
formatting: change CRLF to LF in driver.c and Makefile
estoniec Mar 17, 2026
0e187d3
fix: critical bugs in driver.c has been fixed
estoniec Mar 17, 2026
56b067b
fix-n-refactor-n-feature: update README.md; some fixes and refactors;…
estoniec Mar 17, 2026
d83eb91
refactor: small change in README.md
estoniec Mar 17, 2026
21d11a0
refactor: small change in README.md
estoniec Mar 17, 2026
5df2c40
refactor: small change in README.md
estoniec Mar 17, 2026
1a5654e
update: update README.md; change polynom => seed
estoniec Mar 22, 2026
025c9fd
update: update README.md; change polynom => seed
estoniec Mar 22, 2026
55e1f7e
update: README.md
estoniec Mar 22, 2026
9eafe2a
update: README.md
estoniec Mar 22, 2026
33eb302
sync: update changes from dependency lib
estoniec Mar 28, 2026
38f2fdf
sync: update changes from dependency lib
estoniec Mar 28, 2026
e3a5d8e
refactor: cosmetic changes
estoniec Mar 28, 2026
3736c3d
optimization: up kernel_buf size from 256 to 1024
estoniec Mar 29, 2026
00be63a
feature: new test (#3) - chi-squared test
estoniec Mar 29, 2026
8cfdc19
fix: ci
estoniec Mar 29, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 小褌懈谢褜 褎芯褉屑邪褌懈褉芯胁邪薪懈褟
BasedOnStyle: Google

# 袧邪褋褌褉芯泄泻懈 芯褌褋褌褍锌芯胁
IndentWidth: 4
TabWidth: 4
UseTab: Never

# 袪邪蟹褉褘胁褘 褋褌褉芯泻
ColumnLimit: 100
BreakBeforeBraces: Allman

# 袩褉芯斜械谢褘
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false

# 袙褘褉邪胁薪懈胁邪薪懈械
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true

# 校泻邪蟹邪褌械谢懈 懈 褋褋褘谢泻懈
PointerAlignment: Left

AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false

# 小芯褉褌懈褉芯胁泻邪
SortIncludes: true
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Simple driver tests

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install dependencies
run: sudo apt-get update && sudo apt-get install -y gcc make clang

- name: Static analysis
run: clang-tidy prandom.c Galois_Field_256.c test.c -- -I.

- name: Build driver
run: make

- name: Build test
run: make test_algo

- name: Run test
run: make test

- name: Run with sanitizers
run: make test_sanitized && ./test
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*.o
*.ko
*.mod.c
*.mod
*.order
*.symvers
Module.symvers
.gfrandom.mod.cmd
gfrandom.mod
compile_commands.json
.cache
*.swp
*.swo
*~
test
test_gen
expected_gen
82 changes: 82 additions & 0 deletions Galois_Field_256.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Denis Potapov

#include "Galois_Field_256.h"

#ifdef __KERNEL__
#include <linux/types.h>
u16 Module = 0x11B;
u8 ready_to_work = 0;
#else
#include <stdint.h>
uint16_t Module = 0x11B;
uint8_t ready_to_work = 0;
#endif

#define ORDER 256

GF256_t alogs[ORDER];
GF256_t logs[ORDER];

void GF256_init(void) {
// 锌芯褉芯卸写邪褞褖懈泄 褝谢械屑械薪褌 x + 1
GF256_t elem = 1; // (x + 1)**0

for (int i = 0; i < ORDER - 1; i++) {
alogs[i] = elem; // <=> (x + 1)**i = elem
logs[elem] = i;

uint16_t new_elem = (uint16_t)elem << 1; // <=> new_elem = elem * x

new_elem ^= elem; // <=> new_elem = elem * x + elem <=> new_elem = elem * (x + 1)

if (new_elem >= 0x100) {
new_elem ^= Module;
}
elem = (GF256_t)(new_elem);
}
alogs[ORDER - 1] = alogs[0];

logs[0] = 0;

ready_to_work = 1;
}

GF256_t GF256_Add(GF256_t a, GF256_t b) {
return a ^ b;
}

GF256_t GF256_Sub(GF256_t a, GF256_t b) {
return a ^ b;
}

GF256_t GF256_Mul(GF256_t a, GF256_t b) {
if (!ready_to_work) {
GF256_init();
}
if (a == 0 || b == 0) {
return 0;
}

uint8_t log_a = logs[a];
uint8_t log_b = logs[b];

uint16_t sum = (log_a + log_b) % (ORDER - 1);

return alogs[sum];
}

GF256_t inverse_mul_element(GF256_t a) {
if (a == 0) {
return 0;
}
if (!ready_to_work) {
GF256_init();
}
return alogs[255 - logs[a]];
}

GF256_t GF256_Div(GF256_t a, GF256_t b) {
return GF256_Mul(a, inverse_mul_element(b));
}
24 changes: 24 additions & 0 deletions Galois_Field_256.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Denis Potapov

#ifndef Galois_Field_256
#define Galois_Field_256

#ifdef __KERNEL__
#include <linux/types.h>
typedef u8 GF256_t;
#else
#include <stdint.h>
typedef uint8_t GF256_t;
#endif

GF256_t GF256_Add(GF256_t a, GF256_t b);

GF256_t GF256_Sub(GF256_t a, GF256_t b);

GF256_t GF256_Mul(GF256_t a, GF256_t b);

GF256_t GF256_Div(GF256_t a, GF256_t b);

#endif
33 changes: 22 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
obj-m += prandom.o

KDIR := /lib/modules/$(shell uname -r)/build

PWD := $(shell pwd)

all:
$(MAKE) -C $(KDIR) M=$(PWD) modules

clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean
obj-m += gfrandom.o
gfrandom-objs := driver.o prandom.o Galois_Field_256.o

KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

all:
$(MAKE) -C $(KDIR) M=$(PWD) modules

clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean

test_algo: test.c Galois_Field_256.c prandom.c
gcc -o test test.c Galois_Field_256.c prandom.c

test_sanitized: test.c Galois_Field_256.c prandom.c
gcc -o test test.c Galois_Field_256.c prandom.c -fsanitize=address,undefined -g

test: test_algo
./test

.PHONY: all clean test test
102 changes: 102 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# prandom

[![CI](https://github.com/estoniec/prandom/actions/workflows/ci.yml/badge.svg)](https://github.com/estoniec/prandom/actions/workflows/ci.yml)
[![Clang-Tidy](https://img.shields.io/badge/Clang--Tidy-passing-green)](https://clang.llvm.org/docs/ClangFormat.html)

Linux kernel 屑芯写褍谢褜, 锌褉械写芯褋褌邪胁谢褟褞褖懈泄 谐械薪械褉邪褌芯褉 锌褋械胁写芯褋谢褍褔邪泄薪褘褏 褔懈褋械谢 薪邪 芯褋薪芯胁械 锌芯谢械泄 袚邪谢褍邪 (GF(2鈦�)).

## 袨锌懈褋邪薪懈械

袛褉邪泄胁械褉 褋芯蟹写邪褢褌 misc-褍褋褌褉芯泄褋褌胁芯 `/dev/gfrandom`, 泻芯褌芯褉芯械 胁芯蟹胁褉邪褖邪械褌 锌褋械胁写芯褋谢褍褔邪泄薪褘械 斜邪泄褌褘, 褋谐械薪械褉懈褉芯胁邪薪薪褘械 褋 懈褋锌芯谢褜蟹芯胁邪薪懈械屑 邪褉懈褎屑械褌懈泻懈 锌芯谢械泄 袚邪谢褍邪 GF(2鈦�). 携胁谢褟械褌褋褟 邪薪邪谢芯谐芯屑 `/dev/urandom`.

### 袗谢谐芯褉懈褌屑

袠褋锌芯谢褜蟹褍械褌褋褟 褉械谐懈褋褌褉 褋写胁懈谐邪 褋 谢懈薪械泄薪芯泄 芯斜褉邪褌薪芯泄 褋胁褟蟹褜褞 (LFSR) 薪邪写 锌芯谢械屑 GF(2鈦�):
- 小芯褋褌芯褟薪懈械: 256 斜邪泄褌 (泻芯褝褎褎懈褑懈械薪褌褘)
- 袧邪褔邪谢褜薪芯械 褋芯褋褌芯褟薪懈械 (seed): t^256 + t^10 + t^5 + t^2 + 1 (懈谢懈 锌芯谢褜蟹芯胁邪褌械谢褜褋泻懈泄)
- 袚械薪械褉懈褉褍械屑芯械 蟹薪邪褔械薪懈械: 褋褍屑屑邪 锌褉芯懈蟹胁械写械薪懈泄 薪械薪褍谢械胁褘褏 泻芯褝褎褎懈褑懈械薪褌芯胁 褋芯褋褌芯褟薪懈褟 薪邪 褋芯芯褌胁械褌褋褌胁褍褞褖懈械 泻芯褝褎褎懈褑懈械薪褌褘

### 袠褋锌芯谢褜蟹褍械屑褘械 泻芯屑锌芯薪械薪褌褘

- **gfrandom** 鈥� 写褉邪泄胁械褉 (GPL-3.0)
- **Galois_Field_256** 鈥� [斜懈斜谢懈芯褌械泻邪 写谢褟 褉邪斜芯褌褘 褋 锌芯谢褟屑懈 袚邪谢褍邪](https://github.com/DenisPotapov0/Galua-Field-library) (MIT License)

## 孝褉械斜芯胁邪薪懈褟

- Linux kernel headers (写谢褟 褋斜芯褉泻懈 屑芯写褍谢褟)
- GCC, make
- 袛谢褟 蟹邪锌褍褋泻邪 褌械褋褌芯胁: linux-headers 懈谢懈 root 写芯褋褌褍锌 写谢褟 蟹邪谐褉褍蟹泻懈 屑芯写褍谢褟

## 袛械褌械褉屑懈薪懈褉芯胁邪薪薪褘械 褌械褋褌芯胁褘械 写邪薪薪褘械

袛谢褟 褍写芯斜褋褌胁邪 锌芯谢褜蟹芯胁邪褌械谢械泄, 卸械谢邪褞褖懈褏 锌芯谢褍褔懈褌褜 写械褌械褉屑懈薪懈褉芯胁邪薪薪褘械 锌褉械写褋泻邪蟹褍械屑褘械 褔懈褋谢邪, 胁 锌邪锌泻械 `assets` 锌褉械写褋褌邪胁谢械薪褘 褋褌邪薪写邪褉褌薪褘械 斜懈薪邪褉薪褘械 褎邪泄谢褘 锌芯 256 斜邪泄褌 泻邪卸写褘泄:
- `seed_file.bin` 鈥� 薪邪褔邪谢褜薪芯械 褋芯褋褌芯褟薪懈械 (seed)
- `coeff_file.bin` 鈥� 泻芯褝褎褎懈褑懈械薪褌褘

协褌懈 卸械 褎邪泄谢褘 懈褋锌芯谢褜蟹褍褞褌褋褟 胁 褌械褋褌邪褏 鈩�1 懈 鈩�2. 袩械褉胁褘械 10 芯卸懈写邪械屑褘褏 褋谢褍褔邪泄薪褘褏 斜邪泄褌芯胁: (HEX: `47 6b 77 79 60 1c bd 3f c9 b2`) 鈥� 褋屑. `assets/expected1.bin`.

## 小斜芯褉泻邪

### Linux 屑芯写褍谢褜

```bash
make
```

### Userspace 褌械褋褌褘

```bash
make test_algo
./test
```

## 袠褋锌芯谢褜蟹芯胁邪薪懈械

### 袟邪谐褉褍蟹泻邪 屑芯写褍谢褟

```bash
sudo insmod gfrandom.ko
```

### 袩邪褉邪屑械褌褉褘 屑芯写褍谢褟

- `coeff_file` 鈥� 锌褍褌褜 泻 褎邪泄谢褍 褋 泻芯褝褎褎懈褑懈械薪褌邪屑懈 (256 斜邪泄褌)
- `seed_file` 鈥� 锌褍褌褜 泻 褎邪泄谢褍 褋 seed (256 斜邪泄褌)

袝褋谢懈 锌邪褉邪屑械褌褉褘 薪械 褍泻邪蟹邪薪褘, 懈褋锌芯谢褜蟹褍褞褌褋褟 邪胁褌芯屑邪褌懈褔械褋泻懈 褋谐械薪械褉懈褉芯胁邪薪薪褘械 蟹薪邪褔械薪懈褟.

袩褉懈屑械褉 懈褋锌芯谢褜蟹芯胁邪薪懈褟 褋芯斜褋褌胁械薪薪褘褏 锌邪褉邪屑械褌褉芯胁:
```bash
sudo insmod gfrandom.ko coeff_file=/path/to/coeffs.bin seed_file=/path/to/seed.bin
```

### 效褌械薪懈械 褋谢褍褔邪泄薪褘褏 写邪薪薪褘褏

```bash
sudo cat /dev/gfrandom | head -c 1024 > /dev/null
```

### 袙褘谐褉褍蟹泻邪 屑芯写褍谢褟

```bash
sudo rmmod gfrandom
```

## 孝械褋褌懈褉芯胁邪薪懈械

孝械褋褌褘 蟹邪锌褍褋泻邪褞褌褋褟 胁 CI 懈 谢芯泻邪谢褜薪芯:

```bash
make test
```

## 袥懈褑械薪蟹懈褟

- **gfrandom**: GPL-3.0
- **Galois_Field_256** (Denis Potapov): MIT License

袩芯写褉芯斜薪芯褋褌懈 褋屑. 胁 [LICENSE](LICENSE).

## 袗胁褌芯褉

袦邪泻邪褉 袥懈谢谢褜
2 changes: 2 additions & 0 deletions assets/coeff_file.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
阠閩妟姶z憹杆s�/閘b跔~碔愉�o�q#7�.[�/�> fc崏吥翅S膏#a蓁9方� 蛏�A�楯咻崃䜩�$f6�藢疳a+l峘�� ]$��瀽鉧邯{箋窊踥纺2喐都 謌軂[乏僥b燪S0堇Y2噃渆椅�A}�'S� 惛恂}荣�yx2枷褞舖
鍸硼l砹F瞕��逼渍昢d.7ub焟能钴@QBo�"i蚏�.誅泠戕<葺"S纾
1 change: 1 addition & 0 deletions assets/expected1.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Gkwy`�?刹
Binary file added assets/seed_file.bin
Binary file not shown.
Loading
Loading