~/bend-docscommunity

src/crypto/secp256k1/limbs.bend checks

raw source on the hub · import 0xd9a2fae439ac7ff9e21e0853948f94fe/src/crypto/secp256k1/limbs.bend as Limbs

1 import
import Base

Definitions

def lo16 source · line 37 · raw

@+s:Nat -> Nat

s mod 2^16 and s div 2^16

def hi16 source · line 40 · raw

@+s:Nat -> Nat

def comp1 source · line 44 · raw

@+x:Nat -> Nat

2^16 - 1 - x for x below 2^16

def hd0 source · line 49 · raw

@xs:List<&2, Nat> -> Nat

def tl source · line 54 · raw

@xs:List<&2, Nat> -> List<&2, Nat>

def add source · line 60 · raw

@xs:List<&2, Nat> -> @ys:List<&2, Nat> -> List<&2, Nat>

limb-wise sum; the longer tail is kept

def scal source · line 67 · raw

@+a:Nat -> @ys:List<&2, Nat> -> List<&2, Nat>

every limb times a

def conv source · line 73 · raw

@xs:List<&2, Nat> -> @+ys:List<&2, Nat> -> List<&2, Nat>

the schoolbook product without carries: conv(x :: xs, ys) = x ys + 2^16 conv(xs, ys)

def take source · line 78 · raw

@n:Nat -> @xs:List<&2, Nat> -> List<&2, Nat>

def drop source · line 84 · raw

@n:Nat -> @xs:List<&2, Nat> -> List<&2, Nat>

def horner source · line 91 · raw

@xs:List<&2, Nat> -> Nat

the value of the limbs (only ever of an empty list at run time: see carry)

def carry source · line 101 · raw

@n:Nat -> @xs:List<&2, Nat> -> @u:Nat -> List<&2, Nat>

A carry pass: n limbs, each below 2^16, then one limb holding the carry u out of them plus the value of whatever is left of xs past n limbs (on every call below xs has at most n limbs, so that is the carry alone). Every function here looks at its list before producing anything, so that on a symbolic argument the proof checker stops at once.

def comp source · line 113 · raw

@xs:List<&2, Nat> -> List<&2, Nat>

every limb x becomes 2^16 - 1 - x

def pick1 source · line 119 · raw

@+b:Nat -> @+x:Nat -> @+y:Nat -> Nat

b x + (1 - b) y, limb-wise, for b in {0, 1}

def select source · line 122 · raw

@+b:Nat -> @xs:List<&2, Nat> -> @ys:List<&2, Nat> -> List<&2, Nat>

def norm16 source · line 130 · raw

@xs:List<&2, Nat> -> List<&2, Nat>

the normalized form of a value below 2^256: exactly 16 limbs below 2^16

def fold source · line 134 · raw

@+c:List<&2, Nat> -> @+xs:List<&2, Nat> -> List<&2, Nat>

lo + c hi, for x = lo + 2^256 hi: congruent to x modulo m

def rounds source · line 137 · raw

@k:Nat -> @+c:List<&2, Nat> -> @xs:List<&2, Nat> -> List<&2, Nat>

def top source · line 143 · raw

@xs:List<&2, Nat> -> Nat

the part of a carried value above 2^256 (0 or 1 below)

def canon source · line 148 · raw

@+c:List<&2, Nat> -> @+t:List<&2, Nat> -> List<&2, Nat>

t (16 limbs, value below 2^256) mod m: t + c has bit 256 set exactly when t >= m, and then its low 256 bits are t - m

def reduce_go source · line 152 · raw

@+c:List<&2, Nat> -> @k:Nat -> @xs:List<&2, Nat> -> List<&2, Nat>

def reduce source · line 157 · raw

@+c:List<&2, Nat> -> @k:Nat -> @xs:List<&2, Nat> -> List<&2, Nat>

x mod m for x below 2^512, in k folds (x is looked at first, so that the proof checker keeps an unknown reduction folded)

def neg_raw source · line 166 · raw

@+c:List<&2, Nat> -> @b:List<&2, Nat> -> List<&2, Nat>

m - b = (2^256 - 1 - (b + c)) + 1, for b < m (so b + c < 2^256); no constant but c is ever formed

def ltm source · line 170 · raw

@+c:List<&2, Nat> -> @x:List<&2, Nat> -> Bool

x < m, i.e. x + c < 2^256, for x of 16 limbs below 2^16

def b2n source · line 175 · raw

@b:Bool -> Nat

def borrow source · line 182 · raw

@xs:List<&2, Nat> -> @ys:List<&2, Nat> -> @br:Nat -> Nat

1 when the value of xs is below the value of ys plus br, else 0 (the borrow out of xs - ys - br), for lists of the same length

def lt source · line 187 · raw

@xs:List<&2, Nat> -> @ys:List<&2, Nat> -> Bool

def nonzero source · line 191 · raw

@xs:List<&2, Nat> -> @acc:Nat -> Nat

OR of all limbs being nonzero, without an early exit

def is_zero source · line 196 · raw

@xs:List<&2, Nat> -> Bool

def diff source · line 199 · raw

@xs:List<&2, Nat> -> @ys:List<&2, Nat> -> @acc:Nat -> Nat

def eq source · line 204 · raw

@xs:List<&2, Nat> -> @ys:List<&2, Nat> -> Bool

def bits_of source · line 208 · raw

@k:Nat -> @+x:Nat -> List<&2, Nat>

the k low bits of x, most significant first

def cbits source · line 214 · raw

@bs:List<&2, Nat> -> List<&2, Nat>

every bit flipped: the bits of 2^k - 1 - x from those of x

def bits source · line 220 · raw

@xs:List<&2, Nat> -> List<&2, Nat>

all bits of a limb list, most significant first (16 per limb)