package fiat
import (
"crypto/subtle"
"errors"
)
type P384Element struct {
x p384MontgomeryDomainFieldElement
}
const p384ElementLen = 48
type p384UntypedFieldElement = [6 ]uint64
func (e *P384Element ) One () *P384Element {
p384SetOne (&e .x )
return e
}
func (e *P384Element ) Equal (t *P384Element ) int {
eBytes := e .Bytes ()
tBytes := t .Bytes ()
return subtle .ConstantTimeCompare (eBytes , tBytes )
}
func (e *P384Element ) IsZero () int {
zero := make ([]byte , p384ElementLen )
eBytes := e .Bytes ()
return subtle .ConstantTimeCompare (eBytes , zero )
}
func (e *P384Element ) Set (t *P384Element ) *P384Element {
e .x = t .x
return e
}
func (e *P384Element ) Bytes () []byte {
var out [p384ElementLen ]byte
return e .bytes (&out )
}
func (e *P384Element ) bytes (out *[p384ElementLen ]byte ) []byte {
var tmp p384NonMontgomeryDomainFieldElement
p384FromMontgomery (&tmp , &e .x )
p384ToBytes (out , (*p384UntypedFieldElement )(&tmp ))
p384InvertEndianness (out [:])
return out [:]
}
func (e *P384Element ) SetBytes (v []byte ) (*P384Element , error ) {
if len (v ) != p384ElementLen {
return nil , errors .New ("invalid P384Element encoding" )
}
var minusOneEncoding = new (P384Element ).Sub (
new (P384Element ), new (P384Element ).One ()).Bytes ()
for i := range v {
if v [i ] < minusOneEncoding [i ] {
break
}
if v [i ] > minusOneEncoding [i ] {
return nil , errors .New ("invalid P384Element encoding" )
}
}
var in [p384ElementLen ]byte
copy (in [:], v )
p384InvertEndianness (in [:])
var tmp p384NonMontgomeryDomainFieldElement
p384FromBytes ((*p384UntypedFieldElement )(&tmp ), &in )
p384ToMontgomery (&e .x , &tmp )
return e , nil
}
func (e *P384Element ) Add (t1 , t2 *P384Element ) *P384Element {
p384Add (&e .x , &t1 .x , &t2 .x )
return e
}
func (e *P384Element ) Sub (t1 , t2 *P384Element ) *P384Element {
p384Sub (&e .x , &t1 .x , &t2 .x )
return e
}
func (e *P384Element ) Mul (t1 , t2 *P384Element ) *P384Element {
p384Mul (&e .x , &t1 .x , &t2 .x )
return e
}
func (e *P384Element ) Square (t *P384Element ) *P384Element {
p384Square (&e .x , &t .x )
return e
}
func (v *P384Element ) Select (a , b *P384Element , cond int ) *P384Element {
p384Selectznz ((*p384UntypedFieldElement )(&v .x ), p384Uint1 (cond ),
(*p384UntypedFieldElement )(&b .x ), (*p384UntypedFieldElement )(&a .x ))
return v
}
func p384InvertEndianness (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 .