package fiat
import (
"crypto/subtle"
"errors"
)
type P521Element struct {
x p521MontgomeryDomainFieldElement
}
const p521ElementLen = 66
type p521UntypedFieldElement = [9 ]uint64
func (e *P521Element ) One () *P521Element {
p521SetOne (&e .x )
return e
}
func (e *P521Element ) Equal (t *P521Element ) int {
eBytes := e .Bytes ()
tBytes := t .Bytes ()
return subtle .ConstantTimeCompare (eBytes , tBytes )
}
func (e *P521Element ) IsZero () int {
zero := make ([]byte , p521ElementLen )
eBytes := e .Bytes ()
return subtle .ConstantTimeCompare (eBytes , zero )
}
func (e *P521Element ) Set (t *P521Element ) *P521Element {
e .x = t .x
return e
}
func (e *P521Element ) Bytes () []byte {
var out [p521ElementLen ]byte
return e .bytes (&out )
}
func (e *P521Element ) bytes (out *[p521ElementLen ]byte ) []byte {
var tmp p521NonMontgomeryDomainFieldElement
p521FromMontgomery (&tmp , &e .x )
p521ToBytes (out , (*p521UntypedFieldElement )(&tmp ))
p521InvertEndianness (out [:])
return out [:]
}
func (e *P521Element ) SetBytes (v []byte ) (*P521Element , error ) {
if len (v ) != p521ElementLen {
return nil , errors .New ("invalid P521Element encoding" )
}
var minusOneEncoding = new (P521Element ).Sub (
new (P521Element ), new (P521Element ).One ()).Bytes ()
for i := range v {
if v [i ] < minusOneEncoding [i ] {
break
}
if v [i ] > minusOneEncoding [i ] {
return nil , errors .New ("invalid P521Element encoding" )
}
}
var in [p521ElementLen ]byte
copy (in [:], v )
p521InvertEndianness (in [:])
var tmp p521NonMontgomeryDomainFieldElement
p521FromBytes ((*p521UntypedFieldElement )(&tmp ), &in )
p521ToMontgomery (&e .x , &tmp )
return e , nil
}
func (e *P521Element ) Add (t1 , t2 *P521Element ) *P521Element {
p521Add (&e .x , &t1 .x , &t2 .x )
return e
}
func (e *P521Element ) Sub (t1 , t2 *P521Element ) *P521Element {
p521Sub (&e .x , &t1 .x , &t2 .x )
return e
}
func (e *P521Element ) Mul (t1 , t2 *P521Element ) *P521Element {
p521Mul (&e .x , &t1 .x , &t2 .x )
return e
}
func (e *P521Element ) Square (t *P521Element ) *P521Element {
p521Square (&e .x , &t .x )
return e
}
func (v *P521Element ) Select (a , b *P521Element , cond int ) *P521Element {
p521Selectznz ((*p521UntypedFieldElement )(&v .x ), p521Uint1 (cond ),
(*p521UntypedFieldElement )(&b .x ), (*p521UntypedFieldElement )(&a .x ))
return v
}
func p521InvertEndianness(v []byte ) {
for i := 0 ; i < len (v )/2 ; i ++ {
v [i ], v [len (v )-1 -i ] = v [len (v )-1 -i ], v [i ]
}
}
The pages are generated with Golds v0.7.0-preview . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .