Bitcoin Forum
July 04, 2024, 11:30:02 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: sagemath script  (Read 321 times)
mausuv (OP)
Jr. Member
*
Offline Offline

Activity: 70
Merit: 1


View Profile
December 09, 2022, 03:05:24 PM
Last edit: December 09, 2022, 03:20:50 PM by mausuv
 #1

i am run this 2 script sage error message show Huh
i need alternative code sage in python

scipt 1  NameError: name 'EllipticCurve' is not defined sage
Code:
modi =0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
E=EllipticCurve(GF(modi), [0,7])
print (E)

# generator used with this curve
G=E(0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798, 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8)

PrivK=0x2

PubK=PrivK*G
print ("Public key :", PubK)

scipt 2 NameError: name 'GF' is not defined sage
Code:
p = 2^256 - 2^32 - 2^9 - 2^8 - 2^7 - 2^6 - 2^4 -1
n  = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
a = 0
b = 7
Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
Gy = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8

K = GF(p)
C = EllipticCurve(K, [ a, b ])
BTC_EC_FIXED_POINT = C.point((Gx, Gy))
priv = 0x2
m = priv*BTC_EC_FIXED_POINT
print(priv)
print(hex(m[0]),hex(m[1]))

My python version 3.7
sage version 9.3
i need alternative code sage in python

PowerGlove
Hero Member
*****
hacker
Offline Offline

Activity: 534
Merit: 4358



View Profile
December 09, 2022, 04:29:53 PM
 #2

i am run this 2 script sage error message show Huh
i need alternative code sage in python

If you're asking what you have to do to those scripts in order to get them to run correctly outside of the sage shell, then here you go:

Script1.py

Code:
from sage.all import *

modi = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F

E = EllipticCurve(GF(modi), [0,7])

print(E)

# generator used with this curve

G = E(0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798, 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8)

PrivK = 0x2

PubK = PrivK * G

print("Public key :", PubK)

Script2.py

Code:
from sage.all import *

p = 2**256 - 2**32 - 2**9 - 2**8 - 2**7 - 2**6 - 2**4 - 1

n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141

a = 0

b = 7

Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798

Gy = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8

K = GF(p)

C = EllipticCurve(K, [a, b])

BTC_EC_FIXED_POINT = C.point((Gx, Gy))

priv = 0x2

m = priv * BTC_EC_FIXED_POINT

print(priv)

print(hex(m[0]), hex(m[1]))

These run fine on my system, like this:

Code:
python3 Script1.py

Code:
python3 Script2.py

If that doesn't work for you, then you might have to run them like this, instead:

Code:
sage -python Script1.py

Code:
sage -python Script2.py
mausuv (OP)
Jr. Member
*
Offline Offline

Activity: 70
Merit: 1


View Profile
December 09, 2022, 04:59:19 PM
 #3

i am run this 2 script sage error message show Huh
i need alternative code sage in python

If you're asking what you have to do to those scripts in order to get them to run correctly outside of the sage shell, then here you go:

Script1.py
Script2.py

These run fine on my system, like this:

Code:
python3 Script1.py
Code:
python3 Script2.py

If that doesn't work for you, then you might have to run them like this, instead:

Code:
sage -python Script1.py
Code:
sage -python Script2.py

Yes,iam alredy try
This error show Huh
Code:
/opt/sagemath-9.3/local/lib/python3.7/site-packages/psutil/_pscygwin.py:311: DeprecationWarning: invalid escape sequence \d
  search = re.compile('cpu\d')
/opt/sagemath-9.3/local/lib/python3.7/site-packages/psutil/_pscygwin.py:876: DeprecationWarning: invalid escape sequence \d
  def uids(self, _uids_re=re.compile(b'Uid:\t(\d+)')):
/opt/sagemath-9.3/local/lib/python3.7/site-packages/psutil/_pscygwin.py:887: DeprecationWarning: invalid escape sequence \d
  def gids(self, _gids_re=re.compile(b'Gid:\t(\d+)')):
Code:
NameError: name 'EllipticCurve' is not defined sage
PowerGlove
Hero Member
*****
hacker
Offline Offline

Activity: 534
Merit: 4358



