-
-
Save ZenulAbidin/286a652b160086b3b0f184a886ba68ca to your computer and use it in GitHub Desktop.
See Iceland code https://github.com/iceland2k14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2022 Ali Sherief | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
from fastecdsa import curve | |
from fastecdsa.point import Point | |
import bit | |
G = curve.secp256k1.G | |
N = curve.secp256k1.q | |
pubkey = '03a2efa402fd5268400c77c20e574ba86409ededee7c4020e4b9f0edbee53de0d4' | |
def pub2point(pub_hex): | |
x = int(pub_hex[2:66], 16) | |
if len(pub_hex) < 70: | |
y = bit.format.x_to_y(x, int(pub_hex[:2], 16) % 2) | |
else: | |
y = int(pub_hex[66:], 16) | |
return Point(x, y, curve=curve.secp256k1) | |
def point2pub(R): | |
# Remove the following code if you are doing multiple additions successively | |
# as it brings a speed improvement, and just return R | |
if (R.y % 2 == 0): | |
prefix = "02" | |
else: | |
prefix = "03" | |
hx = hex(R.x)[2:].zfill(64) | |
return hx | |
pk1 = '031656894a2e404e652e3a2b368c7df820b0e92fe32529c41931a9f7b234457d5b' | |
pk2 = '022fa21d1cea4bc1f9911a9d501e3d8b3c97d15aaa76a63fecd0d529b9ef2e22f5' | |
def add(pk1, pk2, convert=True): | |
if convert: | |
P = pub2point(pk1) | |
Q = pub2point(pk2) | |
else: | |
P = pk1 | |
Q = pk2 | |
R = P + Q | |
return R | |
def sub(pk1, pk2, convert=True): | |
if convert: | |
P = pub2point(pk1) | |
Q = pub2point(pk2) | |
else: | |
P = pk1 | |
Q = pk2 | |
R = P - Q | |
return R | |
# This function makes all the downscaled pubkeys obtained from subtracting | |
# numbers between 0 and divisor, before dividing the pubkeys by divisor. | |
# i is between 0 and divisor (inclusive) | |
def shiftdown(pubkey, divisor, i=0, convert=True): | |
if convert: | |
pubkey = pub2point(pubkey) | |
# k = 1/divisor | |
k = pow(divisor, N - 2, N) | |
P = pubkey - (i * G) | |
P = k * P | |
return P | |
# i is between 0 and multiplier (inclusive) | |
def shiftup(pubkey, multiplier, i=0, convert=True): | |
if convert: | |
pubkey = pub2point(pubkey) | |
P = multiplier*(pubkey + (i * G)) | |
return P | |
# example: | |
print(shiftdown(pubkey, 32, i=0)) | |
print(shiftup(pubkey, 32, i=0)) | |
print(add(pk1, pk2)) | |
print(sub(pk1, pk2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment