Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

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

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

1715

1716

1717

1718

1719

1720

1721

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

1952

1953

1954

1955

1956

1957

1958

1959

1960

1961

1962

1963

1964

1965

1966

1967

1968

1969

1970

1971

1972

1973

1974

1975

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

1986

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1998

1999

2000

2001

2002

2003

2004

2005

2006

2007

2008

2009

2010

2011

2012

2013

2014

2015

2016

2017

2018

2019

2020

2021

2022

2023

2024

2025

2026

2027

2028

2029

2030

2031

2032

2033

2034

2035

2036

2037

2038

2039

2040

2041

2042

2043

2044

2045

2046

2047

2048

2049

2050

2051

2052

2053

2054

2055

2056

2057

2058

2059

2060

2061

2062

2063

2064

2065

2066

2067

2068

2069

2070

2071

2072

2073

2074

2075

2076

2077

2078

2079

2080

2081

2082

2083

2084

2085

2086

2087

2088

2089

2090

2091

2092

2093

2094

2095

2096

2097

2098

2099

2100

2101

2102

2103

2104

2105

2106

2107

2108

2109

2110

2111

2112

2113

2114

2115

2116

2117

2118

2119

2120

2121

2122

2123

2124

2125

2126

2127

2128

2129

2130

2131

2132

2133

2134

2135

2136

2137

2138

2139

2140

2141

2142

2143

2144

2145

2146

2147

2148

2149

2150

2151

2152

2153

2154

2155

2156

2157

2158

2159

2160

2161

2162

2163

2164

2165

2166

2167

2168

2169

2170

2171

2172

2173

2174

2175

2176

2177

2178

2179

2180

2181

2182

2183

2184

2185

2186

2187

2188

2189

2190

2191

2192

2193

2194

2195

r""" 

Mini-AES 

 

A simplified variant of the Advanced Encryption Standard (AES). Note that 

Mini-AES is for educational purposes only. It is a small-scale version of 

the AES designed to help beginners understand the basic structure of AES. 

 

AUTHORS: 

 

- Minh Van Nguyen (2009-05): initial version 

""" 

 

########################################################################### 

# Copyright (c) 2009 Minh Van Nguyen <nguyenminh2@gmail.com> 

# 

# This program is free software; you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation; either version 2 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# http://www.gnu.org/licenses/ 

########################################################################### 

from six.moves import range 

 

from sage.matrix.matrix_dense import Matrix_dense 

from sage.matrix.matrix_space import MatrixSpace 

from sage.monoids.string_monoid import BinaryStrings 

from sage.monoids.string_monoid_element import StringMonoidElement 

from sage.rings.finite_rings.finite_field_constructor import FiniteField 

from sage.rings.integer import Integer 

from sage.structure.sage_object import SageObject 

 

class MiniAES(SageObject): 

r""" 

This class implements the Mini Advanced Encryption Standard (Mini-AES) 

described in [Pha2002]_. Note that Phan's Mini-AES is for educational purposes 

only and is not secure for practical purposes. Mini-AES is a version of 

the AES with all parameters significantly reduced, but at the same time 

preserving the structure of AES. The goal of Mini-AES is to allow a 

beginner to understand the structure of AES, thus laying a foundation 

for a thorough study of AES. Its goal is as a teaching tool and is 

different from the :mod:`SR <sage.crypto.mq.sr>` small scale variants 

of the AES. SR defines a family of parameterizable variants of the AES 

suitable as a framework for comparing different cryptanalytic techniques 

that can be brought to bear on the AES. 

 

EXAMPLES: 

 

Encrypt a plaintext:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(16, "x") 

sage: MS = MatrixSpace(K, 2, 2) 

sage: P = MS([K("x^3 + x"), K("x^2 + 1"), K("x^2 + x"), K("x^3 + x^2")]); P 

<BLANKLINE> 

[ x^3 + x x^2 + 1] 

[ x^2 + x x^3 + x^2] 

sage: key = MS([K("x^3 + x^2"), K("x^3 + x"), K("x^3 + x^2 + x"), K("x^2 + x + 1")]); key 

<BLANKLINE> 

[ x^3 + x^2 x^3 + x] 

[x^3 + x^2 + x x^2 + x + 1] 

sage: C = maes.encrypt(P, key); C 

<BLANKLINE> 

[ x x^2 + x] 

[x^3 + x^2 + x x^3 + x] 

 

Decrypt the result:: 

 

sage: plaintxt = maes.decrypt(C, key) 

sage: plaintxt; P 

<BLANKLINE> 

[ x^3 + x x^2 + 1] 

[ x^2 + x x^3 + x^2] 

<BLANKLINE> 

[ x^3 + x x^2 + 1] 

[ x^2 + x x^3 + x^2] 

sage: plaintxt == P 

True 

 

We can also work directly with binary strings:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: bin = BinaryStrings() 

sage: key = bin.encoding("KE"); key 

0100101101000101 

sage: P = bin.encoding("Encrypt this secret message!"); P 

01000101011011100110001101110010011110010111000001110100001000000111010001101000011010010111001100100000011100110110010101100011011100100110010101110100001000000110110101100101011100110111001101100001011001110110010100100001 

sage: C = maes(P, key, algorithm="encrypt"); C 

10001000101001101111000001111000010011001110110101000111011011010101001011101111101011001110011100100011101100101010100010100111110110011001010001000111011011010010000011000110001100000111000011100110101111000000001110001001 

sage: plaintxt = maes(C, key, algorithm="decrypt") 

sage: plaintxt == P 

True 

 

Now we work with integers `n` such that `0 \leq n \leq 15`:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: P = [n for n in range(16)]; P 

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 

sage: key = [2, 3, 11, 0]; key 

[2, 3, 11, 0] 

sage: P = maes.integer_to_binary(P); P 

0000000100100011010001010110011110001001101010111100110111101111 

sage: key = maes.integer_to_binary(key); key 

0010001110110000 

sage: C = maes(P, key, algorithm="encrypt"); C 

1100100000100011111001010101010101011011100111110001000011100001 

sage: plaintxt = maes(C, key, algorithm="decrypt") 

sage: plaintxt == P 

True 

 

Generate some random plaintext and a random secret key. Encrypt the 

plaintext using that secret key and decrypt the result. Then compare the 

decrypted plaintext with the original plaintext:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: MS = MatrixSpace(FiniteField(16, "x"), 2, 2) 

sage: P = MS.random_element() 

sage: key = maes.random_key() 

sage: C = maes.encrypt(P, key) 

sage: plaintxt = maes.decrypt(C, key) 

sage: plaintxt == P 

True 

""" 

 

def __init__(self): 

r""" 

A simplified variant of the Advanced Encryption Standard (AES). 

 

EXAMPLES:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES(); maes 

Mini-AES block cipher with 16-bit keys 

sage: MS = MatrixSpace(FiniteField(16, "x"), 2, 2) 

sage: P = MS.random_element() 

sage: key = maes.random_key() 

sage: C = maes.encrypt(P, key) 

sage: plaintxt = maes.decrypt(C, key) 

sage: plaintxt == P 

True 

""" 

from sage.crypto.sbox import SBox 

self._key_size = 16 # the number of bits in a secret key 

B = BinaryStrings() 

K = FiniteField(self._key_size, "x") 

# the S-box for encryption 

self._sboxE = SBox(14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7) 

# the S-box for decryption 

self._sboxD = SBox(14, 3, 4, 8, 1, 12, 10, 15, 7, 13, 9, 6, 11, 2, 0, 5) 

# nibble to finite field element 

self._bin_to_GF = { B("0000"): K("0"), 

B("0001"): K("1"), 

B("0010"): K("x"), 

B("0011"): K("x + 1"), 

B("0100"): K("x^2"), 

B("0101"): K("x^2 + 1"), 

B("0110"): K("x^2 + x"), 

B("0111"): K("x^2 + x + 1"), 

B("1000"): K("x^3"), 

B("1001"): K("x^3 + 1"), 

B("1010"): K("x^3 + x"), 

B("1011"): K("x^3 + x + 1"), 

B("1100"): K("x^3 + x^2"), 

B("1101"): K("x^3 + x^2 + 1"), 

B("1110"): K("x^3 + x^2 + x"), 

B("1111"): K("x^3 + x^2 + x+ 1") } 

# nibble to integer 

self._bin_to_int = { B("0000"): Integer(0), 

B("0001"): Integer(1), 

B("0010"): Integer(2), 

B("0011"): Integer(3), 

B("0100"): Integer(4), 

B("0101"): Integer(5), 

B("0110"): Integer(6), 

B("0111"): Integer(7), 

B("1000"): Integer(8), 

B("1001"): Integer(9), 

B("1010"): Integer(10), 

B("1011"): Integer(11), 

B("1100"): Integer(12), 

B("1101"): Integer(13), 

B("1110"): Integer(14), 

B("1111"): Integer(15) } 

# finite field element to nibble 

self._GF_to_bin = { K("0"): B("0000"), 

K("1"): B("0001"), 

K("x"): B("0010"), 

K("x + 1"): B("0011"), 

K("x^2"): B("0100"), 

K("x^2 + 1"): B("0101"), 

K("x^2 + x"): B("0110"), 

K("x^2 + x + 1"): B("0111"), 

K("x^3"): B("1000"), 

K("x^3 + 1"): B("1001"), 

K("x^3 + x"): B("1010"), 

K("x^3 + x + 1"): B("1011"), 

K("x^3 + x^2"): B("1100"), 

K("x^3 + x^2 + 1"): B("1101"), 

K("x^3 + x^2 + x"): B("1110"), 

K("x^3 + x^2 + x+ 1"): B("1111") } 

# finite field element to integer 

self._GF_to_int = { K("0"): Integer(0), 

K("1"): Integer(1), 

K("x"): Integer(2), 

K("x + 1"): Integer(3), 

K("x^2"): Integer(4), 

K("x^2 + 1"): Integer(5), 

K("x^2 + x"): Integer(6), 

K("x^2 + x + 1"): Integer(7), 

K("x^3"): Integer(8), 

K("x^3 + 1"): Integer(9), 

K("x^3 + x"): Integer(10), 

K("x^3 + x + 1"): Integer(11), 

K("x^3 + x^2"): Integer(12), 

K("x^3 + x^2 + 1"): Integer(13), 

K("x^3 + x^2 + x"): Integer(14), 

K("x^3 + x^2 + x+ 1"): Integer(15) } 

# integer to nibble 