View Profile
December 09, 2022, 06:46:45 PM
 #4

Yes,iam alredy try
This error show Huh
Do you mean with your original scripts, or with the ones I modified? I get the same errors that you do (minus the deprecation warnings) with the scripts as you've provided them, but I get no errors at all with the modified scripts I provided earlier.

Here's what I get when I run the scripts from the OP:

Code:
python3 AsProvided1.py

Code:
NameError: name 'EllipticCurve' is not defined

Code:
python3 AsProvided2.py

Code:
NameError: name 'GF' is not defined

And here's what I get when I run the scripts that I tweaked (post #3):

Code:
python3 Tweaked1.py

Code:
Elliptic Curve defined by y^2 = x^3 + 7 over Finite Field of size 115792089237316195423570985008687907853269984665640564039457584007908834671663
Public key : (89565891926547004231252920425935692360644145829622209833684329913297188986597 : 12158399299693830322967808612713398636155367887041628176798871954788371653930 : 1)

Code:
python3 Tweaked2.py

Code:
2
0xc6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5 0x1ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a

If you're still getting errors, even with the tweaked scripts, then I suggest you show me exactly the Python code (verbatim) that you're trying to run and exactly the command (again, verbatim) that you're using to run it.
mausuv (OP)
Jr. Member
*
Offline Offline

Activity: 70
Merit: 1


View Profile
December 12, 2022, 06:11:47 AM
 #5

As alternative, you could run the code on online editor such as https://sagecell.sagemath.org/. I tried both script on that website[1-2] and it runs without any problem. If you want to run the code on your device, you could install SageCell server[3] on your own device. I'll try to install it on my Debian VM and share the result later. Update: stuck with various problem related with dependency, so i give up for time being.

[3] https://github.com/sagemath/sagecell


install my pc pip install sagecell

NameError: name 'fabric' is not defined

next i am install pip install fabric
again show NameError: name 'fabric' is not defined

how to install sagecell windows 10 without error
please send your sagecell server windows git code or gdrive
witcher_sense
Legendary
*
Offline Offline

Activity: 2394
Merit: 4372


🔐BitcoinMessage.Tools🔑


View Profile WWW
December 12, 2022, 08:02:52 AM
Merited by ABCbits (1)
 #6

how to install sagecell windows 10 without error
please send your sagecell server windows git code or gdrive

Quote
Microsoft Windows
SageMath used to provide pre-built binaries for Windows based on Cygwin. This has been discontinued, and the old binaries that can be found are no longer supported. Use Windows Subsystem for Linux instead.
https://doc.sagemath.org/html/en/installation/binary.html#microsoft-windows

Quote

    Enable Windows Subsystem for Linux (WSL) by following the official WSL setup guide. Be sure to do the steps to install WSL2 and set it as default. Then go to the Microsoft Store and install Ubuntu (or another Linux distribution). Start Ubuntu from the start menu.

    On the Linux running on WSL, you always have root access, so you can use any of the installation methods described below for Linux.
https://doc.sagemath.org/html/en/installation/index.html

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
mausuv (OP)
Jr. Member
*
Offline Offline

Activity: 70
Merit: 1


View Profile
December 14, 2022, 07:08:03 AM
 #7

@ETFbitcoin
@witcher_sense
@PowerGlove

Example tow Point
Code:
px1 = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
py1 = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8

px2 = 0xc6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5
py2 = 0x1ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a

how to, Two point [ add , sub , div , mul ] in sagemath
please i need sage code
ecdsa123
Full Member
***
Offline Offline

Activity: 211
Merit: 105

Dr WHO on disney+


View Profile
December 14, 2022, 07:24:33 AM
 #8


Code:

p_string = 'FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F'
p = ZZ( '0x' + p_string.replace(' ', ''))
E = EllipticCurve( GF(p), [0, 7] )
n_string = 'FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141'
n = ZZ( '0x' + n_string.replace(' ', '') )

xP = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
yP = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
G = E.point( (xP, yP) )

