package fiat
import (
"crypto/subtle"
"errors"
)
type P256Element struct {
x p256MontgomeryDomainFieldElement
}
const p256ElementLen = 32
type p256UntypedFieldElement = [4 ]uint64
func (e *P256Element ) One () *P256Element {
p256SetOne (&e .x )
return e
}
func (e *P256Element ) Equal (t *P256Element ) int {
eBytes := e .Bytes ()
tBytes := t .Bytes ()
return subtle .ConstantTimeCompare (eBytes , tBytes )
}
func (e *P256Element ) IsZero () int {
zero := make ([]byte , p256ElementLen )
eBytes := e .Bytes ()
return subtle .ConstantTimeCompare (eBytes , zero )
}
func (e *P256Element ) Set (t *P256Element ) *P256Element {
e .x = t .x
return e
}
func (e *P256Element ) Bytes () []byte {
var out [p256ElementLen ]byte
return e .bytes (&out )
}
func (e *P256Element ) bytes (out *[p256ElementLen ]byte ) []byte {
var tmp p256NonMontgomeryDomainFieldElement
p256FromMontgomery (&tmp , &e .x )
p256ToBytes (out , (*p256UntypedFieldElement )(&tmp ))
p256InvertEndianness (out [:])
return out [:]
}
func (e *P256Element ) SetBytes (v []byte ) (*P256Element , error ) {
if len (v ) != p256ElementLen {
return nil , errors .New ("invalid P256Element encoding" )
}
var minusOneEncoding = new (P256Element ).Sub (
new (P256Element ), new (P256Element ).One ()).Bytes ()
for i := range v {
if v [i ] < minusOneEncoding [i ] {
break
}
if v [i ] > minusOneEncoding [i ] {
return nil , errors .New ("invalid P256Element encoding" )
}
}
var in [p256ElementLen ]byte
copy (in [:], v )
p256InvertEndianness (in [:])
var tmp p256NonMontgomeryDomainFieldElement
p256FromBytes ((*p256UntypedFieldElement )(&tmp ), &in )
p256ToMontgomery (&e .x , &tmp )
return e , nil
}
func (e *P256Element ) Add (t1 , t2 *P256Element ) *P256Element {
p256Add (&e .x , &t1 .x , &t2 .x )
return e
}
func (e *P256Element ) Sub (t1 , t2 *P256Element ) *P256Element {
p256Sub (&e .x , &t1 .x , &t2 .x )
return e
}
func (e *P256Element ) Mul (t1 , t2 *P256Element ) *P256Element {
p256Mul (&e .x , &t1 .x , &t2 .x )
return e
}
func (e *P256Element ) Square (t *P256Element ) *P256Element {
p256Square (&e .x , &t .x )
return e
}
func (v *P256Element ) Select (a , b *P256Element , cond int ) *P256Element {
p256Selectznz ((*p256UntypedFieldElement )(&v .x ), p256Uint1 (cond ),
(*p256UntypedFieldElement )(&b .x ), (*p256UntypedFieldElement )(&a .x ))
return v
}
func p256InvertEndianness (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.6.7 . (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 @Go100and1 (reachable from the left QR code) to get the latest news of Golds .