self._int_to_bin = { Integer(0): B("0000"), 

Integer(1): B("0001"), 

Integer(2): B("0010"), 

Integer(3): B("0011"), 

Integer(4): B("0100"), 

Integer(5): B("0101"), 

Integer(6): B("0110"), 

Integer(7): B("0111"), 

Integer(8): B("1000"), 

Integer(9): B("1001"), 

Integer(10): B("1010"), 

Integer(11): B("1011"), 

Integer(12): B("1100"), 

Integer(13): B("1101"), 

Integer(14): B("1110"), 

Integer(15): B("1111") } 

# integer to finite field element 

self._int_to_GF = { Integer(0): K("0"), 

Integer(1): K("1"), 

Integer(2): K("x"), 

Integer(3): K("x + 1"), 

Integer(4): K("x^2"), 

Integer(5): K("x^2 + 1"), 

Integer(6): K("x^2 + x"), 

Integer(7): K("x^2 + x + 1"), 

Integer(8): K("x^3"), 

Integer(9): K("x^3 + 1"), 

Integer(10): K("x^3 + x"), 

Integer(11): K("x^3 + x + 1"), 

Integer(12): K("x^3 + x^2"), 

Integer(13): K("x^3 + x^2 + 1"), 

Integer(14): K("x^3 + x^2 + x"), 

Integer(15): K("x^3 + x^2 + x+ 1") } 

 

def __call__(self, B, key, algorithm="encrypt"): 

r""" 

Apply Mini-AES encryption or decryption on the binary string ``B`` 

using the key ``key``. The flag ``algorithm`` controls what action is 

to be performed on ``B``. 

 

INPUT: 

 

- ``B`` -- a binary string, where the number of bits is positive and 

a multiple of 16. The number of 16 bits is evenly divided into four 

nibbles. Hence 16 bits can be conveniently represented as a 

`2 \times 2` matrix block for encryption or decryption. 

 

- ``key`` -- a secret key; this must be a 16-bit binary string 

 

- ``algorithm`` -- (default: ``"encrypt"``) a string; a flag to signify 

whether encryption or decryption is to be applied to the binary 

string ``B``. The encryption flag is ``"encrypt"`` and the decryption 

flag is ``"decrypt"``. 

 

OUTPUT: 

 

- The ciphertext (respectively plaintext) corresponding to the 

binary string ``B``. 

 

EXAMPLES:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: bin = BinaryStrings() 

sage: key = bin.encoding("KE"); key 

0100101101000101 

sage: P = bin.encoding("Encrypt this secret message!"); P 

01000101011011100110001101110010011110010111000001110100001000000111010001101000011010010111001100100000011100110110010101100011011100100110010101110100001000000110110101100101011100110111001101100001011001110110010100100001 

sage: C = maes(P, key, algorithm="encrypt"); C 

10001000101001101111000001111000010011001110110101000111011011010101001011101111101011001110011100100011101100101010100010100111110110011001010001000111011011010010000011000110001100000111000011100110101111000000001110001001 

sage: plaintxt = maes(C, key, algorithm="decrypt") 

sage: plaintxt == P 

True 

 

TESTS: 

 

The binary string ``B`` must be non-empty and the number of bits must 

be a multiple of 16:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: maes("B", "key") 

Traceback (most recent call last): 

... 

TypeError: input B must be a non-empty binary string with number of bits a multiple of 16 

sage: bin = BinaryStrings() 

sage: B = bin.encoding("A") 

sage: maes(B, "key") 

Traceback (most recent call last): 

... 

ValueError: the number of bits in the binary string B must be positive and a multiple of 16 

 

The secret key ``key`` must be a 16-bit binary string:: 

 

sage: B = bin.encoding("ABCD") 

sage: maes(B, "key") 

Traceback (most recent call last): 

... 

TypeError: secret key must be a 16-bit binary string 

sage: key = bin.encoding("K") 

sage: maes(B, key) 

Traceback (most recent call last): 

... 

ValueError: secret key must be a 16-bit binary string 

 

The value for ``algorithm`` must be either ``"encrypt"`` or 

``"decrypt"``:: 

 

sage: B = bin.encoding("ABCD") 

sage: key = bin.encoding("KE") 

sage: maes(B, key, algorithm="ABC") 

Traceback (most recent call last): 

... 

ValueError: algorithm must be either 'encrypt' or 'decrypt' 

sage: maes(B, key, algorithm="e") 

Traceback (most recent call last): 

... 

ValueError: algorithm must be either 'encrypt' or 'decrypt' 

sage: maes(B, key, algorithm="d") 

Traceback (most recent call last): 

... 

ValueError: algorithm must be either 'encrypt' or 'decrypt' 

""" 

from sage.rings.finite_rings.integer_mod import Mod 

if not isinstance(B, StringMonoidElement): 

raise TypeError("input B must be a non-empty binary string with number of bits a multiple of 16") 

if (len(B) == 0) or (Mod(len(B), self._key_size).lift() != 0): 

raise ValueError("the number of bits in the binary string B must be positive and a multiple of 16") 

if not isinstance(key, StringMonoidElement): 

raise TypeError("secret key must be a 16-bit binary string") 

if len(key) != self._key_size: 

raise ValueError("secret key must be a 16-bit binary string") 

 

N = len(B) // self._key_size # the number of 16-bit blocks 

MS = MatrixSpace(FiniteField(self._key_size, "x"), 2, 2) 

bin = BinaryStrings() 

S = "" 

if algorithm == "encrypt": 

# encrypt each 16-bit block in succession 

for i in range(N): 

# here 16 is the number of bits per encryption block 

block = B[i*16 : (i+1)*16] 

matB = MS(self.binary_to_GF(block)) 

matK = MS(self.binary_to_GF(key)) 

e = self.encrypt(matB, matK) 

e = self.GF_to_binary(e) 

S = "".join([S, str(e)]) 

return bin(S) 

elif algorithm == "decrypt": 

# decrypt each 16-bit block in succession 

for i in range(N): 

# here 16 is the number of bits per encryption block 

block = B[i*16 : (i+1)*16] 

matB = MS(self.binary_to_GF(block)) 

matK = MS(self.binary_to_GF(key)) 

e = self.decrypt(matB, matK) 

e = self.GF_to_binary(e) 

S = "".join([S, str(e)]) 

return bin(S) 

else: 

raise ValueError("algorithm must be either 'encrypt' or 'decrypt'") 

 

def __eq__(self, other): 

r""" 

Compare ``self`` with ``other``. 

 

Mini-AES objects are the same if they have the same key size and 

the same S-boxes. 

 

EXAMPLES:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: m = MiniAES() 

sage: m == loads(dumps(m)) 

True 

""" 

return ( (self._key_size == other._key_size) and 

(self._sboxE == other._sboxE) and 

(self._sboxD == other._sboxD) ) 

 

def __repr__(self): 

r""" 

Return the string representation of self. 

 

EXAMPLES:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: m = MiniAES(); m 

Mini-AES block cipher with 16-bit keys 

""" 

return "Mini-AES block cipher with 16-bit keys" 

 

def add_key(self, block, rkey): 

r""" 

Return the matrix addition of ``block`` and ``rkey``. Both ``block`` 

and ``rkey`` are `2 \times 2` matrices over the finite field 

`\GF{2^4}`. This method just return the matrix addition of 

these two matrices. 

 

INPUT: 

 

- ``block`` -- a `2 \times 2` matrix with entries over 

`\GF{2^4}` 

 

- ``rkey`` -- a round key; a `2 \times 2` matrix with entries over 

`\GF{2^4}` 

 

OUTPUT: 

 

- The matrix addition of ``block`` and ``rkey``. 

 

EXAMPLES: 

 

We can work with elements of `\GF{2^4}`:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(16, "x") 

sage: MS = MatrixSpace(K, 2, 2) 

sage: D = MS([ [K("x^3 + x^2 + x + 1"), K("x^3 + x")], [K("0"), K("x^3 + x^2")] ]); D 

<BLANKLINE> 

[x^3 + x^2 + x + 1 x^3 + x] 

[ 0 x^3 + x^2] 

sage: k = MS([ [K("x^2 + 1"), K("x^3 + x^2 + x + 1")], [K("x + 1"), K("0")] ]); k 

<BLANKLINE> 

[ x^2 + 1 x^3 + x^2 + x + 1] 

[ x + 1 0] 

sage: maes.add_key(D, k) 

<BLANKLINE> 

[ x^3 + x x^2 + 1] 

[ x + 1 x^3 + x^2] 

 

Or work with binary strings:: 

 

sage: bin = BinaryStrings() 

sage: B = bin.encoding("We"); B 

0101011101100101 

sage: B = MS(maes.binary_to_GF(B)); B 

<BLANKLINE> 

[ x^2 + 1 x^2 + x + 1] 

[ x^2 + x x^2 + 1] 

sage: key = bin.encoding("KY"); key 

0100101101011001 

sage: key = MS(maes.binary_to_GF(key)); key 

<BLANKLINE> 

[ x^2 x^3 + x + 1] 

[ x^2 + 1 x^3 + 1] 

sage: maes.add_key(B, key) 

<BLANKLINE> 

[ 1 x^3 + x^2] 

[ x + 1 x^3 + x^2] 

 

We can also work with integers `n` such that `0 \leq n \leq 15`:: 

 

sage: N = [2, 3, 5, 7]; N 

[2, 3, 5, 7] 

sage: key = [9, 11, 13, 15]; key 

[9, 11, 13, 15] 

sage: N = MS(maes.integer_to_GF(N)); N 

<BLANKLINE> 

[ x x + 1] 

[ x^2 + 1 x^2 + x + 1] 

sage: key = MS(maes.integer_to_GF(key)); key 

<BLANKLINE> 

[ x^3 + 1 x^3 + x + 1] 

[ x^3 + x^2 + 1 x^3 + x^2 + x + 1] 

sage: maes.add_key(N, key) 

<BLANKLINE> 

[x^3 + x + 1 x^3] 

[ x^3 x^3] 

 

TESTS: 

 

The input block and key must each be a matrix:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(16, "x") 

sage: MSB = MatrixSpace(K, 2, 2) 

sage: B = MSB([ [K("x^3 + 1"), K("x^2 + x")], [K("x^3 + x^2"), K("x + 1")] ]) 

sage: maes.add_key(B, "key") 

Traceback (most recent call last): 

... 

TypeError: round key must be a 2 x 2 matrix over GF(16) 

sage: maes.add_key("block", "key") 

Traceback (most recent call last): 

... 

TypeError: input block must be a 2 x 2 matrix over GF(16) 

 

In addition, the dimensions of the input matrices must each be 

`2 \times 2`:: 

 

sage: MSB = MatrixSpace(K, 1, 2) 

sage: B = MSB([ [K("x^3 + 1"), K("x^2 + x")] ]) 

sage: maes.add_key(B, "key") 

Traceback (most recent call last): 

... 

TypeError: input block must be a 2 x 2 matrix over GF(16) 

sage: MSB = MatrixSpace(K, 2, 2) 

sage: B = MSB([ [K("x^3 + 1"), K("x^2 + x")], [K("x^3 + x^2"), K("x + 1")] ]) 

sage: MSK = MatrixSpace(K, 1, 2) 

sage: key = MSK([ [K("x^3 + x^2"), K("x^3 + x^2 + x + 1")]]) 

sage: maes.add_key(B, key) 

Traceback (most recent call last): 

... 

TypeError: round key must be a 2 x 2 matrix over GF(16) 

""" 

