pyfinite gives wrong result for multiplication in field GF(2^8)
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am using pyfinte to calculate multiplication for AES over the field it uses which is F(2^8) but when I do as following:
from pyfinite import ffield
a = 0xbf
b = 0x03
F = ffield.FField(8)
c = F.Multiply(a, b)
print(hex(c))
the actual result is 0xdc but it should be 0xda here is how I solve it by hand:
0xbf*0x03 = 0xbf * 0x02 + 0xbf * 0x01 = 0b10111111 * 0x02 + 0xbf = 0b01111110 + 0x1B + 0xbf = 0b11011010 = 0xda
here is better format of my hand soloving:

so where is the wrong? is it on my solving by hand? or in pyfinite? how to solve this?
python aes finite-field
|
show 1 more comment
I am using pyfinte to calculate multiplication for AES over the field it uses which is F(2^8) but when I do as following:
from pyfinite import ffield
a = 0xbf
b = 0x03
F = ffield.FField(8)
c = F.Multiply(a, b)
print(hex(c))
the actual result is 0xdc but it should be 0xda here is how I solve it by hand:
0xbf*0x03 = 0xbf * 0x02 + 0xbf * 0x01 = 0b10111111 * 0x02 + 0xbf = 0b01111110 + 0x1B + 0xbf = 0b11011010 = 0xda
here is better format of my hand soloving:

so where is the wrong? is it on my solving by hand? or in pyfinite? how to solve this?
python aes finite-field
Could you format and/or annotate your hand math a little more? I'm having a hard time following it. It's been a while, but I followed Wikipedia's guidance and got 0xdc just like pyfinite did. (Edit: which I guess makes this more of a math question than a programming question.)
– glibdud
Jan 4 at 13:43
@glibdud just a minute to formate my hand hand solving better
– mark
Jan 4 at 13:50
@glibdud I did what you asked and updated my question to include latex detailed formatted solving by hand
– mark
Jan 4 at 14:18
How do you know that the modulus is 0x11B?
– Matt Timmermans
Jan 4 at 14:31
@MattTimmermans this is the way we learned in college couple days ago but I do not know if I misunderstood here is summary: when multiplying any number X by 03 in MixColumns stage in AES we multipy X by 02 and then Xor it with X . Multiplying X by 2 is as follow: we convert X to it is binary representation and we look at the leftmost bit if it is one or zero then we shift X to the left by one and Xor it with 0x1B if the leftmost bit was one. I do not know if I am confusing what we learn with what I read online about AES I think what we learned is shortcut for multiply over GF(2^8)
– mark
Jan 4 at 14:38
|
show 1 more comment
I am using pyfinte to calculate multiplication for AES over the field it uses which is F(2^8) but when I do as following:
from pyfinite import ffield
a = 0xbf
b = 0x03
F = ffield.FField(8)
c = F.Multiply(a, b)
print(hex(c))
the actual result is 0xdc but it should be 0xda here is how I solve it by hand:
0xbf*0x03 = 0xbf * 0x02 + 0xbf * 0x01 = 0b10111111 * 0x02 + 0xbf = 0b01111110 + 0x1B + 0xbf = 0b11011010 = 0xda
here is better format of my hand soloving:

so where is the wrong? is it on my solving by hand? or in pyfinite? how to solve this?
python aes finite-field
I am using pyfinte to calculate multiplication for AES over the field it uses which is F(2^8) but when I do as following:
from pyfinite import ffield
a = 0xbf
b = 0x03
F = ffield.FField(8)
c = F.Multiply(a, b)
print(hex(c))
the actual result is 0xdc but it should be 0xda here is how I solve it by hand:
0xbf*0x03 = 0xbf * 0x02 + 0xbf * 0x01 = 0b10111111 * 0x02 + 0xbf = 0b01111110 + 0x1B + 0xbf = 0b11011010 = 0xda
here is better format of my hand soloving:

