Implement ToScalar from spec, and small refactor to match spec more closely
Test vectors from sapling_key_components.py and sapling_signatures.py are not altered by this commit.
This commit is contained in:
parent
1fbb3b2036
commit
739ec65c52
|
@ -106,10 +106,6 @@ class Fq(FieldElement):
|
||||||
|
|
||||||
|
|
||||||
class Fr(FieldElement):
|
class Fr(FieldElement):
|
||||||
@staticmethod
|
|
||||||
def from_bytes(buf):
|
|
||||||
return Fr(leos2ip(buf))
|
|
||||||
|
|
||||||
def __init__(self, s):
|
def __init__(self, s):
|
||||||
FieldElement.__init__(self, Fr, s, r_j)
|
FieldElement.__init__(self, Fr, s, r_j)
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,15 @@ from sapling_generators import PROVING_KEY_BASE, SPENDING_KEY_BASE, group_hash
|
||||||
from sapling_jubjub import Fr
|
from sapling_jubjub import Fr
|
||||||
from sapling_merkle_tree import MERKLE_DEPTH
|
from sapling_merkle_tree import MERKLE_DEPTH
|
||||||
from sapling_notes import note_commit, note_nullifier
|
from sapling_notes import note_commit, note_nullifier
|
||||||
from sapling_utils import chunk, leos2bsp
|
from sapling_utils import chunk, leos2bsp, leos2ip
|
||||||
|
|
||||||
|
#
|
||||||
|
# Utilities
|
||||||
|
#
|
||||||
|
|
||||||
|
def to_scalar(buf):
|
||||||
|
return Fr(leos2ip(buf))
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# PRFs and hashes
|
# PRFs and hashes
|
||||||
|
@ -23,8 +31,7 @@ def crh_ivk(ak, nk):
|
||||||
digest.update(ak)
|
digest.update(ak)
|
||||||
digest.update(nk)
|
digest.update(nk)
|
||||||
ivk = digest.digest()
|
ivk = digest.digest()
|
||||||
ivk = ivk[:31] + bytes([ivk[31] & 0b00000111])
|
return leos2ip(ivk) % 2**251
|
||||||
return ivk
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -46,11 +53,11 @@ class SpendingKey(object):
|
||||||
|
|
||||||
@cached
|
@cached
|
||||||
def ask(self):
|
def ask(self):
|
||||||
return Fr.from_bytes(prf_expand(self.data, b'\0'))
|
return to_scalar(prf_expand(self.data, b'\0'))
|
||||||
|
|
||||||
@cached
|
@cached
|
||||||
def nsk(self):
|
def nsk(self):
|
||||||
return Fr.from_bytes(prf_expand(self.data, b'\1'))
|
return to_scalar(prf_expand(self.data, b'\1'))
|
||||||
|
|
||||||
@cached
|
@cached
|
||||||
def ovk(self):
|
def ovk(self):
|
||||||
|
@ -66,7 +73,7 @@ class SpendingKey(object):
|
||||||
|
|
||||||
@cached
|
@cached
|
||||||
def ivk(self):
|
def ivk(self):
|
||||||
return Fr.from_bytes(crh_ivk(bytes(self.ak()), bytes(self.nk())))
|
return Fr(crh_ivk(bytes(self.ak()), bytes(self.nk())))
|
||||||
|
|
||||||
@cached
|
@cached
|
||||||
def default_d(self):
|
def default_d(self):
|
||||||
|
|
|
@ -4,7 +4,8 @@ import os
|
||||||
from pyblake2 import blake2b
|
from pyblake2 import blake2b
|
||||||
|
|
||||||
from sapling_generators import SPENDING_KEY_BASE
|
from sapling_generators import SPENDING_KEY_BASE
|
||||||
from sapling_jubjub import Fr, Point
|
from sapling_jubjub import Fr, Point, r_j
|
||||||
|
from sapling_key_components import to_scalar
|
||||||
from sapling_utils import cldiv, chunk, leos2ip
|
from sapling_utils import cldiv, chunk, leos2ip
|
||||||
|
|
||||||
|
|
||||||
|
@ -29,7 +30,7 @@ class RedJubjub(object):
|
||||||
self._random = random
|
self._random = random
|
||||||
|
|
||||||
def gen_private(self):
|
def gen_private(self):
|
||||||
return self.Private.from_bytes(self._random(64))
|
return to_scalar(self._random(64))
|
||||||
|
|
||||||
def derive_public(self, sk):
|
def derive_public(self, sk):
|
||||||
return self.P_g * sk
|
return self.P_g * sk
|
||||||
|
@ -58,9 +59,9 @@ class RedJubjub(object):
|
||||||
mid = cldiv(self.l_G, 8)
|
mid = cldiv(self.l_G, 8)
|
||||||
(Rbar, Sbar) = (sig[:mid], sig[mid:]) # TODO: bitlength(r_j)
|
(Rbar, Sbar) = (sig[:mid], sig[mid:]) # TODO: bitlength(r_j)
|
||||||
R = Point.from_bytes(Rbar)
|
R = Point.from_bytes(Rbar)
|
||||||
S = Fr.from_bytes(Sbar)
|
S = leos2ip(Sbar)
|
||||||
c = h_star(Rbar + M)
|
c = h_star(Rbar + M)
|
||||||
return R and S.s == leos2ip(Sbar) and self.P_g * S == R + vk * c
|
return R and S < r_j and self.P_g * Fr(S) == R + vk * c
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
Loading…
Reference in New Issue