if not isinstance(block, Matrix_dense) or \ 

not (block.base_ring().order() == 16 and block.base_ring().is_field()): 

raise TypeError("input block must be a 2 x 2 matrix over GF(16)") 

if not (block.nrows() == block.ncols() == 2): 

raise TypeError("input block must be a 2 x 2 matrix over GF(16)") 

 

if not isinstance(rkey, Matrix_dense) or \ 

not (rkey.base_ring().order() == 16 and rkey.base_ring().is_field()): 

raise TypeError("round key must be a 2 x 2 matrix over GF(16)") 

if not (rkey.nrows() == rkey.ncols() == 2): 

raise TypeError("round key must be a 2 x 2 matrix over GF(16)") 

 

return block + rkey 

 

def block_length(self): 

r""" 

Return the block length of Phan's Mini-AES block cipher. A key in 

Phan's Mini-AES is a block of 16 bits. Each nibble of a key can be 

considered as an element of the finite field `\GF{2^4}`. 

Therefore the key consists of four elements from `\GF{2^4}`. 

 

OUTPUT: 

 

- The block (or key) length in number of bits. 

 

EXAMPLES:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: maes.block_length() 

16 

""" 

return self._key_size 

 

def decrypt(self, C, key): 

r""" 

Use Phan's Mini-AES to decrypt the ciphertext ``C`` with the secret 

key ``key``. Both ``C`` and ``key`` must be `2 \times 2` matrices over 

the finite field `\GF{2^4}`. Let `\gamma` denote the 

operation of nibble-sub, `\pi` denote shift-row, `\theta` denote 

mix-column, and `\sigma_{K_i}` denote add-key with the round key 

`K_i`. Then decryption `D` using Phan's Mini-AES is the function 

composition 

 

.. MATH:: 

 

D = \sigma_{K_0} \circ \gamma^{-1} \circ \pi \circ \theta \circ \sigma_{K_1} \circ \gamma^{-1} \circ \pi \circ \sigma_{K_2} 

 

where `\gamma^{-1}` is the nibble-sub operation that uses the S-box 

for decryption, and the order of execution is from right to left. 

 

INPUT: 

 

- ``C`` -- a ciphertext block; must be a `2 \times 2` matrix over 

the finite field `\GF{2^4}` 

 

- ``key`` -- a secret key for this Mini-AES block cipher; must be a 

`2 \times 2` matrix over the finite field `\GF{2^4}` 

 

OUTPUT: 

 

- The plaintext corresponding to ``C``. 

 

EXAMPLES: 

 

We encrypt a plaintext, decrypt the ciphertext, then compare the 

decrypted plaintext with the original plaintext. Here we work with 

elements of `\GF{2^4}`:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(16, "x") 

sage: MS = MatrixSpace(K, 2, 2) 

sage: P = MS([ [K("x^3 + 1"), K("x^2 + x")], [K("x^3 + x^2"), K("x + 1")] ]); P 

<BLANKLINE> 

[ x^3 + 1 x^2 + x] 

[x^3 + x^2 x + 1] 

sage: key = MS([ [K("x^3 + x^2"), K("x^3 + x^2 + x + 1")], [K("x + 1"), K("0")] ]); key 

<BLANKLINE> 

[ x^3 + x^2 x^3 + x^2 + x + 1] 

[ x + 1 0] 

sage: C = maes.encrypt(P, key); C 

<BLANKLINE> 

[x^2 + x + 1 x^3 + x^2] 

[ x x^2 + x] 

sage: plaintxt = maes.decrypt(C, key) 

sage: plaintxt; P 

<BLANKLINE> 

[ x^3 + 1 x^2 + x] 

[x^3 + x^2 x + 1] 

<BLANKLINE> 

[ x^3 + 1 x^2 + x] 

[x^3 + x^2 x + 1] 

sage: plaintxt == P 

True 

 

But we can also work with binary strings:: 

 

sage: bin = BinaryStrings() 

sage: P = bin.encoding("de"); P 

0110010001100101 

sage: P = MS(maes.binary_to_GF(P)); P 

<BLANKLINE> 

[x^2 + x x^2] 

[x^2 + x x^2 + 1] 

sage: key = bin.encoding("ke"); key 

0110101101100101 

sage: key = MS(maes.binary_to_GF(key)); key 

<BLANKLINE> 

[ x^2 + x x^3 + x + 1] 

[ x^2 + x x^2 + 1] 

sage: C = maes.encrypt(P, key) 

sage: plaintxt = maes.decrypt(C, key) 

sage: plaintxt == P 

True 

 

Here we work with integers `n` such that `0 \leq n \leq 15`:: 

 

sage: P = [3, 5, 7, 14]; P 

[3, 5, 7, 14] 

sage: key = [2, 6, 7, 8]; key 

[2, 6, 7, 8] 

sage: P = MS(maes.integer_to_GF(P)); P 

<BLANKLINE> 

[ x + 1 x^2 + 1] 

[ x^2 + x + 1 x^3 + x^2 + x] 

sage: key = MS(maes.integer_to_GF(key)); key 

<BLANKLINE> 

[ x x^2 + x] 

[x^2 + x + 1 x^3] 

sage: C = maes.encrypt(P, key) 

sage: plaintxt = maes.decrypt(C, key) 

sage: plaintxt == P 

True 

 

TESTS: 

 

The input block must be a matrix:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(16, "x") 

sage: MS = MatrixSpace(K, 2, 2) 

sage: key = MS([ [K("x^3 + x^2"), K("x^3 + x^2 + x + 1")], [K("x + 1"), K("0")] ]) 

sage: maes.decrypt("C", key) 

Traceback (most recent call last): 

... 

TypeError: ciphertext block must be a 2 x 2 matrix over GF(16) 

sage: C = MS([ [K("x^3 + 1"), K("x^2 + x")], [K("x^3 + x^2"), K("x + 1")] ]) 

sage: maes.decrypt(C, "key") 

Traceback (most recent call last): 

... 

TypeError: secret key must be a 2 x 2 matrix over GF(16) 

 

In addition, the dimensions of the input matrices must be 

`2 \times 2`:: 

 

sage: MS = MatrixSpace(K, 1, 2) 

sage: C = MS([ [K("x^3 + 1"), K("x^2 + x")]]) 

sage: maes.decrypt(C, "key") 

Traceback (most recent call last): 

... 

TypeError: ciphertext block must be a 2 x 2 matrix over GF(16) 

sage: MSC = MatrixSpace(K, 2, 2) 

sage: C = MSC([ [K("x^3 + 1"), K("x^2 + x")], [K("x^3 + x^2"), K("x + 1")] ]) 

sage: MSK = MatrixSpace(K, 1, 2) 

sage: key = MSK([ [K("x^3 + x^2"), K("x^3 + x^2 + x + 1")]]) 

sage: maes.decrypt(C, key) 

Traceback (most recent call last): 

... 

TypeError: secret key must be a 2 x 2 matrix over GF(16) 

""" 

if not isinstance(C, Matrix_dense) or \ 

not (C.base_ring().order() == 16 and C.base_ring().is_field()): 

raise TypeError("ciphertext block must be a 2 x 2 matrix over GF(16)") 

if not (C.nrows() == C.ncols() == 2): 

raise TypeError("ciphertext block must be a 2 x 2 matrix over GF(16)") 

if not isinstance(key, Matrix_dense) or \ 

not (key.base_ring().order() == 16 and key.base_ring().is_field()): 

raise TypeError("secret key must be a 2 x 2 matrix over GF(16)") 

if not (key.nrows() == key.ncols() == 2): 

raise TypeError("secret key must be a 2 x 2 matrix over GF(16)") 

 

# pre-compute the round keys 

rkey0 = self.round_key(key, 0) 

rkey1 = self.round_key(key, 1) 

rkey2 = self.round_key(key, 2) 

 

# now proceed with decrypting the ciphertext 

 

# undo the result of round 2 

plaintext = self.add_key(C, rkey2) 

plaintext = self.shift_row(plaintext) 

plaintext = self.nibble_sub(plaintext, algorithm="decrypt") 

 

# undo the result of round 1 

plaintext = self.add_key(plaintext, rkey1) 

plaintext = self.mix_column(plaintext) 

plaintext = self.shift_row(plaintext) 

plaintext = self.nibble_sub(plaintext, algorithm="decrypt") 

 

# undo the result of round 0 

plaintext = self.add_key(plaintext, rkey0) 

 

return plaintext 

 

def encrypt(self, P, key): 