so where is the wrong? is it on my solving by hand? or in pyfinite? how to solve this?
python aes finite-field
python aes finite-field
edited Jan 4 at 14:16
mark
asked Jan 4 at 13:19
markmark
477
477
Could you format and/or annotate your hand math a little more? I'm having a hard time following it. It's been a while, but I followed Wikipedia's guidance and got 0xdc just like pyfinite did. (Edit: which I guess makes this more of a math question than a programming question.)
– glibdud
Jan 4 at 13:43
@glibdud just a minute to formate my hand hand solving better
– mark
Jan 4 at 13:50
@glibdud I did what you asked and updated my question to include latex detailed formatted solving by hand
– mark
Jan 4 at 14:18
How do you know that the modulus is 0x11B?
– Matt Timmermans
Jan 4 at 14:31
@MattTimmermans this is the way we learned in college couple days ago but I do not know if I misunderstood here is summary: when multiplying any number X by 03 in MixColumns stage in AES we multipy X by 02 and then Xor it with X . Multiplying X by 2 is as follow: we convert X to it is binary representation and we look at the leftmost bit if it is one or zero then we shift X to the left by one and Xor it with 0x1B if the leftmost bit was one. I do not know if I am confusing what we learn with what I read online about AES I think what we learned is shortcut for multiply over GF(2^8)
– mark
Jan 4 at 14:38
|
show 1 more comment
Could you format and/or annotate your hand math a little more? I'm having a hard time following it. It's been a while, but I followed Wikipedia's guidance and got 0xdc just like pyfinite did. (Edit: which I guess makes this more of a math question than a programming question.)
– glibdud
Jan 4 at 13:43
@glibdud just a minute to formate my hand hand solving better
– mark
Jan 4 at 13:50
@glibdud I did what you asked and updated my question to include latex detailed formatted solving by hand
– mark
Jan 4 at 14:18
How do you know that the modulus is 0x11B?
– Matt Timmermans
Jan 4 at 14:31
@MattTimmermans this is the way we learned in college couple days ago but I do not know if I misunderstood here is summary: when multiplying any number X by 03 in MixColumns stage in AES we multipy X by 02 and then Xor it with X . Multiplying X by 2 is as follow: we convert X to it is binary representation and we look at the leftmost bit if it is one or zero then we shift X to the left by one and Xor it with 0x1B if the leftmost bit was one. I do not know if I am confusing what we learn with what I read online about AES I think what we learned is shortcut for multiply over GF(2^8)
– mark
Jan 4 at 14:38
Could you format and/or annotate your hand math a little more? I'm having a hard time following it. It's been a while, but I followed Wikipedia's guidance and got 0xdc just like pyfinite did. (Edit: which I guess makes this more of a math question than a programming question.)
– glibdud
Jan 4 at 13:43
Could you format and/or annotate your hand math a little more? I'm having a hard time following it. It's been a while, but I followed Wikipedia's guidance and got 0xdc just like pyfinite did. (Edit: which I guess makes this more of a math question than a programming question.)
– glibdud
Jan 4 at 13:43
@glibdud just a minute to formate my hand hand solving better
– mark
Jan 4 at 13:50
@glibdud just a minute to formate my hand hand solving better
– mark
Jan 4 at 13:50
@glibdud I did what you asked and updated my question to include latex detailed formatted solving by hand
– mark
Jan 4 at 14:18
@glibdud I did what you asked and updated my question to include latex detailed formatted solving by hand
– mark
Jan 4 at 14:18
How do you know that the modulus is 0x11B?
– Matt Timmermans
Jan 4 at 14:31
How do you know that the modulus is 0x11B?
– Matt Timmermans
Jan 4 at 14:31
@MattTimmermans this is the way we learned in college couple days ago but I do not know if I misunderstood here is summary: when multiplying any number X by 03 in MixColumns stage in AES we multipy X by 02 and then Xor it with X . Multiplying X by 2 is as follow: we convert X to it is binary representation and we look at the leftmost bit if it is one or zero then we shift X to the left by one and Xor it with 0x1B if the leftmost bit was one. I do not know if I am confusing what we learn with what I read online about AES I think what we learned is shortcut for multiply over GF(2^8)
– mark
Jan 4 at 14:38
@MattTimmermans this is the way we learned in college couple days ago but I do not know if I misunderstood here is summary: when multiplying any number X by 03 in MixColumns stage in AES we multipy X by 02 and then Xor it with X . Multiplying X by 2 is as follow: we convert X to it is binary representation and we look at the leftmost bit if it is one or zero then we shift X to the left by one and Xor it with 0x1B if the leftmost bit was one. I do not know if I am confusing what we learn with what I read online about AES I think what we learned is shortcut for multiply over GF(2^8)
– mark
Jan 4 at 14:38
|
show 1 more comment
1 Answer
1
active
oldest
votes
I'm not intimately familiar with AES, but from the link you provided it appears that the generator polynomial for the field is 0x11b. By default, pyfinite uses a different generator, which will produce different multiplication results:
>>> f = ffield.FField(8)
>>> hex(f.generator)
'0x11d'
If you specify the generator when you create the field object, you get the result you're expecting:
>>> f = ffield.FField(8, gen=0x11b, useLUT=0)
>>> hex(f.Multiply(0xbf, 0x03))
'0xda'
I can confirm AES uses 0x11b, where all non-zero elements can be considered to be some power of 0x03. For 0x11d, all non-zero elements can be considered to be a power of 0x02. Most implementations involving finite fields will choose a polynomial where all non-zero elements are a power of 2. I don't know why AES choose 0x11b.
– rcgldr
Jan 4 at 16:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54039723%2fpyfinite-gives-wrong-result-for-multiplication-in-field-gf28%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm not intimately familiar with AES, but from the link you provided it appears that the generator polynomial for the field is 0x11b. By default, pyfinite uses a different generator, which will produce different multiplication results:
>>> f = ffield.FField(8)
>>> hex(f.generator)
'0x11d'
If you specify the generator when you create the field object, you get the result you're expecting:
>>> f = ffield.FField(8, gen=0x11b, useLUT=0)
>>> hex(f.Multiply(0xbf, 0x03))
'0xda'
I can confirm AES uses 0x11b, where all non-zero elements can be considered to be some power of 0x03. For 0x11d, all non-zero elements can be considered to be a power of 0x02. Most implementations involving finite fields will choose a polynomial where all non-zero elements are a power of 2. I don't know why AES choose 0x11b.
– rcgldr
Jan 4 at 16:50
add a comment |
I'm not intimately familiar with AES, but from the link you provided it appears that the generator polynomial for the field is 0x11b. By default, pyfinite uses a different generator, which will produce different multiplication results:
>>> f = ffield.FField(8)
>>> hex(f.generator)
'0x11d'
If you specify the generator when you create the field object, you get the result you're expecting:
>>> f = ffield.FField(8, gen=0x11b, useLUT=0)
>>> hex(f.Multiply(0xbf, 0x03))
'0xda'
I can confirm AES uses 0x11b, where all non-zero elements can be considered to be some power of 0x03. For 0x11d, all non-zero elements can be considered to be a power of 0x02. Most implementations involving finite fields will choose a polynomial where all non-zero elements are a power of 2. I don't know why AES choose 0x11b.
– rcgldr
Jan 4 at 16:50
add a comment |
I'm not intimately familiar with AES, but from the link you provided it appears that the generator polynomial for the field is 0x11b. By default, pyfinite uses a different generator, which will produce different multiplication results:
>>> f = ffield.FField(8)
>>> hex(f.generator)
'0x11d'
If you specify the generator when you create the field object, you get the result you're expecting:
>>> f = ffield.FField(8, gen=0x11b, useLUT=0)
>>> hex(f.Multiply(0xbf, 0x03))
'0xda'
I'm not intimately familiar with AES, but from the link you provided it appears that the generator polynomial for the field is 0x11b. By default, pyfinite uses a different generator, which will produce different multiplication results:
>>> f = ffield.FField(8)
>>> hex(f.generator)
'0x11d'
If you specify the generator when you create the field object, you get the result you're expecting:
>>> f = ffield.FField(8, gen=0x11b, useLUT=0)
>>> hex(f.Multiply(0xbf, 0x03))
'0xda'
edited Jan 4 at 15:55
answered Jan 4 at 15:45
glibdudglibdud
5,71921731
5,71921731
I can confirm AES uses 0x11b, where all non-zero elements can be considered to be some power of 0x03. For 0x11d, all non-zero elements can be considered to be a power of 0x02. Most implementations involving finite fields will choose a polynomial where all non-zero elements are a power of 2. I don't know why AES choose 0x11b.
– rcgldr
Jan 4 at 16:50
add a comment |
I can confirm AES uses 0x11b, where all non-zero elements can be considered to be some power of 0x03. For 0x11d, all non-zero elements can be considered to be a power of 0x02. Most implementations involving finite fields will choose a polynomial where all non-zero elements are a power of 2. I don't know why AES choose 0x11b.
– rcgldr
Jan 4 at 16:50
I can confirm AES uses 0x11b, where all non-zero elements can be considered to be some power of 0x03. For 0x11d, all non-zero elements can be considered to be a power of 0x02. Most implementations involving finite fields will choose a polynomial where all non-zero elements are a power of 2. I don't know why AES choose 0x11b.
– rcgldr
Jan 4 at 16:50
I can confirm AES uses 0x11b, where all non-zero elements can be considered to be some power of 0x03. For 0x11d, all non-zero elements can be considered to be a power of 0x02. Most implementations involving finite fields will choose a polynomial where all non-zero elements are a power of 2. I don't know why AES choose 0x11b.
– rcgldr
Jan 4 at 16:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54039723%2fpyfinite-gives-wrong-result-for-multiplication-in-field-gf28%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Could you format and/or annotate your hand math a little more? I'm having a hard time following it. It's been a while, but I followed Wikipedia's guidance and got 0xdc just like pyfinite did. (Edit: which I guess makes this more of a math question than a programming question.)
– glibdud
Jan 4 at 13:43
@glibdud just a minute to formate my hand hand solving better
– mark
Jan 4 at 13:50
@glibdud I did what you asked and updated my question to include latex detailed formatted solving by hand
– mark
Jan 4 at 14:18
How do you know that the modulus is 0x11B?
– Matt Timmermans
Jan 4 at 14:31
@MattTimmermans this is the way we learned in college couple days ago but I do not know if I misunderstood here is summary: when multiplying any number X by 03 in MixColumns stage in AES we multipy X by 02 and then Xor it with X . Multiplying X by 2 is as follow: we convert X to it is binary representation and we look at the leftmost bit if it is one or zero then we shift X to the left by one and Xor it with 0x1B if the leftmost bit was one. I do not know if I am confusing what we learn with what I read online about AES I think what we learned is shortcut for multiply over GF(2^8)
– mark
Jan 4 at 14:38