def egcd(a, b):
    "Euclidean greatest common divisor"
    if a == 0:
        return (b, 0, 1)
    else:
        g, y, x = egcd(b % a, a)
        return (g, x - (b // a) * y, y)

def modinv(a, m):
    "Modular inverse"
    # in Python 3.8 you can simply return pow(a,-1,m)
    g, x, y = egcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m

#making point from two coordinates as x,y to P
px1 = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
py1 = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8

P = E.point( (px1,py1) )

px2 = 0xc6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5
py2 = 0x1ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a

P2 = E.point( (px2,py2) )

#add Two point toget P3:

P3= P+P2

#substract
P3= P-P2

# div by scalar

P4= P2*modinv(2,n)
# 2*(1/2)=1
assert P4==P

#mul by scalar

P5=P1*2
assert P5==P2

Donate: bc1q0sezldfgm7rf2r78p5scasrrcfkpzxnrfcvdc6

Subscribe : http://www.youtube.com/@Ecdsa_Solutions
mausuv (OP)
Jr. Member
*
Offline Offline

Activity: 70
Merit: 1


View Profile
December 14, 2022, 08:59:34 AM
Last edit: January 10, 2023, 03:46:21 AM by mausuv
 #9


div is so hard P5= P2*modinv(0x2,n)
any easy to div like this P5 = P2/0x2
mausuv (OP)
Jr. Member
*
Offline Offline

Activity: 70
Merit: 1


View Profile
January 10, 2023, 03:39:33 AM
 #10

@ecdsa123
@PowerGlove
@ETFbitcoin
@witcher_sense

public x to Address convert compressed, uncompressed sagemath code please

Enter one point x a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd convert 02 and 03 >> uncompressed Address,compressed Address

02 a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd >> 1KAqCkyrNu5PmK6bwmg5h1VN8ags4iXxm4, 1K5kLbqNncT2oGbj8KNwkYi49es9CeEtnp
03 a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd >> 1HZwkjkeaoZfTSaJxDw6aKkxp45agDiEzN, 1F3sAm6ZtwLAUnj7d38pGFxtP3RVEvtsbV

any one help me sagemath
witcher_sense
Legendary
*
Offline Offline

Activity: 2394
Merit: 4372


🔐BitcoinMessage.Tools🔑


View Profile WWW
January 10, 2023, 10:09:06 AM
Merited by ABCbits (1)
 #11


public x to Address convert compressed, uncompressed sagemath code please

Enter one point x a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd convert 02 and 03 >> uncompressed Address,compressed Address

02 a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd >> 1KAqCkyrNu5PmK6bwmg5h1VN8ags4iXxm4, 1K5kLbqNncT2oGbj8KNwkYi49es9CeEtnp
03 a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd >> 1HZwkjkeaoZfTSaJxDw6aKkxp45agDiEzN, 1F3sAm6ZtwLAUnj7d38pGFxtP3RVEvtsbV

any one help me sagemath
Since you have tagged me again, I must ask. What are you trying to achieve exactly? I mean, if you're not very sure how to attain a given goal with a given tool, maybe you made a mistake when choosing a tool and should look for something more suitable and simpler. You have already been given good answers and even working scripts, but you still don't want to put in a little effort to transform them into something that you're looking for.

Here is another script, but I really doubt it is going to be useful to you:

Code:
#secp256k1
p = Integer(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F)

a = Integer(0x0000000000000000000000000000000000000000000000000000000000000000)
b = Integer(0x0000000000000000000000000000000000000000000000000000000000000007)

K = GF(p)
E = EllipticCurve(K,[a,b])

Gx = Integer(0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798)
Gy = Integer(0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8)
G = E(Gx, Gy)

privkey = Integer(0xA34B99F22C790C4E36B2B3C2C35A36DB06226E41C692FC82B8B56AC1C540C5BD)

pubkey = privkey * G
print('Uncompressed: ', '04' + hex(pubkey[0])[2:] + hex(pubkey[1])[2:])
if (Integer(pubkey[1]) % 2) == 0:
    print('Compressed: ', '02' + hex(pubkey[0])[2:])
else:
    print('Compressed: ', '03' + hex(pubkey[0])[2:])

Try it online

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
mausuv (OP)
Jr. Member
*
Offline Offline

Activity: 70
Merit: 1


View Profile
January 10, 2023, 11:30:09 AM
 #12


public key to Address convert compressed, uncompressed sagemath code please


Here is another script, but I really doubt it is going to be useful to you:

yes,but i need output Address from public key
like this:
Uncompressed:  1EBH5s8KswNw3cSv4yF52M8JhkxyMV74eg
compressed:  14h2grbBy9uXrUgAhr1znnnQy8e6zTzDs4

update please..
mausuv (OP)
Jr. Member
*
Offline Offline

Activity: 70
Merit: 1


View Profile
January 11, 2023, 12:55:55 PM
 #13


Code:
import hashlib
import base58

def hash160(hex_str):
sha = hashlib.sha256()
rip = hashlib.new('ripemd160')
sha.update(hex_str)
rip.update(sha.digest())
return rip.hexdigest()  # .hexdigest() is hex ASCII

pub_keys = open('pubkey.json', 'r', encoding='utf-8')
new_file = open('addresses.json', 'a', encoding='utf-8')
compress_pubkey = False

for pub_key in pub_keys:
pub_key = pub_key.replace('\n', '')
if compress_pubkey:
if (ord(bytearray.fromhex(pub_key[-2:])) % 2 == 0):
pubkey_compressed = '02'
else:
pubkey_compressed = '03'
pubkey_compressed += pub_key[2:66]
hex_str = bytearray.fromhex(pubkey_compressed)
else:
hex_str = bytearray.fromhex(pub_key)

key_hash = '00' + hash160(hex_str)


sha = hashlib.sha256()
sha.update(bytearray.fromhex(key_hash))
checksum = sha.digest()
sha = hashlib.sha256()
sha.update(checksum)
checksum = sha.hexdigest()[0:8]


new_file.write("" + (base58.b58encode(bytes(bytearray.fromhex(key_hash + checksum)))).decode('utf-8'))

new_file.write((base58.b58encode(bytes(bytearray.fromhex(key_hash + checksum)))).decode('utf-8') + "\n")


pub_keys.close()
new_file.close()

this code python public 2 addresses
any easy sage method please... public 2 addresses
witcher_sense
Legendary
*
Offline Offline

Activity: 2394
Merit: 4372


🔐BitcoinMessage.Tools🔑


View Profile WWW
January 11, 2023, 02:40:36 PM
 #14


Code:
import hashlib
import base58

def hash160(hex_str):
sha = hashlib.sha256()
rip = hashlib.new('ripemd160')
sha.update(hex_str)
rip.update(sha.digest())
return rip.hexdigest()  # .hexdigest() is hex ASCII

pub_keys = open('pubkey.json', 'r', encoding='utf-8')
new_file = open('addresses.json', 'a', encoding='utf-8')
compress_pubkey = False

for pub_key in pub_keys:
pub_key = pub_key.replace('\n', '')
if compress_pubkey:
if (ord(bytearray.fromhex(pub_key[-2:])) % 2 == 0):
pubkey_compressed = '02'
else:
pubkey_compressed = '03'
pubkey_compressed += pub_key[2:66]
hex_str = bytearray.fromhex(pubkey_compressed)
else:
hex_str = bytearray.fromhex(pub_key)

key_hash = '00' + hash160(hex_str)


sha = hashlib.sha256()
sha.update(bytearray.fromhex(key_hash))
checksum = sha.digest()
sha = hashlib.sha256()
sha.update(checksum)
checksum = sha.hexdigest()[0:8]


new_file.write("" + (base58.b58encode(bytes(bytearray.fromhex(key_hash + checksum)))).decode('utf-8'))

new_file.write((base58.b58encode(bytes(bytearray.fromhex(key_hash + checksum)))).decode('utf-8') + "\n")


pub_keys.close()
new_file.close()

this code python public 2 addresses
any easy sage method please... public 2 addresses
As you can see, in the python code that you posted, the base58 module is imported to encode public key hashes using base58_check (which is basically a bitcoin address). Unless you find people who voluntarily agree to implement such an algorithm in sagemath (I think it is not that hard to implement it, but still...), you're not going to convert these public keys to address. But you haven't answered my questions: why don't you just use a python code that works?

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
Pages: [1]
  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!