r""" 

Use Phan's Mini-AES to encrypt the plaintext ``P`` with the secret 

key ``key``. Both ``P`` and ``key`` must be `2 \times 2` matrices 

over the finite field `\GF{2^4}`. Let `\gamma` denote the 

operation of nibble-sub, `\pi` denote shift-row, `\theta` denote 

mix-column, and `\sigma_{K_i}` denote add-key with the round key 

`K_i`. Then encryption `E` using Phan's Mini-AES is the function 

composition 

 

.. MATH:: 

 

E = \sigma_{K_2} \circ \pi \circ \gamma \circ \sigma_{K_1} \circ \theta \circ \pi \circ \gamma \circ \sigma_{K_0} 

 

where the order of execution is from right to left. Note that 

`\gamma` is the nibble-sub operation that uses the S-box for 

encryption. 

 

INPUT: 

 

- ``P`` -- a plaintext block; must be a `2 \times 2` matrix over 

the finite field `\GF{2^4}` 

 

- ``key`` -- a secret key for this Mini-AES block cipher; must be a 

`2 \times 2` matrix over the finite field `\GF{2^4}` 

 

OUTPUT: 

 

- The ciphertext corresponding to ``P``. 

 

EXAMPLES: 

 

Here we work with elements of `\GF{2^4}`:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(16, "x") 

sage: MS = MatrixSpace(K, 2, 2) 

sage: P = MS([ [K("x^3 + 1"), K("x^2 + x")], [K("x^3 + x^2"), K("x + 1")] ]); P 

<BLANKLINE> 

[ x^3 + 1 x^2 + x] 

[x^3 + x^2 x + 1] 

sage: key = MS([ [K("x^3 + x^2"), K("x^3 + x^2 + x + 1")], [K("x + 1"), K("0")] ]); key 

<BLANKLINE> 

[ x^3 + x^2 x^3 + x^2 + x + 1] 

[ x + 1 0] 

sage: maes.encrypt(P, key) 

<BLANKLINE> 

[x^2 + x + 1 x^3 + x^2] 

[ x x^2 + x] 

 

But we can also work with binary strings:: 

 

sage: bin = BinaryStrings() 

sage: P = bin.encoding("de"); P 

0110010001100101 

sage: P = MS(maes.binary_to_GF(P)); P 

<BLANKLINE> 

[x^2 + x x^2] 

[x^2 + x x^2 + 1] 

sage: key = bin.encoding("ke"); key 

0110101101100101 

sage: key = MS(maes.binary_to_GF(key)); key 

<BLANKLINE> 

[ x^2 + x x^3 + x + 1] 

[ x^2 + x x^2 + 1] 

sage: C = maes.encrypt(P, key) 

sage: plaintxt = maes.decrypt(C, key) 

sage: plaintxt == P 

True 

 

Now we work with integers `n` such that `0 \leq n \leq 15`:: 

 

sage: P = [1, 5, 8, 12]; P 

[1, 5, 8, 12] 

sage: key = [5, 9, 15, 0]; key 

[5, 9, 15, 0] 

sage: P = MS(maes.integer_to_GF(P)); P 

<BLANKLINE> 

[ 1 x^2 + 1] 

[ x^3 x^3 + x^2] 

sage: key = MS(maes.integer_to_GF(key)); key 

<BLANKLINE> 

[ x^2 + 1 x^3 + 1] 

[x^3 + x^2 + x + 1 0] 

sage: C = maes.encrypt(P, key) 

sage: plaintxt = maes.decrypt(C, key) 

sage: plaintxt == P 

True 

 

TESTS: 

 

The input block must be a matrix:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(16, "x") 

sage: MS = MatrixSpace(K, 2, 2) 

sage: key = MS([ [K("x^3 + x^2"), K("x^3 + x^2 + x + 1")], [K("x + 1"), K("0")] ]) 

sage: maes.encrypt("P", key) 

Traceback (most recent call last): 

... 

TypeError: plaintext block must be a 2 x 2 matrix over GF(16) 

sage: P = MS([ [K("x^3 + 1"), K("x^2 + x")], [K("x^3 + x^2"), K("x + 1")] ]) 

sage: maes.encrypt(P, "key") 

Traceback (most recent call last): 

... 

TypeError: secret key must be a 2 x 2 matrix over GF(16) 

 

In addition, the dimensions of the input matrices must be 

`2 \times 2`:: 

 

sage: MS = MatrixSpace(K, 1, 2) 

sage: P = MS([ [K("x^3 + 1"), K("x^2 + x")]]) 

sage: maes.encrypt(P, "key") 

Traceback (most recent call last): 

... 

TypeError: plaintext block must be a 2 x 2 matrix over GF(16) 

sage: MSP = MatrixSpace(K, 2, 2) 

sage: P = MSP([ [K("x^3 + 1"), K("x^2 + x")], [K("x^3 + x^2"), K("x + 1")] ]) 

sage: MSK = MatrixSpace(K, 1, 2) 

sage: key = MSK([ [K("x^3 + x^2"), K("x^3 + x^2 + x + 1")]]) 

sage: maes.encrypt(P, key) 

Traceback (most recent call last): 

... 

TypeError: secret key must be a 2 x 2 matrix over GF(16) 

""" 

if not isinstance(P, Matrix_dense) or \ 

not (P.base_ring().order() == 16 and P.base_ring().is_field()): 

raise TypeError("plaintext block must be a 2 x 2 matrix over GF(16)") 

if not (P.nrows() == P.ncols() == 2): 

raise TypeError("plaintext block must be a 2 x 2 matrix over GF(16)") 

if not isinstance(key, Matrix_dense) or \ 

not (key.base_ring().order() == 16 and key.base_ring().is_field()): 

raise TypeError("secret key must be a 2 x 2 matrix over GF(16)") 

if not (key.nrows() == key.ncols() == 2): 

raise TypeError("secret key must be a 2 x 2 matrix over GF(16)") 

 

# pre-compute the round keys 

rkey0 = self.round_key(key, 0) 

rkey1 = self.round_key(key, 1) 

rkey2 = self.round_key(key, 2) 

 

# now proceed with encrypting the plaintext 

 

# round 0 

ciphertext = self.add_key(P, rkey0) 

 

# round 1 

ciphertext = self.nibble_sub(ciphertext, algorithm="encrypt") 

ciphertext = self.shift_row(ciphertext) 

ciphertext = self.mix_column(ciphertext) 

ciphertext = self.add_key(ciphertext, rkey1) 

 

# round 2 

ciphertext = self.nibble_sub(ciphertext, algorithm="encrypt") 

ciphertext = self.shift_row(ciphertext) 

ciphertext = self.add_key(ciphertext, rkey2) 

 

return ciphertext 

 

def mix_column(self, block): 

r""" 

Return the matrix multiplication of ``block`` with a constant matrix. 

The constant matrix is 

 

.. MATH:: 

 

\begin{bmatrix} 

x + 1 & x \\ 

x & x + 1 

\end{bmatrix} 

 

If the input block is 

 

.. MATH:: 

 

\begin{bmatrix} 

c_0 & c_2 \\ 

c_1 & c_3 

\end{bmatrix} 

 

then the output block is 

 

.. MATH:: 

 

\begin{bmatrix} 

d_0 & d_2 \\ 

d_1 & d_3 

\end{bmatrix} 

= 

\begin{bmatrix} 

x + 1 & x \\ 

x & x + 1 

\end{bmatrix} 

\begin{bmatrix} 

c_0 & c_2 \\ 

c_1 & c_3 

\end{bmatrix} 

 

INPUT: 

 

- ``block`` -- a `2 \times 2` matrix with entries over 

`\GF{2^4}` 

 

OUTPUT: 

 

- A `2 \times 2` matrix resulting from multiplying the above constant 

matrix with the input matrix ``block``. 

 

EXAMPLES: 

 

Here we work with elements of `\GF{2^4}`:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(16, "x") 

sage: MS = MatrixSpace(K, 2, 2) 

sage: mat = MS([ [K("x^2 + x + 1"), K("x^3 + x^2 + 1")], [K("x^3"), K("x")] ]) 

sage: maes.mix_column(mat) 

<BLANKLINE> 

[ x^3 + x 0] 

[ x^2 + 1 x^3 + x^2 + x + 1] 

 

Multiplying by the identity matrix should leave the constant matrix 

unchanged:: 

 

sage: eye = MS([ [K("1"), K("0")], [K("0"), K("1")] ]) 

sage: maes.mix_column(eye) 

<BLANKLINE> 

[x + 1 x] 

[ x x + 1] 

 

We can also work with binary strings:: 

 

sage: bin = BinaryStrings() 

sage: B = bin.encoding("rT"); B 

0111001001010100 

sage: B = MS(maes.binary_to_GF(B)); B 

<BLANKLINE> 

[x^2 + x + 1 x] 

[ x^2 + 1 x^2] 

sage: maes.mix_column(B) 

<BLANKLINE> 

[ x + 1 x^3 + x^2 + x] 

[ 1 x^3] 

 

We can also work with integers `n` such that `0 \leq n \leq 15`:: 

 

sage: P = [10, 5, 2, 7]; P 

[10, 5, 2, 7] 

sage: P = MS(maes.integer_to_GF(P)); P 

<BLANKLINE> 

[ x^3 + x x^2 + 1] 

[ x x^2 + x + 1] 

sage: maes.mix_column(P) 

<BLANKLINE> 

[x^3 + 1 1] 

[ 1 x + 1] 

 

TESTS: 

 

The input block must be a matrix:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: maes.mix_column("mat") 

Traceback (most recent call last): 

... 

TypeError: input block must be a 2 x 2 matrix over GF(16) 

 

In addition, the dimensions of the input matrix must be `2 \times 2`:: 

 

sage: K = FiniteField(16, "x") 

sage: MS = MatrixSpace(K, 1, 2) 

sage: mat = MS([[K("x^3 + x^2 + x + 1"), K("0")]]) 

sage: maes.mix_column(mat) 

Traceback (most recent call last): 

... 

TypeError: input block must be a 2 x 2 matrix over GF(16) 

""" 

if not isinstance(block, Matrix_dense) or \ 

not (block.base_ring().order() == 16 and block.base_ring().is_field()): 

raise TypeError("input block must be a 2 x 2 matrix over GF(16)") 

if not (block.nrows() == block.ncols() == 2): 

raise TypeError("input block must be a 2 x 2 matrix over GF(16)") 

 

K = FiniteField(self._key_size, "x") 

MS = MatrixSpace(K, 2, 2) 

M = MS( [ [K("x + 1"), K("x")], 

[K("x"), K("x + 1")] ] ) 

return M * block 

 

def nibble_sub(self, block, algorithm="encrypt"): 

