github.com/cloudflare/circl@v1.5.0/dh/sidh/internal/common/utils.go (about)

     1  package common
     2  
     3  import "encoding/binary"
     4  
     5  // Constant time select.
     6  // if pick == 1 (out = in1)
     7  // if pick == 0 (out = in2)
     8  // else out is undefined.
     9  func Cpick(pick int, out, in1, in2 []byte) {
    10  	which := byte((int8(pick << 7)) >> 7)
    11  	for i := range out {
    12  		out[i] = (in1[i] & which) | (in2[i] & ^which)
    13  	}
    14  }
    15  
    16  // Read 2*bytelen(p) bytes into the given ExtensionFieldElement.
    17  //
    18  // It is an error to call this function if the input byte slice is less than 2*bytelen(p) bytes long.
    19  func BytesToFp2(fp2 *Fp2, input []byte, bytelen int) {
    20  	if len(input) < 2*bytelen {
    21  		panic("input byte slice too short")
    22  	}
    23  	numW64 := (bytelen*8 + 63) / 64
    24  	a := make([]byte, 8*numW64)
    25  	b := make([]byte, 8*numW64)
    26  	copy(a[:bytelen], input[:bytelen])
    27  	copy(b[:bytelen], input[bytelen:])
    28  	for i := 0; i < numW64; i++ {
    29  		fp2.A[i] = binary.LittleEndian.Uint64(a[i*8 : (i+1)*8])
    30  		fp2.B[i] = binary.LittleEndian.Uint64(b[i*8 : (i+1)*8])
    31  	}
    32  }
    33  
    34  // Convert the input to wire format.
    35  //
    36  // The output byte slice must be at least 2*bytelen(p) bytes long.
    37  func Fp2ToBytes(output []byte, fp2 *Fp2, bytelen int) {
    38  	if len(output) < 2*bytelen {
    39  		panic("output byte slice too short")
    40  	}
    41  	numW64 := (bytelen*8 + 63) / 64
    42  	a := make([]byte, 8*numW64)
    43  	b := make([]byte, 8*numW64)
    44  	for i := 0; i < numW64; i++ {
    45  		binary.LittleEndian.PutUint64(a[i*8:(i+1)*8], fp2.A[i])
    46  		binary.LittleEndian.PutUint64(b[i*8:(i+1)*8], fp2.B[i])
    47  	}
    48  	copy(output[:bytelen], a[:bytelen])
    49  	copy(output[bytelen:], b[:bytelen])
    50  }