Bitcoin Forum
June 03, 2024, 02:36:47 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: « 1 [2]  All
  Print  
Author Topic: [C#] Trying to implement EC Multiplication in pure code  (Read 360 times)
BlackHatCoiner (OP)
Legendary
*
Offline Offline

Activity: 1540
Merit: 7468


Farewell, Leo


View Profile
July 28, 2021, 03:18:42 PM
 #21

Thank you!

Your code works fine, there's just one thing which slips (?) the process. It adds an additional zero sometimes in front of the x & y and I don't know what's responsible for that.  For example, if I run it with k=1, it returns it properly:
Code:
x: 79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
y: 483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8

Trying it with k=2, you'll get:
Code:
x: 0C6047F9441ED7D6D3045406E95C07CD85C778E4B8CEF3CA7ABAC09B95C709EE5
y: 1AE168FEA63DC339A3C58419466CEAEEF7F632653266D0E1236431A950CFE52A

That extra zero (0C60...) shouldn't exist, because obviously, once is hashed with SHA256 it'll give a different result.




Seriously now, I'll say this again; thank you. You wasted your time to write me code? The least I can do is merit you.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
j2002ba2
Full Member
***
Offline Offline

Activity: 204
Merit: 437


View Profile
July 28, 2021, 03:46:31 PM
Merited by ABCbits (1), BlackHatCoiner (1)
 #22

Thank you!

Your code works fine, there's just one thing which slips (?) the process. It adds an additional zero sometimes in front of the x & y and I don't know what's responsible for that.  For example, if I run it with k=1, it returns it properly:
Code:
x: 79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
y: 483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8

Trying it with k=2, you'll get:
Code:
x: 0C6047F9441ED7D6D3045406E95C07CD85C778E4B8CEF3CA7ABAC09B95C709EE5
y: 1AE168FEA63DC339A3C58419466CEAEEF7F632653266D0E1236431A950CFE52A

That extra zero (0C60...) shouldn't exist, because obviously, once is hashed with SHA256 it'll give a different result.

This is a C# thing, it adds a leading zero when MSB of the top byte is one, something to do with distinguishing between positive and negative numbers. You have to be careful when parsing hex numbers as well: BigInteger.Parse("ff") = -1.

Overall C# is a language with many such intricacies (or I would say stupid choices).
pooya87
Legendary
*
Offline Offline

Activity: 3472
Merit: 10606



View Profile
July 29, 2021, 03:37:04 AM
 #23

This is a C# thing, it adds a leading zero when MSB of the top byte is one, something to do with distinguishing between positive and negative numbers. You have to be careful when parsing hex numbers as well: BigInteger.Parse("ff") = -1.

Overall C# is a language with many such intricacies (or I would say stupid choices).
This is not a C# thing at all. This is a popular way in computers to indicate positive/negative numbers which the BigInteger class also uses. In fact we are using a similar logic in bitcoin signatures that are encoded using DER encoding (use most significant bit of the most significant byte to indicate sign).

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
j2002ba2
Full Member
***
Offline Offline

Activity: 204
Merit: 437


View Profile
July 29, 2021, 06:44:08 AM
 #24

This is a C# thing, it adds a leading zero when MSB of the top byte is one, something to do with distinguishing between positive and negative numbers. You have to be careful when parsing hex numbers as well: BigInteger.Parse("ff") = -1.

Overall C# is a language with many such intricacies (or I would say stupid choices).
This is not a C# thing at all. This is a popular way in computers to indicate positive/negative numbers which the BigInteger class also uses. In fact we are using a similar logic in bitcoin signatures that are encoded using DER encoding (use most significant bit of the most significant byte to indicate sign).

The most popular way to indicate a negative number is with "-". They are doing a memory dump. Is there a way to print and parse the number properly? In DER encoding it is justified, we get a single long hex digit sequence, but when printing a single number it just looks wrong, and consequently increases the cognitive load. One could write in the source code -0xff, and all is good, but AFAIK there's no (easy) way to parse "-0xff" as a BigInteger.
pooya87
Legendary
*
Offline Offline

Activity: 3472
Merit: 10606



View Profile
July 29, 2021, 08:27:21 AM
 #25

The most popular way to indicate a negative number is with "-". They are doing a memory dump. Is there a way to print and parse the number properly? In DER encoding it is justified, we get a single long hex digit sequence, but when printing a single number it just looks wrong, and consequently increases the cognitive load. One could write in the source code -0xff, and all is good, but AFAIK there's no (easy) way to parse "-0xff" as a BigInteger.
You are thinking as a "human" not a "computer". In computer there is no "-" there are only bits and when we are representing an arbitrary length octet string there has to be a certain way we represent the sign and that is the most significant bit.
0xff is 0b11111111 and since the most significant bit is set this number is negative.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
Pages: « 1 [2]  All
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!