r""" 

Substitute a nibble (or a block of 4 bits) using the following S-box: 

 

.. MATH:: 

 

\begin{tabular}{ll|ll} \hline 

Input & Output & Input & Output \\\hline 

0000 & 1110 & 1000 & 0011 \\ 

0001 & 0100 & 1001 & 1010 \\ 

0010 & 1101 & 1010 & 0110 \\ 

0011 & 0001 & 1011 & 1100 \\ 

0100 & 0010 & 1100 & 0101 \\ 

0101 & 1111 & 1101 & 1001 \\ 

0110 & 1011 & 1110 & 0000 \\ 

0111 & 1000 & 1111 & 0111 \\\hline 

\end{tabular} 

 

The values in the above S-box are taken from the first row of the first 

S-box of the Data Encryption Standard (DES). Each nibble can be 

thought of as an element of the finite field `\GF{2^4}` of 16 

elements. Thus in terms of `\GF{2^4}`, the S-box can also be 

specified as: 

 

.. MATH:: 

 

\begin{tabular}{ll} \hline 

Input & Output \\\hline 

$0$ & $x^3 + x^2 + x$ \\ 

$1$ & $x^2$ \\ 

$x$ & $x^3 + x^2 + 1$ \\ 

$x + 1$ & $1$ \\ 

$x^2$ & $x$ \\ 

$x^2 + 1$ & $x^3 + x^2 + x + 1$ \\ 

$x^2 + x$ & $x^3 + x + 1$ \\ 

$x^2 + x + 1$ & $x^3$ \\ 

$x^3$ & $x + 1$ \\ 

$x^3 + 1$ & $x^3 + x$ \\ 

$x^3 + x$ & $x^2 + x$ \\ 

$x^3 + x + 1$ & $x^3 + x^2$ \\ 

$x^3 + x^2$ & $x^2 + 1$ \\ 

$x^3 + x^2 + 1$ & $x^3 + 1$ \\ 

$x^3 + x^2 + x$ & $0$ \\ 

$x^3 + x^2 + x + 1$ & $x^2 + x + 1$ \\\hline 

\end{tabular} 

 

Note that the above S-box is used for encryption. The S-box for 

decryption is obtained from the above S-box by reversing the role of 

the Input and Output columns. Thus the previous Input column for 

encryption now becomes the Output column for decryption, and the 

previous Output column for encryption is now the Input column for 

decryption. The S-box used for decryption can be specified as: 

 

.. MATH:: 

 

\begin{tabular}{ll} \hline 

Input & Output \\\hline 

$0$ & $x^3 + x^2 + x$ \\ 

$1$ & $x + 1$ \\ 

$x$ & $x^2$ \\ 

$x + 1$ & $x^3$ \\ 

$x^2$ & $1$ \\ 

$x^2 + 1$ & $x^3 + x^2$ \\ 

$x^2 + x$ & $x^3 + x$ \\ 

$x^2 + x + 1$ & $x^3 + x^2 + x + 1$ \\ 

$x^3$ & $x^2 + x + 1$ \\ 

$x^3 + 1$ & $x^3 + x^2 + 1$ \\ 

$x^3 + x$ & $x^3 + 1$ \\ 

$x^3 + x + 1$ & $x^2 + x$ \\ 

$x^3 + x^2$ & $x^3 + x + 1$ \\ 

$x^3 + x^2 + 1$ & $x$ \\ 

$x^3 + x^2 + x$ & $0$ \\ 

$x^3 + x^2 + x + 1$ & $x^2 + 1$ \\\hline 

\end{tabular} 

 

INPUT: 

 

- ``block`` -- a `2 \times 2` matrix with entries over 

`\GF{2^4}` 

 

- ``algorithm`` -- (default: ``"encrypt"``) a string; a flag to signify 

whether this nibble-sub operation is used for encryption or 

decryption. The encryption flag is ``"encrypt"`` and the decryption 

flag is ``"decrypt"``. 

 

OUTPUT: 

 

- A `2 \times 2` matrix resulting from applying an S-box on 

entries of the `2 \times 2` matrix ``block``. 

 

EXAMPLES: 

 

Here we work with elements of the finite field `\GF{2^4}`:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(16, "x") 

sage: MS = MatrixSpace(K, 2, 2) 

sage: mat = MS([[K("x^3 + x^2 + x + 1"), K("0")], [K("x^2 + x + 1"), K("x^3 + x")]]) 

sage: maes.nibble_sub(mat, algorithm="encrypt") 

<BLANKLINE> 

[ x^2 + x + 1 x^3 + x^2 + x] 

[ x^3 x^2 + x] 

 

But we can also work with binary strings:: 

 

sage: bin = BinaryStrings() 

sage: B = bin.encoding("bi"); B 

0110001001101001 

sage: B = MS(maes.binary_to_GF(B)); B 

<BLANKLINE> 

[x^2 + x x] 

[x^2 + x x^3 + 1] 

sage: maes.nibble_sub(B, algorithm="encrypt") 

<BLANKLINE> 

[ x^3 + x + 1 x^3 + x^2 + 1] 

[ x^3 + x + 1 x^3 + x] 

sage: maes.nibble_sub(B, algorithm="decrypt") 

<BLANKLINE> 

[ x^3 + x x^2] 

[ x^3 + x x^3 + x^2 + 1] 

 

Here we work with integers `n` such that `0 \leq n \leq 15`:: 

 

sage: P = [2, 6, 8, 14]; P 

[2, 6, 8, 14] 

sage: P = MS(maes.integer_to_GF(P)); P 

<BLANKLINE> 

[ x x^2 + x] 

[ x^3 x^3 + x^2 + x] 

sage: maes.nibble_sub(P, algorithm="encrypt") 

<BLANKLINE> 

[x^3 + x^2 + 1 x^3 + x + 1] 

[ x + 1 0] 

sage: maes.nibble_sub(P, algorithm="decrypt") 

<BLANKLINE> 

[ x^2 x^3 + x] 

[x^2 + x + 1 0] 

 

TESTS: 

 

The input block must be a matrix:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: maes.nibble_sub("mat") 

Traceback (most recent call last): 

... 

TypeError: input block must be a 2 x 2 matrix over GF(16) 

 

In addition, the dimensions of the input matrix must be `2 \times 2`:: 

 

sage: K = FiniteField(16, "x") 

sage: MS = MatrixSpace(K, 1, 2) 

sage: mat = MS([[K("x^3 + x^2 + x + 1"), K("0")]]) 

sage: maes.nibble_sub(mat) 

Traceback (most recent call last): 

... 

TypeError: input block must be a 2 x 2 matrix over GF(16) 

 

The value for the option ``algorithm`` must be either the string 

``"encrypt"`` or ``"decrypt"``:: 

 

sage: K = FiniteField(16, "x") 

sage: MS = MatrixSpace(K, 2, 2) 

sage: mat = MS([[K("x^3 + x^2 + x + 1"), K("0")], [K("x^2 + x + 1"), K("x^3 + x")]]) 

sage: maes.nibble_sub(mat, algorithm="abc") 

Traceback (most recent call last): 

... 

ValueError: the algorithm for nibble-sub must be either 'encrypt' or 'decrypt' 

sage: maes.nibble_sub(mat, algorithm="e") 

Traceback (most recent call last): 

... 

ValueError: the algorithm for nibble-sub must be either 'encrypt' or 'decrypt' 

sage: maes.nibble_sub(mat, algorithm="d") 

Traceback (most recent call last): 

... 

ValueError: the algorithm for nibble-sub must be either 'encrypt' or 'decrypt' 

""" 

if not isinstance(block, Matrix_dense) or \ 

not (block.base_ring().order() == 16 and block.base_ring().is_field()): 

raise TypeError("input block must be a 2 x 2 matrix over GF(16)") 

if not (block.nrows() == block.ncols() == 2): 

raise TypeError("input block must be a 2 x 2 matrix over GF(16)") 

 

MS = MatrixSpace(FiniteField(self._key_size, "x"), 2, 2) 

# get the integer representation of each GF(2^4) element 

# in the input matrix block 

lst = [self._GF_to_int[block[i][j]] for i in range(block.nrows()) for j in range(block.ncols())] 

if algorithm == "encrypt": 

# Now run each resulting integer through the S-box for 

# encryption. Then convert the result output by the S-box 

# to an element of GF(2^4). 

return MS([self._int_to_GF[self._sboxE[e]] for e in lst]) 

elif algorithm == "decrypt": 

# Now run each resulting integer through the S-box for 

# decryption. Then convert the result output by the S-box 

# to an element of GF(2^4). 

return MS([self._int_to_GF[self._sboxD[e]] for e in lst]) 

else: 

raise ValueError("the algorithm for nibble-sub must be either 'encrypt' or 'decrypt'") 

 

def random_key(self): 

r""" 

A random key within the key space of this Mini-AES block cipher. Like 

the AES, Phan's Mini-AES is a symmetric-key block cipher. A Mini-AES 

key is a block of 16 bits, or a `2 \times 2` matrix with entries over 

the finite field `\GF{2^4}`. Thus the number of possible keys is 

`2^{16} = 16^4`. 

 

OUTPUT: 

 

- A `2 \times 2` matrix over the finite field `\GF{2^4}`, used 

as a secret key for this Mini-AES block cipher. 

 

EXAMPLES: 

 

Each nibble of a key is an element of the finite field 

`\GF{2^4}`:: 

 

sage: K = FiniteField(16, "x") 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: key = maes.random_key() 

sage: [key[i][j] in K for i in range(key.nrows()) for j in range(key.ncols())] 

[True, True, True, True] 

 

Generate a random key, then perform encryption and decryption using 

that key:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(16, "x") 

sage: MS = MatrixSpace(K, 2, 2) 

sage: key = maes.random_key() 

sage: P = MS.random_element() 

sage: C = maes.encrypt(P, key) 

sage: plaintxt = maes.decrypt(C, key) 

sage: plaintxt == P 

True 

""" 

MS = MatrixSpace(FiniteField(16, "x"), 2, 2) 

return MS.random_element() 

 

def round_key(self, key, n): 

r""" 

Return the round key for round ``n``. Phan's Mini-AES is defined to 

have two rounds. The round key `K_0` is generated and used prior to 

the first round, with round keys `K_1` and `K_2` being used in rounds 

1 and 2 respectively. In total, there are three round keys, each 

generated from the secret key ``key``. 

 

INPUT: 

 

- ``key`` -- the secret key 

 

- ``n`` -- non-negative integer; the round number 

 

OUTPUT: 

 

- The `n`-th round key. 

 

EXAMPLES: 

 

Obtaining the round keys from the secret key:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(16, "x") 

sage: MS = MatrixSpace(K, 2, 2) 

sage: key = MS([ [K("x^3 + x^2"), K("x^3 + x^2 + x + 1")], [K("x + 1"), K("0")] ]) 

sage: maes.round_key(key, 0) 

<BLANKLINE> 

[ x^3 + x^2 x^3 + x^2 + x + 1] 

[ x + 1 0] 

sage: key 

<BLANKLINE> 

[ x^3 + x^2 x^3 + x^2 + x + 1] 

[ x + 1 0] 

sage: maes.round_key(key, 1) 

<BLANKLINE> 

[ x + 1 x^3 + x^2 + x + 1] 

[ 0 x^3 + x^2 + x + 1] 

sage: maes.round_key(key, 2) 

<BLANKLINE> 

[x^2 + x x^3 + 1] 

[x^2 + x x^2 + x] 

 

TESTS: 

 

Only two rounds are defined for this AES variant:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(16, "x") 

sage: MS = MatrixSpace(K, 2, 2) 

sage: key = MS([ [K("x^3 + x^2"), K("x^3 + x^2 + x + 1")], [K("x + 1"), K("0")] ]) 

sage: maes.round_key(key, -1) 

Traceback (most recent call last): 

... 

ValueError: Mini-AES only defines two rounds 

sage: maes.round_key(key, 3) 

Traceback (most recent call last): 

... 

ValueError: Mini-AES only defines two rounds 

 

The input key must be a matrix:: 

 

sage: maes.round_key("key", 0) 

Traceback (most recent call last): 

... 

TypeError: secret key must be a 2 x 2 matrix over GF(16) 

 

In addition, the dimensions of the key matrix must be `2 \times 2`:: 

 

sage: K = FiniteField(16, "x") 

sage: MS = MatrixSpace(K, 1, 2) 

sage: key = MS([[K("x^3 + x^2 + x + 1"), K("0")]]) 

sage: maes.round_key(key, 2) 

Traceback (most recent call last): 

... 

TypeError: secret key must be a 2 x 2 matrix over GF(16) 

""" 

if not isinstance(key, Matrix_dense) or \ 

not (key.base_ring().order() == 16 and key.base_ring().is_field()): 

raise TypeError("secret key must be a 2 x 2 matrix over GF(16)") 

if not (key.nrows() == key.ncols() == 2): 

raise TypeError("secret key must be a 2 x 2 matrix over GF(16)") 

 

K = FiniteField(self._key_size, "x") 

MS = MatrixSpace(K, 2, 2) 

# round 0 

if n == 0: 

return key 

# round 1 

if n == 1: 

round_constant_1 = K("1") 

w4 = key[0][0] + self._sboxE[key[1][1]] + round_constant_1 

w5 = key[1][0] + w4 

w6 = key[0][1] + w5 

w7 = key[1][1] + w6 

return MS([ [w4, w6], [w5, w7] ]) 

# round 2 

if n == 2: 

round_constant_2 = K("x") 

key1 = self.round_key(key, 1) 

w8 = key1[0][0] + self._sboxE[key1[1][1]] + round_constant_2 

w9 = key1[1][0] + w8 

w10 = key1[0][1] + w9 

w11 = key1[1][1] + w10 

return MS([ [w8, w10], [w9, w11] ]) 

# unsupported round number 

if (n < 0) or (n > 2): 

raise ValueError("Mini-AES only defines two rounds") 

 

def sbox(self): 

r""" 

Return the S-box of Mini-AES. 

 

EXAMPLES:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: maes.sbox() 

(14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7) 

""" 

return self._sboxE 

 

def shift_row(self, block): 

r""" 

Rotate each row of ``block`` to the left by different nibble 

amounts. The first or zero-th row is left unchanged, while the 

second or row one is rotated left by one nibble. This has the effect 

of only interchanging the nibbles in the second row. Let 

`b_0, b_1, b_2, b_3` be four nibbles arranged as the following 

`2 \times 2` matrix 

 

.. MATH:: 

 

\begin{bmatrix} 

b_0 & b_2 \\ 

b_1 & b_3 

\end{bmatrix} 

 

Then the operation of shift-row is the mapping 

 

.. MATH:: 

 

\begin{bmatrix} 

b_0 & b_2 \\ 

b_1 & b_3 

\end{bmatrix} 

\longmapsto 

\begin{bmatrix} 

b_0 & b_2 \\ 

b_3 & b_1 

\end{bmatrix} 

 

INPUT: 

 

- ``block`` -- a `2 \times 2` matrix with entries over 

`\GF{2^4}` 

 

OUTPUT: 

 

- A `2 \times 2` matrix resulting from applying shift-row on ``block``. 

 

EXAMPLES: 

 

Here we work with elements of the finite field `\GF{2^4}`:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(16, "x") 

sage: MS = MatrixSpace(K, 2, 2) 

sage: mat = MS([[K("x^3 + x^2 + x + 1"), K("0")], [K("x^2 + x + 1"), K("x^3 + x")]]) 

sage: maes.shift_row(mat) 

<BLANKLINE> 

[x^3 + x^2 + x + 1 0] 

[ x^3 + x x^2 + x + 1] 

sage: mat 

<BLANKLINE> 

[x^3 + x^2 + x + 1 0] 

[ x^2 + x + 1 x^3 + x] 

 

But we can also work with binary strings:: 

 

sage: bin = BinaryStrings() 

sage: B = bin.encoding("Qt"); B 

0101000101110100 

sage: B = MS(maes.binary_to_GF(B)); B 

<BLANKLINE> 

[ x^2 + 1 1] 

[x^2 + x + 1 x^2] 

sage: maes.shift_row(B) 

<BLANKLINE> 

[ x^2 + 1 1] 

[ x^2 x^2 + x + 1] 

 

Here we work with integers `n` such that `0 \leq n \leq 15`:: 

 

sage: P = [3, 6, 9, 12]; P 

[3, 6, 9, 12] 

sage: P = MS(maes.integer_to_GF(P)); P 

<BLANKLINE> 

[ x + 1 x^2 + x] 

[ x^3 + 1 x^3 + x^2] 

sage: maes.shift_row(P) 

<BLANKLINE> 

[ x + 1 x^2 + x] 

[x^3 + x^2 x^3 + 1] 

 

TESTS: 

 

The input block must be a matrix:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: maes.shift_row("block") 

Traceback (most recent call last): 

... 

TypeError: input block must be a 2 x 2 matrix over GF(16) 

 

In addition, the dimensions of the input matrix must be `2 \times 2`:: 

 

sage: K = FiniteField(16, "x") 

sage: MS = MatrixSpace(K, 1, 2) 

sage: mat = MS([[K("x^3 + x^2 + x + 1"), K("0")]]) 

sage: maes.shift_row(mat) 

Traceback (most recent call last): 

... 

TypeError: input block must be a 2 x 2 matrix over GF(16) 

""" 

if not isinstance(block, Matrix_dense) or \ 

not (block.base_ring().order() == 16 and block.base_ring().is_field()): 

raise TypeError("input block must be a 2 x 2 matrix over GF(16)") 

if not (block.nrows() == block.ncols() == 2): 

raise TypeError("input block must be a 2 x 2 matrix over GF(16)") 

 

MS = MatrixSpace(FiniteField(self._key_size, "x"), 2, 2) 

mat = MS([ [block[0][0], block[0][1]], 

[block[1][1], block[1][0]] ] ) 

return mat 

 

 

### conversion functions to convert between different data formats 

 

def GF_to_binary(self, G): 

r""" 

Return the binary representation of ``G``. 

If ``G`` is an element of the finite field `\GF{2^4}`, then 

obtain the binary representation of ``G``. If ``G`` is a list of 

elements belonging to `\GF{2^4}`, obtain the 4-bit 

representation of each element of the list, then concatenate the 

resulting 4-bit strings into a binary string. If ``G`` is a matrix 

with entries over `\GF{2^4}`, convert each matrix entry to its 

4-bit representation, then concatenate the 4-bit strings. The 

concatenation is performed starting from the top-left corner of the 

matrix, working across left to right, top to bottom. Each element of 

`\GF{2^4}` can be associated with a unique 4-bit string 

according to the following table: 

 

.. MATH:: 

 

\begin{tabular}{ll|ll} \hline 

4-bit string & $\GF{2^4}$ & 4-bit string & $\GF{2^4}$ \\\hline 

0000 & $0$ & 1000 & $x^3$ \\ 

0001 & $1$ & 1001 & $x^3 + 1$ \\ 

0010 & $x$ & 1010 & $x^3 + x$ \\ 

0011 & $x + 1$ & 1011 & $x^3 + x + 1$ \\ 

0100 & $x^2$ & 1100 & $x^3 + x^2$ \\ 

0101 & $x^2 + 1$ & 1101 & $x^3 + x^2 + 1$ \\ 

0110 & $x^2 + x$ & 1110 & $x^3 + x^2 + x$ \\ 

0111 & $x^2 + x + 1$ & 1111 & $x^3 + x^2 + x+ 1$ \\\hline 

\end{tabular} 

 

INPUT: 

 

- ``G`` -- an element of `\GF{2^4}`, a list of elements of 

`\GF{2^4}`, or a matrix over `\GF{2^4}` 

 

OUTPUT: 

 

- A binary string representation of ``G``. 

 

EXAMPLES: 

 

Obtain the binary representation of all elements of `\GF{2^4}`:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(16, "x") 

sage: S = Set(K); len(S) # GF(2^4) has this many elements 

16 

sage: [maes.GF_to_binary(S[i]) for i in range(len(S))] 

<BLANKLINE> 

[0000, 

0001, 

0010, 

0011, 

0100, 

0101, 

0110, 

0111, 

1000, 

1001, 

1010, 

1011, 

1100, 

1101, 

1110, 

1111] 

 

The binary representation of a list of elements belonging to 

`\GF{2^4}`:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(16, "x") 

sage: G = [K("x^2 + x + 1"), K("x^3 + x^2"), K("x"), K("x^3 + x + 1"), K("x^3 + x^2 + x + 1"), K("x^2 + x"), K("1"), K("x^2 + x + 1")] 

sage: maes.GF_to_binary(G) 

01111100001010111111011000010111 

 

The binary representation of a matrix over `\GF{2^4}`:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(16, "x") 

sage: MS = MatrixSpace(K, 2, 2) 

sage: G = MS([K("x^3 + x^2"), K("x + 1"), K("x^2 + x + 1"), K("x^3 + x^2 + x")]); G 

<BLANKLINE> 

[ x^3 + x^2 x + 1] 

[ x^2 + x + 1 x^3 + x^2 + x] 

sage: maes.GF_to_binary(G) 

1100001101111110 

sage: MS = MatrixSpace(K, 2, 4) 

sage: G = MS([K("x^2 + x + 1"), K("x^3 + x^2"), K("x"), K("x^3 + x + 1"), K("x^3 + x^2 + x + 1"), K("x^2 + x"), K("1"), K("x^2 + x + 1")]); G 

<BLANKLINE> 

[ x^2 + x + 1 x^3 + x^2 x x^3 + x + 1] 

[x^3 + x^2 + x + 1 x^2 + x 1 x^2 + x + 1] 

sage: maes.GF_to_binary(G) 

01111100001010111111011000010111 

 

TESTS: 

 

Input must be an element of `\GF{2^4}`:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(8, "x") 

sage: G = K.random_element() 

sage: maes.GF_to_binary(G) 

Traceback (most recent call last): 

... 

TypeError: input G must be an element of GF(16), a list of elements of GF(16), or a matrix over GF(16) 

 

A list of elements belonging to `\GF{2^4}`:: 

 

sage: maes.GF_to_binary([]) 

Traceback (most recent call last): 

... 

ValueError: input G must be an element of GF(16), a list of elements of GF(16), or a matrix over GF(16) 

sage: G = [K.random_element() for i in range(5)] 

sage: maes.GF_to_binary(G) 

Traceback (most recent call last): 

... 

KeyError:... 

 

A matrix over `\GF{2^4}`:: 

 

sage: MS = MatrixSpace(FiniteField(7, "x"), 4, 5) 

sage: maes.GF_to_binary(MS.random_element()) 

Traceback (most recent call last): 

... 

TypeError: input G must be an element of GF(16), a list of elements of GF(16), or a matrix over GF(16) 

""" 

B = BinaryStrings() 

K = FiniteField(self._key_size, "x") 

# G is an element over GF(16) 

if G in K: 

return self._GF_to_bin[G] 

# G is a list of elements over GF(16) 

elif isinstance(G, list): 

if len(G) == 0: 

raise ValueError("input G must be an element of GF(16), a list of elements of GF(16), or a matrix over GF(16)") 

S = "".join([str(self._GF_to_bin[g]) for g in G]) 

return B(S) 

# G is a matrix over GF(16) 

elif isinstance(G, Matrix_dense): 

if not (G.base_ring() is K): 

raise TypeError("input G must be an element of GF(16), a list of elements of GF(16), or a matrix over GF(16)") 

S = "".join([str(self._GF_to_bin[G[i][j]]) for i in range(G.nrows()) for j in range(G.ncols())]) 

return B(S) 

# the type of G doesn't match the supported types 

else: 

raise TypeError("input G must be an element of GF(16), a list of elements of GF(16), or a matrix over GF(16)") 

 

def GF_to_integer(self, G): 

r""" 

Return the integer representation of the finite field element ``G``. 

If ``G`` is an element of the finite field `\GF{2^4}`, then 

obtain the integer representation of ``G``. If ``G`` is a list of 

elements belonging to `\GF{2^4}`, obtain the integer 

representation of each element of the list, and return the result 

as a list of integers. If ``G`` is a matrix with entries over 

`\GF{2^4}`, convert each matrix entry to its integer representation, 

and return the result as a list of integers. The resulting list is 

obtained by starting from the top-left corner of the matrix, working 

across left to right, top to bottom. Each element of `\GF{2^4}` can 

be associated with a unique integer according to the following table: 

 

.. MATH:: 

 

\begin{tabular}{ll|ll} \hline 

integer & $\GF{2^4}$ & integer & $\GF{2^4}$ \\\hline 

0 & $0$ & 8 & $x^3$ \\ 

1 & $1$ & 9 & $x^3 + 1$ \\ 

2 & $x$ & 10 & $x^3 + x$ \\ 

3 & $x + 1$ & 11 & $x^3 + x + 1$ \\ 

4 & $x^2$ & 12 & $x^3 + x^2$ \\ 

5 & $x^2 + 1$ & 13 & $x^3 + x^2 + 1$ \\ 

6 & $x^2 + x$ & 14 & $x^3 + x^2 + x$ \\ 

7 & $x^2 + x + 1$ & 15 & $x^3 + x^2 + x+ 1$ \\\hline 

\end{tabular} 

 

INPUT: 

 

- ``G`` -- an element of `\GF{2^4}`, a list of elements belonging to 

`\GF{2^4}`, or a matrix over `\GF{2^4}` 

 

OUTPUT: 

 

- The integer representation of ``G``. 

 

EXAMPLES: 

 

Obtain the integer representation of all elements of `\GF{2^4}`:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(16, "x") 

sage: S = Set(K); len(S) # GF(2^4) has this many elements 

16 

sage: [maes.GF_to_integer(S[i]) for i in range(len(S))] 

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 

 

The integer representation of a list of elements belonging to 

`\GF{2^4}`:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(16, "x") 

sage: G = [K("x^2 + x + 1"), K("x^3 + x^2"), K("x"), K("x^3 + x + 1"), K("x^3 + x^2 + x + 1"), K("x^2 + x"), K("1"), K("x^2 + x + 1")] 

sage: maes.GF_to_integer(G) 

[7, 12, 2, 11, 15, 6, 1, 7] 

 

The integer representation of a matrix over `\GF{2^4}`:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(16, "x") 

sage: MS = MatrixSpace(K, 2, 2) 

sage: G = MS([K("x^3 + x^2"), K("x + 1"), K("x^2 + x + 1"), K("x^3 + x^2 + x")]); G 

<BLANKLINE> 

[ x^3 + x^2 x + 1] 

[ x^2 + x + 1 x^3 + x^2 + x] 

sage: maes.GF_to_integer(G) 

[12, 3, 7, 14] 

sage: MS = MatrixSpace(K, 2, 4) 

sage: G = MS([K("x^2 + x + 1"), K("x^3 + x^2"), K("x"), K("x^3 + x + 1"), K("x^3 + x^2 + x + 1"), K("x^2 + x"), K("1"), K("x^2 + x + 1")]); G 

<BLANKLINE> 

[ x^2 + x + 1 x^3 + x^2 x x^3 + x + 1] 

[x^3 + x^2 + x + 1 x^2 + x 1 x^2 + x + 1] 

sage: maes.GF_to_integer(G) 

[7, 12, 2, 11, 15, 6, 1, 7] 

 

TESTS: 

 

Input must be an element of `\GF{2^4}`:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: K = FiniteField(7, "x") 

sage: G = K.random_element() 

sage: maes.GF_to_integer(G) 

Traceback (most recent call last): 

... 

TypeError: input G must be an element of GF(16), a list of elements of GF(16), or a matrix over GF(16) 

 

A list of elements belonging to `\GF{2^4}`:: 

 

sage: maes.GF_to_integer([]) 

Traceback (most recent call last): 

... 

ValueError: input G must be an element of GF(16), a list of elements of GF(16), or a matrix over GF(16) 

sage: G = [K.random_element() for i in range(5)] 

sage: maes.GF_to_integer(G) 

Traceback (most recent call last): 

... 

KeyError:... 

 

A matrix over `\GF{2^4}`:: 

 

sage: MS = MatrixSpace(FiniteField(7, "x"), 4, 5) 

sage: maes.GF_to_integer(MS.random_element()) 

Traceback (most recent call last): 

... 

TypeError: input G must be an element of GF(16), a list of elements of GF(16), or a matrix over GF(16) 

""" 

K = FiniteField(self._key_size, "x") 

# G is an element over GF(16) 

if G in K: 

return self._GF_to_int[G] 

# G is a list of elements over GF(16) 

elif isinstance(G, list): 

if len(G) == 0: 

raise ValueError("input G must be an element of GF(16), a list of elements of GF(16), or a matrix over GF(16)") 

return [self._GF_to_int[g] for g in G] 

# G is a matrix over GF(16) 

elif isinstance(G, Matrix_dense): 

if not (G.base_ring() is K): 

raise TypeError("input G must be an element of GF(16), a list of elements of GF(16), or a matrix over GF(16)") 

return [self._GF_to_int[G[i][j]] for i in range(G.nrows()) for j in range(G.ncols())] 

# the type of G doesn't match the supported types 

else: 

raise TypeError("input G must be an element of GF(16), a list of elements of GF(16), or a matrix over GF(16)") 

 

def binary_to_GF(self, B): 

r""" 

Return a list of elements of `\GF{2^4}` that represents the 

binary string ``B``. The number of bits in ``B`` must be greater 

than zero and a multiple of 4. Each nibble (or 4-bit string) is 

uniquely associated with an element of `\GF{2^4}` as 

specified by the following table: 

 

.. MATH:: 

 

\begin{tabular}{ll|ll} \hline 

4-bit string & $\GF{2^4}$ & 4-bit string & $\GF{2^4}$ \\\hline 

0000 & $0$ & 1000 & $x^3$ \\ 

0001 & $1$ & 1001 & $x^3 + 1$ \\ 

0010 & $x$ & 1010 & $x^3 + x$ \\ 

0011 & $x + 1$ & 1011 & $x^3 + x + 1$ \\ 

0100 & $x^2$ & 1100 & $x^3 + x^2$ \\ 

0101 & $x^2 + 1$ & 1101 & $x^3 + x^2 + 1$ \\ 

0110 & $x^2 + x$ & 1110 & $x^3 + x^2 + x$ \\ 

0111 & $x^2 + x + 1$ & 1111 & $x^3 + x^2 + x+ 1$ \\\hline 

\end{tabular} 

 

INPUT: 

 

- ``B`` -- a binary string, where the number of bits is positive and 

a multiple of 4 

 

OUTPUT: 

 

- A list of elements of the finite field `\GF{2^4}` that 

represent the binary string ``B``. 

 

EXAMPLES: 

 

Obtain all the elements of the finite field `\GF{2^4}`:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: bin = BinaryStrings() 

sage: B = bin("0000000100100011010001010110011110001001101010111100110111101111") 

sage: maes.binary_to_GF(B) 

<BLANKLINE> 

[0, 

1, 

x, 

x + 1, 

x^2, 

x^2 + 1, 

x^2 + x, 

x^2 + x + 1, 

x^3, 

x^3 + 1, 

x^3 + x, 

x^3 + x + 1, 

x^3 + x^2, 

x^3 + x^2 + 1, 

x^3 + x^2 + x, 

x^3 + x^2 + x + 1] 

 

TESTS: 

 

The input ``B`` must be a non-empty binary string, where the number 

of bits is a multiple of 4:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: maes.binary_to_GF("") 

Traceback (most recent call last): 

... 

ValueError: the number of bits in the binary string B must be positive and a multiple of 4 

sage: maes.binary_to_GF("101") 

Traceback (most recent call last): 

... 

ValueError: the number of bits in the binary string B must be positive and a multiple of 4 

""" 

from sage.rings.finite_rings.integer_mod import Mod 

bin = BinaryStrings() 

b = bin(B) 

# an empty string 

if len(b) == 0: 

raise ValueError("the number of bits in the binary string B must be positive and a multiple of 4") 

# a string with number of bits that is a multiple of 4 

if Mod(len(b), 4).lift() == 0: 

M = len(b) // 4 # the number of nibbles 

return [self._bin_to_GF[b[i*4 : (i+1)*4]] for i in range(M)] 

else: 

raise ValueError("the number of bits in the binary string B must be positive and a multiple of 4") 

 

def binary_to_integer(self, B): 

r""" 

Return a list of integers representing the binary string ``B``. The 

number of bits in ``B`` must be greater than zero and a multiple of 

4. Each nibble (or 4-bit string) is uniquely associated with an 

integer as specified by the following table: 

 

.. MATH:: 

 

\begin{tabular}{ll|ll} \hline 

4-bit string & integer & 4-bit string & integer \\\hline 

0000 & 0 & 1000 & 8 \\ 

0001 & 1 & 1001 & 9 \\ 

0010 & 2 & 1010 & 10 \\ 

0011 & 3 & 1011 & 11 \\ 

0100 & 4 & 1100 & 12 \\ 

0101 & 5 & 1101 & 13 \\ 

0110 & 6 & 1110 & 14 \\ 

0111 & 7 & 1111 & 15 \\\hline 

\end{tabular} 

 

INPUT: 

 

- ``B`` -- a binary string, where the number of bits is positive and 

a multiple of 4 

 

OUTPUT: 

 

- A list of integers that represent the binary string ``B``. 

 

EXAMPLES: 

 

Obtain the integer representation of every 4-bit string:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: bin = BinaryStrings() 

sage: B = bin("0000000100100011010001010110011110001001101010111100110111101111") 

sage: maes.binary_to_integer(B) 

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 

 

TESTS: 

 

The input ``B`` must be a non-empty binary string, where the number 

of bits is a multiple of 4:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: maes.binary_to_integer("") 

Traceback (most recent call last): 

... 

ValueError: the number of bits in the binary string B must be positive and a multiple of 4 

sage: maes.binary_to_integer("101") 

Traceback (most recent call last): 

... 

ValueError: the number of bits in the binary string B must be positive and a multiple of 4 

""" 

from sage.rings.finite_rings.integer_mod import Mod 

bin = BinaryStrings() 

b = bin(B) 

# an empty string 

if len(b) == 0: 

raise ValueError("the number of bits in the binary string B must be positive and a multiple of 4") 

# a string with number of bits that is a multiple of 4 

if Mod(len(b), 4).lift() == 0: 

M = len(b) // 4 # the number of nibbles 

return [self._bin_to_int[b[i*4 : (i+1)*4]] for i in range(M)] 

else: 

raise ValueError("the number of bits in the binary string B must be positive and a multiple of 4") 

 

def integer_to_binary(self, N): 

r""" 

Return the binary representation of ``N``. If `N` is an integer such 

that `0 \leq N \leq 15`, return the binary representation of ``N``. 

If ``N`` is a list of integers each of which is `\geq 0` and 

`\leq 15`, then obtain the binary representation of each integer, 

and concatenate the individual binary representations into a single 

binary string. Each integer between 0 and 15, inclusive, can be 

associated with a unique 4-bit string according to the following 

table: 

 

.. MATH:: 

 

\begin{tabular}{ll|ll} \hline 

4-bit string & integer & 4-bit string & integer \\\hline 

0000 & 0 & 1000 & 8 \\ 

0001 & 1 & 1001 & 9 \\ 

0010 & 2 & 1010 & 10 \\ 

0011 & 3 & 1011 & 11 \\ 

0100 & 4 & 1100 & 12 \\ 

0101 & 5 & 1101 & 13 \\ 

0110 & 6 & 1110 & 14 \\ 

0111 & 7 & 1111 & 15 \\\hline 

\end{tabular} 

 

INPUT: 

 

- ``N`` -- a non-negative integer less than or equal to 15, or a list 

of such integers 

 

OUTPUT: 

 

- A binary string representing ``N``. 

 

EXAMPLES: 

 

The binary representations of all integers between 0 and 

15, inclusive:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: lst = [n for n in range(16)]; lst 

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 

sage: maes.integer_to_binary(lst) 

0000000100100011010001010110011110001001101010111100110111101111 

 

The binary representation of an integer between 0 and 15, 

inclusive:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: maes.integer_to_binary(3) 

0011 

sage: maes.integer_to_binary(5) 

0101 

sage: maes.integer_to_binary(7) 

0111 

 

TESTS: 

 

The input ``N`` can be an integer, but must be bounded such that 

`0 \leq N \leq 15`:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: maes.integer_to_binary(-1) 

Traceback (most recent call last): 

... 

KeyError:... 

sage: maes.integer_to_binary("1") 

Traceback (most recent call last): 

... 

TypeError: N must be an integer 0 <= N <= 15 or a list of such integers 

sage: maes.integer_to_binary("") 

Traceback (most recent call last): 

... 

TypeError: N must be an integer 0 <= N <= 15 or a list of such integers 

 

The input ``N`` can be a list of integers, but each integer `n` of 

the list must be `0 \leq n \leq 15`:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: maes.integer_to_binary([]) 

Traceback (most recent call last): 

... 

ValueError: N must be an integer 0 <= N <= 15 or a list of such integers 

sage: maes.integer_to_binary([""]) 

Traceback (most recent call last): 

... 

KeyError:... 

sage: maes.integer_to_binary([0, 1, 2, 16]) 

Traceback (most recent call last): 

... 

KeyError:... 

""" 

if isinstance(N, list): 

if len(N) == 0: 

raise ValueError("N must be an integer 0 <= N <= 15 or a list of such integers") 

bin = BinaryStrings() 

# Here, we assume that each element of the list is an integer n 

# such that 0 <= n <= 15. An error will be raised if otherwise. 

b = "".join([str(self._int_to_bin[n]) for n in N]) 

return bin(b) 

elif isinstance(N, Integer): 

# Here, we assume that N is an integer such that 0 <= n <= 15. 

# An error will be raised if otherwise. 

return self._int_to_bin[N] 

else: 

raise TypeError("N must be an integer 0 <= N <= 15 or a list of such integers") 

 

def integer_to_GF(self, N): 

r""" 

Return the finite field representation of ``N``. If `N` is an 

integer such that `0 \leq N \leq 15`, return the element of 

`\GF{2^4}` that represents ``N``. If ``N`` is a list of integers 

each of which is `\geq 0` and `\leq 15`, then obtain the element 

of `\GF{2^4}` that represents each such integer, and return a list 

of such finite field representations. Each integer between 0 and 15, 

inclusive, can be associated with a unique element of `\GF{2^4}` 

according to the following table: 

 

.. MATH:: 

 

\begin{tabular}{ll|ll} \hline 

integer & $\GF{2^4}$ & integer & $\GF{2^4}$ \\\hline 

0 & $0$ & 8 & $x^3$ \\ 

1 & $1$ & 9 & $x^3 + 1$ \\ 

2 & $x$ & 10 & $x^3 + x$ \\ 

3 & $x + 1$ & 11 & $x^3 + x + 1$ \\ 

4 & $x^2$ & 12 & $x^3 + x^2$ \\ 

5 & $x^2 + 1$ & 13 & $x^3 + x^2 + 1$ \\ 

6 & $x^2 + x$ & 14 & $x^3 + x^2 + x$ \\ 

7 & $x^2 + x + 1$ & 15 & $x^3 + x^2 + x+ 1$ \\\hline 

\end{tabular} 

 

INPUT: 

 

- ``N`` -- a non-negative integer less than or equal to 15, or a list 

of such integers 

 

OUTPUT: 

 

- Elements of the finite field `\GF{2^4}`. 

 

EXAMPLES: 

 

Obtain the element of `\GF{2^4}` representing an integer `n`, where 

`0 \leq n \leq 15`:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: maes.integer_to_GF(0) 

0 

sage: maes.integer_to_GF(2) 

x 

sage: maes.integer_to_GF(7) 

x^2 + x + 1 

 

Obtain the finite field elements corresponding to all non-negative 

integers less than or equal to 15:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: lst = [n for n in range(16)]; lst 

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 

sage: maes.integer_to_GF(lst) 

<BLANKLINE> 

[0, 

1, 

x, 

x + 1, 

x^2, 

x^2 + 1, 

x^2 + x, 

x^2 + x + 1, 

x^3, 

x^3 + 1, 

x^3 + x, 

x^3 + x + 1, 

x^3 + x^2, 

x^3 + x^2 + 1, 

x^3 + x^2 + x, 

x^3 + x^2 + x + 1] 

 

TESTS: 

 

The input ``N`` can be an integer, but it must be such that 

`0 \leq N \leq 15`:: 

 

sage: from sage.crypto.block_cipher.miniaes import MiniAES 

sage: maes = MiniAES() 

sage: maes.integer_to_GF(-1) 

Traceback (most recent call last): 

... 

KeyError:... 

sage: maes.integer_to_GF(16) 

Traceback (most recent call last): 

... 

KeyError:... 

sage: maes.integer_to_GF("2") 

Traceback (most recent call last): 

... 

TypeError: N must be an integer 0 <= N <= 15 or a list of such integers 

 

The input ``N`` can be a list of integers, but each integer `n` in 

the list must be bounded such that `0 \leq n \leq 15`:: 

 

sage: maes.integer_to_GF([]) 

Traceback (most recent call last): 

... 

ValueError: N must be an integer 0 <= N <= 15 or a list of such integers 

sage: maes.integer_to_GF([""]) 

Traceback (most recent call last): 

... 

KeyError:... 

sage: maes.integer_to_GF([0, 2, 3, "4"]) 

Traceback (most recent call last): 

... 

KeyError:... 

sage: maes.integer_to_GF([0, 2, 3, 16]) 

Traceback (most recent call last): 

... 

KeyError:... 

""" 

if isinstance(N, list): 

if len(N) == 0: 

raise ValueError("N must be an integer 0 <= N <= 15 or a list of such integers") 

# Here, we assume that each element of the list is an integer n 

# such that 0 <= n <= 15. An error will be raised if otherwise. 

return [self._int_to_GF[n] for n in N] 

elif isinstance(N, Integer): 

# Here, we assume that N is an integer such that 0 <= n <= 15. 

# An error will be raised if otherwise. 

return self._int_to_GF[N] 

else: 

raise TypeError("N must be an integer 0 <= N <= 15 or a list of such integers")